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

beta #686

Merged
merged 4 commits into from
Nov 30, 2019
Merged

beta #686

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Task onBeforeTunnelConnectResponse(object sender, TunnelConnectSessionEv
return Task.FromResult(false);
}

// intecept & cancel redirect or update requests
// intercept & cancel redirect or update requests
private async Task onRequest(object sender, SessionEventArgs e)
{
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
Expand Down
95 changes: 93 additions & 2 deletions src/Titanium.Web.Proxy/EventArguments/LimitedStream.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.StreamExtended.BufferPool;
using Titanium.Web.Proxy.StreamExtended.Network;

Expand Down Expand Up @@ -51,11 +51,22 @@ private void getNextChunk()
{
// read the chunk trail of the previous chunk
string? s = baseReader.ReadLineAsync().Result;
if (s == null)
{
bytesRemaining = -1;
return;
}
}

readChunkTrail = true;

string? chunkHead = baseReader.ReadLineAsync().Result!;
string? chunkHead = baseReader.ReadLineAsync().Result;
if (chunkHead == null)
{
bytesRemaining = -1;
return;
}

int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
if (idx >= 0)
{
Expand All @@ -80,6 +91,50 @@ private void getNextChunk()
}
}

private async Task getNextChunkAsync()
{
if (readChunkTrail)
{
// read the chunk trail of the previous chunk
string? s = await baseReader.ReadLineAsync();
if (s == null)
{
bytesRemaining = -1;
return;
}
}

readChunkTrail = true;

string? chunkHead = await baseReader.ReadLineAsync();
if (chunkHead == null)
{
bytesRemaining = -1;
return;
}

int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
if (idx >= 0)
{
chunkHead = chunkHead.Substring(0, idx);
}

if (!int.TryParse(chunkHead, NumberStyles.HexNumber, null, out int chunkSize))
{
throw new ProxyHttpException($"Invalid chunk length: '{chunkHead}'", null, null);
}

bytesRemaining = chunkSize;

if (chunkSize == 0)
{
bytesRemaining = -1;

// chunk trail
await baseReader.ReadLineAsync();
}
}

public override void Flush()
{
throw new NotSupportedException();
Expand Down Expand Up @@ -131,6 +186,42 @@ public override int Read(byte[] buffer, int offset, int count)
return res;
}

public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (bytesRemaining == -1)
{
return 0;
}

if (bytesRemaining == 0)
{
if (isChunked)
{
await getNextChunkAsync();
}
else
{
bytesRemaining = -1;
}
}

if (bytesRemaining == -1)
{
return 0;
}

int toRead = (int)Math.Min(count, bytesRemaining);
int res = await baseReader.ReadAsync(buffer, offset, toRead, cancellationToken);
bytesRemaining -= res;

if (res == 0)
{
bytesRemaining = -1;
}

return res;
}

public async Task Finish()
{
if (bytesRemaining != -1)
Expand Down
6 changes: 3 additions & 3 deletions src/Titanium.Web.Proxy/Helpers/HttpStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ public async ValueTask<bool> FillBufferAsync(CancellationToken cancellationToken

if (bufferDataLength == buffer.Length)
{
ResizeBuffer(ref buffer, bufferDataLength * 2);
resizeBuffer(ref buffer, bufferDataLength * 2);
}
}
}
Expand Down Expand Up @@ -683,7 +683,7 @@ public async Task ReadAndIgnoreAllLinesAsync(CancellationToken cancellationToken
/// </summary>
/// <param name="buffer"></param>
/// <param name="size"></param>
private static void ResizeBuffer(ref byte[] buffer, long size)
private static void resizeBuffer(ref byte[] buffer, long size)
{
var newBuffer = new byte[size];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
Expand Down Expand Up @@ -889,7 +889,7 @@ public async Task CopyBodyAsync(RequestResponseBase requestResponse, bool useOri
string? contentEncoding = useOriginalHeaderValues ? requestResponse.OriginalContentEncoding : requestResponse.ContentEncoding;

Stream s = limitedStream = new LimitedStream(this, bufferPool, isChunked, contentLength);

if (transformation == TransformationMode.Uncompress && contentEncoding != null)
{
s = decompressStream = DecompressionFactory.Create(CompressionUtil.CompressionNameToEnum(contentEncoding), s);
Expand Down
12 changes: 0 additions & 12 deletions src/Titanium.Web.Proxy/Http/HeaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,5 @@ internal static async ValueTask ReadHeaders(ILineStream reader, HeaderCollection
headerCollection.AddHeader(headerName, headerValue);
}
}

/// <summary>
/// Increase size of buffer and copy existing content to new buffer
/// </summary>
/// <param name="buffer"></param>
/// <param name="size"></param>
private static void resizeBuffer(ref byte[] buffer, long size)
{
var newBuffer = new byte[size];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
buffer = newBuffer;
}
}
}
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/StreamExtended/SslTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static async Task<bool> IsServerHello(IPeekStream stream, IBufferPool buf
int recordLength = ((recordType & 0x7f) << 8) + peekStream.ReadByte();
if (recordLength < 38)
{
// Message body too short.
// Message body too short.
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
<PackageReference Include="System.Buffers" Version="4.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<ItemGroup>
<PackageReference Include="BrotliSharpLib" Version="0.3.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5.2" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.1'">
Expand Down
8 changes: 4 additions & 4 deletions src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<dependencies>
<group targetFramework="net45">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
<dependency id="System.Threading.Tasks.Extensions" version="4.5.3" />
</group>
<group targetFramework="net461">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
Expand All @@ -32,7 +32,7 @@
</group>
<group targetFramework="netstandard2.0">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Buffers" version="4.5.0" />
<dependency id="System.Memory" version="4.5.3" />
Expand All @@ -41,7 +41,7 @@
</group>
<group targetFramework="netstandard2.1">
<dependency id="BrotliSharpLib" version="0.3.3" />
<dependency id="Portable.BouncyCastle" version="1.8.5.2" />
<dependency id="Portable.BouncyCastle" version="1.8.5" />
<dependency id="Microsoft.Win32.Registry" version="4.6.0" />
<dependency id="System.Security.Principal.Windows" version="4.6.0" />
</group>
Expand Down