1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
module gemini_protocol
implicit none
integer, parameter::STATUS_INPUT = 1
integer, parameter::STATUS_SUCCESS = 2
integer, parameter::STATUS_REDIRECT = 3
integer, parameter::STATUS_TEMPFAIL = 4
integer, parameter::STATUS_PERMFAIL = 5
integer, parameter::STATUS_CERTREQ = 6
integer, parameter::STATUS_LOCALFAIL = -1
integer, parameter::BUFFER_SIZE = 256
contains
function request_url(url, unit_number, server_name) result(returncode)
use request
use iso_c_binding
use file_handling, only: mark_file_end
implicit none
character(*), intent(in)::url
integer, intent(in)::unit_number
character(*), intent(in), optional::server_name
integer::returncode
character(:), allocatable::server
type(connection)::conn
integer::bytes_received, i
character, dimension(BUFFER_SIZE)::buffer
returncode = -1
rewind(unit_number)
if(present(server_name)) then
allocate(character(len=len_trim(server_name)) :: server)
server = server_name
else
server = get_server_from_url(url)
end if
conn = open_connection(server)
if(conn%code == CONNECTION_OPEN) then
if(send_string(conn%ssl, url//c_carriage_return//c_new_line, trimming=.false.)) then
bytes_received = retrieve_characters(conn%ssl, buffer)
do while(bytes_received > 0)
do i=1, bytes_received
write(unit_number, '(A1)', advance='no') buffer(i)
end do
bytes_received = retrieve_characters(conn%ssl, buffer)
end do
call mark_file_end(unit_number)
rewind(unit_number)
read(unit_number, '(I1)') returncode
else
returncode = -1
write(unit_number, *) "Send Error: Could Not Send Request"
write(*, *) "Send Error: Could Not Send Request"
end if
else
returncode = -1
write(unit_number, *) "Connection Error: "//trim(translate_connection_code(conn%code))
write(*, *) "Connection Error: "//trim(translate_connection_code(conn%code))
end if
call close_connection(conn)
end function request_url
subroutine get_redirect_url(unit_number, url)
implicit none
integer, intent(in)::unit_number
character(*), intent(inout)::url
character::search
integer::i, istat
rewind(unit_number)
! Status code
read(unit_number, '(A1)', advance='no') search
read(unit_number, '(A1)', advance='no') search
! Clear the url
url = repeat(" ", len(url))
! At least one whitespace, but whatever...
read(unit_number, '(A1)', advance='no') search
do while(search == " " .or. search == CHAR(9))
read(unit_number, '(A1)', advance='no', iostat=istat) search
end do
! Now search contains our first url component
i = 0
do while(search /= CHAR(13) .and. i < len(url) .and. istat == 0)
i = i + 1
url(i:i) = search
read(unit_number, '(A1)', advance='no', iostat=istat) search
end do
end subroutine get_redirect_url
subroutine handle_relative_url(current_url, path)
implicit none
character(*), intent(inout)::current_url
character(*), intent(in)::path
integer::past_protocol, first_slash, last_slash
past_protocol = index(current_url, "://")
if(past_protocol > 0) then
past_protocol = past_protocol + 3
if(path(1:1) == "/") then
first_slash = index(current_url(past_protocol:len_trim(current_url)), "/")
current_url = current_url(1:(past_protocol + first_slash - 1))//path
else
last_slash = index(current_url, "/", back=.true.)
if(last_slash > 0) then
current_url = current_url(1:last_slash)//path
end if
end if
end if
end subroutine handle_relative_url
end module gemini_protocol
|