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,11 +212,24 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -210,11 +212,24 @@ namespace Titanium.Web.Proxy.Helpers
sb.Append(ProxyConstants.NewLine); sb.Append(ProxyConstants.NewLine);
} }
var tcpConnection = await tcpConnectionFactory.CreateClient(server, bool connectionCreated = false;
TcpConnection tcpConnection;
//create new connection if connection is null
if (connection == null)
{
tcpConnection = await tcpConnectionFactory.CreateClient(server,
remoteHostName, remotePort, remoteHostName, remotePort,
httpVersion, isHttps, httpVersion, isHttps,
null, null); null, null);
connectionCreated = true;
}
else
{
tcpConnection = connection;
}
try try
{ {
Stream tunnelStream = tcpConnection.Stream; Stream tunnelStream = tcpConnection.Stream;
...@@ -227,10 +242,16 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -227,10 +242,16 @@ namespace Titanium.Web.Proxy.Helpers
await Task.WhenAll(sendRelay, receiveRelay); await Task.WhenAll(sendRelay, receiveRelay);
} }
finally finally
{
//if connection was null
//then a new connection was created
//so dispose the new connection
if (connectionCreated)
{ {
tcpConnection.Dispose(); tcpConnection.Dispose();
Interlocked.Decrement(ref server.serverConnectionCount); 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