aboutsummaryrefslogtreecommitdiff
path: root/captain/response.f90
blob: 1c7a0b5382418382161327758207fccf7066c411 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
module server_response
use iso_c_binding
implicit none

    integer, parameter::GEMINI_CODE_INPUT    = 10
    integer, parameter::GEMINI_CODE_SUCCESS  = 20
    integer, parameter::GEMINI_CODE_REDIRECT = 30
    integer, parameter::GEMINI_CODE_PERMFAIL = 50

    type :: response
    
        integer::code
        
        ! Used for redirects
        character(len=:), pointer::url => null()
        
        ! Used for inputs in gemini
        character(len=:), pointer::message => null()
        
        logical::temporary_file = .false.
        character(len=:), pointer::body_filename => null()
        character(len=64)::body_mimetype
        
        contains
        
        procedure :: destroy => response_destroy
        procedure :: set_message => response_set_message
        procedure :: set_url => response_set_url
        procedure :: set_body_contents => response_temp_file_contents
        procedure :: set_filename => response_set_filename_using_allocation
        
    end type
    
    type :: request
    
        character(len=:), pointer::url => null()
        character(len=:), pointer::server => null()
        character(len=:), pointer::protocol => null()
        character(len=:), pointer::location => null()
        character(len=:), pointer::query_string => null()
    
        contains
        
        procedure :: init => request_init
        procedure :: destroy => request_destroy
        procedure :: last_component => request_last_component
        procedure :: path_component => request_component
        procedure :: path_component_int => request_component_int
        procedure :: component => request_component_func

    end type request
    
    type, extends(request) :: titan_request

        integer(kind=8)::size
        character(len=:), pointer::mimetype
        character(len=:), pointer::token
        
        type(c_ptr)::ssl_connection
        
        contains
        
        procedure :: init_from_request => titan_request_init
        procedure :: destroy => titan_request_destroy
        procedure :: write_to => titan_write_to_filename
    
    end type titan_request
    
