From a25527272916f342875809d7c3606ba0ebd350b4 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Tue, 11 May 2021 20:09:08 -0400 Subject: Moved some command handling into requtils to share with web and gemini modes. Started work on handling post requests to add players. --- captain/external.f90 | 39 +----------------------------- captain/levitating-captain.prj | 6 +++++ captain/requtils.f90 | 37 ++++++++++++++++++++++++++++ captain/response.f90 | 33 +++++++++++++++++++++++-- captain/web.f90 | 55 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 122 insertions(+), 48 deletions(-) diff --git a/captain/external.f90 b/captain/external.f90 index 8ca5c97..f3efaaf 100644 --- a/captain/external.f90 +++ b/captain/external.f90 @@ -387,44 +387,6 @@ contains end function generate_one_instuction_gemini - subroutine handle_instruction_command(req) - use captain_db - use server_response - use remote_launch - implicit none - - type(request), intent(in)::req - character(32)::command - character(PLAYER_NAME_LENGTH)::argument, instruction_name - - integer::i, j - - i = index(req%location, "/", back=.true.) - j = index(req%location, ".", back=.true.) - - instruction_name = req%location(i+1:j-1) - - i = index(req%query_string, "=") - command = req%query_string(1:i-1) - argument = req%query_string(i+1:len_trim(req%query_string)) - - if(trim(command) == "launch") then - call launch_instructions_on_player(instruction_name, argument) - - else if(trim(command) == "assign") then - i = get_instruction_id(trim(instruction_name)) - j = get_player_id(trim(argument)) - call add_player_for_instruction(i, j) - - else if(trim(command) == "remove") then - i = get_instruction_id(trim(instruction_name)) - j = get_player_id(trim(argument)) - call remove_player_for_instruction(i, j) - - end if - - end subroutine handle_instruction_command - function generate_instructions_gemini() result(res) use captain_db implicit none @@ -514,6 +476,7 @@ contains function external_input_request_gemini(req) result(resp) use server_response use captain_db + use request_utils, only: handle_instruction_command implicit none class(request), intent(in)::req diff --git a/captain/levitating-captain.prj b/captain/levitating-captain.prj index 34def04..261c717 100644 --- a/captain/levitating-captain.prj +++ b/captain/levitating-captain.prj @@ -1,4 +1,10 @@ { + "Tools":[{ + "Command":"/home/jeff/Workspace/levitating/captain/bin/startlight.sh", + "Open Console":"1", + "Name":"Start Webserver", + "Directory":"/home/jeff/Workspace/levitating/captain/bin" + }], "Root":{ "Folders":[{ "Folders":[], diff --git a/captain/requtils.f90 b/captain/requtils.f90 index 7010bcb..cb52113 100644 --- a/captain/requtils.f90 +++ b/captain/requtils.f90 @@ -319,4 +319,41 @@ contains end subroutine get_job_page_title + subroutine handle_instruction_command(req) + use captain_db + use server_response + use remote_launch + implicit none + + type(request), intent(in)::req + character(32)::command + character(PLAYER_NAME_LENGTH)::argument, instruction_name + + integer::i, j + + i = index(req%page, ".", back=.true.) + + instruction_name = req%page(1:i-1) + + i = index(req%query_string, "=") + command = req%query_string(1:i-1) + argument = req%query_string(i+1:len_trim(req%query_string)) + + if(trim(command) == "launch") then + call launch_instructions_on_player(instruction_name, argument) + + else if(trim(command) == "assign") then + i = get_instruction_id(trim(instruction_name)) + j = get_player_id(trim(argument)) + call add_player_for_instruction(i, j) + + else if(trim(command) == "remove") then + i = get_instruction_id(trim(instruction_name)) + j = get_player_id(trim(argument)) + call remove_player_for_instruction(i, j) + + end if + + end subroutine handle_instruction_command + end module request_utils \ No newline at end of file diff --git a/captain/response.f90 b/captain/response.f90 index 610b070..dfce5e7 100644 --- a/captain/response.f90 +++ b/captain/response.f90 @@ -63,6 +63,7 @@ implicit none character(len=:), pointer::location => null() character(len=:), pointer::page => null() character(len=:), pointer::query_string => null() + character(len=4)::method = "GET" contains @@ -73,6 +74,8 @@ implicit none procedure :: path_component_int => request_component_int procedure :: path_starting_with_component => request_component_starting_with procedure :: component => request_component_func + procedure :: is_get => request_is_get + procedure :: is_post => request_is_post end type request @@ -94,13 +97,14 @@ implicit none contains - subroutine request_init(self, str, server_explicit, protocol_explicit) + subroutine request_init(self, str, server_explicit, protocol_explicit, method) use logging + use utilities, only: toupper implicit none class(request) :: self character(*), intent(in)::str - character(*), intent(in), optional::server_explicit, protocol_explicit + character(*), intent(in), optional::server_explicit, protocol_explicit, method character(len=:), allocatable::temppage integer::i, j, n @@ -179,6 +183,11 @@ contains self%page = temppage end if deallocate(temppage) + + if(present(method)) then + self%method = method + call toupper(self%method) + end if end subroutine request_init @@ -328,6 +337,26 @@ contains end subroutine request_last_component + function request_is_get(self) result(res) + implicit none + + class(request)::self + logical::res + + res = (self%method == "GET") + + end function request_is_get + + function request_is_post(self) result(res) + implicit none + + class(request)::self + logical::res + + res = (self%method == "POST") + + end function request_is_post + subroutine request_destroy(self) implicit none diff --git a/captain/web.f90 b/captain/web.f90 index 55f12a0..0f741d8 100644 --- a/captain/web.f90 +++ b/captain/web.f90 @@ -35,15 +35,18 @@ contains type(request), intent(out)::req character(len=:), allocatable::url, script_name + character(len=4)::method 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) + + call get_environment_variable("REQUEST_METHOD", method) ! If we're in CGI mode, treat the "server" as the script name - call req%init(url, server_explicit=script_name, protocol_explicit="http") + call req%init(url, server_explicit=script_name, protocol_explicit="http", method=method) deallocate(url) deallocate(script_name) @@ -193,15 +196,17 @@ contains end function generate_one_instuction_html - function generate_instructions_html() result(res) + function generate_instructions_html(req) result(res) use captain_db + use server_response, only:request implicit none + type(request)::req character(len=:), pointer::res character(len=PLAYER_NAME_LENGTH), dimension(:), pointer::instruction_names integer::n, i, nsize - character(len=:), pointer::one_player + character(len=:), pointer::one_player, scanlink n = get_instuctions_count() @@ -225,6 +230,7 @@ contains one_player => html_link("instructions/"//trim(instruction_names(i))//".html", & trim(instruction_names(i))) res = trim(res)//new_line(' ')//"
  • "//trim(one_player)//"
  • " + deallocate(one_player) end do res = trim(res)//new_line(' ')//"" @@ -233,8 +239,11 @@ contains end if - res = trim(res)//new_line(' ')//"

    Management

    "// & - new_line(' ')//"coming soon (Scan for Instructions)" + res = trim(res)//new_line(' ')//"

    Management

    " + + scanlink => html_link(req%page//"?scan", "Scan for instructions now") + res = trim(res)//new_line(' ')//"

    "//scanlink//"

    " + deallocate(scanlink) end function generate_instructions_html @@ -279,7 +288,8 @@ contains end if res = trim(res)//new_line(' ')//"

    Management

    "// & - new_line(' ')//"

    coming soon (add player)

    " + new_line(' ')//'
    '// & + '
    ' end function generate_players_html @@ -535,7 +545,7 @@ contains 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 + use request_utils, only: get_job_page_title, handle_instruction_command implicit none type(request), intent(in)::req @@ -586,12 +596,22 @@ contains else if(trim(req%location) == "/instructions.html") then + if(associated(req%query_string) .and. len_trim(req%query_string) > 0) then + if(trim(req%query_string) == "scan") then + + end if + end if + call page%assign('title', 'Build Instructions') - contents => generate_instructions_html() + contents => generate_instructions_html(req) call page%assign('contents', contents) else if(trim(first) == "instructions") then + if(associated(req%query_string) .and. len_trim(req%query_string) > 0) then + call handle_instruction_command(req) + end if + call page%assign('title', 'Build Instructions') contents => generate_one_instuction_html(req) call page%assign('contents', contents) @@ -622,6 +642,22 @@ contains resp%body_mimetype = "text/html" end function request_templated + + function handle_post(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, handle_instruction_command + implicit none + + type(request), intent(in)::req + type(response)::resp + + + + end function handle_post subroutine handle_request() use server_response, only:request, response @@ -644,6 +680,9 @@ contains call write_log("Req static", LOG_INFO) resp = request_static(req) + else if(req%is_post()) then + resp = handle_post(req) + else call write_log("Req template", LOG_INFO) resp = request_templated(req) -- cgit v1.2.3