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

Commit 6453375

Browse files
Merge pull request #95 from justcoding121/perf-improve
Perf improve
2 parents 2d85486 + 7f00209 commit 6453375

18 files changed

+491
-160
lines changed

Examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ public Task OnCertificateValidation(object sender, CertificateValidationEventArg
141141
{
142142
//set IsValid to true/false based on Certificate Errors
143143
if (e.SslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
144+
{
144145
e.IsValid = true;
146+
}
145147

146148
return Task.FromResult(0);
147149
}

Titanium.Web.Proxy/CertificateHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ internal bool ValidateServerCertificate(
4747
}
4848

4949
if (sslPolicyErrors == SslPolicyErrors.None)
50+
{
5051
return true;
52+
}
5153

5254
//By default
5355
//do not allow this client to communicate with unauthenticated servers.
@@ -82,13 +84,17 @@ internal X509Certificate SelectClientCertificate(
8284
{
8385
string issuer = certificate.Issuer;
8486
if (Array.IndexOf(acceptableIssuers, issuer) != -1)
87+
{
8588
clientCertificate = certificate;
89+
}
8690
}
8791
}
8892

8993
if (localCertificates != null &&
9094
localCertificates.Count > 0)
95+
{
9196
clientCertificate = localCertificates[0];
97+
}
9298

9399
//If user call back is registered
94100
if (ClientCertificateSelectionCallback != null)
@@ -115,7 +121,6 @@ internal X509Certificate SelectClientCertificate(
115121
}
116122

117123
return clientCertificate;
118-
119124
}
120125
}
121126
}

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ private async Task ReadRequestBody()
7272
if ((WebSession.Request.Method.ToUpper() != "POST" && WebSession.Request.Method.ToUpper() != "PUT"))
7373
{
7474
throw new BodyNotFoundException("Request don't have a body." +
75-
"Please verify that this request is a Http POST/PUT and request content length is greater than zero before accessing the body.");
75+
"Please verify that this request is a Http POST/PUT and request " +
76+
"content length is greater than zero before accessing the body.");
7677
}
7778

7879
//Caching check
7980
if (WebSession.Request.RequestBody == null)
8081
{
8182

8283
//If chunked then its easy just read the whole body with the content length mentioned in the request header
83-
8484
using (var requestBodyStream = new MemoryStream())
8585
{
8686
//For chunked request we need to read data as they arrive, until we reach a chunk end symbol
@@ -94,13 +94,17 @@ private async Task ReadRequestBody()
9494
if (WebSession.Request.ContentLength > 0)
9595
{
9696
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
97-
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream, WebSession.Request.ContentLength);
97+
await this.ProxyClient.ClientStreamReader.CopyBytesToStream(bufferSize, requestBodyStream,
98+
WebSession.Request.ContentLength);
9899

99100
}
100101
else if (WebSession.Request.HttpVersion.Major == 1 && WebSession.Request.HttpVersion.Minor == 0)
102+
{
101103
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, requestBodyStream, long.MaxValue);
104+
}
102105
}
103-
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray());
106+
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding,
107+
requestBodyStream.ToArray());
104108
}
105109

106110
//Now set the flag to true
@@ -130,14 +134,18 @@ private async Task ReadResponseBody()
130134
if (WebSession.Response.ContentLength > 0)
131135
{
132136
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
133-
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, WebSession.Response.ContentLength);
137+
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream,
138+
WebSession.Response.ContentLength);
134139

135140
}
136141
else if (WebSession.Response.HttpVersion.Major == 1 && WebSession.Response.HttpVersion.Minor == 0)
142+
{
137143
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(bufferSize, responseBodyStream, long.MaxValue);
144+
}
138145
}
139146

140-
WebSession.Response.ResponseBody = await GetDecompressedResponseBody(WebSession.Response.ContentEncoding, responseBodyStream.ToArray());
147+
WebSession.Response.ResponseBody = await GetDecompressedResponseBody(WebSession.Response.ContentEncoding,
148+
responseBodyStream.ToArray());
141149

