Commit 5d461f5b authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #237 from honfika/develop

move Dispose calls to finally blocks
parents ff41363f 32a55f2c
...@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -22,7 +22,7 @@ namespace Titanium.Web.Proxy.Helpers
private static readonly ConcurrentQueue<byte[]> buffers private static readonly ConcurrentQueue<byte[]> buffers
= new ConcurrentQueue<byte[]>(); = new ConcurrentQueue<byte[]>();
private volatile bool disposed = false; private volatile bool disposed;
internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize) internal CustomBinaryReader(CustomBufferedStream stream, int bufferSize)
{ {
......
...@@ -383,8 +383,7 @@ namespace Titanium.Web.Proxy ...@@ -383,8 +383,7 @@ namespace Titanium.Web.Proxy
#if !DEBUG #if !DEBUG
firefoxProxySettingsManager.AddFirefox(); firefoxProxySettingsManager.AddFirefox();
#endif #endif
Console.WriteLine("Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy", Console.WriteLine("Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy", endPoint.IpAddress, endPoint.Port);
endPoint.IpAddress, endPoint.Port);
} }
/// <summary> /// <summary>
......
...@@ -117,6 +117,7 @@ namespace Titanium.Web.Proxy ...@@ -117,6 +117,7 @@ namespace Titanium.Web.Proxy
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, await sslStream.AuthenticateAsServerAsync(certificate, false,
SupportedSslProtocols, false); SupportedSslProtocols, false);
//HTTPS server created - we can now decrypt the client's traffic //HTTPS server created - we can now decrypt the client's traffic
clientStream = new CustomBufferedStream(sslStream, BufferSize); clientStream = new CustomBufferedStream(sslStream, BufferSize);
...@@ -138,6 +139,7 @@ namespace Titanium.Web.Proxy ...@@ -138,6 +139,7 @@ namespace Titanium.Web.Proxy
{ {
//Siphon out CONNECT request headers //Siphon out CONNECT request headers
await clientStreamReader.ReadAndIgnoreAllLinesAsync(); await clientStreamReader.ReadAndIgnoreAllLinesAsync();
//write back successfull CONNECT response //write back successfull CONNECT response
await WriteConnectResponse(clientStreamWriter, version); await WriteConnectResponse(clientStreamWriter, version);
...@@ -172,8 +174,7 @@ namespace Titanium.Web.Proxy ...@@ -172,8 +174,7 @@ namespace Titanium.Web.Proxy
//So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client //So for HTTPS requests we would start SSL negotiation right away without expecting a CONNECT request from client
private async Task HandleClient(TransparentProxyEndPoint endPoint, TcpClient tcpClient) private async Task HandleClient(TransparentProxyEndPoint endPoint, TcpClient tcpClient)
{ {
var disposed = false; bool disposed = false;
var clientStream = new CustomBufferedStream(tcpClient.GetStream(), BufferSize); var clientStream = new CustomBufferedStream(tcpClient.GetStream(), BufferSize);
clientStream.ReadTimeout = ConnectionTimeOutSeconds * 1000; clientStream.ReadTimeout = ConnectionTimeOutSeconds * 1000;
...@@ -182,52 +183,39 @@ namespace Titanium.Web.Proxy ...@@ -182,52 +183,39 @@ namespace Titanium.Web.Proxy
CustomBinaryReader clientStreamReader = null; CustomBinaryReader clientStreamReader = null;
StreamWriter clientStreamWriter = null; StreamWriter clientStreamWriter = null;
if (endPoint.EnableSsl) try
{ {
var sslStream = new SslStream(clientStream, true); if (endPoint.EnableSsl)
{
var sslStream = new SslStream(clientStream, true);
clientStream = new CustomBufferedStream(sslStream, BufferSize);
//implement in future once SNI supported by SSL stream, for now use the same certificate //implement in future once SNI supported by SSL stream, for now use the same certificate
var certificate = CertificateManager.CreateCertificate(endPoint.GenericCertificateName, false); var certificate = CertificateManager.CreateCertificate(endPoint.GenericCertificateName, false);
try
{
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, await sslStream.AuthenticateAsServerAsync(certificate, false,
SslProtocols.Tls, false); SslProtocols.Tls, false);
clientStream = new CustomBufferedStream(sslStream, BufferSize);
clientStreamReader = new CustomBinaryReader(clientStream, BufferSize);
clientStreamWriter = new StreamWriter(clientStream) { NewLine = ProxyConstants.NewLine };
//HTTPS server created - we can now decrypt the client's traffic //HTTPS server created - we can now decrypt the client's traffic
} }
catch (Exception)
{
sslStream.Dispose();
Dispose(sslStream,
clientStreamReader,
clientStreamWriter, null);
disposed = true;
return;
}
}
else
{
clientStreamReader = new CustomBinaryReader(clientStream, BufferSize); clientStreamReader = new CustomBinaryReader(clientStream, BufferSize);
clientStreamWriter = new StreamWriter(clientStream) { NewLine = ProxyConstants.NewLine }; clientStreamWriter = new StreamWriter(clientStream) { NewLine = ProxyConstants.NewLine };
}
//now read the request line
var httpCmd = await clientStreamReader.ReadLineAsync();
//Now create the request //now read the request line
disposed = await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter, var httpCmd = await clientStreamReader.ReadLineAsync();
endPoint.EnableSsl ? endPoint.GenericCertificateName : null, endPoint, null);
if (!disposed) //Now create the request
disposed = await HandleHttpSessionRequest(tcpClient, httpCmd, clientStream, clientStreamReader, clientStreamWriter,
endPoint.EnableSsl ? endPoint.GenericCertificateName : null, endPoint, null);
}
finally
{ {
Dispose(clientStream, clientStreamReader, clientStreamWriter, null); if (!disposed)
{
Dispose(clientStream, clientStreamReader, clientStreamWriter, null);
}
} }
} }
...@@ -271,9 +259,11 @@ namespace Titanium.Web.Proxy ...@@ -271,9 +259,11 @@ namespace Titanium.Web.Proxy
} }
private async Task<bool> HandleHttpSessionRequestInternal(TcpConnection connection, private async Task<bool> HandleHttpSessionRequestInternal(TcpConnection connection, SessionEventArgs args, bool closeConnection)
SessionEventArgs args, bool closeConnection)
{ {
bool disposed = false;
bool keepAlive = false;
try try
{ {
args.WebSession.Request.RequestLocked = true; args.WebSession.Request.RequestLocked = true;
...@@ -281,11 +271,6 @@ namespace Titanium.Web.Proxy ...@@ -281,11 +271,6 @@ namespace Titanium.Web.Proxy
//If request was cancelled by user then dispose the client //If request was cancelled by user then dispose the client
if (args.WebSession.Request.CancelRequest) if (args.WebSession.Request.CancelRequest)
{ {
Dispose(args.ProxyClient.ClientStream,
args.ProxyClient.ClientStreamReader,
args.ProxyClient.ClientStreamWriter,
args.WebSession.ServerConnection);
return true; return true;
} }
...@@ -350,7 +335,7 @@ namespace Titanium.Web.Proxy ...@@ -350,7 +335,7 @@ namespace Titanium.Web.Proxy
//If not expectation failed response was returned by server then parse response //If not expectation failed response was returned by server then parse response
if (!args.WebSession.Request.ExpectationFailed) if (!args.WebSession.Request.ExpectationFailed)
{ {
var disposed = await HandleHttpSessionResponse(args); disposed = await HandleHttpSessionResponse(args);
//already disposed inside above method //already disposed inside above method
if (disposed) if (disposed)
...@@ -362,38 +347,30 @@ namespace Titanium.Web.Proxy ...@@ -362,38 +347,30 @@ namespace Titanium.Web.Proxy
//if connection is closing exit //if connection is closing exit
if (args.WebSession.Response.ResponseKeepAlive == false) if (args.WebSession.Response.ResponseKeepAlive == false)
{ {
Dispose(args.ProxyClient.ClientStream,
args.ProxyClient.ClientStreamReader,
args.ProxyClient.ClientStreamWriter,
args.WebSession.ServerConnection);
return true; return true;
} }
if (!closeConnection)
{
keepAlive = true;
return false;
}
} }
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new ProxyHttpException("Error occured whilst handling session request (internal)", e, args)); ExceptionFunc(new ProxyHttpException("Error occured whilst handling session request (internal)", e, args));
Dispose(args.ProxyClient.ClientStream,
args.ProxyClient.ClientStreamReader,
args.ProxyClient.ClientStreamWriter,
args.WebSession.ServerConnection);
return true; return true;
} }
finally
if (closeConnection)
{ {
//dispose if (!disposed && !keepAlive)
Dispose(args.ProxyClient.ClientStream, {
args.ProxyClient.ClientStreamReader, //dispose
args.ProxyClient.ClientStreamWriter, Dispose(args.ProxyClient.ClientStream, args.ProxyClient.ClientStreamReader, args.ProxyClient.ClientStreamWriter, args.WebSession.ServerConnection);
args.WebSession.ServerConnection); }
return true;
} }
return false; return true;
} }
/// <summary> /// <summary>
...@@ -412,7 +389,7 @@ namespace Titanium.Web.Proxy ...@@ -412,7 +389,7 @@ namespace Titanium.Web.Proxy
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName, CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName,
ProxyEndPoint endPoint, List<HttpHeader> connectHeaders) ProxyEndPoint endPoint, List<HttpHeader> connectHeaders)
{ {
var disposed = false; bool disposed = false;
TcpConnection connection = null; TcpConnection connection = null;
...@@ -422,12 +399,6 @@ namespace Titanium.Web.Proxy ...@@ -422,12 +399,6 @@ namespace Titanium.Web.Proxy
{ {
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
{ {
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
...@@ -489,12 +460,6 @@ namespace Titanium.Web.Proxy ...@@ -489,12 +460,6 @@ namespace Titanium.Web.Proxy
await CheckAuthorization(clientStreamWriter, await CheckAuthorization(clientStreamWriter,
args.WebSession.Request.RequestHeaders.Values) == false) args.WebSession.Request.RequestHeaders.Values) == false)
{ {
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
...@@ -515,12 +480,6 @@ namespace Titanium.Web.Proxy ...@@ -515,12 +480,6 @@ namespace Titanium.Web.Proxy
httpCmd, httpVersion, args.WebSession.Request.RequestHeaders, args.IsHttps, httpCmd, httpVersion, args.WebSession.Request.RequestHeaders, args.IsHttps,
clientStream, tcpConnectionFactory); clientStream, tcpConnectionFactory);
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
...@@ -540,24 +499,12 @@ namespace Titanium.Web.Proxy ...@@ -540,24 +499,12 @@ namespace Titanium.Web.Proxy
if (args.WebSession.Request.CancelRequest) if (args.WebSession.Request.CancelRequest)
{ {
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
//if connection is closing exit //if connection is closing exit
if (args.WebSession.Response.ResponseKeepAlive == false) if (args.WebSession.Response.ResponseKeepAlive == false)
{ {
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
...@@ -567,18 +514,16 @@ namespace Titanium.Web.Proxy ...@@ -567,18 +514,16 @@ namespace Titanium.Web.Proxy
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new ProxyHttpException("Error occured whilst handling session request", e, args)); ExceptionFunc(new ProxyHttpException("Error occured whilst handling session request", e, args));
Dispose(clientStream,
clientStreamReader,
clientStreamWriter,
connection);
disposed = true;
break; break;
} }
} }
return disposed; if (!disposed)
{
Dispose(clientStream, clientStreamReader, clientStreamWriter, connection);
}
return true;
} }
/// <summary> /// <summary>
......
...@@ -53,8 +53,8 @@ namespace Titanium.Web.Proxy ...@@ -53,8 +53,8 @@ namespace Titanium.Web.Proxy
} }
var connection = await GetServerConnection(args); var connection = await GetServerConnection(args);
var result = await HandleHttpSessionRequestInternal(null, args, true); var disposed = await HandleHttpSessionRequestInternal(null, args, true);
return result; return disposed;
} }
args.WebSession.Response.ResponseLocked = true; args.WebSession.Response.ResponseLocked = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment