aboutsummaryrefslogtreecommitdiff
path: root/captain/web.f90
blob: 089915913513533bed710ccac1761d93a555ca4b (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
module web
implicit none

    private
    
    public :: handle_request

    integer, parameter::REQUEST_UNKNOWN = 0
    integer, parameter::REQUEST_GET  = 1
    integer, parameter::REQUEST_POST = 2

contains

    function method()
    use utilities, only: toupper
    implicit none
    
        integer::method
        character(4)::method_text
    
        call get_environment_variable("REQUEST_METHOD", method_text)
        call toupper(method_text)
        if(trim(method_text) == "GET") then
            method = REQUEST_GET
        else if(method_text == "POST") then
            method = REQUEST_POST
        else
            method = REQUEST_UNKNOWN
        end if
        
    end function method
    
    subroutine build_request_object(req)
    use server_response, only:request
    
        type(request), intent(out)::req
        character(len=:), allocatable::url, script_name
        integer::url_size
        
        call get_environment_variable("REQUEST_URI", length=url_size)
        allocate(character(len=url_size)::url, script_name)
        call get_environment_variable("REQUEST_URI", url)
        call get_environment_variable("SCRIPT_NAME", script_name)

        ! If we're in CGI mode, treat the "server" as the script name
        call req%init(url, server_explicit=script_name, protocol_explicit="http")
        
        deallocate(url)
        deallocate(script_name)
        
    end subroutine build_request_object

    function request_templated(req) result(resp)
    use server_response, only:request, response
    implicit none
    
        type(request), intent(in)::req
        type(response)::resp
        
        
        
    end function request_templated

    subroutine handle_request()
    use server_response, only:request, response
    use logging
    use request_utils
    use http
    use iso_fortran_env, only: output_unit
    use utilities, only: echo_file_stdout
    implicit none
    
        type(request)::req
        type(response)::resp
        integer::response_size
        
        call build_request_object(req)
        
        if(is_request_static(req)) then
            call write_log("Req static", LOG_INFO)
            resp = request_static(req)
    
        else 
            call write_log("Req template", LOG_INFO)
            resp = request_templated(req)
    
        end if
        
        ! Perform the response
        select case(resp%code)
            case(HTTP_CODE_REDIRECT)
                call write_redirect(output_unit, resp%code, trim(resp%url))
                
            case(HTTP_CODE_SUCCESS)
                inquire(file=resp%body_filename, size=response_size)
                call write_response_headers(output_unit, resp%code, response_size, trim(resp%body_mimetype))
                call echo_file_stdout(resp%body_filename)
                
            ! Need some more...
            
        end select
    
        call resp%destroy()
        call req%destroy()
    
    end subroutine handle_request

end module web