Commit ace7c729 authored by justcoding121's avatar justcoding121 Committed by justcoding121

use existing connection for websocket upgrade if available

parent f1410e9f
...@@ -179,12 +179,14 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -179,12 +179,14 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="isHttps"></param> /// <param name="isHttps"></param>
/// <param name="clientStream"></param> /// <param name="clientStream"></param>
/// <param name="tcpConnectionFactory"></param> /// <param name="tcpConnectionFactory"></param>
/// <param name="connection"></param>
/// <returns></returns> /// <returns></returns>
internal static async Task SendRaw(ProxyServer server, internal static async Task SendRaw(ProxyServer server,
string remoteHostName, int remotePort, string remoteHostName, int remotePort,
string httpCmd, Version httpVersion, Dictionary<string, HttpHeader> requestHeaders, string httpCmd, Version httpVersion, Dictionary<string, HttpHeader> requestHeaders,
bool isHttps, bool isHttps,
Stream clientStream, TcpConnectionFactory tcpConnectionFactory) Stream clientStream, TcpConnectionFactory tcpConnectionFactory,
TcpConnection connection = null)
{ {
//prepare the prefix content //prepare the prefix content
StringBuilder sb = null; StringBuilder sb = null;
...@@ -210,10 +212,23 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -210,10 +212,23 @@ namespace Titanium.Web.Proxy.Helpers
sb.Append(ProxyConstants.NewLine); sb.Append(ProxyConstants.NewLine);
} }
var tcpConnection = await tcpConnectionFactory.CreateClient(server, bool connectionCreated = false;
remoteHostName, remotePort, TcpConnection tcpConnection;
httpVersion, isHttps,
null, null); //create new connection if connection is null
if (connection == null)
{
tcpConnection = await tcpConnectionFactory.CreateClient(server,
remoteHostName, remotePort,
httpVersion, isHttps,
null, null);
connectionCreated = true;
}
else
{
tcpConnection = connection;
}
try try
{ {
...@@ -228,8 +243,14 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -228,8 +243,14 @@ namespace Titanium.Web.Proxy.Helpers
} }
finally finally
{ {
tcpConnection.Dispose(); //if connection was null
Interlocked.Decrement(ref server.serverConnectionCount); //then a new connection was created
//so dispose the new connection
if (connectionCreated)
{
tcpConnection.Dispose();
Interlocked.Decrement(ref server.serverConnectionCount);
}
} }
} }
} }
......
...@@ -334,7 +334,7 @@ namespace Titanium.Web.Proxy ...@@ -334,7 +334,7 @@ namespace Titanium.Web.Proxy
await TcpHelper.SendRaw(this, await TcpHelper.SendRaw(this,
httpRemoteUri.Host, httpRemoteUri.Port, httpRemoteUri.Host, httpRemoteUri.Port,
httpCmd, httpVersion, args.WebSession.Request.RequestHeaders, args.IsHttps, httpCmd, httpVersion, args.WebSession.Request.RequestHeaders, args.IsHttps,
clientStream, tcpConnectionFactory); clientStream, tcpConnectionFactory, connection);
args.Dispose(); args.Dispose();
break; break;
......
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