contains

    subroutine request_init(self, str)
    use logging
    implicit none
    
        class(request) :: self
        character(*), intent(in)::str
        
        integer::i, j, n
        
        n = len_trim(str)
        allocate(character(len=n) :: self%url)
        self%url = trim(str)
        call write_log("URL: "//self%url)
        
        i = index(str, "://")
        allocate(character(len=(i-1)) :: self%protocol)
        self%protocol = str(1:i-1)
        call write_log("Protocol: "//self%protocol)
        
        i = i + 3
        j = index(str(i:n), "/")
        if(j <= 0) then
            j = len_trim(str) + 1 - i
        end if
        allocate(character(len=(j-1)) :: self%server)
        self%server = str(i:(i+j-1))
        call write_log("Server: "//self%server//"|")
        
        i = j+i-1
        j = index(str, "?")
        if(j == 0) then
            if(n-i+1 == 0) then
                allocate(character(len=1) :: self%location)
                self%location = "/"
            else
                allocate(character(len=(n - i + 1)) :: self%location)
                self%location = str(i:n)
            end if
            call write_log("Location: "//self%location)
            
        else
            allocate(character(len=(n-j)) :: self%query_string)
            self%query_string = str(j+1:n)
            call write_log("Query: "//self%query_string)
            
            allocate(character(len=(j-i)) :: self%location)
            self%location = str(i:j-1)
            call write_log("Location: "//self%location)
        end if
    
    end subroutine request_init
    
    subroutine request_component(self, i_component, res)
    use logging
    implicit none
    
        class(request) :: self
        integer, intent(in)::i_component
        character(*), intent(out)::res
        
        integer::i, j, i_last, n
        
        res = " "
        
        n = len_trim(self%location)

        i_last = 0
        j = 0
        i = index(self%location, "/")
        do while(i /= i_last .and. j < i_component)
            j = j + 1
            
            call write_log("RC: "//self%location(i:n))
            
            i_last = i
            i = index(self%location(i_last+1:n), "/")
            i = i_last + i
        end do
        
        ! Found
        if(j == i_component) then
            if(i == i_last) then
                res = self%location(i_last+1:n)
                call write_log("Last! "//trim(res))
            else
                res = self%location(i_last+1:i-1)
            end if
        end if
        
    end subroutine request_component
    
    
    function request_component_int(self, i) result(res)
    implicit none
    
        class(request) :: self
        integer, intent(in)::i
        integer :: res
        
        character(24)::restext
        
        restext = " "
        
        call self%path_component(i, restext)
        
        read(restext, '(I16)') res
    
    end function request_component_int
    
    
    function request_component_func(self, i) result(res)
    implicit none
    
        class(request) :: self
        integer, intent(in)::i
        character(128) :: res
        
        res = " "
        
        call self%path_component(i, res)
    
    end function request_component_func

    subroutine request_last_component(self, res)
    implicit none
    
        class(request) :: self
        character(*), intent(out)::res
        
        integer::i, n
        
        n = len_trim(self%location) 
        if(n == 1) then
            res = "/"
        else
        
            i = index(self%location, "/", back=.true.)
        
            if(i == n) then
                i = index(self%location(1:n-1), "/", back=.true.)
                if(i > 0) then
                    res = self%location(i+1:n-1)
                else
                    res = "/"
                end if
            else
                res = self%location((i+1):n)
            end if
        end if
        
    end subroutine request_last_component
    
    subroutine request_destroy(self)
    implicit none
    
        class(request) :: self
        
        if(associated(self%url)) then
            deallocate(self%url)
        end if
        
        if(associated(self%server)) then
            deallocate(self%server)
        end if
        
        if(associated(self%location)) then
            deallocate(self%location)
        end if
        
        if(associated(self%query_string)) then
            deallocate(self%query_string)
        end if
        
        if(associated(self%protocol)) then
            deallocate(self%protocol)
        end if
        
    end subroutine request_destroy

    subroutine response_destroy(resp)
    implicit none
    
        class(response)::resp
    
        if(associated(resp%url)) then
            deallocate(resp%url)
        end if
        
        if(associated(resp%message)) then
            deallocate(resp%message)
        end if
        
        if(associated(resp%body_filename)) then
            if(resp%temporary_file) then
                call unlink(resp%body_filename)
            end if
            deallocate(resp%body_filename)
        end if
    
    end subroutine response_destroy
    
    subroutine response_set_message(resp, str)
    implicit none
    
        class(response)::resp
        character(*), intent(in)::str
        
        if(len_trim(str) > 0) then
            allocate(character(len=len_trim(str)) :: resp%message)
            resp%message = trim(str)
        end if
    
    end subroutine response_set_message
    
    subroutine response_set_url(resp, str)
    implicit none
    
        class(response)::resp
        character(*), intent(in)::str
        
        if(len_trim(str) > 0) then
            allocate(character(len=len_trim(str)) :: resp%url)
            resp%url = trim(str)
        end if
    
    end subroutine response_set_url
    
    subroutine response_temp_file_contents(resp, str, mimetype)
    use utilities, only: generate_temporary_filename
    implicit none
    
        class(response)::resp
        character(*), intent(in)::str
        character(*), intent(in), optional::mimetype
        
        integer::unum
        
        resp%body_filename => generate_temporary_filename()
        open(newunit=unum, file=resp%body_filename, action="write", status="new")
        write(unum, *) str
        close(unum)
        
        resp%temporary_file = .true.
        
        if(present(mimetype)) then
            resp%body_mimetype = mimetype
        else
            resp%body_mimetype = "text/plain"
        end if
    
    end subroutine response_temp_file_contents
    
    subroutine response_set_filename_using_allocation(resp, fname, mimetype)
    implicit none
    
        class(response)::resp
        character(*), intent(in)::fname
        character(*), intent(in), optional::mimetype
            
        allocate(character(len=len_trim(fname)) :: resp%body_filename)
        resp%body_filename = trim(fname)
        
        if(present(mimetype)) then
            resp%body_mimetype = mimetype
        else
            resp%body_mimetype = "text/plain"
        end if
        
    end subroutine response_set_filename_using_allocation
    
    function titan_get_request_value( full_location, label) result(p)
    implicit none
    
        character(*), intent(in)::full_location, label
        character(len=:), pointer::p
        
        integer::i, j, n, label_width
        
        label_width = len_trim(label)
        
        n = len_trim(full_location)
        i = index(full_location, ";"//trim(label)//"=")
        j = index(full_location(i+1:n), ";")
        if(j<=0) then 
            j = n
        else
            j = j+i
        end if
        i = i + label_width + 2
        
        allocate(character(len=(j-i+1))::p)
        p = full_location(i:j)
        
    end function titan_get_request_value
    
    subroutine titan_request_init(self, regular_request, ssl_connection)
    implicit none
    
        class(titan_request)::self
        class(request), intent(inout)::regular_request
        type(c_ptr)::ssl_connection
        
        character(len=:), pointer::size_text
        integer::i, ierr
        
        self%url => regular_request%url
        regular_request%url => null()
        
        self%protocol => regular_request%protocol
        regular_request%protocol => null()
        
        self%server => regular_request%server
        regular_request%server => null()
        
        self%query_string => regular_request%query_string
        
        i = index(regular_request%location, ";")
        allocate(character(len=(i-1)) :: self%location)
        self%location = regular_request%location(1:i-1)
        
        size_text => titan_get_request_value(regular_request%location, "size")
        read(size_text, *, iostat=ierr) self%size
        if(ierr <= 0) then
            self%size = 0
        end if
        deallocate(size_text)
        
        self%mimetype => titan_get_request_value(regular_request%location, "mime")
        self%token => titan_get_request_value(regular_request%location, "token")
        
        self%ssl_connection = ssl_connection
        
    end subroutine titan_request_init
    
    subroutine titan_request_destroy(self)
    implicit none
    
        class(titan_request)::self
        
        if(associated(self%mimetype)) then
            deallocate(self%mimetype)
        end if
        if(associated(self%token)) then
            deallocate(self%token)
        end if
        
        call request_destroy(self)
        
    end subroutine titan_request_destroy
    
    subroutine titan_write_to_filename(self, filename)
    use jessl, only: ssl_read
    implicit none
    
        class(titan_request)::self
        character(*), intent(in)::filename
        integer::unum
        
        character, dimension(64)::buf
        integer::bufread
        
        integer(kind=8)::bytes_to_go
        integer::i
        
        open(newunit=unum, file=filename, status="unknown", action="write", access="stream")

        bytes_to_go = self%size

        do while(bytes_to_go > 0)
            bufread = ssl_read(self%ssl_connection, buf)
            bytes_to_go = bytes_to_go - bufread
            
            do i = 1, bufread
                write(unum, '(A1)', advance='no') buf(i:i)
            end do

        end do
        
        close(unum)
        
    end subroutine titan_write_to_filename
    
end module server_response