From 996d9490966c728eeb2b9d9f0ae3bf14b44adaf9 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 11 Oct 2019 12:37:16 +0200 Subject: [PATCH] HttpServer: Track current connection, ignore canceled connections --- HTTPServerConnection.cs | 9 +++++++-- HttpReader.cs | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/HTTPServerConnection.cs b/HTTPServerConnection.cs index 55e642d..5166a45 100644 --- a/HTTPServerConnection.cs +++ b/HTTPServerConnection.cs @@ -44,7 +44,8 @@ namespace ln.http { HTTPServerConnection saveCurrent = Current.Value; Current.Value = this; - currentConnections.Add(this); + lock (currentConnections) + currentConnections.Add(this); try { @@ -53,6 +54,9 @@ namespace ln.http HttpReader httpReader = new HttpReader(TcpClient.GetStream()); httpReader.Read(); + if (!httpReader.Valid) + return; + HttpResponse response = null; using (CurrentRequest = new HttpRequest(httpReader, (IPEndPoint)TcpClient.Client.LocalEndPoint)) @@ -108,7 +112,8 @@ namespace ln.http finally { Current.Value = saveCurrent; - currentConnections.Remove(this); + lock (currentConnections) + currentConnections.Remove(this); } } diff --git a/HttpReader.cs b/HttpReader.cs index 57674bc..f0bf80f 100644 --- a/HttpReader.cs +++ b/HttpReader.cs @@ -25,6 +25,8 @@ namespace ln.http public Dictionary Headers { get; } = new Dictionary(); + public bool Valid { get; private set; } = false; + public HttpReader(Stream stream) { Stream = stream; @@ -39,6 +41,9 @@ namespace ln.http { ReadRequestHead(); + if (blen == 0) + return; + Method = ReadToken(); SkipWhiteSpace(); URL = ReadToken(); @@ -47,6 +52,8 @@ namespace ln.http ReadLine(); ReadHeaders(); + + Valid = true; } public int ReadByte()