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

Commit 0cc53a6

Browse files
committed
Reduce exceptions
1 parent f898a59 commit 0cc53a6

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,27 @@ internal CustomBinaryReader(Stream stream, Encoding encoding)
1515
internal string ReadLine()
1616
{
1717
var readBuffer = new StringBuilder();
18-
18+
1919
try
2020
{
2121
var lastChar = default(char);
22+
var buffer = new char[1];
2223

23-
while (true)
24+
while (Read(buffer, 0, 1) > 0)
2425
{
25-
var buf = ReadChar();
26-
if (lastChar == '\r' && buf == '\n')
26+
if (lastChar == '\r' && buffer[0] == '\n')
2727
{
2828
return readBuffer.Remove(readBuffer.Length - 1, 1).ToString();
2929
}
30-
if (buf == '\0')
30+
if (buffer[0] == '\0')
3131
{
3232
return readBuffer.ToString();
3333
}
34-
readBuffer.Append(buf);
35-
36-
lastChar = buf;
34+
readBuffer.Append(buffer);
35+
lastChar = buffer[0];
3736
}
3837

38+
return readBuffer.ToString();
3939
}
4040
catch (IOException)
4141
{

Titanium.Web.Proxy/RequestHandler.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ private static void HandleClient(TcpClient client)
3333

3434
if (string.IsNullOrEmpty(httpCmd))
3535
{
36-
throw new EndOfStreamException();
36+
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
37+
return;
3738
}
3839

3940
//break up the line into three components (method, remote URL & Http Version)
@@ -80,11 +81,13 @@ private static void HandleClient(TcpClient client)
8081
if (sslStream != null)
8182
sslStream.Dispose();
8283

83-
throw;
84+
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
85+
return;
8486
}
8587

8688

8789
httpCmd = clientStreamReader.ReadLine();
90+
8891
}
8992
else if (httpVerb.ToUpper() == "CONNECT")
9093
{
@@ -121,7 +124,6 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
121124
var args = new SessionEventArgs(BUFFER_SIZE);
122125
args.Client = client;
123126

124-
125127
try
126128
{
127129
//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
227229

228230
HandleHttpSessionResponse(args);
229231

232+
//if connection is closing exit
230233
if (args.ResponseHeaders.Any(x => x.Name.ToLower() == "connection" && x.Value.ToLower() == "close"))
231234
{
232235
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
233236
return;
234237
}
235-
//Now read the next request (if keep-Alive is enabled, otherwise exit this thread)
236-
//If client is pipeling the request, this will be immediately hit before response for previous request was made
237-
httpCmd = clientStreamReader.ReadLine();
238-
//Http request body sent, now wait for next request
239238

240-
client = args.Client;
241-
clientStream = args.ClientStream;
242-
clientStreamReader = args.ClientStreamReader;
243-
args.ClientStreamWriter = clientStreamWriter;
239+
// read the next request
240+
httpCmd = clientStreamReader.ReadLine();
244241

245242
}
246243
catch
247244
{
248245
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
249-
throw;
246+
return;
250247
}
251248
}
252249
}

0 commit comments

Comments
 (0)