From 4f1ee0a63c9d4911c668f14a5b2210130c73ec72 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Sun, 29 Nov 2020 14:20:51 +0100 Subject: [PATCH] further correct handling of closed connections (throwing NullReference) --- ln.http/HttpRequest.cs | 4 ++++ ln.http/connections/Connection.cs | 5 +++++ ln.http/exceptions/ConnectionClosedException.cs | 13 +++++++++++++ ln.http/ln.http.csproj | 4 +--- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 ln.http/exceptions/ConnectionClosedException.cs diff --git a/ln.http/HttpRequest.cs b/ln.http/HttpRequest.cs index 29b3d50..cd69139 100644 --- a/ln.http/HttpRequest.cs +++ b/ln.http/HttpRequest.cs @@ -96,6 +96,10 @@ namespace ln.http void ReadRequestLine() { string requestLine = connectionReader.ReadLine(); + + if (requestLine == null) + throw new ConnectionClosedException("UnbufferedStreamReader.ReadLine() returned null"); + string[] requestTokens = requestLine.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); if (requestTokens.Length != 3) diff --git a/ln.http/connections/Connection.cs b/ln.http/connections/Connection.cs index 4b40f2c..904c339 100644 --- a/ln.http/connections/Connection.cs +++ b/ln.http/connections/Connection.cs @@ -12,6 +12,8 @@ using ln.type; using System.IO; using ln.logging; using ln.http.listener; +using ln.http.exceptions; + namespace ln.http.connections { public abstract class Connection : IDisposable @@ -34,6 +36,9 @@ namespace ln.http.connections { return new HttpRequest(httpServer, GetStream(), Listener.LocalEndpoint, new Endpoint(RemoteHost, RemotePort)); } catch (IOException) + { + return null; + } catch (ConnectionClosedException) { return null; } catch (Exception e) diff --git a/ln.http/exceptions/ConnectionClosedException.cs b/ln.http/exceptions/ConnectionClosedException.cs new file mode 100644 index 0000000..ab67122 --- /dev/null +++ b/ln.http/exceptions/ConnectionClosedException.cs @@ -0,0 +1,13 @@ +using System; + +namespace ln.http.exceptions +{ + + public class ConnectionClosedException : Exception + { + public ConnectionClosedException(string message) : base(message) { } + + public ConnectionClosedException(){ } + } + +} diff --git a/ln.http/ln.http.csproj b/ln.http/ln.http.csproj index 1b09fb1..c4f44a4 100644 --- a/ln.http/ln.http.csproj +++ b/ln.http/ln.http.csproj @@ -3,14 +3,12 @@ netcoreapp3.1 true - 0.1.1 + 0.1.2 Harald Wolff-Thobaben l--n.de (c) 2020 Harald Wolff-Thobaben http server - 0.0.1.0 - 0.0.1.0