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

Commit 54cfea8

Browse files
Merge pull request #221 from justcoding121/develop
Beta Merge
2 parents e82ad51 + 84660dd commit 54cfea8

File tree

17 files changed

+149
-115
lines changed

17 files changed

+149
-115
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net;
4-
using System.Security.Cryptography.X509Certificates;
54
using System.Threading.Tasks;
65
using Titanium.Web.Proxy.EventArguments;
76
using Titanium.Web.Proxy.Models;
@@ -18,6 +17,7 @@ public class ProxyTestController
1817
public ProxyTestController()
1918
{
2019
proxyServer = new ProxyServer();
20+
proxyServer.ExceptionFunc = exception => Console.WriteLine(exception.Message);
2121
proxyServer.TrustRootCertificate = true;
2222

2323
//optionally set the Certificate Engine
@@ -99,7 +99,7 @@ public async Task OnRequest(object sender, SessionEventArgs e)
9999
{
100100
Console.WriteLine(e.WebSession.Request.Url);
101101

102-
////read request headers
102+
//read request headers
103103
var requestHeaders = e.WebSession.Request.RequestHeaders;
104104

105105
var method = e.WebSession.Request.Method.ToUpper();
@@ -128,6 +128,7 @@ await e.Ok("<!DOCTYPE html>" +
128128
"</body>" +
129129
"</html>");
130130
}
131+
131132
//Redirect example
132133
if (e.WebSession.Request.RequestUri.AbsoluteUri.Contains("wikipedia.org"))
133134
{
@@ -143,6 +144,7 @@ public async Task OnResponse(object sender, SessionEventArgs e)
143144
//access request body by looking up the shared dictionary using requestId
144145
var requestBody = requestBodyHistory[e.Id];
145146
}
147+
146148
//read response headers
147149
var responseHeaders = e.WebSession.Response.ResponseHeaders;
148150

Tests/Titanium.Web.Proxy.IntegrationTests/SslTests.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using System.Net;
4-
using System.Collections.Generic;
54
using System.Threading.Tasks;
65
using Titanium.Web.Proxy.EventArguments;
76
using Titanium.Web.Proxy.Models;
@@ -28,14 +27,13 @@ public void TestSsl()
2827
{
2928
var response = client.GetAsync(new Uri(testUrl)).Result;
3029
}
31-
3230
}
3331

3432
private HttpClient CreateHttpClient(string url, int localProxyPort)
3533
{
3634
var handler = new HttpClientHandler
3735
{
38-
Proxy = new WebProxy(string.Format("http://localhost:{0}", localProxyPort), false),
36+
Proxy = new WebProxy($"http://localhost:{localProxyPort}", false),
3937
UseProxy = true,
4038
};
4139

@@ -47,7 +45,7 @@ private HttpClient CreateHttpClient(string url, int localProxyPort)
4745

4846
public class ProxyTestController
4947
{
50-
private ProxyServer proxyServer;
48+
private readonly ProxyServer proxyServer;
5149

5250
public ProxyTestController()
5351
{
@@ -63,7 +61,6 @@ public void StartProxy(int proxyPort)
6361
proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;
6462

6563
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, proxyPort, true);
66-
6764

6865
//An explicit endpoint is where the client knows about the existance of a proxy
6966
//So client sends request in a proxy friendly manner
@@ -73,7 +70,6 @@ public void StartProxy(int proxyPort)
7370
foreach (var endPoint in proxyServer.ProxyEndPoints)
7471
Console.WriteLine("Listening on '{0}' endpoint at Ip {1} and port: {2} ",
7572
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
76-
7773
}
7874

7975
public void Stop()

Tests/Titanium.Web.Proxy.UnitTests/CertificateManagerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public async Task Simple_Create_Certificate_Stress_Test()
2020
{
2121
var tasks = new List<Task>();
2222

23-
var mgr = new CertificateManager(CertificateEngine.DefaultWindows, "Titanium", "Titanium Root Certificate Authority",
24-
new Lazy<Action<Exception>>(() => (e => { })).Value);
23+
var mgr = new CertificateManager(new Lazy<Action<Exception>>(() => (e => { })).Value);
2524

2625
mgr.ClearIdleCertificates(1);
2726

Titanium.Web.Proxy.sln.DotSettings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=BC/@EntryIndexedValue">BC</s:String>
23
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
34
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
5+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
46
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
57
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=dda2ffa1_002D435c_002D4111_002D88eb_002D1a7c93c382f0/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static, Instance" AccessRightKinds="Private" Description="Property (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="PROPERTY" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String></wpf:ResourceDictionary>

Titanium.Web.Proxy/Decompression/DeflateDecompression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<byte[]> Decompress(byte[] compressedArray, int bufferSize)
2121
int read;
2222
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length)) > 0)
2323
{
24-
await output.WriteAsync(buffer, 0, read);
24+
output.Write(buffer, 0, read);
2525
}
2626

2727
return output.ToArray();

Titanium.Web.Proxy/Decompression/GZipDecompression.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public async Task<byte[]> Decompress(byte[] compressedArray, int bufferSize)
1919
int read;
2020
while ((read = await decompressor.ReadAsync(buffer, 0, buffer.Length)) > 0)
2121
{
22-
await output.WriteAsync(buffer, 0, read);
22+
output.Write(buffer, 0, read);
2323
}
24+
2425
return output.ToArray();
2526
}
2627
}

