~nytpu/tlsada

ac6c35c0ea95db8963bd7f8171b0ee289172c52f — nytpu 1 year, 11 months ago a315917
Server_Example: Fix erroneous closing of connection

The server example was failing to read the request header prior to writing the
(static) response, which made many Gemini clients (including the bundled Client
example) unhappy and they reported an erroneous "connection reset" error.
1 files changed, 23 insertions(+), 7 deletions(-)

M src/example/server_example.adb
M src/example/server_example.adb => src/example/server_example.adb +23 -7
@@ 13,6 13,7 @@ with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;
with GNAT.Sockets;  use GNAT.Sockets;

with TLS;
with TLS.Configure;
with TLS.Contexts;
with TLS.Contexts.Server;


@@ 81,13 82,28 @@ begin

			Put_Line("Got connection from '" & Image(Address.Addr) & "'!");

			-- Read/write work exactly the same as for Client_Contexts
			String'Write(
				Connection_TLS'Access,
				"20 text/gemini;charset=utf-8" &
				ASCII.CR & ASCII.LF &
				"hello, world!" & ASCII.LF
			);
			-- Read request header (and discard).  Read/write work exactly the
			-- same as for Client_Contexts
			declare
				E : Boolean;
				S : constant String :=
					TLS.Get_Delim(Connection_TLS'Access, ASCII.CR & ASCII.LF, E, 1024);
			begin
				if not E then
					Put_Line("Malformed request: " & S);
					String'Write(
						Connection_TLS'Access,
						"59 malformed request" & ASCII.CR & ASCII.LF
					);
				else
					String'Write(
						Connection_TLS'Access,
						"20 text/gemini;charset=utf-8" &
						ASCII.CR & ASCII.LF &
						"hello, world!" & ASCII.LF
					);
				end if;
			end;

			Connection_TLS.Close;
			Close_Socket(Connection_Socket);