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

Commit f3ab526

Browse files
authored
Merge pull request #686 from justcoding121/master
beta
2 parents 51e83ba + 4bdf8f3 commit f3ab526

File tree

9 files changed

+105
-26
lines changed

9 files changed

+105
-26
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private Task onBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEv
151151
return Task.FromResult(false);
152152
}
153153

154-
// intecept & cancel redirect or update requests
154+
// intercept & cancel redirect or update requests
155155
private async Task onRequest(object sender, SessionEventArgs e)
156156
{
157157
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);

src/Titanium.Web.Proxy/EventArguments/LimitedStream.cs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Globalization;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Titanium.Web.Proxy.Exceptions;
6-
using Titanium.Web.Proxy.Helpers;
77
using Titanium.Web.Proxy.StreamExtended.BufferPool;
88
using Titanium.Web.Proxy.StreamExtended.Network;
99

@@ -51,11 +51,22 @@ private void getNextChunk()
5151
{
5252
// read the chunk trail of the previous chunk
5353
string? s = baseReader.ReadLineAsync().Result;
54+
if (s == null)
55+
{
56+
bytesRemaining = -1;
57+
return;
58+
}
5459
}
5560

5661
readChunkTrail = true;
5762

58-
string? chunkHead = baseReader.ReadLineAsync().Result!;
63+
string? chunkHead = baseReader.ReadLineAsync().Result;
64+
if (chunkHead == null)
65+
{
66+
bytesRemaining = -1;
67+
return;
68+
}
69+
5970
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
6071
if (idx >= 0)
6172
{
@@ -80,6 +91,50 @@ private void getNextChunk()
8091
}
8192
}
8293

94+
private async Task getNextChunkAsync()
95+
{
96+
if (readChunkTrail)
97+
{
98+
// read the chunk trail of the previous chunk
99+
string? s = await baseReader.ReadLineAsync();
100+
if (s == null)
101+
{
102+
bytesRemaining = -1;
103+
return;
104+
}
105+
}
106+
107+
readChunkTrail = true;
108+
109+
string? chunkHead = await baseReader.ReadLineAsync();
110+
if (chunkHead == null)
111+
{
112+
bytesRemaining = -1;
113+
return;
114+
}
115+
116+
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
117+
if (idx >= 0)
118+
{
119+
chunkHead = chunkHead.Substring(0, idx);
120+
}
121+
122+
if (!int.TryParse(chunkHead, NumberStyles.HexNumber, null, out int chunkSize))
123+
{
124+
throw new ProxyHttpException($"Invalid chunk length: '{chunkHead}'", null, null);
125+
}
126+
127+
bytesRemaining = chunkSize;
128+
129+
if (chunkSize == 0)
130+
{
131+
bytesRemaining = -1;
132+
133+
// chunk trail
134+
await baseReader.ReadLineAsync();
135+
}
136+
}
137+
83138
public override void Flush()
84139
{
85140
throw new NotSupportedException();
@@ -131,6 +186,42 @@ public override int Read(byte[] buffer, int offset, int count)
131186
return res;
132187
}
133188