Titanium.Web.Proxy/Extensions/StreamExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal static async Task CopyBytesToStream(this CustomBinaryReader streamReade
4646

4747
while (totalbytesRead < totalBytesToRead)
4848
{
49-
var buffer = await streamReader.ReadBytesAsync(bufferSize, bytesToRead);
49+
var buffer = await streamReader.ReadBytesAsync(bytesToRead);
5050

5151
if (buffer.Length == 0)
5252
{
@@ -81,7 +81,7 @@ internal static async Task CopyBytesToStreamChunked(this CustomBinaryReader clie
8181

8282
if (chunkSize != 0)
8383
{
84-
var buffer = await clientStreamReader.ReadBytesAsync(bufferSize, chunkSize);
84+
var buffer = await clientStreamReader.ReadBytesAsync(chunkSize);
8585
await stream.WriteAsync(buffer, 0, buffer.Length);
8686
//chunk trail
8787
await clientStreamReader.ReadLineAsync();
@@ -179,7 +179,7 @@ internal static async Task WriteResponseBodyChunked(this CustomBinaryReader inSt
179179

180180
if (chunkSize != 0)
181181
{
182-
var buffer = await inStreamReader.ReadBytesAsync(bufferSize, chunkSize);
182+
var buffer = await inStreamReader.ReadBytesAsync(chunkSize);
183183

184184
var chunkHeadBytes = Encoding.ASCII.GetBytes(chunkSize.ToString("x2"));
185185

Titanium.Web.Proxy/Extensions/StringExtensions.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
1+
using System.Globalization;
72

83
namespace Titanium.Web.Proxy.Extensions
94
{

Titanium.Web.Proxy/Helpers/CustomBinaryReader.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ internal class CustomBinaryReader : IDisposable
1919
private readonly Encoding encoding;
2020

2121
[ThreadStatic]
22-
private static byte[] staticBuffer;
22+
private static byte[] staticBufferField;
2323

24-
private byte[] buffer
24+
private byte[] staticBuffer
2525
{
2626
get
2727
{
28-
if (staticBuffer == null || staticBuffer.Length != bufferSize)
28+
if (staticBufferField == null || staticBufferField.Length != bufferSize)
2929
{
30-
staticBuffer = new byte[bufferSize];
30+
staticBufferField = new byte[bufferSize];
3131
}
3232

33-
return staticBuffer;
33+
return staticBufferField;
3434
}
3535
}
3636

@@ -56,7 +56,7 @@ internal async Task<string> ReadLineAsync()
5656
int bufferDataLength = 0;
5757

5858
// try to use the thread static buffer, usually it is enough
59-
var buffer = this.buffer;
59+
var buffer = staticBuffer;
6060

6161
while (stream.DataAvailable || await stream.FillBufferAsync())
6262
{
@@ -117,22 +117,16 @@ internal async Task ReadAndIgnoreAllLinesAsync()
117117
/// <summary>
118118
/// Read the specified number of raw bytes from the base stream
119119
/// </summary>
120-
/// <param name="bufferSize"></param>
121120
/// <param name="totalBytesToRead"></param>
122121
/// <returns></returns>
123-
internal async Task<byte[]> ReadBytesAsync(int bufferSize, long totalBytesToRead)
122+
internal async Task<byte[]> ReadBytesAsync(long totalBytesToRead)
124123
{
125124
int bytesToRead = bufferSize;
126125

127126
if (totalBytesToRead < bufferSize)
128127
bytesToRead = (int)totalBytesToRead;
129128

130-
var buffer = this.buffer;
131-
if (bytesToRead > buffer.Length)
132-
{
133-
buffer = new byte[bytesToRead];
134-
}
135-
129+
var buffer = staticBuffer;
136130
int bytesRead;
137131
var totalBytesRead = 0;
138132

Titanium.Web.Proxy/Helpers/CustomBufferedStream.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class CustomBufferedStream : Stream
1616
{
1717
private readonly Stream baseStream;
1818

19-
private readonly byte[] buffer;
19+
private readonly byte[] streamBuffer;
2020

2121
private int bufferLength;
2222

@@ -30,7 +30,7 @@ internal class CustomBufferedStream : Stream
3030
public CustomBufferedStream(Stream baseStream, int bufferSize)
3131
{
3232
this.baseStream = baseStream;
33-
buffer = new byte[bufferSize];
33+
streamBuffer = new byte[bufferSize];
3434
}
3535

3636
/// <summary>
@@ -84,7 +84,7 @@ public override int Read(byte[] buffer, int offset, int count)
8484
int available = Math.Min(bufferLength, count);
8585
if (available > 0)
8686
{
87-
Buffer.BlockCopy(this.buffer, bufferPos, buffer, offset, available);
87+
Buffer.BlockCopy(streamBuffer, bufferPos, buffer, offset, available);
8888
bufferPos += available;
8989
bufferLength -= available;
9090
}
@@ -120,7 +120,7 @@ public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, Asy
120120
if (bufferLength > 0)
121121
{
122122
int available = Math.Min(bufferLength, count);
123-
Buffer.BlockCopy(this.buffer, bufferPos, buffer, offset, available);
123+
Buffer.BlockCopy(streamBuffer, bufferPos, buffer, offset, available);
124124
bufferPos += available;
125125
bufferLength -= available;
126126
return new ReadAsyncResult(available);
@@ -167,7 +167,7 @@ public override async Task CopyToAsync(Stream destination, int bufferSize, Cance
167167
{
168168
if (bufferLength > 0)
169169
{
170-
await destination.WriteAsync(buffer, bufferPos, bufferLength, cancellationToken);
170+
await destination.WriteAsync(streamBuffer, bufferPos, bufferLength, cancellationToken);
171171
}
172172

173173
await baseStream.CopyToAsync(destination, bufferSize, cancellationToken);
@@ -256,7 +256,7 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
256256
int available = Math.Min(bufferLength, count);
257257
if (available > 0)
258258
{
259-
Buffer.BlockCopy(this.buffer, bufferPos, buffer, offset, available);
259+
Buffer.BlockCopy(streamBuffer, bufferPos, buffer, offset, available);
260260
bufferPos += available;
261261
bufferLength -= available;
262262
}
@@ -283,7 +283,7 @@ public override int ReadByte()
283283
}
284284

285285
bufferLength--;
286-
return buffer[bufferPos++];
286+
return streamBuffer[bufferPos++];
287287
}
288288

289289
public byte ReadByteFromBuffer()
@@ -294,7 +294,7 @@ public byte ReadByteFromBuffer()
294294
}
295295

296296
bufferLength--;
297-
return buffer[bufferPos++];
297+
return streamBuffer[bufferPos++];
298298
}
299299

300300
/// <summary>
@@ -390,7 +390,7 @@ public override int WriteTimeout
390390
/// </summary>
391391
public bool FillBuffer()
392392
{
393-
bufferLength = baseStream.Read(buffer, 0, buffer.Length);
393+
bufferLength = baseStream.Read(streamBuffer, 0, streamBuffer.Length);
394394
bufferPos = 0;
395395
return bufferLength > 0;
396396
}
@@ -411,7 +411,7 @@ public Task<bool> FillBufferAsync()
411411
/// <returns></returns>
412412
public async Task<bool> FillBufferAsync(CancellationToken cancellationToken)
413413
{
414-
bufferLength = await baseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken);
414+
bufferLength = await baseStream.ReadAsync(streamBuffer, 0, streamBuffer.Length, cancellationToken);
415415
bufferPos = 0;
416416
return bufferLength > 0;
417417
}

0 commit comments

Comments
 (0)