aboutsummaryrefslogtreecommitdiff
path: root/captain/response.f90
blob: 8934e4e10f545249554fb8914403918686a462bc (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
! Copyright (c) 2021 Approximatrix, LLC <support@approximatrix.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 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_TEMPFAIL = 40
    integer, parameter::GEMINI_CODE_PERMFAIL = 50

    character(*), parameter::RESPONSE_JSON_OKAY = '{"status": "okay"}'

    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::page => null()
        character(len=:), pointer::query_string => null()
        character(len=4)::method = "GET"
    
        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 :: path_starting_with_component => request_component_starting_with
        procedure :: component => request_component_func
        procedure :: is_get => request_is_get
        procedure :: is_post => request_is_post

    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, server_explicit, protocol_explicit, method)
    use logging
    use utilities, only: toupper
    implicit none
    
        class(request) :: self
        character(*), intent(in)::str
        character(*), intent(in), optional::server_explicit, protocol_explicit, method
        
        character(len=:), allocatable::temppage
        integer::i, j, n
        
        character(64)::msg
        
        n = len_trim(str)
        allocate(character(len=n) :: self%url)
        self%url = trim(str)
        call write_log("URL: "//self%url, LOG_NORMAL)
        
        i = index(str, "://")
        if(i <= 0 .and. present(protocol_explicit)) then
            allocate(character(len=len_trim(protocol_explicit)) :: self%protocol)
            self%protocol = protocol_explicit
            i = 1
        else
            allocate(character(len=(i-1)) :: self%protocol)
            self%protocol = str(1:i-1)
            i = i + 3
        end if
        call write_log("Protocol: "//self%protocol, LOG_DEBUG)
        
        ! We only want to "assume" the server if a :// was never found
        if(i == 1 .and. present(server_explicit)) then
            allocate(character(len=len_trim(server_explicit)) :: self%server)
            self%server = server_explicit
            
            ! This string, in CGI cases, actually represents the SCRIPT root,
            ! so we need to skip ahead of it in the URL if it is there...
            i = index(str, self%server)
            if(i > 0) then
                i = i + len(self%server)
            else
                i = 1
            end if
            
        else
        
            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))
            
            i = j+i-1
             
        end if
        call write_log("Server: "//self%server//"|", LOG_DEBUG)
        
        j = index(str, "?")
        if(j == 0) then
            if(n-i+1 == 0) then
                allocate(character(len=1) :: self%location)
                self%location = "/"
            else
                write(msg, '(I12,1X,I12)') i, n
                call write_log("i n: "//trim(msg), LOG_DEBUG)
                
                allocate(character(len=(n - i + 1)) :: self%location)
                call write_log("str(i:n): "//str(i:n), LOG_DEBUG)
                self%location = str(i:n)
            end if
            call write_log("Location: "//self%location, LOG_DEBUG)
            
        else
            allocate(character(len=(n-j)) :: self%query_string)
            self%query_string = str(j+1:n)
            call write_log("Query: "//self%query_string, LOG_DEBUG)
            
            allocate(character(len=(j-i)) :: self%location)
            self%location = str(i:j-1)
            call write_log("Location: "//self%location, LOG_DEBUG)
        end if
    
        ! and page, which is really just a last location if there is an extension...
        ! I realize this is not so great, but whatever.
        allocate(character(len=len(self%location)) :: temppage)
        call self%last_component(temppage)
        if(index(temppage, ".") > 0) then
            allocate(character(len=len_trim(temppage)) :: self%page)
            self%page = temppage
        end if
        deallocate(temppage)
        
        if(present(method)) then
            self%method = method
            call toupper(self%method)
        end if
    
    end subroutine request_init
    
    function request_component_start_location(self, i_component) result(res)
    implicit none
    
        class(request) :: self
        integer, intent(in)::i_component
        integer::res
        
        integer::i, j, i_last, n
        
        res = -1
    
        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
            
            i_last = i
            i = index(self%location(i_last+1:n), "/")
            i = i_last + i
        end do
        
        ! Found
        if(j == i_component) then
            res = i_last + 1
        end if
    
    end function request_component_start_location
    
    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
            
            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)
            else
                res = self%location(i_last+1:i-1)
            end if
        end if
        
    end subroutine request_component
    
    subroutine request_component_starting_with(self, i_component, res)
    implicit none
    
        class(request) :: self
        integer, intent(in)::i_component
        character(*), intent(out)::res
        
        integer::string_index_component
        
        string_index_component = request_component_start_location(self, i_component)
        
        if(string_index_component > 0) then
            res = self%location(string_index_component:len_trim(self%location))
        else
            res = " "
        end if
        
    end subroutine request_component_starting_with
    
    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
    
    function request_is_get(self) result(res)
    implicit none
    
        class(request)::self
        logical::res
        
        res = (self%method == "GET")
    
    end function request_is_get
    
    function request_is_post(self) result(res)
    implicit none
    
        class(request)::self
        logical::res
        
        res = (self%method == "POST")
    
    end function request_is_post
    
    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
        
        if(associated(self%page)) then
            deallocate(self%page)
        end if
        
    end subroutine request_destroy

    subroutine response_destroy(resp)
    use utilities, only: delete_file
    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 delete_file(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-1
        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, '(I16)', 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
    
    function titan_write_to_filename(self, filename) result(success)
    use jessl, only: ssl_read
    use logging
    implicit none
    
        class(titan_request)::self
        character(*), intent(in)::filename
        integer::unum
        
        character(len=1), dimension(1024)::buf
        integer::bufread
        
        integer(kind=8)::bytes_to_go
        integer::istat
        
        logical::success
        
        !character(128)::msg
        
        success = .false.
        
        open(newunit=unum, file=filename, status="unknown", &
             action="write", access='stream', iostat=istat) 

        bytes_to_go = self%size
        
        do while(bytes_to_go > 0 .and. istat == 0)
            bufread = ssl_read(self%ssl_connection, buf)
            bytes_to_go = bytes_to_go - bufread

            write(unum, iostat=istat) buf(1:bufread)

        end do
        
        close(unum)
        
        success = (istat == 0)
        
    end function titan_write_to_filename
    
end module server_response