diff --git a/Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs b/Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs index 92089f2fd..331f85961 100644 --- a/Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs +++ b/Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs @@ -15,27 +15,27 @@ internal CustomBinaryReader(Stream stream, Encoding encoding) internal string ReadLine() { var readBuffer = new StringBuilder(); - + try { var lastChar = default(char); + var buffer = new char[1]; - while (true) + while (Read(buffer, 0, 1) > 0) { - var buf = ReadChar(); - if (lastChar == '\r' && buf == '\n') + if (lastChar == '\r' && buffer[0] == '\n') { return readBuffer.Remove(readBuffer.Length - 1, 1).ToString(); } - if (buf == '\0') + if (buffer[0] == '\0') { return readBuffer.ToString(); } - readBuffer.Append(buf); - - lastChar = buf; + readBuffer.Append(buffer); + lastChar = buffer[0]; } + return readBuffer.ToString(); } catch (IOException) { diff --git a/Titanium.Web.Proxy/RequestHandler.cs b/Titanium.Web.Proxy/RequestHandler.cs index 461192b39..6942a3e0d 100644 --- a/Titanium.Web.Proxy/RequestHandler.cs +++ b/Titanium.Web.Proxy/RequestHandler.cs @@ -33,7 +33,8 @@ private static void HandleClient(TcpClient client) if (string.IsNullOrEmpty(httpCmd)) { - throw new EndOfStreamException(); + Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null); + return; } //break up the line into three components (method, remote URL & Http Version) @@ -80,11 +81,13 @@ private static void HandleClient(TcpClient client) if (sslStream != null) sslStream.Dispose(); - throw; + Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null); + return; } httpCmd = clientStreamReader.ReadLine(); + } else if (httpVerb.ToUpper() == "CONNECT") { @@ -121,7 +124,6 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S var args = new SessionEventArgs(BUFFER_SIZE); args.Client = client; - try { //break up the line into three components (method, remote URL & Http Version) @@ -227,26 +229,21 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S HandleHttpSessionResponse(args); + //if connection is closing exit if (args.ResponseHeaders.Any(x => x.Name.ToLower() == "connection" && x.Value.ToLower() == "close")) { Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); return; } - //Now read the next request (if keep-Alive is enabled, otherwise exit this thread) - //If client is pipeling the request, this will be immediately hit before response for previous request was made - httpCmd = clientStreamReader.ReadLine(); - //Http request body sent, now wait for next request - client = args.Client; - clientStream = args.ClientStream; - clientStreamReader = args.ClientStreamReader; - args.ClientStreamWriter = clientStreamWriter; + // read the next request + httpCmd = clientStreamReader.ReadLine(); } catch { Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); - throw; + return; } } }