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

Commit 34a6807

Browse files
committed
Asyncify fixes
1 parent 0320507 commit 34a6807

15 files changed

+239
-218
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class ProxyTestController
1212
{
1313
public void StartProxy()
1414
{
15-
ProxyServer.BeforeRequest += OnRequest;
16-
ProxyServer.BeforeResponse += OnResponse;
17-
ProxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
15+
// ProxyServer.BeforeRequest += OnRequest;
16+
// ProxyServer.BeforeResponse += OnResponse;
17+
// ProxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
1818

1919
//Exclude Https addresses you don't want to proxy
2020
//Usefull for clients that use certificate pinning
@@ -112,7 +112,7 @@ public async Task OnResponse(object sender, SessionEventArgs e)
112112
{
113113
if (e.WebSession.Response.ResponseStatusCode == "200")
114114
{
115-
if (e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
115+
if (e.WebSession.Response.ContentType!=null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
116116
{
117117
byte[] bodyBytes = await e.GetResponseBody();
118118
await e.SetResponseBody(bodyBytes);

README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Sample request and response event handlers
136136
{
137137
if (e.WebSession.Response.ResponseStatusCode == "200")
138138
{
139-
if (e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
139+
if (e.WebSession.Response.ContentType!=null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html"))
140140
{
141141
byte[] bodyBytes = await e.GetResponseBody();
142142
await e.SetResponseBody(bodyBytes);
@@ -148,11 +148,8 @@ Sample request and response event handlers
148148
}
149149
}
150150

151-
/// <summary>
152-
/// Allows overriding default certificate validation logic
153-
/// </summary>
154-
/// <param name="sender"></param>
155-
/// <param name="e"></param>
151+
152+
/// Allows overriding default certificate validation logic
156153
public async Task OnCertificateValidation(object sender, CertificateValidationEventArgs e)
157154
{
158155
//set IsValid to true/false based on Certificate Errors

Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

+23-16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Titanium.Web.Proxy.Http.Responses;
88
using Titanium.Web.Proxy.Extensions;
99
using System.Threading.Tasks;
10+
using Titanium.Web.Proxy.Network;
1011

1112
namespace Titanium.Web.Proxy.EventArguments
1213
{
@@ -86,14 +87,17 @@ private async Task ReadRequestBody()
8687
await this.Client.ClientStreamReader.CopyBytesToStream(requestBodyStream, WebSession.Request.ContentLength).ConfigureAwait(false);
8788

8889
}
90+
else if (WebSession.Request.HttpVersion.ToLower().Trim().Equals("http/1.0"))
91+
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(requestBodyStream, long.MaxValue).ConfigureAwait(false);
8992
}
9093
WebSession.Request.RequestBody = await GetDecompressedResponseBody(WebSession.Request.ContentEncoding, requestBodyStream.ToArray()).ConfigureAwait(false);
9194
}
92-
}
9395

94-
//Now set the flag to true
95-
//So that next time we can deliver body from cache
96-
WebSession.Request.RequestBodyRead = true;
96+
//Now set the flag to true
97+
//So that next time we can deliver body from cache
98+
WebSession.Request.RequestBodyRead = true;
99+
}
100+
97101
}
98102

99103
/// <summary>
@@ -109,16 +113,18 @@ private async Task ReadResponseBody()
109113
//If chuncked the read chunk by chunk until we hit chunk end symbol
110114
if (WebSession.Response.IsChunked)
111115
{
112-
await WebSession.ProxyClient.ServerStreamReader.CopyBytesToStreamChunked(responseBodyStream).ConfigureAwait(false);
116+
await WebSession.ServerConnection.StreamReader.CopyBytesToStreamChunked(responseBodyStream).ConfigureAwait(false);
113117
}
114118
else
115119
{
116120
if (WebSession.Response.ContentLength > 0)
117121
{
118122
//If not chunked then its easy just read the amount of bytes mentioned in content length header of response
119-
await WebSession.ProxyClient.ServerStreamReader.CopyBytesToStream(responseBodyStream, WebSession.Response.ContentLength).ConfigureAwait(false);
123+
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(responseBodyStream, WebSession.Response.ContentLength).ConfigureAwait(false);
120124

121125
}
126+
else if(WebSession.Response.HttpVersion.ToLower().Trim().Equals("http/1.0"))
127+
await WebSession.ServerConnection.StreamReader.CopyBytesToStream(responseBodyStream, long.MaxValue).ConfigureAwait(false);
122128
}
123129

124130
WebSession.Response.ResponseBody = await GetDecompressedResponseBody(WebSession.Response.ContentEncoding, responseBodyStream.ToArray()).ConfigureAwait(false);
@@ -173,7 +179,11 @@ public async Task SetRequestBody(byte[] body)
173179
}
174180

175181
WebSession.Request.RequestBody = body;
176-
WebSession.Request.RequestBodyRead = true;
182+
183+
if (WebSession.Request.IsChunked == false)
184+
WebSession.Request.ContentLength = body.Length;
185+
else
186+
WebSession.Request.ContentLength = -1;
177187
}
178188

179189
/// <summary>
@@ -191,13 +201,8 @@ public async Task SetRequestBodyString(string body)
191201
await ReadRequestBody().ConfigureAwait(false);
192202
}
193203

194-
WebSession.Request.RequestBody = WebSession.Request.Encoding.GetBytes(body);
195-
196-
//If there is a content length header update it
197-
if (!WebSession.Request.IsChunked)
198-
WebSession.Request.ContentLength = body.Length;
204+
await SetRequestBody(WebSession.Request.Encoding.GetBytes(body));
199205

200-
WebSession.Request.RequestBodyRead = true;
201206
}
202207

203208
/// <summary>
@@ -245,9 +250,10 @@ public async Task SetResponseBody(byte[] body)
245250
WebSession.Response.ResponseBody = body;
246251

247252
//If there is a content length header update it
248-
if (!WebSession.Response.IsChunked)
253+
if (WebSession.Response.IsChunked == false)
249254
WebSession.Response.ContentLength = body.Length;
250-
255+
else
256+
WebSession.Response.ContentLength = -1;
251257
}
252258

253259
/// <summary>
@@ -266,8 +272,9 @@ public async Task SetResponseBodyString(string body)
266272
}
267273

