module request_utils implicit none contains pure function success_code(req) use gemini_protocol, only: GEMINI_SUCCESS => STATUS_SUCCESS use http, only: HTTP_SUCCESS => HTTP_CODE_SUCCESS use server_response, only: request implicit none class(request), intent(in)::req integer::success_code if(req%protocol == 'gemini') then success_code = GEMINI_SUCCESS else success_code = HTTP_SUCCESS end if end function success_code pure function notfound_code(req) use gemini_protocol, only: GEMINI_FAIL => STATUS_PERMFAIL use http, only: HTTP_FAIL => HTTP_CODE_NOTFOUND use server_response, only: request implicit none class(request), intent(in)::req integer::notfound_code if(req%protocol == 'gemini') then notfound_code = GEMINI_FAIL else notfound_code = HTTP_FAIL end if end function notfound_code subroutine basic_mimetype(actual_filename, mimetype) use utilities, only: get_one_line_output_shell_command implicit none character(*), intent(in)::actual_filename character(*), intent(out)::mimetype logical::exists ! Check for gemini first since it's fake... if(index(actual_filename, ".gmi") /= 0) then mimetype = "text/gemini" else inquire(file=actual_filename, exist=exists) if(exists) then call get_one_line_output_shell_command("mimetype -b "//trim(actual_filename), mimetype) else ! If it doesn't exist, use the extension dumbly if(index(actual_filename, ".txt") /= 0) then mimetype = "text/plain" else if(index(actual_filename, ".json") /= 0) then mimetype = "text/plain" else if(index(actual_filename, ".html") /= 0) then mimetype = "text/html" else if(index(actual_filename, ".css") /= 0) then mimetype = "text/css" ! Just a catch-all, whatever... else mimetype = "application/octet-stream" end if end if end if end subroutine basic_mimetype pure function get_status_utf8(status) result(res) use captain_db implicit none integer, intent(in)::status character(4)::res select case(status) case(JOB_STATUS_SUCCESS) ! Check mark 0x2714 res = char(226)//char(156)//char(148)//" " case(JOB_STATUS_FAILURE) ! Ballot x 0x2717 res = char(226)//char(156)//char(151)//" " case(JOB_STATUS_WORKING) ! Runner 0x1f3c3 res = char(240)//char(159)//char(143)//char(131) case(PLAYER_STATUS_IDLE) ! Sleeping 0x1f4a4 res = char(240)//char(159)//char(146)//char(164) case(JOB_STATUS_PENDING) ! Envelope 0x2709 res = char(226)//char(156)//char(137)//" " end select end function get_status_utf8 function is_request_static(req) use server_response use logging implicit none class(request), intent(in)::req logical::is_request_static character(64)::first, last character(4)::ext integer::j call req%path_component(1, first) call req%last_component(last) j = index(last, ".", back=.true.) if(j > 0) then ext = last(j+1:len_trim(last)) else ext = " " end if call write_log("Static check: "//trim(first), LOG_DEBUG) is_request_static = ((trim(first) == "releases") .or. & (trim(first) == "uploads") .or. & (trim(first) == "results") .or. & (trim(first) == "static") .or. & (trim(first) == "favicon.txt") .or. & (trim(first) == "instructions" .and. trim(ext) == "json")) end function is_request_static function request_static(req) result(resp) use logging use config use utilities use server_response use special_filenames 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%path_starting_with_component(2, filename) resp%body_filename => get_special_full_filename(trim(category), trim(filename)) inquire(file=resp%body_filename, exist=exists) if(.not. exists) then resp%code = notfound_code(req) call write_log("File did not exist: "//resp%body_filename, LOG_NORMAL) else resp%code = success_code(req) call basic_mimetype(resp%body_filename, resp%body_mimetype) end if end function request_static function build_link(link, label, gemini_mode) result(res) implicit none character(*), intent(in)::link, label logical, intent(in)::gemini_mode character(len=:), pointer::res integer::nl if(gemini_mode) then nl = len_trim(link) + len_trim(label) + len('=> ') allocate(character(len=nl)::res) res = '=> '//trim(link)//' '//trim(label) else nl = len_trim(link) + len_trim(label) + len('') allocate(character(len=nl)::res) res = ''//trim(label)//'' end if end function build_link function generate_simple_pager(startindex, stopindex, step, maxcount, page, gemini_mode) result(res) use logging implicit none integer, intent(in)::startindex, stopindex, step, maxcount character(*), intent(in)::page logical, intent(in)::gemini_mode character(len=:), pointer::res integer::i character(len=:), allocatable::back_page, forward_page character(len=:), pointer::back_link, forward_link character(5)::num integer::res_size res_size = 128 back_link => null() forward_link => null() if(startindex > 1) then i = max(startindex-step, 1) allocate(character(len=(len_trim(page)+6)) :: back_page) write(num, '(I5)') i num = adjustl(num) back_page = trim(page)//"?"//trim(num) back_link => build_link(back_page, "Newer", gemini_mode) deallocate(back_page) end if if(stopindex < maxcount) then i = stopindex+1 allocate(character(len=(len_trim(page)+6)) :: forward_page) write(num, '(I5)') i num = adjustl(num) forward_page = trim(page)//"?"//trim(num) forward_link => build_link(forward_page, "Older", gemini_mode) deallocate(forward_page) end if if(.not. associated(back_link) .and. .not. associated(forward_link)) then res => null() else if(gemini_mode) then res_size = 1 else res_size = len('
-
') end if if(associated(back_link)) then res_size = res_size + len(back_link) end if if(associated(forward_link)) then res_size = res_size + len(forward_link) end if allocate(character(len=res_size) :: res) res = " " if(gemini_mode) then if(associated(back_link)) then res = back_link deallocate(back_link) end if if(associated(forward_link)) then if(len_trim(res) > 0) then res = trim(res)//new_line(' ')//forward_link else res = forward_link end if deallocate(forward_link) end if else if(associated(back_link)) then res = ''//back_link deallocate(back_link) end if if(associated(forward_link)) then if(len_trim(res) > 0) then res = trim(res)//" - "//forward_link else res = '
'//forward_link end if deallocate(forward_link) end if res = trim(res)//"
" end if end if end function generate_simple_pager function render_jobs_links(jobs, startindex, stopindex, gemini_mode, link_prefix) result(res) use captain_db implicit none type(job), dimension(:), pointer, intent(in)::jobs integer, intent(in), optional::startindex, stopindex logical, intent(in)::gemini_mode character(*), intent(in), optional::link_prefix character(len=:), pointer::res integer::nsize, i, first, last character(len=16)::int_text character(len=(2*PLAYER_NAME_LENGTH + 64))::link character(PLAYER_NAME_LENGTH)::player, instruction character(1)::nl = new_line(' ') character(64)::int_link_prefix if(present(link_prefix)) then int_link_prefix = link_prefix else int_link_prefix = " " end if if(.not. associated(jobs)) then allocate(character(len=32)::res) res = "None Yet" else first = 1 if(present(startindex)) then first = startindex end if last = size(jobs) if(present(stopindex)) then last = min(size(jobs), stopindex) end if if(first <= last .and. first <= size(jobs)) then nsize = (last-first+1)*(2*PLAYER_NAME_LENGTH + 64) + 32 allocate(character(len=nsize) :: res) if(gemini_mode) then res = " " else res ='" endif select case(jobs(i)%status) case(JOB_STATUS_SUCCESS, JOB_STATUS_FAILURE) res = trim(res)//nl//"Completed on" case(JOB_STATUS_PENDING) res = trim(res)//nl//"Waiting to run on" case(JOB_STATUS_WORKING) res = trim(res)//nl//"Running on" end select res = trim(res)//" "//trim(player)// & " - Last Update: "//trim(jobs(i)%time) if(.not. gemini_mode) then res = trim(res)//"
"//nl//"