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
|
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 html_link(link, label) result(res)
implicit none
character(*), intent(in)::link, label
character(len=:), pointer::res
integer::nl
nl = len_trim(link) + len_trim(label) + len('<a href=""></a>')
allocate(character(len=nl)::res)
res = '<a href="'//trim(link)//'">'//trim(label)//'</a>'
end function html_link
function generate_one_job_html(req) result(res)
use captain_db
use server_response
use special_filenames, only: get_task_result_static_filename
use request_utils, only: get_status_utf8
implicit none
type(request), intent(in)::req
character(len=:), pointer::res
character(4)::status
integer::i, j, job_id
type(job)::one_job
character(PLAYER_NAME_LENGTH)::player
character(1)::nl = new_line(' ')
type(task), dimension(:), pointer::tasks
character(32)::task_text, job_text
character(len=:), pointer::task_results_filename, one_link, local_task_results_filename
logical::file_exists
res => null()
! Ugh, all this nonsense
call req%last_component(job_text)
j = index(job_text, ".", back=.true.)
job_text(j:len(job_text)) = " "
read(job_text, '(I8)') job_id
one_job = get_job(job_id)
if(one_job%id >= 0) then
allocate(character(len=1024) :: res)
status = get_status_utf8(one_job%status)
res = "<h2>Status - "//status//"</h2>"
call get_player_name(one_job%player, player)
res = trim(res)//nl//nl//"<p><em>Running on "//trim(player)//nl// &
"Last update at: "//one_job%time//"</em></p>"
res = trim(res)//nl//nl//"<h3>Task Results</h3>"
tasks => get_job_tasks(job_id)
if(associated(tasks)) then
res = trim(res)//nl//"<ul>"
do i = 1, size(tasks)
status = get_status_utf8(tasks(i)%status)
task_results_filename => get_task_result_static_filename(one_job%id, i, no_path=.true.)
local_task_results_filename => get_task_result_static_filename(one_job%id, i, no_path=.false.)
inquire(file=local_task_results_filename, exist=file_exists)
write(task_text, '(I8)') i
if(file_exists) then
one_link => html_link("../results/"//task_results_filename, &
trim(status)//" - Task "//trim(adjustl(task_text)))
res = trim(res)//nl//" <li>"//trim(one_link)//"</li>"
deallocate(one_link)
else
res = trim(res)//nl//" <li>"// &
trim(status)//" - Task "//trim(adjustl(task_text))//"</li>"
end if
deallocate(local_task_results_filename)
deallocate(task_results_filename)
end do
res = trim(res)//nl//"</ul>"
deallocate(tasks)
else
res = trim(res)//nl//nl//"<p><strong>None reported yet</strong></p>"
end if
else
allocate(character(len=64) :: res)
write(res, '(A20, 1X, I5)') "No record of:", job_id
end if
end function generate_one_job_html
function generate_releases_html(req) result(res)
use utilities
use server_response
use config
implicit none
type(request), intent(in)::req
character(len=:), pointer::res
character(len=DIR_LIST_STRING_LENGTH), dimension(:), pointer::directories
character(len=DIR_LIST_STRING_LENGTH), dimension(:), pointer::files
character(1024)::public_path, local_path, subpath
integer::allocation_size, i
character(1)::nl = new_line(' ')
character(4)::folder_icon = char(240)//char(159)//char(147)//char(129)
character(len=:), pointer::one_link
if(.not. associated(req%query_string)) then
public_path = "/releases"
local_path = release_dir
else if(len_trim(req%query_string) == 0) then
public_path = "/releases"
local_path = release_dir
else
call combine_paths("/releases", req%query_string, public_path)
call combine_paths(release_dir, req%query_string, local_path)
end if
res => null()
! Easy safety check - no relative paths
if(index(local_path, '..') > 0) then
allocate(character(len=64)::res)
res = "None Found"
return
end if
directories => get_directories_in_directory(local_path)
files => get_files_in_directory(local_path)
allocation_size = 1024
if(associated(directories)) then
allocation_size = allocation_size + 2 * size(directories) * (DIR_LIST_STRING_LENGTH+32)
end if
if(associated(files)) then
allocation_size = allocation_size + 2 * size(files) * (DIR_LIST_STRING_LENGTH+32)
end if
allocate(character(len=allocation_size) :: res)
res = "<h2>Listing for "//trim(public_path)//"</h2>"//nl//"<ul>"
! Add an "Up" link
if(trim(public_path) /= "/releases") then
i = index(req%query_string, "/", back=.true.)
if(i > 0) then
one_link => html_link("releases.html?"//req%query_string(1:(i-1)), "Up a directory")
else
one_link => html_link("releases.html", "Up a directory")
end if
res = trim(res)//nl//"<li>"//one_link//"</li>"
deallocate(one_link)
end if
if(associated(directories)) then
do i = 1, size(directories)
if(associated(req%query_string)) then
call combine_paths(req%query_string, trim(directories(i)), subpath)
else
subpath = trim(directories(i))
end if
one_link => html_link("releases.html?"//trim(subpath), folder_icon//" "//trim(directories(i)))
res = trim(res)//nl//"<li>"//one_link//"</li>"
deallocate(one_link)
end do
deallocate(directories)
end if
if(associated(files)) then
do i = 1, size(files)
call combine_paths(public_path(2:len_trim(public_path)), trim(files(i)), subpath)
one_link => html_link(trim(subpath), trim(files(i)))
res = trim(res)//nl//"<li>"//one_link//"</li>"
deallocate(one_link)
end do
deallocate(files)
end if
res = trim(res)//nl//"</ul>"
end function generate_releases_html
function generate_jobs_html(req) result(res)
use captain_db
use server_response
use request_utils, only: render_jobs_links
use logging
implicit none
type(request)::req
character(len=:), pointer::res
type(job), dimension(:), pointer::jobs
integer::n, nsize
integer::i_start_jobs, ierr
character(len=:), pointer::linklist
character(10)::pager
n = get_jobs_count()
if(n == 0) then
allocate(character(len=1024) :: res)
res = "None Yet"
else
if(associated(req%query_string)) then
read(req%query_string, *, iostat=ierr) i_start_jobs
else
ierr = -1
end if
if(ierr /= 0) then
i_start_jobs = 1
end if
jobs => get_jobs()
! 15 per page
nsize = 15*(2*PLAYER_NAME_LENGTH + 64) + 1024
allocate(character(len=nsize) :: res)
res = " "
linklist => render_jobs_links(jobs, i_start_jobs, min(i_start_jobs+14, n), .false.)
res = trim(res)//new_line(' ')//trim(linklist)
! Pagers
res = trim(res)//new_line(' ')//"<p>"
if(i_start_jobs /= 1) then
write(pager, '(I8)') max(i_start_jobs - 15, 1)
res = trim(res)//'<a href="jobs.html?'//trim(adjustl(pager))//'"><< Newer</a> |'
end if
if(i_start_jobs+14 < n) then
write(pager, '(I8)') i_start_jobs + 15
res = trim(res)//' <a href="jobs.html?'//trim(adjustl(pager))//'">Older >></a>'
end if
res = trim(res)//"</p>"
deallocate(linklist)
end if
end function generate_jobs_html
function request_templated(req) result(resp)
use page_template
use config, only: template_filepath, project
use logging
use server_response, only:request, response
use http, only: HTTP_CODE_SUCCESS, HTTP_CODE_NOTFOUND
use request_utils, only: get_job_page_title
implicit none
type(request), intent(in)::req
type(response)::resp
character(1024)::template_file
type(template)::page
character(64)::first
character(len=:), pointer::contents
character(128)::job_page_title
call template_filepath("index.html", template_file)
call page%init(trim(template_file))
! Initialize with success
resp%code = HTTP_CODE_SUCCESS
call req%path_component(1, first)
if(trim(req%location) == "/" .or. trim(req%location) == "/index.html" .or. trim(req%location) == "/home.html") then
call page%assign('title', 'Home')
else if(trim(req%location) == "/releases.html") then
call page%assign('title', 'Releases')
contents => generate_releases_html(req)
call page%assign('contents', contents)
else if(trim(req%location) == "/jobs.html") then
call page%assign('title', 'Jobs')
contents => generate_jobs_html(req)
call page%assign('contents', contents)
else if(trim(req%location) == "/players.html") 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.html") then
call page%assign('title', 'About')
else if(trim(req%location) == "/instructions.html") then
call page%assign('title', 'Build Instructions')
!contents => generate_instructions_gemini()
!call page%assign('contents', contents)
else if(trim(first) == "instructions") then
call page%assign('title', 'Build Instructions')
!contents => generate_one_instuction_gemini(req)
!call page%assign('contents', contents)
else if(trim(first) == "jobs") then
call get_job_page_title(req, job_page_title)
call page%assign('title', trim(job_page_title))
contents => generate_one_job_html(req)
call page%assign('contents', contents)
else
call page%assign('title', 'Not Found')
resp%code = HTTP_CODE_NOTFOUND
end if
call page%assign('project', project)
call page%assign('base_url', req%server)
call write_log("Rendering page for "//req%location)
call page%render()
call write_log("Finalizing response", LOG_INFO)
resp%temporary_file = .true.
resp%body_filename => page%output_filename
resp%body_mimetype = "text/html"
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
character(64)::logresponse
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
call write_log("Publishing response", LOG_INFO)
write(logresponse, *) "Response code is: ", resp%code
call write_log(trim(logresponse), LOG_INFO)
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)
case(HTTP_CODE_FAILURE)
call write_log("Failure reported for location: "//trim(req%location), LOG_NORMAL)
! Need some more...
end select
call write_log("Cleanup", LOG_INFO)
call resp%destroy()
call req%destroy()
call write_log("Exit handler", LOG_INFO)
end subroutine handle_request
end module web
|