268274
var bodyBytes = WebSession.Response.Encoding.GetBytes(body);
275+
269276
await SetResponseBody(bodyBytes).ConfigureAwait(false);
270-
}
277+
}
271278

272279
private async Task<byte[]> GetDecompressedResponseBody(string encodingType, byte[] responseBodyStream)
273280
{

Titanium.Web.Proxy/Extensions/HttpWebRequestExtensions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public static Encoding GetEncoding(this Request request)
1515
try
1616
{
1717
//return default if not specified
18-
if (request.ContentType == null) return Encoding.GetEncoding("ISO-8859-1");
18+
if (request.ContentType == null)
19+
return Encoding.GetEncoding("ISO-8859-1");
1920

2021
//extract the encoding by finding the charset
2122
var contentTypes = request.ContentType.Split(Constants.SemiColonSplit);
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
using System.Text;
22
using Titanium.Web.Proxy.Http;
3+
using Titanium.Web.Proxy.Shared;
34

45
namespace Titanium.Web.Proxy.Extensions
56
{
67
public static class HttpWebResponseExtensions
78
{
8-
public static Encoding GetResponseEncoding(this Response response)
9+
public static Encoding GetResponseCharacterEncoding(this Response response)
910
{
10-
if (string.IsNullOrEmpty(response.CharacterSet))
11-
return Encoding.GetEncoding("ISO-8859-1");
12-
1311
try
1412
{
15-
return Encoding.GetEncoding(response.CharacterSet.Replace(@"""", string.Empty));
13+
//return default if not specified
14+
if (response.ContentType == null)
15+
return Encoding.GetEncoding("ISO-8859-1");
16+
17+
//extract the encoding by finding the charset
18+
var contentTypes = response.ContentType.Split(Constants.SemiColonSplit);
19+
foreach (var contentType in contentTypes)
20+
{
21+
var encodingSplit = contentType.Split('=');
22+
if (encodingSplit.Length == 2 && encodingSplit[0].ToLower().Trim() == "charset")
23+
{
24+
return Encoding.GetEncoding(encodingSplit[1]);
25+
}
26+
}
1627
}
17-
catch { return Encoding.GetEncoding("ISO-8859-1"); }
28+
catch
29+
{
30+
//parsing errors
31+
// ignored
32+
}
33+
34+
//return default if not specified
35+
return Encoding.GetEncoding("ISO-8859-1");
1836
}
1937
}
2038
}

Titanium.Web.Proxy/Extensions/StreamExtensions.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,29 @@ public static async Task CopyToAsync(this Stream input, string initialData, Stre
2020
await input.CopyToAsync(output);
2121
}
2222

23-
internal static async Task CopyBytesToStream(this CustomBinaryReader clientStreamReader, Stream stream, long totalBytesToRead)
23+
internal static async Task CopyBytesToStream(this CustomBinaryReader streamReader, Stream stream, long totalBytesToRead)
2424
{
2525
var totalbytesRead = 0;
2626

27-
int bytesToRead;
27+
long bytesToRead;
2828
if (totalBytesToRead < Constants.BUFFER_SIZE)
2929
{
30-
bytesToRead = (int)totalBytesToRead;
30+
bytesToRead = totalBytesToRead;
3131
}
3232
else
3333
bytesToRead = Constants.BUFFER_SIZE;
3434

3535

36-
while (totalbytesRead < (int)totalBytesToRead)
36+
while (totalbytesRead < totalBytesToRead)
3737
{
38-
var buffer = await clientStreamReader.ReadBytesAsync(bytesToRead);
38+
var buffer = await streamReader.ReadBytesAsync(bytesToRead);
39+
40+
if (buffer.Length == 0)
41+
break;
42+
3943
totalbytesRead += buffer.Length;
4044

41-
var remainingBytes = (int)totalBytesToRead - totalbytesRead;
45+
var remainingBytes = totalBytesToRead - totalbytesRead;
4246
if (remainingBytes < bytesToRead)
4347
{
4448
bytesToRead = remainingBytes;

0 commit comments

Comments
 (0)