diff options
author | Jeffrey Armstrong <jeff@approximatrix.com> | 2020-05-01 20:45:13 -0400 |
---|---|---|
committer | Jeffrey Armstrong <jeff@approximatrix.com> | 2020-05-01 20:45:13 -0400 |
commit | 1ef40339cc161484b3e70d34ab4d507b758b29eb (patch) | |
tree | f1530f7dc01842ffe1dab393ec8fc374dc42b5e5 | |
parent | 4108ae479d67067789f98267175e127e6a2a61ad (diff) | |
download | LR-87-1ef40339cc161484b3e70d34ab4d507b758b29eb.tar.gz LR-87-1ef40339cc161484b3e70d34ab4d507b758b29eb.zip |
Simple, single request function for retrieving a gemini page into a unit number
-rw-r--r-- | protocol.f90 | 45 | ||||
-rw-r--r-- | request.f90 | 29 |
2 files changed, 72 insertions, 2 deletions
diff --git a/protocol.f90 b/protocol.f90 index fa36fe7..bd109c1 100644 --- a/protocol.f90 +++ b/protocol.f90 @@ -8,11 +8,14 @@ implicit none integer, parameter::STATUS_PERMFAIL = 5 integer, parameter::STATUS_CERTREQ = 6 integer, parameter::STATUS_LOCALFAIL = -1 - + + integer, parameter::BUFFER_SIZE = 256 + contains function request_url(url, unit_number, server_name) result(returncode) use request + use iso_c_binding implicit none character(*), intent(in)::url @@ -23,7 +26,13 @@ contains character(:), allocatable::server + type(connection)::conn + + integer::bytes_received, i + character, dimension(BUFFER_SIZE)::buffer + returncode = -1 + rewind(unit_number) if(present(server_name)) then allocate(character(len=len_trim(server_name)) :: server) @@ -32,8 +41,40 @@ contains server = get_server_from_url(url) end if + conn = open_connection(server) - + if(conn%code == CONNECTION_OPEN) then + if(send_string(conn%ssl, url//c_carriage_return//c_new_line, trimming=.false.)) then + + bytes_received = retrieve_characters(conn%ssl, buffer) + + do while(bytes_received > 0) + + do i=1, bytes_received + write(unit_number, '(A1)', advance='no') buffer(i) + end do + + bytes_received = retrieve_characters(conn%ssl, buffer) + end do + + rewind(unit_number) + read(unit_number, '(I1)') returncode + + else + + returncode = -1 + write(unit_number, *) "Send Error: Could Not Send Request" + + end if + + else + + returncode = -1 + write(unit_number, *) "Connection Error: "//trim(translate_connection_code(conn%code)) + + end if + + call close_connection(conn) end function request_url diff --git a/request.f90 b/request.f90 index 3cc8cbf..67cc4f7 100644 --- a/request.f90 +++ b/request.f90 @@ -13,6 +13,16 @@ implicit none integer, parameter::CONNECTION_SSL_CONN_FAILURE = 5 integer, parameter::CONNECTION_OPEN = 6 + character(23), dimension(0:6), parameter:: connection_code_str = & + ["No Connection ", & + "Connection Closed ", & + "Server Not Found ", & + "Local Socket Failure ", & + "SSL Configuration Error", & + "SSL Connection Failure ", & + "SSL Connection Open " ] + + type :: connection integer::code @@ -26,6 +36,25 @@ implicit none contains + function translate_connection_code(code) + implicit none + + integer, intent(in)::code + character(23)::translate_connection_code + + if(code >= lbound(connection_code_str, 1) .and. & + code <= ubound(connection_code_str, 1)) then + + translate_connection_code = connection_code_str(code) + + else + + translate_connection_code = "Unknown Error" + + end if + + end function translate_connection_code + function open_connection(server, port) result(conn) use jessl use network |