Commit d49e55ad authored by justcoding121's avatar justcoding121

Fix server connection reuse

parent 0786662d
...@@ -217,13 +217,12 @@ namespace Titanium.Web.Proxy ...@@ -217,13 +217,12 @@ namespace Titanium.Web.Proxy
endPoint.EnableSsl ? endPoint.GenericCertificateName : null, endPoint, null); endPoint.EnableSsl ? endPoint.GenericCertificateName : null, endPoint, null);
} }
private async Task<bool> HandleHttpSessionRequestInternal(TcpConnection connection, SessionEventArgs args, private async Task<TcpConnection> GetServerConnection(
ExternalProxy customUpStreamHttpProxy, ExternalProxy customUpStreamHttpsProxy, bool closeConnection) SessionEventArgs args,
{ ExternalProxy customUpStreamHttpProxy,
try ExternalProxy customUpStreamHttpsProxy, bool closeConnection)
{
if (connection == null)
{ {
if (args.WebSession.Request.RequestUri.Scheme == "http") if (args.WebSession.Request.RequestUri.Scheme == "http")
{ {
if (GetCustomUpStreamHttpProxyFunc != null) if (GetCustomUpStreamHttpProxyFunc != null)
...@@ -242,7 +241,7 @@ namespace Titanium.Web.Proxy ...@@ -242,7 +241,7 @@ namespace Titanium.Web.Proxy
args.CustomUpStreamHttpProxyUsed = customUpStreamHttpProxy; args.CustomUpStreamHttpProxyUsed = customUpStreamHttpProxy;
args.CustomUpStreamHttpsProxyUsed = customUpStreamHttpsProxy; args.CustomUpStreamHttpsProxyUsed = customUpStreamHttpsProxy;
connection = await tcpConnectionFactory.CreateClient(this, return await tcpConnectionFactory.CreateClient(this,
args.WebSession.Request.RequestUri.Host, args.WebSession.Request.RequestUri.Host,
args.WebSession.Request.RequestUri.Port, args.WebSession.Request.HttpVersion, args.WebSession.Request.RequestUri.Port, args.WebSession.Request.HttpVersion,
args.IsHttps, args.IsHttps,
...@@ -251,6 +250,12 @@ namespace Titanium.Web.Proxy ...@@ -251,6 +250,12 @@ namespace Titanium.Web.Proxy
args.ProxyClient.ClientStream); args.ProxyClient.ClientStream);
} }
private async Task<bool> HandleHttpSessionRequestInternal(TcpConnection connection,
SessionEventArgs args, bool closeConnection)
{
try
{
args.WebSession.Request.RequestLocked = true; args.WebSession.Request.RequestLocked = true;
//If request was cancelled by user then dispose the client //If request was cancelled by user then dispose the client
...@@ -389,6 +394,8 @@ namespace Titanium.Web.Proxy ...@@ -389,6 +394,8 @@ namespace Titanium.Web.Proxy
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName, CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string httpsHostName,
ProxyEndPoint endPoint, List<HttpHeader> connectHeaders, ExternalProxy customUpStreamHttpProxy = null, ExternalProxy customUpStreamHttpsProxy = null) ProxyEndPoint endPoint, List<HttpHeader> connectHeaders, ExternalProxy customUpStreamHttpProxy = null, ExternalProxy customUpStreamHttpsProxy = null)
{ {
TcpConnection connection = null;
//Loop through each subsequest request on this particular client connection //Loop through each subsequest request on this particular client connection
//(assuming HTTP connection is kept alive by client) //(assuming HTTP connection is kept alive by client)
while (true) while (true)
...@@ -500,8 +507,13 @@ namespace Titanium.Web.Proxy ...@@ -500,8 +507,13 @@ namespace Titanium.Web.Proxy
break; break;
} }
if (connection == null)
{
connection = await GetServerConnection(args, customUpStreamHttpProxy, customUpStreamHttpsProxy, false);
}
//construct the web request that we are going to issue on behalf of the client. //construct the web request that we are going to issue on behalf of the client.
var result = await HandleHttpSessionRequestInternal(null, args, customUpStreamHttpProxy, customUpStreamHttpsProxy, false); var result = await HandleHttpSessionRequestInternal(connection, args, false);
if (result == false) if (result == false)
{ {
...@@ -545,6 +557,8 @@ namespace Titanium.Web.Proxy ...@@ -545,6 +557,8 @@ namespace Titanium.Web.Proxy
} }
} }
connection?.Dispose();
} }
/// <summary> /// <summary>
......
...@@ -53,7 +53,14 @@ namespace Titanium.Web.Proxy ...@@ -53,7 +53,14 @@ namespace Titanium.Web.Proxy
if (args.ReRequest) if (args.ReRequest)
{ {
await HandleHttpSessionRequestInternal(null, args, null, null, true); if(args.WebSession.ServerConnection != null)
{
args.WebSession.ServerConnection.Dispose();
ServerConnectionCount--;
}
var connection = await GetServerConnection(args, null, null, true);
await HandleHttpSessionRequestInternal(null, args, true);
return true; return 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