142150
}
143151
//set this to true for caching
@@ -154,7 +162,9 @@ public async Task<byte[]> GetRequestBody()
154162
if (!WebSession.Request.RequestBodyRead)
155163
{
156164
if (WebSession.Request.RequestLocked)
165+
{
157166
throw new Exception("You cannot call this function after request is made to server.");
167+
}
158168

159169
await ReadRequestBody();
160170
}
@@ -169,7 +179,9 @@ public async Task<string> GetRequestBodyAsString()
169179
if (!WebSession.Request.RequestBodyRead)
170180
{
171181
if (WebSession.Request.RequestLocked)
182+
{
172183
throw new Exception("You cannot call this function after request is made to server.");
184+
}
173185

174186
await ReadRequestBody();
175187
}
@@ -184,7 +196,9 @@ public async Task<string> GetRequestBodyAsString()
184196
public async Task SetRequestBody(byte[] body)
185197
{
186198
if (WebSession.Request.RequestLocked)
199+
{
187200
throw new Exception("You cannot call this function after request is made to server.");
201+
}
188202

189203
//syphon out the request body from client before setting the new body
190204
if (!WebSession.Request.RequestBodyRead)
@@ -195,9 +209,13 @@ public async Task SetRequestBody(byte[] body)
195209
WebSession.Request.RequestBody = body;
196210

197211
if (WebSession.Request.IsChunked == false)
212+
{
198213
WebSession.Request.ContentLength = body.Length;
214+
}
199215
else
216+
{
200217
WebSession.Request.ContentLength = -1;
218+
}
201219
}
202220

