aboutsummaryrefslogtreecommitdiff
path: root/request.f90
blob: 139b368083cebfed5292a3fe495e29ac08a8042f (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
! Copyright (c) 2020 Jeffrey Armstrong <jeff@rainbow-100.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module request
use network
use iso_c_binding
implicit none

    integer, parameter::bufsize = 65536
    
    integer, parameter::CONNECTION_NONE              = 0
    integer, parameter::CONNECTION_CLOSED            = 1
    integer, parameter::CONNECTION_SERVER_NOT_FOUND  = 2
    integer, parameter::CONNECTION_SOCKET_FAILURE    = 3
    integer, parameter::CONNECTION_SSL_SETUP_FAILURE = 4
    integer, parameter::CONNECTION_SSL_CONN_FAILURE  = 5
    integer, parameter::CONNECTION_OPEN              = 6
    
    character(23), dimension(0:6), parameter:: connection_code_str = &
        ["No Connection          ", &
         "Connection Closed      ", &
         "Server Not Found       ", &
         "Local Socket Failure   ", &
         "SSL Configuration Error", &
         "SSL Connection Failure ", &
         "SSL Connection Open    " ]
           
    
    type :: connection
        
        integer::code
        
        type(simple_hostent)::host
        integer::socket
        type(c_ptr)::ssl_ctx
        type(c_ptr)::ssl
    
    end type connection

contains

    function translate_connection_code(code)
    implicit none
    
        integer, intent(in)::code
        character(23)::translate_connection_code
        
        if(code >= lbound(connection_code_str, 1) .and. &
           code <= ubound(connection_code_str, 1)) then
        
            translate_connection_code = connection_code_str(code)
            
        else
            
            translate_connection_code = "Unknown Error"
            
        end if
        
    end function translate_connection_code

    function open_connection(server, port) result(conn)
    use jessl
    use network
    implicit none
    
        character(*), intent(in)::server
        integer, intent(in), optional::port
        type(connection)::conn
        
        type(sockaddr_in), target::sa
        type(c_ptr)::ssl_method
        
        conn%code = CONNECTION_NONE

        ! Lookup host
        conn%host = gethostbyname(server)
        if((.not. allocated(conn%host%h_name)) .or. (conn%host%h_addr4 == 0)) then
            conn%code = CONNECTION_SERVER_NOT_FOUND
            return
        end if

        ! Build the socket
        sa%sin_family = AF_INET
        sa%sin_addr%s_addr = conn%host%h_addr4
        if(present(port)) then
            sa%sin_port = htons(port)
        else
            sa%sin_port = htons(1965)
        end if
        conn%socket = socket(AF_INET, SOCK_STREAM, 0)
        if(.not. connect(conn%socket, sa)) then
            conn%code = CONNECTION_SOCKET_FAILURE
            return
        end if
        
        ! Set up ssl now
        ssl_method = tls_client_method()
        conn%ssl_ctx = ctx_new(ssl_method)
        
        conn%ssl = ssl_new(conn%ssl_ctx)
        if((.not. c_associated(conn%ssl)) .or. &
           (set_tlsext_host_name(conn%ssl, server) == 0) .OR. &
           (set_fd(conn%ssl, conn%socket) /= 1)) then
           
            conn%code = CONNECTION_SSL_SETUP_FAILURE
            return
            
        end if
        
        ! Connect via ssl
        if(ssl_connect(conn%ssl) /= 1) then
            conn%code = CONNECTION_SSL_CONN_FAILURE
            return
        end if
        
        ! Here, the connection is live
        conn%code = CONNECTION_OPEN

    end function open_connection
    
    subroutine close_connection(conn)
    use jessl
    use network
    implicit none
    
        type(connection), intent(inout)::conn
        integer::res
        
        if(conn%code >= CONNECTION_OPEN) then
            res = ssl_shutdown(conn%ssl)
        end if
        
        if(conn%code >= CONNECTION_SSL_CONN_FAILURE) then
            res = ssl_free(conn%ssl)
        end if
        
        if(conn%code >= CONNECTION_SSL_SETUP_FAILURE) then
            res = ctx_free(conn%ssl_ctx)
        end if
        
        if(conn%code > CONNECTION_SOCKET_FAILURE) then
            call close_socket(conn%socket)
        end if
        
        if(conn%code > CONNECTION_SERVER_NOT_FOUND) then
            deallocate(conn%host%h_name)
        end if
        
        conn%code = CONNECTION_CLOSED
        
    end subroutine close_connection
    
    subroutine get_server_from_url(url, server, port)
    implicit none
    
        character(*), intent(in)::url
        character(:), allocatable, intent(out)::server
        integer, intent(out)::port
        
        integer::start_server, end_server, length
        integer::start_port, iostatus
        
        port = -1
        
        start_server = index(url, "://")
        if(start_server > 0) then
        
            start_server = start_server + 3
            end_server = index(url(start_server:len_trim(url)), "/")
            if(end_server <= 0) then
                end_server = len_trim(url)
            else
                ! Get rid of trailing slash
                end_server = end_server + start_server - 2
            end if
            length = end_server - start_server + 1
    
            allocate(character(len=length) :: server)
            server = url(start_server:end_server)
            
        end if
        
        ! Need to check if a port was specified too
        start_port = index(server, ":")
        if(start_port > 0) then
        
            read(server(start_port+1:len_trim(server)), *, iostat=iostatus) port
            if(iostatus /= 0) then
                port = -1
            end if
            
            server = server(1:start_port-1)
        
        end if
    
    end subroutine get_server_from_url

    function send_string(ssl, str, trimming) result(success)
    use iso_c_binding
    use jessl
    implicit none
    
        logical::success
        type(c_ptr)::ssl
        character(*), intent(in)::str
        logical, intent(in), optional::trimming
        
        integer::start_send
        integer::chars_sent_this_time, chars_sending
        integer::i, bytes
        integer::string_length
        
        character, dimension(bufsize)::buffer
        
        if(present(trimming)) then
            if(trimming) then
                string_length = len_trim(str)
            else
                string_length = len(str)
            end if
        else 
            string_length = len_trim(str)
        end if
        
        success = .true.
        start_send = 1
        do while(start_send <= string_length) 
            
            chars_sending = 0
            do i = start_send, string_length
                buffer(i-start_send+1) = str(i:i) 
                chars_sending = chars_sending + 1
                if(chars_sending == bufsize) then
                    exit
                end if
            end do
            
            ! A null character seems necessary at the end of the request
            if(i >= string_length) then
                chars_sending = chars_sending + 1
                buffer(chars_sending) = c_null_char
            end if
            
            ! Minus 1 because we're sending start_send as well
            chars_sent_this_time = ssl_write(ssl, buffer(start_send:(start_send+chars_sending-1)))
            
            if(chars_sent_this_time < 0) then
                success = .false.
                exit
            end if
            
            start_send = start_send + chars_sent_this_time
            
        end do
        
    end function send_string
    
    function retrieve_characters(ssl, arr) result(chars_read)
    use iso_c_binding
    use jessl
    implicit none
    
        integer::chars_read
        type(c_ptr)::ssl
        character(len=1), dimension(:), intent(inout)::arr

        chars_read = ssl_read(ssl, arr)
        
    end function retrieve_characters
    
end module request