Commit b343ce05 authored by justcoding121's avatar justcoding121

align lines

parent d2814921
......@@ -16,7 +16,6 @@ using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Network.Tcp
{
/// <summary>
/// A class that manages Tcp Connection to server used by this proxy server.
/// </summary>
......@@ -35,14 +34,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
private bool runCleanUpTask = true;
internal ProxyServer server { get; set; }
internal TcpConnectionFactory(ProxyServer server)
{
this.server = server;
Task.Run(async () => await clearOutdatedConnections());
}
internal ProxyServer server { get; set; }
/// <summary>
/// Gets a TCP connection to server from connection pool.
/// </summary>
......@@ -73,10 +72,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
}
cacheKeyBuilder.Append(upStreamEndPoint != null ? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-" : string.Empty);
cacheKeyBuilder.Append(upStreamEndPoint != null
? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-"
: string.Empty);
cacheKeyBuilder.Append(externalProxy != null ? $"{externalProxy.GetCacheKey()}-" : string.Empty);
var cacheKey = cacheKeyBuilder.ToString();
string cacheKey = cacheKeyBuilder.ToString();
if (proxyServer.EnableConnectionPool)
{
......@@ -95,7 +96,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag.Add(recentConnection);
}
}
}
......@@ -126,7 +126,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
CancellationToken cancellationToken)
{
bool useUpstreamProxy = false;
// check if external proxy is set for HTTP/HTTPS
......@@ -223,7 +222,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
negotiatedApplicationProtocol = sslStream.NegotiatedApplicationProtocol;
#endif
}
}
catch (Exception)
{
......@@ -274,9 +272,11 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (cache.TryAdd(connection.CacheKey, new ConcurrentQueue<TcpServerConnection>(new[] { connection })))
{
break;
};
}
;
}
@lock.Release();
}
......@@ -295,6 +295,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
disposalBag.Add(connection);
continue;
}
queue.Enqueue(connection);
break;
}
......@@ -303,13 +304,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
//clear empty queues
await @lock.WaitAsync();
var emptyKeys = cache.Where(x => x.Value.Count == 0).Select(x => x.Key).ToList();
foreach (var key in emptyKeys)
foreach (string key in emptyKeys)
{
cache.TryRemove(key, out var _);
}
@lock.Release();
while (disposalBag.TryTake(out TcpServerConnection connection))
while (disposalBag.TryTake(out var connection))
{
connection?.Dispose();
}
......@@ -317,7 +319,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
//cleanup every ten seconds by default
await Task.Delay(1000 * 10);
}
}
/// <summary>
......
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
......@@ -26,7 +24,8 @@ namespace Titanium.Web.Proxy
private static readonly Regex uriSchemeRegex =
new Regex("^[a-z]*://", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static readonly HashSet<string> proxySupportedCompressions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
private static readonly HashSet<string> proxySupportedCompressions =
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"gzip",
"deflate"
......@@ -178,6 +177,7 @@ namespace Titanium.Web.Proxy
continue;
}
bool connectionChanged = false;
// create a new connection if hostname/upstream end point changes
if (serverConnection != null
&& (!serverConnection.HostName.EqualsIgnoreCase(request.RequestUri.Host)
......@@ -186,17 +186,18 @@ namespace Titanium.Web.Proxy
{
tcpConnectionFactory.Release(serverConnection, true);
serverConnection = null;
connectionChanged = true;
}
var connectionChanged = false;
if (serverConnection == null)
{
serverConnection = await getServerConnection(args, false, clientConnection.NegotiatedApplicationProtocol, cancellationToken);
serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
connectionChanged = true;
}
//for connection pool retry attempt until we get a good connection
var attemt = 0;
int attemt = 0;
while (attemt < 3)
{
try
......@@ -223,7 +224,8 @@ namespace Titanium.Web.Proxy
//get new connection from pool
tcpConnectionFactory.Release(serverConnection, true);
serverConnection = await getServerConnection(args, false, clientConnection.NegotiatedApplicationProtocol, cancellationToken);
serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
attemt++;
continue;
}
......@@ -348,7 +350,6 @@ namespace Titanium.Web.Proxy
{
await handleHttpSessionResponse(args);
}
}
/// <summary>
......@@ -407,7 +408,7 @@ namespace Titanium.Web.Proxy
/// </summary>
private void prepareRequestHeaders(HeaderCollection requestHeaders)
{
var acceptEncoding = requestHeaders.GetHeaderValueOrNull(KnownHeaders.AcceptEncoding);
string acceptEncoding = requestHeaders.GetHeaderValueOrNull(KnownHeaders.AcceptEncoding);
if (acceptEncoding != null)
{
......@@ -421,7 +422,8 @@ namespace Titanium.Web.Proxy
//uncompressed is always supported by proxy
supportedAcceptEncoding.Add("identity");
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding, string.Join(",", supportedAcceptEncoding));
requestHeaders.SetOrAddHeaderValue(KnownHeaders.AcceptEncoding,
string.Join(",", supportedAcceptEncoding));
}
requestHeaders.FixProxyHeaders();
......@@ -436,7 +438,6 @@ namespace Titanium.Web.Proxy
TcpServerConnection serverConnection,
CancellationTokenSource cancellationTokenSource, CancellationToken cancellationToken)
{
// prepare the prefix content
await serverConnection.StreamWriter.WriteLineAsync(httpCmd, cancellationToken);
await serverConnection.StreamWriter.WriteHeadersAsync(request.Headers,
......
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