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
|
module external_handling
implicit none
contains
function generate_players_gemini() result(res)
use captain_db
implicit none
character(len=:), pointer::res
character(len=PLAYER_NAME_LENGTH), dimension(:), pointer::players
integer::n, i, nsize
character(len=3*PLAYER_NAME_LENGTH)::one_player
n = get_player_count()
if(n == 0) then
allocate(character(len=1024) :: res)
res = "None Yet"
else
players => get_player_names()
nsize = 1024
do i = 1, size(players)
nsize = nsize + 16 + 2*len_trim(players(i))
end do
allocate(character(len=nsize) :: res)
res = "## Existing Players"
do i = 1, n
one_player = "=> /players/"//trim(players(i))//".gmi "//trim(players(i))
if(i == 1) then
res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//trim(one_player)
else
res = trim(res)//new_line(res(1:1))//trim(one_player)
end if
end do
deallocate(players)
end if
res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//"## Management"// &
new_line(res(1:1))//"=> /players/add.gmi Add Player"
end function generate_players_gemini
pure function is_input_provided_request(req)
use server_response, only: request
implicit none
class(request), intent(in)::req
logical::is_input_provided_request
is_input_provided_request = associated(req%query_string)
end function is_input_provided_request
pure function is_input_required_request(req)
use server_response, only: request
implicit none
class(request), intent(in)::req
logical::is_input_required_request
is_input_required_request = .false.
if(req%location == "/players/add.gmi") then
is_input_required_request = .true.
end if
end function is_input_required_request
function external_input_required_gemini(req) result(resp)
use server_response
implicit none
class(request), intent(in)::req
type(response)::resp
resp%code = GEMINI_CODE_INPUT
if(req%location == "/players/add.gmi") then
call resp%set_message("Enter name of new player to add")
end if
end function external_input_required_gemini
function external_input_request_gemini(req) result(resp)
use server_response
use captain_db
implicit none
class(request), intent(in)::req
type(response)::resp
if(req%location == "/players/add.gmi") then
call add_player_db(req%query_string)
resp%code = GEMINI_CODE_REDIRECT
call resp%set_url("/players.gmi")
end if
end function external_input_request_gemini
function is_request_static(req)
use server_response
implicit none
class(request), intent(in)::req
logical::is_request_static
character(64)::first
call req%path_component(1, first)
is_request_static = ((first == "releases") .or. (first == "uploads"))
end function is_request_static
function external_request_static(req) result(resp)
use logging, only: write_log
use config
use utilities
use server_response
implicit none
class(request), intent(in)::req
type(response)::resp
character(64)::category
character(256)::filename
logical::exists
resp%temporary_file = .false.
call req%path_component(1, category)
call req%last_component(filename)
if(category == "releases") then
allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: resp%body_filename)
call combine_paths(release_dir, filename, resp%body_filename)
else if(category == "uploads") then
allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: resp%body_filename)
call combine_paths(release_dir, filename, resp%body_filename)
end if
inquire(file=resp%body_filename, exist=exists)
if(.not. exists) then
resp%code = GEMINI_CODE_PERMFAIL
else
resp%code = GEMINI_CODE_SUCCESS
if(index(filename, ".gmi") /= 0) then
resp%body_mimetype = "text/gemini"
else if(index(filename, ".txt") /= 0) then
resp%body_mimetype = "text/plain"
! Just a catch-all, whatever...
else
resp%body_mimetype = "application/octet-stream"
end if
end if
end function external_request_static
function external_request_templated(req) result(resp)
use page_template
use config, only: template_filepath, project
use logging, only: write_log
use server_response
implicit none
class(request), intent(in)::req
type(response)::resp
character(1024)::template_file
type(template)::page
character(len=:), pointer::contents
! Open the base template
call template_filepath("index.gmi", template_file)
call page%init(trim(template_file))
call write_log("Processing request")
if(trim(req%location) == "/" .or. trim(req%location) == "/index.gmi") then
call write_log("Assign")
call page%assign('title', 'Home')
call write_log("Assign done")
else if(trim(req%location) == "/releases.gmi") then
call page%assign('title', 'Releases')
else if(trim(req%location) == "/jobs.gmi") then
call page%assign('title', 'Jobs')
else if(trim(req%location) == "/players.gmi") then
call page%assign('title', 'Players')
contents => generate_players_gemini()
call page%assign('contents', contents)
else if(req%location(1:9) == '/players/') then
else if(trim(req%location) == "/about.gmi") then
call page%assign('title', 'About')
else
call page%assign('title', 'Not Found')
end if
call page%assign('project', project)
call write_log("Rendering page for "//req%location)
call page%render()
call write_log("Finalizing response")
resp%temporary_file = .true.
resp%body_filename => page%output_filename
resp%body_mimetype = "text/gemini"
resp%code = GEMINI_CODE_SUCCESS
end function external_request_templated
function external_request_gemini(req) result(resp)
use server_response
use logging
implicit none
class(request), intent(in)::req
type(response)::resp
if(is_input_provided_request(req)) then
call write_log("Input request")
resp = external_input_request_gemini(req)
else if(is_input_required_request(req)) then
call write_log("Input required")
resp = external_input_required_gemini(req)
else if(is_request_static(req)) then
call write_log("Req static")
resp = external_request_static(req)
else
call write_log("Req template")
resp = external_request_templated(req)
end if
end function external_request_gemini
end module external_handling
|