Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit ecfc7ff

Browse files
committed
Remove code used for HttpWebRequest
1 parent c7b20f9 commit ecfc7ff

File tree

6 files changed

+28
-98
lines changed

6 files changed

+28
-98
lines changed

Titanium.Web.Proxy/Helpers/NetFramework.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

Titanium.Web.Proxy/Network/HttpWebClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class Request
2626
public string RequestHost { get; set; }
2727

2828
public string RequestUrl { get; internal set; }
29-
public string RequestHostname { get; internal set; }
3029

3130
internal Encoding RequestEncoding { get; set; }
3231
internal Version RequestHttpVersion { get; set; }
@@ -95,6 +94,7 @@ public bool IsSecure
9594

9695
public void SetConnection(TcpConnection Connection)
9796
{
97+
Connection.LastAccess = DateTime.Now;
9898
ProxyClient = Connection;
9999
}
100100

Titanium.Web.Proxy/Network/TcpConnectionManager.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ internal class TcpConnectionManager
3636

3737
public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
3838
{
39-
39+
TcpConnection cached = null;
4040
while (true)
4141
{
42-
TcpConnection cached = null;
4342
lock (ConnectionCache)
4443
{
4544
cached = ConnectionCache.FirstOrDefault(x => x.HostName == Hostname && x.port == port && x.IsSecure == IsSecure && x.TcpClient.Connected);
@@ -55,7 +54,15 @@ public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
5554
break;
5655
}
5756

58-
return CreateClient(Hostname, port, IsSecure);
57+
if (cached == null)
58+
cached = CreateClient(Hostname, port, IsSecure);
59+
60+
if (ConnectionCache.Where(x => x.HostName == Hostname && x.port == port && x.IsSecure == IsSecure && x.TcpClient.Connected).Count() < 2)
61+
{
62+
Task.Factory.StartNew(() => ReleaseClient(CreateClient(Hostname, port, IsSecure)));
63+
}
64+
65+
return cached;
5966
}
6067

6168
private static TcpConnection CreateClient(string Hostname, int port, bool IsSecure)

Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,7 @@ static ProxyServer()
5959
public static event EventHandler<SessionEventArgs> BeforeResponse;
6060

6161
public static void Initialize()
62-
{
63-
ServicePointManager.Expect100Continue = false;
64-
WebRequest.DefaultWebProxy = null;
65-
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
66-
ServicePointManager.DnsRefreshTimeout = 3 * 60 * 1000; //3 minutes
67-
ServicePointManager.MaxServicePointIdleTime = 3 * 60 * 1000;
68-
69-
//HttpWebRequest certificate validation callback
70-
ServicePointManager.ServerCertificateValidationCallback =
71-
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
72-
{
73-
if (sslPolicyErrors == SslPolicyErrors.None) return true;
74-
return false;
75-
};
76-
77-
#if NET40
78-
//Fix a bug in .NET 4.0
79-
NetFrameworkHelper.UrlPeriodFix();
80-
//useUnsafeHeaderParsing
81-
#endif
82-
NetFrameworkHelper.ToggleAllowUnsafeHeaderParsing(true);
62+
{
8363
Task.Factory.StartNew(()=>TcpConnectionManager.ClearIdleConnections());
8464
}
8565

Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ private static void HandleClient(TcpClient client)
115115
private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, Stream clientStream,
116116
CustomBinaryReader clientStreamReader, StreamWriter clientStreamWriter, string secureTunnelHostName)
117117
{
118+
TcpConnection connection = null;
119+
string lastRequestHostName = null;
118120

119121
while (true)
120122
{
121123
if (string.IsNullOrEmpty(httpCmd))
122124
{
123125
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, null);
124-
return;
126+
break;
125127
}
126128

127129
var args = new SessionEventArgs(BUFFER_SIZE);
@@ -156,14 +158,14 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
156158
args.ProxySession.Request.RequestHeaders = new List<HttpHeader>();
157159

158160
string tmpLine;
159-
160161
while (!string.IsNullOrEmpty(tmpLine = clientStreamReader.ReadLine()))
161162
{
162163
var header = tmpLine.Split(new char[] { ':' }, 2);
163164
args.ProxySession.Request.RequestHeaders.Add(new HttpHeader(header[0], header[1]));
164165
}
165166

166167
SetRequestHeaders(args.ProxySession.Request.RequestHeaders, args.ProxySession);
168+
167169
if (args.ProxySession.Request.UpgradeToWebSocket)
168170
{
169171
TcpHelper.SendRaw(clientStreamReader.BaseStream, httpCmd, args.ProxySession.Request.RequestHeaders,
@@ -179,7 +181,7 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
179181
args.Client.ClientStream = clientStream;
180182
args.Client.ClientStreamReader = clientStreamReader;
181183
args.Client.ClientStreamWriter = clientStreamWriter;
182-
args.ProxySession.Request.RequestHostname = args.ProxySession.Request.RequestUri.Host;
184+
args.ProxySession.Request.RequestHost = args.ProxySession.Request.RequestUri.Host;
183185
args.ProxySession.Request.RequestUrl = args.ProxySession.Request.RequestUri.OriginalString;
184186
args.Client.ClientPort = ((IPEndPoint)client.Client.RemoteEndPoint).Port;
185187
args.Client.ClientIpAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
@@ -198,14 +200,19 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
198200
if (args.ProxySession.Request.CancelRequest)
199201
{
200202
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
201-
return;
203+
break;
202204
}
203205

204206

205207
//construct the web request that we are going to issue on behalf of the client.
206-
var connection = TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps);
207-
args.ProxySession.SetConnection(connection);
208+
connection = connection == null ?
209+
TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps)
210+
: lastRequestHostName != args.ProxySession.Request.RequestHost ? TcpConnectionManager.GetClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps)
211+
: connection;
212+
213+
lastRequestHostName = args.ProxySession.Request.RequestHost;
208214

215+
args.ProxySession.SetConnection(connection);
209216
args.ProxySession.SendRequest();
210217

211218
//If request was modified by user
@@ -233,8 +240,6 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
233240
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
234241
return;
235242
}
236-
else
237-
TcpConnectionManager.ReleaseClient(connection);
238243

239244
// read the next request
240245
httpCmd = clientStreamReader.ReadLine();
@@ -245,8 +250,11 @@ private static void HandleHttpSessionRequest(TcpClient client, string httpCmd, S
245250
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
246251
break;
247252
}
253+
248254
}
249255

256+
if (connection != null)
257+
TcpConnectionManager.ReleaseClient(connection);
250258
}
251259

252260
private static void WriteConnectResponse(StreamWriter clientStreamWriter, string httpVersion)

Titanium.Web.Proxy/Titanium.Web.Proxy.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
<Compile Include="RequestHandler.cs" />
9494
<Compile Include="ResponseHandler.cs" />
9595
<Compile Include="Helpers\CustomBinaryReader.cs" />
96-
<Compile Include="Helpers\NetFramework.cs" />
9796
<Compile Include="Helpers\Compression.cs" />
9897
<Compile Include="ProxyServer.cs" />
9998
<Compile Include="EventArguments\SessionEventArgs.cs" />

0 commit comments

Comments
 (0)