aboutsummaryrefslogtreecommitdiff
path: root/captain/response.f90
blob: 02ed6ddce9257f2e8984e6ca1af289965631c7c6 (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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
! 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
use query_utilities
implicit none

    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

        character(len=:), pointer::cookiecmd => null()
        
        contains
        
        procedure :: destroy => response_destroy
        procedure :: set_message => response_set_message
        procedure :: set_url => response_set_url
        procedure :: set_gemini_session_url => reponse_set_gemini_session_url
        procedure :: set_body_contents => response_temp_file_contents
        procedure :: set_filename => response_set_filename_using_allocation
        procedure :: set_cookie_cmd => response_set_cookie_cmd
        procedure :: set_cookie => response_set_cookie
        
    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=:), pointer::token => null()
        character(len=4)::method = "GET"
        
        integer::auth_level
        
        type(query)::q
        type(cookies)::c
    
        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
        procedure :: has_query => request_has_query
        procedure :: is_authenticated_user => request_is_authenticated_user
        procedure :: set_token => request_set_token
        procedure :: remove_first_path_component => request_remove_first_path_component
        procedure :: clear_token => request_clear_token
        
    end type request
    
    type, extends(request) :: titan_request

        integer(kind=8)::size
        character(len=:), pointer::mimetype
        
        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

    function gemini_session_link_url(link, session_token) result(res)
    implicit none
    
        character(*), intent(in)::link
        character(len=:), pointer, intent(in), optional::session_token
        character(len=:), pointer::res
        logical::prepend_session
        integer::nl
    
        nl = len_trim(link)
    
        prepend_session = .false.
        if(link(1:1) == "/") then
            if(present(session_token)) then
                if(associated(session_token)) then
                    nl = nl + 9 + len_trim(session_token)
                    prepend_session = .true.
                end if
            end if
        end if
        
        allocate(character(len=nl)::res)
        
        if(prepend_session) then
            res = "/session-"//trim(session_token)//trim(link)
        else
            res = link
        end if
    
    end function gemini_session_link_url

    subroutine request_init(self, str, server_explicit, protocol_explicit, method, cookiestring)
    use logging
    use utilities, only: toupper
    use captain_db, only: get_session_auth_db
    use auth_levels, only: AUTH_NONE
    implicit none
    
        class(request) :: self
        character(*), intent(in)::str
        character(*), intent(in), optional::server_explicit, protocol_explicit, method, cookiestring
        
        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
    
        if(associated(self%query_string)) then
            call self%q%init(self%query_string)
            
            if(associated(self%q%get_value("token"))) then
                self%token => self%q%get_value("token")
            end if
        else
            call self%q%init()
        end if
    
        if(present(cookiestring)) then
            call self%c%init(cookiestring)
            if(.not.associated(self%token) .and. associated(self%c%get_value("token"))) then
                self%token => self%c%get_value("token")
            end if
        else
            call self%c%init()
        end if
        
        if(associated(self%token)) then
            self%auth_level = get_session_auth_db(self%token)
        else
            self%auth_level = AUTH_NONE
        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
    
    function request_is_authenticated_user(self)
    use captain_db
    implicit none
    
        class(request), intent(in)::self
        logical::request_is_authenticated_user
        
        request_is_authenticated_user = .false.
        if(associated(self%token)) then
            request_is_authenticated_user = is_valid_session_db(self%token)
            if(request_is_authenticated_user) then
                call update_session_db(self%token)
            end if
        end if
    
    end function request_is_authenticated_user
    
    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
    
    function request_has_query(self)
    implicit none
    
        class(request)::self
        logical::request_has_query
        
        request_has_query = associated(self%query_string) .and. self%q%component_count() > 0
        
    end function request_has_query
    
    subroutine request_set_token(self, token)
    use captain_db
    implicit none
    
        class(request), intent(inout)::self
        character(len=*), intent(in)::token
        
        allocate(character(len=len_trim(token)) :: self%token)
        self%token = token
        self%auth_level = get_session_auth_db(self%token)
        
    end subroutine request_set_token
    
    subroutine request_clear_token(self)
    implicit none
    
        class(request), intent(inout)::self
        logical::safe_to_deallocate
        
        if(associated(self%token)) then
            safe_to_deallocate = .not. (associated(self%c%get_value("token"), self%token) .or. &
                                        associated(self%q%get_value("token"), self%token))
            
            if(safe_to_deallocate) then
                deallocate(self%token)
            end if
           
        end if
        
        self%token => null()
        self%auth_level = 0
        
    end subroutine request_clear_token
    
    ! This routine is needed if we're stripping session identifiers
    ! in Gemini URLs
    subroutine request_remove_first_path_component(self)
    implicit none
    
        class(request), intent(inout)::self
        character(len=:), pointer::first, newloc
        
        integer::i, j, n
        
        i = 1
        if(self%location(1:1) == "/") then
            i = i + 1
        end if
        
        n = len_trim(self%location)
        j = index(self%location(i:n), "/") + (i-1)
        if(j == 0) then
            j = n
        end if
        
        ! First component should be self%location(i:j)
        n = n - (j-i+1)
        
        allocate(character(len=n)::newloc)
        if(i > 1) then
            newloc(1:1) = self%location(1:1)
        end if
        
        newloc(2:n) = self%location(j+1:len_trim(self%location))
        deallocate(self%location)
        self%location => newloc
        newloc => null()
    
    end subroutine request_remove_first_path_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
        
        if(associated(self%page)) then
            deallocate(self%page)
        end if
        
        ! Needs to happen before we destroy the cookies and query string
        call self%clear_token()
        
        call self%q%destroy()
        call self%c%destroy()
        
    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
        
        if(associated(resp%cookiecmd)) then
            deallocate(resp%cookiecmd)
        end if
    
    end subroutine response_destroy
    
    subroutine response_set_cookie_cmd(resp, str)
    implicit none
    
        class(response)::resp
        character(*), intent(in)::str
        
        integer::newlength
        character(len=:), pointer::tmp
        
        if(len_trim(str) > 0) then
            if(associated(resp%cookiecmd)) then
                newlength = len(resp%cookiecmd)+len_trim(str)+1
                allocate(character(len=newlength)::tmp)
                tmp = resp%cookiecmd//new_line(' ')//trim(str)
                deallocate(resp%cookiecmd)
                resp%cookiecmd => tmp
                tmp => null()
            else
                allocate(character(len=len_trim(str)) :: resp%cookiecmd)
                resp%cookiecmd = trim(str)
            end if
        end if
    
    end subroutine response_set_cookie_cmd
    
    subroutine response_set_cookie(resp, k, v, httponly)
    implicit none
    
        class(response)::resp
        character(len=*), intent(in)::k, v
        logical, optional::httponly
        
        character(len=10)::httponly_trailing
        
        httponly_trailing = "; HttpOnly"
        if(present(httponly)) then
            if(.not. httponly) then
                httponly_trailing = " "
            end if
        end if
        
        call resp%set_cookie_cmd("Set-Cookie: "//trim(k)//"="//trim(v)//"; SameSite=Strict"//trim(httponly_trailing))
        
    end subroutine response_set_cookie
    
    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 reponse_set_gemini_session_url(resp, str, session_token)
    !use request_utils, only: gemini_session_link_url
    implicit none
    
        class(response), intent(inout)::resp
        character(*), intent(in)::str
        character(len=:), pointer, intent(in)::session_token
        character(len=:), pointer::session_url
        
        if(associated(session_token)) then
            session_url => gemini_session_link_url(str, session_token)
            call resp%set_url(session_url)
            deallocate(session_url)
        else
            call resp%set_url(str)
        end if
    
    end subroutine reponse_set_gemini_session_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
            if(.not. associated(self%q%get_value("token"), self%token)) then
                deallocate(self%token)
            end if
        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, file_exists
        
        !character(128)::msg
        
        success = .false.
        
        ! Writing to an existing file has been broken for some reason...
        inquire(file=filename, exist=file_exists)
        if(file_exists) then
            ! GNU Extension, boo...
            call unlink(filename)
        end if
        
        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