189+
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
190+
{
191+
if (bytesRemaining == -1)
192+
{
193+
return 0;
194+
}
195+
196+
if (bytesRemaining == 0)
197+
{
198+
if (isChunked)
199+
{
200+
await getNextChunkAsync();
201+
}
202+
else
203+
{
204+
bytesRemaining = -1;
205+
}
206+
}
207+
208+
if (bytesRemaining == -1)
209+
{
210+
return 0;
211+
}
212+
213+
int toRead = (int)Math.Min(count, bytesRemaining);
214+
int res = await baseReader.ReadAsync(buffer, offset, toRead, cancellationToken);
215+
bytesRemaining -= res;
216+
217+
if (res == 0)
218+
{
219+
bytesRemaining = -1;
220+
}
221+
222+
return res;
223+
}
224+
134225
public async Task Finish()
135226
{
136227
if (bytesRemaining != -1)

src/Titanium.Web.Proxy/Helpers/HttpStream.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ public async ValueTask<bool> FillBufferAsync(CancellationToken cancellationToken
650650

651651
if (bufferDataLength == buffer.Length)
652652
{
653-
ResizeBuffer(ref buffer, bufferDataLength * 2);
653+
resizeBuffer(ref buffer, bufferDataLength * 2);
654654
}
655655
}
656656
}
@@ -683,7 +683,7 @@ public async Task ReadAndIgnoreAllLinesAsync(CancellationToken cancellationToken
683683
/// </summary>
684684
/// <param name="buffer"></param>
685685
/// <param name="size"></param>
686-
private static void ResizeBuffer(ref byte[] buffer, long size)
686+
private static void resizeBuffer(ref byte[] buffer, long size)
687687
{
688688
var newBuffer = new byte[size];
689689
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
@@ -889,7 +889,7 @@ public async Task CopyBodyAsync(RequestResponseBase requestResponse, bool useOri
889889
string? contentEncoding = useOriginalHeaderValues ? requestResponse.OriginalContentEncoding : requestResponse.ContentEncoding;
890890

891891
Stream s = limitedStream = new LimitedStream(this, bufferPool, isChunked, contentLength);
892-
892+
893893
if (transformation == TransformationMode.Uncompress && contentEncoding != null)
894894
{
895895
s = decompressStream = DecompressionFactory.Create(CompressionUtil.CompressionNameToEnum(contentEncoding), s);

src/Titanium.Web.Proxy/Http/HeaderParser.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,5 @@ internal static async ValueTask ReadHeaders(ILineStream reader, HeaderCollection
2424
headerCollection.AddHeader(headerName, headerValue);
2525
}
2626
}
27-
28-
/// <summary>
29-
/// Increase size of buffer and copy existing content to new buffer
30-
/// </summary>
31-
/// <param name="buffer"></param>
32-
/// <param name="size"></param>
33-
private static void resizeBuffer(ref byte[] buffer, long size)
34-
{
35-
var newBuffer = new byte[size];
36-
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
37-
buffer = newBuffer;
38-
}
3927
}
4028
}

src/Titanium.Web.Proxy/StreamExtended/SslTools.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public static async Task<bool> IsServerHello(IPeekStream stream, IBufferPool buf
216216
int recordLength = ((recordType & 0x7f) << 8) + peekStream.ReadByte();
217217
if (recordLength < 38)
218218
{
219-
// Message body too short.
219+
// Message body too short.
220220
return null;
221221
}
222222

src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
16-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
16+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
1717
<PackageReference Include="System.Buffers" Version="4.5.0" />
1818
</ItemGroup>
1919

src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
16-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
16+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
1717
<PackageReference Include="System.Buffers" Version="4.5.0" />
1818
</ItemGroup>
1919

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<ItemGroup>
1616
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
17-
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
17+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
1818
</ItemGroup>
1919

2020
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">

src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
<dependencies>
1717
<group targetFramework="net45">
1818
<dependency id="BrotliSharpLib" version="0.3.3" />
19-
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
19+
<dependency id="Portable.BouncyCastle" version="1.8.5" />
2020
<dependency id="System.Buffers" version="4.5.0" />
2121
<dependency id="System.Memory" version="4.5.3" />
2222
<dependency id="System.Threading.Tasks.Extensions" version="4.5.3" />
2323
</group>
2424
<group targetFramework="net461">
2525
<dependency id="BrotliSharpLib" version="0.3.3" />
26-
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
26+
<dependency id="Portable.BouncyCastle" version="1.8.5" />
2727
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
2828
<dependency id="System.Buffers" version="4.5.0" />
2929
<dependency id="System.Memory" version="4.5.3" />
@@ -32,7 +32,7 @@
3232
</group>
3333
<group targetFramework="netstandard2.0">
3434
<dependency id="BrotliSharpLib" version="0.3.3" />
35-
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
35+
<dependency id="Portable.BouncyCastle" version="1.8.5" />
3636
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
3737
<dependency id="System.Buffers" version="4.5.0" />
3838
<dependency id="System.Memory" version="4.5.3" />
@@ -41,7 +41,7 @@
4141
</group>
4242
<group targetFramework="netstandard2.1">
4343
<dependency id="BrotliSharpLib" version="0.3.3" />
44-
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
44+
<dependency id="Portable.BouncyCastle" version="1.8.5" />
4545
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
4646
<dependency id="System.Security.Principal.Windows" version="4.6.0" />
4747
</group>

0 commit comments

Comments
 (0)