aboutsummaryrefslogtreecommitdiff
path: root/common/protocol.f90
blob: 9fc1171a0f7b9bf61f762e033309014ac9ab1f86 (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
! 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 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_BADRESPONSE  = 7
    integer, parameter::STATUS_LOCALFAIL    = -1
    integer, parameter::STATUS_CONNECTFAIL  = -2
    integer, parameter::STATUS_PROTOCOLFAIL = -3

    integer, parameter::BUFFER_SIZE = 256
    
    integer, parameter::gemini_default_port = 1965

contains
    
    function is_failure_code(return_code)
    implicit none
    
        integer, intent(in)::return_code
        logical::is_failure_code
        
        is_failure_code = any(return_code == &
            [STATUS_CONNECTFAIL, &
             STATUS_LOCALFAIL, &
             STATUS_BADRESPONSE, &
             STATUS_PERMFAIL, &
             STATUS_TEMPFAIL, &
             STATUS_PROTOCOLFAIL])

    end function is_failure_code
    
    subroutine get_mimetype(status_line, return_type)
    implicit none
    
        character(*), intent(in)::status_line
        character(*), intent(out)::return_type
        
        integer::i, j
        
        return_type = " "
        
        ! Advance past status and space
        i = 1
        do while(status_line(i:i) /= " " .and. status_line(i:i) /= char(9) .and. i < len_trim(status_line))
            i = i + 1
        end do
        do while((status_line(i:i) == " " .or. status_line(i:i) == char(9)) .and. i < len_trim(status_line))
            i = i + 1
        end do
        
        j = 0
        do while(.not. any([char(13), char(10)] == status_line(i:i)))
            j = j + 1
            return_type(j:j) = status_line(i:i)
            i = i + 1
        end do
        
        ! Default
        if(len_trim(return_type) == 0) then
            return_type = "text/gemini"
        end if
    
    end subroutine get_mimetype
    
    function get_return_code(status_line) result(ret)
    implicit none
    
        integer::ret
        character(*), intent(in)::status_line
        integer::istatus
        
        read(status_line, '(i1)', iostat=istatus) ret
        
        if(istatus /= 0) then 
            ret = STATUS_BADRESPONSE
        end if
        
    end function get_return_code

    function request_url(url, unit_number, return_type) result(returncode)
    use request
    use iso_c_binding
    implicit none
    
        character(*), intent(inout)::url
        integer, intent(in)::unit_number
        character(*), intent(out)::return_type
        integer::port
        
        integer::returncode
        
        character(:), allocatable::server
        
        type(connection)::conn
        
        integer::bytes_received, i
        character, dimension(BUFFER_SIZE)::buffer
        
        ! For direct processing of the reponse line
        integer::response_line_index
        character(1024)::response_line
        logical::response_line_completed
        
        returncode = -1
        rewind(unit_number)
        

        allocate(character(len=len_trim(url)) :: server)
        call get_server_from_url(url, server, port)
        if(port < 0) then
            port = gemini_default_port
        end if
    
        ! Correct URL relative paths
        call fix_url_relative_paths(url)
    
        conn = open_connection(server)
        
        if(conn%code == CONNECTION_OPEN) then
            if(send_string(conn%ssl, trim(url)//c_carriage_return//c_new_line, trimming=.false.)) then
                
                response_line_completed = .false.
                response_line = " "
                response_line_index = 0

                bytes_received = retrieve_characters(conn%ssl, buffer)
                
                do while(bytes_received > 0)
                    do i=1, bytes_received
                        if(.not. response_line_completed) then
                            response_line_index = response_line_index + 1
                            response_line(response_line_index:response_line_index) = buffer(i)
                            
                            ! If we encountered our first newline, we have a complete status
                            ! line - handle it here
                            if(buffer(i) == char(10) .or. response_line_index == 1024) then
                                response_line_completed = .true.
                                
                                returncode = get_return_code(response_line)
                                
                                if(returncode == STATUS_SUCCESS) then
                                
                                    call get_mimetype(response_line, return_type)

                                else
                                
                                    exit

                                end if
                                
                            end if
                            
                        else
                        
                            !write(unit_number, '(A1)', advance='no') buffer(i)
                            write(unit_number) buffer(i)

                        end if

                    end do

                    bytes_received = retrieve_characters(conn%ssl, buffer)
                end do
                
            else
            
                returncode = STATUS_CONNECTFAIL
                
            end if
            
        else
        
            Print *, "Connection Failed with Code: ", conn%code
            
            returncode = STATUS_CONNECTFAIL
            
        end if
        
        call close_connection(conn)
    
    end function request_url

    function titan_post_url(url, unit_number, file_length, token, is_plain_text, extra) result(returncode)
    use request
    use iso_c_binding
    use utilities, only: read_into_buffer
    use jessl, only: ssl_write
    implicit none
    
        character(*), intent(inout)::url
        character(*), intent(in)::token
        integer, intent(in)::unit_number
        logical, intent(in), optional::is_plain_text
        character(*), intent(in), optional::extra
        integer::port
        
        integer::returncode

        integer(kind=8), intent(in)::file_length
        
        character(:), allocatable::server
        character(len=16)::file_length_text
        
        type(connection)::conn
        
        character, dimension(BUFFER_SIZE)::buffer
        logical, dimension(5)::successes
        integer::i, bytes_read, bytes_written, total_written
        
        ! For direct processing of the reponse line
        integer::response_line_index, bytes_received
        character(1024)::response_line
        logical::response_line_completed
        
        returncode = -1
        rewind(unit_number)
        
        allocate(character(len=len_trim(url)) :: server)
        call get_server_from_url(url, server, port)
        if(port < 0) then
            port = gemini_default_port
        end if
    
        ! Correct URL relative paths
        call fix_url_relative_paths(url)
    
        conn = open_connection(server)
        
        if(conn%code == CONNECTION_OPEN) then
        
            successes(1) = send_string(conn%ssl, trim(url), trimming=.false., allow_trailing_null=.false.)
            successes(2) = send_string(conn%ssl, ";token="//trim(token), &
                                       trimming=.false., allow_trailing_null=.false.)
            
            if(present(is_plain_text)) then
                if(is_plain_text) then
                    successes(3) = send_string(conn%ssl, ";mime=text/plain", &
                                               trimming=.false., allow_trailing_null=.false.)
                else
                    successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", &
                                               trimming=.false., allow_trailing_null=.false.)
                end if
            else
                successes(3) = send_string(conn%ssl, ";mime=application/octet-stream", &
                                           trimming=.false., allow_trailing_null=.false.)
            end if
            
            if(present(extra)) then
                successes(4) = send_string(conn%ssl, ";"//trim(extra), trimming=.false., allow_trailing_null=.false.)
            else
                successes(4) = .true.
            end if
            
            write(file_length_text, '(I14)') file_length
            file_length_text = adjustl(file_length_text)
            
            successes(5) = send_string(conn%ssl, &
                                       ";size="//trim(file_length_text)//c_carriage_return//c_new_line, &
                                       trimming=.false., &
                                       allow_trailing_null=.false.)
        
            if(all(successes, 1)) then
                
                total_written = 0
                bytes_read = read_into_buffer(unit_number, buffer)
                
                !Print *, "bytes read for sending: ", bytes_read
                
                bytes_written = 0
                do while(bytes_read > 0 .and. bytes_written >= 0)
                    bytes_written = ssl_write(conn%ssl, buffer(1:bytes_read))
                    total_written = total_written + bytes_written
                    bytes_read = read_into_buffer(unit_number, buffer)
                    !Print *, "bytes read for sending now: ", bytes_read, " and so far, we wrote", total_written 
                end do
                
                if(total_written >= file_length .and. bytes_written >= 0) then
                    
                    Print *, "Upload ok: ", total_written, file_length
                    
                    response_line_completed = .false.
                    response_line = " "
                    response_line_index = 0
                    
                    bytes_received = retrieve_characters(conn%ssl, buffer)
                    do while(bytes_received > 0)
                    
                        do i=1, bytes_received
                            response_line_index = response_line_index + 1
                            response_line(response_line_index:response_line_index) = buffer(i)
                            
                            ! If we encountered our first newline, we have a complete status
                            ! line - handle it here
                            if(buffer(i) == char(10) .or. response_line_index == 1024) then
                                response_line_completed = .true.
                                
                                returncode = get_return_code(response_line)
                                
                            end if

                        end do

                        bytes_received = retrieve_characters(conn%ssl, buffer)
                    end do
                    
                else
                    Print *, "Marking localfail"
                    returncode = STATUS_LOCALFAIL
                end if
                
            else
            
                returncode = STATUS_CONNECTFAIL
                
            end if
            
        else
        
            returncode = STATUS_CONNECTFAIL
            
        end if
        
        call close_connection(conn)

    end function titan_post_url

    subroutine get_redirect_url(unit_number, url)
    implicit none
    
        integer, intent(in)::unit_number
        character(*), intent(inout)::url
        
        character(1024)::new_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
        
        new_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
            new_url(i:i) = search
            read(unit_number, '(A1)', advance='no', iostat=istat) search
        end do

        ! Process this url properly
        if(index(new_url, "://") > 0) then
            url = new_url
        else
            call handle_relative_url(url, new_url)
        end if
        
    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(path(1:2) == "//") then
            current_url = "gemini:"//path
            
        else 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)), "/")
                if(first_slash < 1) then
                    current_url = trim(current_url)//trim(path)
                else
                    current_url = current_url(1:(past_protocol + first_slash - 1))//path(2:len_trim(path))
                end if
                
            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
    
    subroutine replace_text(string, pattern, replacement, once)
    implicit none
    
        character(*), intent(inout)::string
        character(*), intent(in)::pattern
        character(*), intent(in), optional::replacement
        logical, intent(in), optional::once
        
        integer::i,j
                
        i = index(string, pattern)
        do while(i > 0) 
            j = i + len(pattern) ! First character after match
            
            ! First
            if(i == 1) then
                if(present(replacement)) then
                    string = replacement//string(j:len_trim(string))
                else
                    string = string(j:len_trim(string))
                end if
                
            ! Last
            elseif(j > len_trim(string)) then
                if(present(replacement)) then
                    string = string(1:i-1)//replacement
                else
                    string = string(1:i-1)
                end if
        
            ! Middle
            else
                if(present(replacement)) then
                    string = string(1:i-1)//replacement//string(j:len_trim(string))
                else
                    string = string(1:i-1)//string(j:len_trim(string))
                end if
            end if
            
            ! All on the same line crashes GNU Fortran...
            if(present(once)) then
                if(once) then
                    exit
                end if
            end if
            
            i = index(string, pattern)
        end do
    
    end subroutine replace_text

    subroutine fix_url_relative_paths(url)
    implicit none
    
        character(*), intent(inout)::url
        integer::i, pattern_start
        
        ! These shouldn't be there
        i = index(url, '/./')
        if(i > 0) then
            call replace_text(url, '/./', replacement="/")
        end if
            
        ! Remove ..
        i = index(url, '/../')
        do while(i > 0)
        
            ! Need to build the pattern
            pattern_start = i - 1
            do while(url(pattern_start:pattern_start) /= "/" .and. pattern_start > 1)
                pattern_start = pattern_start - 1
            end do
            
            if(pattern_start == 1) then
                ! Error state - just exit
                exit
            end if
            
            ! Check for root...
            if(url(pattern_start-1:pattern_start) == "//") then
                call replace_text(url, "/../", replacement="/", once=.true.)
            else
                call replace_text(url, url(pattern_start:i+2), once=.true.)
            end if
            
            i = index(url, '/../')
        end do
        
    end subroutine fix_url_relative_paths

end module gemini_protocol