Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

Commit

Permalink
Reduce exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Dec 26, 2015
1 parent f898a59 commit 0cc53a6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
16 changes: 8 additions & 8 deletions Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
21 changes: 9 additions & 12 deletions Titanium.Web.Proxy/RequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 0cc53a6

Please sign in to comment.