HttpServer: Track current connection, ignore canceled connections

master
Harald Wolff 2019-10-11 12:37:16 +02:00
parent ad38b48ae3
commit 996d949096
2 changed files with 14 additions and 2 deletions

View File

@ -44,7 +44,8 @@ namespace ln.http
{ {
HTTPServerConnection saveCurrent = Current.Value; HTTPServerConnection saveCurrent = Current.Value;
Current.Value = this; Current.Value = this;
currentConnections.Add(this); lock (currentConnections)
currentConnections.Add(this);
try try
{ {
@ -53,6 +54,9 @@ namespace ln.http
HttpReader httpReader = new HttpReader(TcpClient.GetStream()); HttpReader httpReader = new HttpReader(TcpClient.GetStream());
httpReader.Read(); httpReader.Read();
if (!httpReader.Valid)
return;
HttpResponse response = null; HttpResponse response = null;
using (CurrentRequest = new HttpRequest(httpReader, (IPEndPoint)TcpClient.Client.LocalEndPoint)) using (CurrentRequest = new HttpRequest(httpReader, (IPEndPoint)TcpClient.Client.LocalEndPoint))
@ -108,7 +112,8 @@ namespace ln.http
finally finally
{ {
Current.Value = saveCurrent; Current.Value = saveCurrent;
currentConnections.Remove(this); lock (currentConnections)
currentConnections.Remove(this);
} }
} }

View File

@ -25,6 +25,8 @@ namespace ln.http
public Dictionary<string, string> Headers { get; } = new Dictionary<string, string>(); public Dictionary<string, string> Headers { get; } = new Dictionary<string, string>();
public bool Valid { get; private set; } = false;
public HttpReader(Stream stream) public HttpReader(Stream stream)
{ {
Stream = stream; Stream = stream;
@ -39,6 +41,9 @@ namespace ln.http
{ {
ReadRequestHead(); ReadRequestHead();
if (blen == 0)
return;
Method = ReadToken(); Method = ReadToken();
SkipWhiteSpace(); SkipWhiteSpace();
URL = ReadToken(); URL = ReadToken();
@ -47,6 +52,8 @@ namespace ln.http
ReadLine(); ReadLine();
ReadHeaders(); ReadHeaders();
Valid = true;
} }
public int ReadByte() public int ReadByte()