From 8ca6d21882c521fb78f1325e9a88a558364b3ad4 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Thu, 6 May 2021 10:17:03 -0400 Subject: CGI interface at least starts properly. Fixed rendering of templates to actually take place in CGI mode. --- captain/bin/local.conf | 50 +++++++++++++++++++++++++++++++++++++++++ captain/bin/startlight.sh | 7 ++++++ captain/captian.f90 | 27 +++++++++++++++++++++- captain/example/levitating.conf | 2 +- captain/http.f90 | 11 ++++++++- captain/levitating-captain.prj | 10 +++++++++ captain/response.f90 | 2 +- captain/web.f90 | 32 +++++++++++++++++++++++--- 8 files changed, 134 insertions(+), 7 deletions(-) create mode 100644 captain/bin/local.conf create mode 100755 captain/bin/startlight.sh (limited to 'captain') diff --git a/captain/bin/local.conf b/captain/bin/local.conf new file mode 100644 index 0000000..9c4b6a9 --- /dev/null +++ b/captain/bin/local.conf @@ -0,0 +1,50 @@ +server.bind = "0.0.0.0" +server.port = 8000 +server.document-root = var.CWD + "/.." + +#"/home/jeff/Workspace/muffdive/pages" + +index-file.names = ( "index.php", "index.html", "index.htm", "default.htm" ) + +server.modules = ( "mod_cgi", "mod_access", "mod_accesslog", "mod_alias", "mod_setenv" ) + +setenv.add-environment = ( "LEVITATING_CONFIG_CGI" => env.LEVITATING_CONFIG_CGI ) + +cgi.assign = ( ".html" => server.document-root + "/levitating-captain" ) + +$HTTP["url"] =~ "^/cgi-bin" { + alias.url += ( "/cgi-bin" => server.document-root ) + cgi.assign = ( "" => "" ) +} + +$HTTP["url"] =~ "^/static" { + alias.url += ( "/static" => server.document-root + "/example/static" ) +} + +$HTTP["url"] =~ "^/json" { + cgi.assign = ( ".json" => server.document-root + "/levitating-captain" ) +} + +mimetype.assign = ( + ".css" => "text/css", + ".gif" => "image/gif", + ".htm" => "text/html", + ".html" => "text/html", + ".jpeg" => "image/jpeg", + ".jpg" => "image/jpeg", + ".js" => "text/javascript", + ".png" => "image/png", + ".swf" => "application/x-shockwave-flash", + ".txt" => "text/plain" +) + +# Making sure file uploads above 64k always work when using IE or Safari +# For more information, see http://trac.lighttpd.net/trac/ticket/360 +$HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" { + server.max-keep-alive-requests = 0 +} + + + +server.errorlog = server.document-root + "/example/log/error.log" +accesslog.filename = server.document-root + "/example/log/access.log" diff --git a/captain/bin/startlight.sh b/captain/bin/startlight.sh new file mode 100755 index 0000000..7ec35c6 --- /dev/null +++ b/captain/bin/startlight.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +export LEVITATING_CONFIG_CGI=$PWD/../example/levitating.conf +echo Using $LEVITATING_CONFIG_CGI for configuration + +echo Starting up in $PWD +lighttpd -D -f local.conf diff --git a/captain/captian.f90 b/captain/captian.f90 index 4ee637e..76ea768 100644 --- a/captain/captian.f90 +++ b/captain/captian.f90 @@ -23,7 +23,7 @@ program captain use captain_db use config -use logging, only: initialize_log => initialize, shutdown_log => shutdown +use logging, only: initialize_log => initialize, shutdown_log => shutdown, write_log, LOG_INFO use gemini, only: handle_gemini => handle_request use web, only: handle_web => handle_request implicit none @@ -48,6 +48,9 @@ implicit none end select call shutdown_db() + + call write_log("Process complete", LOG_INFO) + call shutdown_log() contains @@ -67,6 +70,10 @@ contains Print *, " -c Use the specified config file" Print *, " -g Operate in Gemini mode" Print *, " -w Operate in CGI mode (default)" + Print *, " " + Print *, "Config file can also be specified via the environment variables:" + Print *, " LEVITATING_CONFIG_CGI Path to config for CGI mode" + Print *, " LEVITATING_CONFIG_GEMINI Path to config for Gemini mode" end subroutine usage @@ -104,6 +111,24 @@ contains i = i + 1 end do + if(.not. config_loaded) then + call get_environment_variable("LEVITATING_CONFIG_CGI", value=option, status=i) + if(i == 0) then + call load_configuration(trim(option)) + config_loaded = .true. + mode = MODE_CGI_HTML + end if + end if + + if(.not. config_loaded) then + call get_environment_variable("LEVITATING_CONFIG_GEMINI", value=option, status=i) + if(i == 0) then + call load_configuration(trim(option)) + config_loaded = .true. + mode = MODE_GEMINI + end if + end if + ! Assign working directory from command if not specified if(.not. config_loaded) then Print *, "No configuration file specified" diff --git a/captain/example/levitating.conf b/captain/example/levitating.conf index dc4bcfe..127c6e6 100644 --- a/captain/example/levitating.conf +++ b/captain/example/levitating.conf @@ -3,7 +3,7 @@ template-directory = /home/jeff/Workspace/levitating/captain/templates database = /home/jeff/Workspace/levitating/captain/example/store.db -log-filename = /tmp/levitating.log +log-filename = /home/jeff/Workspace/levitating/captain/example/log/levitating.log log-level = 10 diff --git a/captain/http.f90 b/captain/http.f90 index 08f0fbd..806d025 100644 --- a/captain/http.f90 +++ b/captain/http.f90 @@ -9,14 +9,21 @@ implicit none contains subroutine write_status(outunit, code) + use logging implicit none integer, intent(in)::outunit, code + character(len=32)::confirm + write(outunit,'(A7,1X,I3)') "Status:", code - + + write(confirm,'(A7,1X,I3)') "Status:", code + call write_log(trim(confirm), LOG_INFO) + end subroutine write_status subroutine write_response_headers(outunit, code, filesize, mimetype) + use logging implicit none integer, intent(in)::outunit, code, filesize @@ -24,6 +31,8 @@ contains character(16)::num_txt + character(len=128)::confirm + call write_status(outunit, code) write(num_txt, '(I16)') filesize diff --git a/captain/levitating-captain.prj b/captain/levitating-captain.prj index f98e72b..5f7ce18 100644 --- a/captain/levitating-captain.prj +++ b/captain/levitating-captain.prj @@ -1,6 +1,16 @@ { "Root":{ "Folders":[{ + "Folders":[], + "Name":"+bin", + "Files":[{ + "filename":"bin/local.conf", + "enabled":"1" + },{ + "filename":"bin/startlight.sh", + "enabled":"1" + }] + },{ "Folders":[], "Name":"+common", "Files":[{ diff --git a/captain/response.f90 b/captain/response.f90 index 1fc21b1..3982f1e 100644 --- a/captain/response.f90 +++ b/captain/response.f90 @@ -128,7 +128,7 @@ contains ! so we need to skip ahead of it in the URL if it is there... i = index(str, self%server) if(i > 0) then - i = i + len(self%server) + i = i + len(self%server)+1 else i = 1 end if diff --git a/captain/web.f90 b/captain/web.f90 index a0e7a90..ec91d88 100644 --- a/captain/web.f90 +++ b/captain/web.f90 @@ -163,6 +163,7 @@ contains use config, only: template_filepath, project use logging use server_response, only:request, response + use http, only: HTTP_CODE_SUCCESS, HTTP_CODE_NOTFOUND implicit none type(request), intent(in)::req @@ -177,9 +178,12 @@ contains 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") then + if(trim(req%location) == "/" .or. trim(req%location) == "/index.html" .or. trim(req%location) == "/home.html") then call page%assign('title', 'Home') @@ -229,11 +233,20 @@ contains 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 @@ -250,6 +263,8 @@ contains type(response)::resp integer::response_size + character(64)::logresponse + call build_request_object(req) if(is_request_static(req)) then @@ -263,6 +278,11 @@ contains 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)) @@ -272,13 +292,19 @@ contains 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 -- cgit v1.2.3