-
Notifications
You must be signed in to change notification settings - Fork 0
/
fortran_fcgi.f90
116 lines (89 loc) · 3.84 KB
/
fortran_fcgi.f90
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
!
! Fortran FastCGI stack
! Based on Fortran FastCGI by Ricolindo.Carino@gmail.com and arjen.markus895@gmail.com
!
! Requires:
! - the FLIBS modules cgi_protocol and fcgi_protocol
! - the FastCGI library
! See 'readme' for setup instructions
!
program test_fcgi
use fcgi_protocol
use jade
implicit none
type(DICT_STRUCT), pointer :: dict => null() ! Initialisation is important!
logical :: stopped = .false. ! set to true in respond() to terminate program
integer :: unitNo ! unit number for a scratch file
! open scratch file
open(newunit=unitNo, status='scratch')
! comment previous line AND uncomment next line for debugging;
!open(newunit=unitNo, file='fcgiout', status='unknown') ! file 'fcgiout' will show %REMARKS%
! wait for environment variables from webserver
do while (fcgip_accept_environment_variables() >= 0)
! build dictionary from GET or POST data, environment variables
call fcgip_make_dictionary( dict, unitNo )
! give dictionary to the user supplied routine
! routine writes the response to unitNo
! routine sets stopped to true to terminate program
call respond(dict, unitNo, stopped)
! copy file unitNo to the webserver
call fcgip_put_file( unitNo, 'text/html' )
! terminate?
if (stopped) exit
end do ! while (fcgip_accept_environment_variables() >= 0)
! before termination, it is good practice to close files that are open
close(unitNo)
! webserver will return an error since this process will now terminate
unitNo = fcgip_accept_environment_variables()
contains
subroutine respond ( dict, unitNo, stopped )
type(DICT_STRUCT), pointer :: dict
integer, intent(in) :: unitNo
logical, intent(out) :: stopped
! the following are defined in fcgi_protocol
!character(len=3), parameter :: AFORMAT = '(a)'
!character(len=2), parameter :: CRLF = achar(13)//achar(10)
!character(len=1), parameter :: NUL = achar(0)
! retrieve params from model and pass them to view
character(len=50), dimension(10,2) :: pagevars
character(len=50), dimension(8) :: names, latinNames, wikiLinks, descriptions
! the script name
character(len=80) :: scriptName, query
character(len=12000) :: templatefile
logical :: okInputs
! start of response
! lines starting with %REMARK% are for debugging & will not be copied to webserver
write(unitNo, AFORMAT) &
'%REMARK% respond() started ...', &
'<!DOCTYPE html>', &
'<html>', &
'<head>', &
'<meta charset="utf-8"/>', &
'<meta name="viewport" content="width=device-width, initial-scale=1"/>', &
'<title>fortran</title>', &
'<link rel="stylesheet" type="text/css" href="/static/bootstrap.min.css"/>', &
'</head>', &
'<body>'
! retrieve script name (key=DOCUMENT_URI) from dictionary
call cgi_get( dict, "DOCUMENT_URI", scriptName )
select case (trim(scriptName))
case ('/')
! most pages look like this
templatefile = 'template/index.jade'
call jadefile(templatefile, unitNo)
case ('/test')
templatefile = 'template/test.jade'
call jadefile(templatefile, unitNo)
case ('/search')
write(unitNo,AFORMAT) '</div>'
case DEFAULT
! your 404 page
write(unitNo,AFORMAT) 'Page not found!'
end select
! end of response
write(unitNo,AFORMAT) '</body>', &
'</html>', &
'%REMARK% respond() completed ...'
return
end subroutine respond
end program test_fcgi