Commit 4aad40e9 authored by justcoding121's avatar justcoding121

use existing connection for websocket upgrade if available

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