Commit 4025cf2a authored by Honfika's avatar Honfika

Connection Reset #375 fix, disposing the connections/readers/writers in the...

Connection Reset #375 fix, disposing the connections/readers/writers in the same method where they are created
parent 5476c5e9
...@@ -170,18 +170,18 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -170,18 +170,18 @@ namespace Titanium.Web.Proxy.Examples.Basic
//requestBodyHistory[e.Id] = bodyString; //requestBodyHistory[e.Id] = bodyString;
} }
////To cancel a request with a custom HTML content //To cancel a request with a custom HTML content
////Filter URL //Filter URL
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com")) if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("google.com"))
//{ {
// await e.Ok("<!DOCTYPE html>" + await e.Ok("<!DOCTYPE html>" +
// "<html><body><h1>" + "<html><body><h1>" +
// "Website Blocked" + "Website Blocked" +
// "</h1>" + "</h1>" +
// "<p>Blocked by titanium web proxy.</p>" + "<p>Blocked by titanium web proxy.</p>" +
// "</body>" + "</body>" +
// "</html>"); "</html>");
//} }
////Redirect example ////Redirect example
//if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org")) //if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
......
...@@ -29,11 +29,6 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -29,11 +29,6 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
private readonly int bufferSize; private readonly int bufferSize;
/// <summary>
/// Holds a reference to proxy response handler method
/// </summary>
private Func<SessionEventArgs, Task> httpResponseHandler;
private readonly Action<Exception> exceptionFunc; private readonly Action<Exception> exceptionFunc;
/// <summary> /// <summary>
...@@ -108,11 +103,9 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -108,11 +103,9 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
internal SessionEventArgs(int bufferSize, internal SessionEventArgs(int bufferSize,
ProxyEndPoint endPoint, ProxyEndPoint endPoint,
Func<SessionEventArgs, Task> httpResponseHandler,
Action<Exception> exceptionFunc) Action<Exception> exceptionFunc)
{ {
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
this.httpResponseHandler = httpResponseHandler;
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
ProxyClient = new ProxyClient(); ProxyClient = new ProxyClient();
...@@ -637,8 +630,6 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -637,8 +630,6 @@ namespace Titanium.Web.Proxy.EventArguments
WebSession.Response = response; WebSession.Response = response;
await httpResponseHandler(this);
WebSession.Request.CancelRequest = true; WebSession.Request.CancelRequest = true;
} }
...@@ -647,7 +638,6 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -647,7 +638,6 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
httpResponseHandler = null;
CustomUpStreamProxyUsed = null; CustomUpStreamProxyUsed = null;
DataSent = null; DataSent = null;
......
...@@ -9,7 +9,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -9,7 +9,7 @@ namespace Titanium.Web.Proxy.EventArguments
public bool IsHttpsConnect { get; set; } public bool IsHttpsConnect { get; set; }
internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, Action<Exception> exceptionFunc) internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, Action<Exception> exceptionFunc)
: base(bufferSize, endPoint, null, exceptionFunc) : base(bufferSize, endPoint, exceptionFunc)
{ {
WebSession.Request = connectRequest; WebSession.Request = connectRequest;
} }
......
...@@ -88,10 +88,10 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -88,10 +88,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
{ {
string httpStatus = await reader.ReadLineAsync(); string httpStatus = await reader.ReadLineAsync();
Response.ParseResponseLine(httpStatus, out var version, out int statusCode, out string statusDescription); Response.ParseResponseLine(httpStatus, out _, out int statusCode, out string statusDescription);
if (!statusDescription.EqualsIgnoreCase("200 OK") if (statusCode != 200 && !statusDescription.EqualsIgnoreCase("OK")
&& !statusDescription.EqualsIgnoreCase("connection established")) && !statusDescription.EqualsIgnoreCase("Connection Established"))
{ {
throw new Exception("Upstream proxy failed to create a secure tunnel"); throw new Exception("Upstream proxy failed to create a secure tunnel");
} }
......
...@@ -654,26 +654,6 @@ namespace Titanium.Web.Proxy ...@@ -654,26 +654,6 @@ namespace Titanium.Web.Proxy
ProxyRunning = false; ProxyRunning = false;
} }
/// <summary>
/// Handle dispose of a client/server session
/// </summary>
/// <param name="clientStream"></param>
/// <param name="clientStreamReader"></param>
/// <param name="clientStreamWriter"></param>
/// <param name="serverConnection"></param>
private void Dispose(CustomBufferedStream clientStream, CustomBinaryReader clientStreamReader, HttpResponseWriter clientStreamWriter, TcpConnection serverConnection)
{
clientStreamReader?.Dispose();
clientStream?.Dispose();
if (serverConnection != null)
{
serverConnection.Dispose();
serverConnection = null;
}
}
/// <summary> /// <summary>
/// Dispose Proxy. /// Dispose Proxy.
/// </summary> /// </summary>
......
This diff is collapsed.
...@@ -18,7 +18,7 @@ namespace Titanium.Web.Proxy ...@@ -18,7 +18,7 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
/// <param name="args"></param> /// <param name="args"></param>
/// <returns>true if client/server connection was terminated (and disposed) </returns> /// <returns>true if client/server connection was terminated (and disposed) </returns>
private async Task<bool> HandleHttpSessionResponse(SessionEventArgs args) private async Task HandleHttpSessionResponse(SessionEventArgs args)
{ {
try try
{ {
...@@ -30,12 +30,7 @@ namespace Titanium.Web.Proxy ...@@ -30,12 +30,7 @@ namespace Titanium.Web.Proxy
//check for windows authentication //check for windows authentication
if (isWindowsAuthenticationEnabledAndSupported && response.StatusCode == (int)HttpStatusCode.Unauthorized) if (isWindowsAuthenticationEnabledAndSupported && response.StatusCode == (int)HttpStatusCode.Unauthorized)
{ {
bool disposed = await Handle401UnAuthorized(args); await Handle401UnAuthorized(args);
if (disposed)
{
return true;
}
} }
args.ReRequest = false; args.ReRequest = false;
...@@ -52,8 +47,8 @@ namespace Titanium.Web.Proxy ...@@ -52,8 +47,8 @@ namespace Titanium.Web.Proxy
{ {
//clear current response //clear current response
await args.ClearResponse(); await args.ClearResponse();
bool disposed = await HandleHttpSessionRequestInternal(args.WebSession.ServerConnection, args, false); await HandleHttpSessionRequestInternal(args.WebSession.ServerConnection, args);
return disposed; return;
} }
response.ResponseLocked = true; response.ResponseLocked = true;
...@@ -109,16 +104,10 @@ namespace Titanium.Web.Proxy ...@@ -109,16 +104,10 @@ namespace Titanium.Web.Proxy
} }
} }
} }
catch (Exception e) catch (Exception e) when (!(e is ProxyHttpException))
{ {
ExceptionFunc(new ProxyHttpException("Error occured whilst handling session response", e, args)); throw new ProxyHttpException("Error occured whilst handling session response", e, args);
Dispose(args.ProxyClient.ClientStream, args.ProxyClient.ClientStreamReader, args.ProxyClient.ClientStreamWriter,
args.WebSession.ServerConnection);
return true;
} }
return false;
} }
/// <summary> /// <summary>
......
...@@ -135,8 +135,7 @@ namespace Titanium.Web.Proxy ...@@ -135,8 +135,7 @@ namespace Titanium.Web.Proxy
//request again with updated authorization header //request again with updated authorization header
//and server cookies //and server cookies
bool disposed = await HandleHttpSessionRequestInternal(args.WebSession.ServerConnection, args, false); await HandleHttpSessionRequestInternal(args.WebSession.ServerConnection, args);
return disposed;
} }
return false; return false;
......
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