203221
/// <summary>
@@ -207,7 +225,9 @@ public async Task SetRequestBody(byte[] body)
207225
public async Task SetRequestBodyString(string body)
208226
{
209227
if (WebSession.Request.RequestLocked)
228+
{
210229
throw new Exception("You cannot call this function after request is made to server.");
230+
}
211231

212232
//syphon out the request body from client before setting the new body
213233
if (!WebSession.Request.RequestBodyRead)
@@ -226,7 +246,9 @@ public async Task SetRequestBodyString(string body)
226246
public async Task<byte[]> GetResponseBody()
227247
{
228248
if (!WebSession.Request.RequestLocked)
249+
{
229250
throw new Exception("You cannot call this function before request is made to server.");
251+
}
230252

231253
await ReadResponseBody();
232254
return WebSession.Response.ResponseBody;
@@ -239,11 +261,14 @@ public async Task<byte[]> GetResponseBody()
239261
public async Task<string> GetResponseBodyAsString()
240262
{
241263
if (!WebSession.Request.RequestLocked)
264+
{
242265
throw new Exception("You cannot call this function before request is made to server.");
266+
}
243267

244268
await GetResponseBody();
245269

246-
return WebSession.Response.ResponseBodyString ?? (WebSession.Response.ResponseBodyString = WebSession.Response.Encoding.GetString(WebSession.Response.ResponseBody));
270+
return WebSession.Response.ResponseBodyString ??
271+
(WebSession.Response.ResponseBodyString = WebSession.Response.Encoding.GetString(WebSession.Response.ResponseBody));
247272
}
248273

249274
/// <summary>
@@ -253,7 +278,9 @@ public async Task<string> GetResponseBodyAsString()
253278
public async Task SetResponseBody(byte[] body)
254279
{
255280
if (!WebSession.Request.RequestLocked)
281+
{
256282
throw new Exception("You cannot call this function before request is made to server.");
283+
}
257284

258285
//syphon out the response body from server before setting the new body
259286
if (WebSession.Response.ResponseBody == null)
@@ -265,9 +292,13 @@ public async Task SetResponseBody(byte[] body)
265292

266293
//If there is a content length header update it
267294
if (WebSession.Response.IsChunked == false)
295+
{
268296
WebSession.Response.ContentLength = body.Length;
297+
}
269298
else
299+
{
270300
WebSession.Response.ContentLength = -1;
301+
}
271302
}
272303

273304
/// <summary>
@@ -277,7 +308,9 @@ public async Task SetResponseBody(byte[] body)
277308
public async Task SetResponseBodyString(string body)
278309
{
279310
if (!WebSession.Request.RequestLocked)
311+
{
280312
throw new Exception("You cannot call this function before request is made to server.");
313+
}
281314

282315
//syphon out the response body from server before setting the new body
283316
if (WebSession.Response.ResponseBody == null)
@@ -308,10 +341,14 @@ private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte
308341
public async Task Ok(string html)
309342
{
310343
if (WebSession.Request.RequestLocked)
344+
{
311345
throw new Exception("You cannot call this function after request is made to server.");
346+
}
312347

313348
if (html == null)
349+
{
314350
html = string.Empty;
351+
}
315352

316353
var result = Encoding.Default.GetBytes(html);
317354

@@ -341,7 +378,7 @@ public async Task Redirect(string url)
341378
var response = new RedirectResponse();
342379

343380
response.HttpVersion = WebSession.Request.HttpVersion;
344-
response.ResponseHeaders.Add(new Models.HttpHeader("Location", url));
381+
response.ResponseHeaders.Add("Location", new Models.HttpHeader("Location", url));
345382
response.ResponseBody = Encoding.ASCII.GetBytes(string.Empty);
346383

347384
await Respond(response);

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ internal static Encoding GetEncoding(this Request request)
2020
{
2121
//return default if not specified
2222
if (request.ContentType == null)
23+
{
2324
return Encoding.GetEncoding("ISO-8859-1");
25+
}
2426

2527
//extract the encoding by finding the charset
2628
var contentTypes = request.ContentType.Split(ProxyConstants.SemiColonSplit);

Titanium.Web.Proxy/Extensions/HttpWebResponseExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ internal static Encoding GetResponseCharacterEncoding(this Response response)
1717
{
1818
//return default if not specified
1919
if (response.ContentType == null)
20+
{
2021
return Encoding.GetEncoding("ISO-8859-1");
22+
}
2123

2224
//extract the encoding by finding the charset
2325
var contentTypes = response.ContentType.Split(ProxyConstants.SemiColonSplit);

Titanium.Web.Proxy/Extensions/StreamExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal static async Task CopyToAsync(this Stream input, string initialData, St
2626
var bytes = Encoding.ASCII.GetBytes(initialData);
2727
await output.WriteAsync(bytes, 0, bytes.Length);
2828
}
29+
2930
await input.CopyToAsync(output);
3031
}
3132
/// <summary>
@@ -45,15 +46,19 @@ internal static async Task CopyBytesToStream(this CustomBinaryReader streamReade
4546
bytesToRead = totalBytesToRead;
4647
}
4748
else
49+
{
4850
bytesToRead = bufferSize;
51+
}
4952

5053

5154
while (totalbytesRead < totalBytesToRead)
5255
{
5356
var buffer = await streamReader.ReadBytesAsync(bufferSize, bytesToRead);
5457

5558
if (buffer.Length == 0)
59+
{
5660
break;
61+
}
5762

5863
totalbytesRead += buffer.Length;
5964

@@ -62,6 +67,7 @@ internal static async Task CopyBytesToStream(this CustomBinaryReader streamReade
6267
{
6368
bytesToRead = remainingBytes;
6469
}
70+
6571
await stream.WriteAsync(buffer, 0, buffer.Length);
6672
}
6773
}
@@ -107,7 +113,9 @@ internal static async Task WriteResponseBody(this Stream clientStream, byte[] da
107113
await clientStream.WriteAsync(data, 0, data.Length);
108114
}
109115
else
116+
{
110117
await WriteResponseBodyChunked(data, clientStream);
118+
}
111119
}
112120
/// <summary>
113121
/// Copies the specified content length number of bytes to the output stream from the given inputs stream
@@ -124,12 +132,16 @@ internal static async Task WriteResponseBody(this CustomBinaryReader inStreamRea
124132
{
125133
//http 1.0
126134
if (ContentLength == -1)
135+
{
127136
ContentLength = long.MaxValue;
137+
}
128138

129139
int bytesToRead = bufferSize;
130140

131141
if (ContentLength < bufferSize)
142+
{
132143
bytesToRead = (int)ContentLength;
144+
}
133145

134146
var buffer = new byte[bufferSize];
135147

@@ -150,7 +162,9 @@ internal static async Task WriteResponseBody(this CustomBinaryReader inStreamRea
150162
}
151163
}
152164
else
165+
{
153166
await WriteResponseBodyChunked(inStreamReader, bufferSize, outStream);
167+
}
154168
}
155169

156170
/// <summary>

Titanium.Web.Proxy/Extensions/TcpExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal static bool IsConnected(this Socket client)
1313
{
1414
// This is how you can determine whether a socket is still connected.
1515
bool blockingState = client.Blocking;
16+
1617
try
1718
{
1819
byte[] tmp = new byte[1];
@@ -25,7 +26,9 @@ internal static bool IsConnected(this Socket client)
2526
{
2627
// 10035 == WSAEWOULDBLOCK
2728
if (e.NativeErrorCode.Equals(10035))
29+
{
2830
return true;
31+
}
2932
else
3033
{
3134
return false;

Titanium.Web.Proxy/Helpers/SystemProxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ internal class HttpSystemProxyValue
2727
public override string ToString()
2828
{
2929
if (!IsHttps)
30+
{
3031
return "http=" + HostName + ":" + Port;
32+
}
3133
else
34+
{
3235
return "https=" + HostName + ":" + Port;
36+
}
3337
}
3438
}
3539
/// <summary>
@@ -44,6 +48,7 @@ internal void SetHttpProxy(string hostname, int port)
4448
{
4549
var reg = Registry.CurrentUser.OpenSubKey(
4650
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
51+
4752
if (reg != null)
4853
{
4954
prepareRegistry(reg);

0 commit comments

Comments
 (0)