Commit ded334fc authored by honfika's avatar honfika

small fixes

parent abf159fc
...@@ -151,7 +151,7 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -151,7 +151,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
return Task.FromResult(false); return Task.FromResult(false);
} }
// intecept & cancel redirect or update requests // intercept & cancel redirect or update requests
private async Task onRequest(object sender, SessionEventArgs e) private async Task onRequest(object sender, SessionEventArgs e)
{ {
await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount); await writeToConsole("Active Client Connections:" + ((ProxyServer)sender).ClientConnectionCount);
......
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.Exceptions; using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.StreamExtended.BufferPool; using Titanium.Web.Proxy.StreamExtended.BufferPool;
using Titanium.Web.Proxy.StreamExtended.Network; using Titanium.Web.Proxy.StreamExtended.Network;
...@@ -51,11 +51,22 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -51,11 +51,22 @@ namespace Titanium.Web.Proxy.EventArguments
{ {
// read the chunk trail of the previous chunk // read the chunk trail of the previous chunk
string? s = baseReader.ReadLineAsync().Result; string? s = baseReader.ReadLineAsync().Result;
if (s == null)
{
bytesRemaining = -1;
return;
}
} }
readChunkTrail = true; readChunkTrail = true;
string? chunkHead = baseReader.ReadLineAsync().Result!; string? chunkHead = baseReader.ReadLineAsync().Result;
if (chunkHead == null)
{
bytesRemaining = -1;
return;
}
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal); int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
if (idx >= 0) if (idx >= 0)
{ {
...@@ -80,6 +91,50 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -80,6 +91,50 @@ namespace Titanium.Web.Proxy.EventArguments
} }
} }
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() public override void Flush()
{ {
throw new NotSupportedException(); throw new NotSupportedException();
...@@ -131,6 +186,42 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -131,6 +186,42 @@ namespace Titanium.Web.Proxy.EventArguments
return res; 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() public async Task Finish()
{ {
if (bytesRemaining != -1) if (bytesRemaining != -1)
......
...@@ -650,7 +650,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -650,7 +650,7 @@ namespace Titanium.Web.Proxy.Helpers
if (bufferDataLength == buffer.Length) if (bufferDataLength == buffer.Length)
{ {
ResizeBuffer(ref buffer, bufferDataLength * 2); resizeBuffer(ref buffer, bufferDataLength * 2);
} }
} }
} }
...@@ -683,7 +683,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -683,7 +683,7 @@ namespace Titanium.Web.Proxy.Helpers
/// </summary> /// </summary>
/// <param name="buffer"></param> /// <param name="buffer"></param>
/// <param name="size"></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]; var newBuffer = new byte[size];
Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length); Buffer.BlockCopy(buffer, 0, newBuffer, 0, buffer.Length);
...@@ -889,7 +889,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -889,7 +889,7 @@ namespace Titanium.Web.Proxy.Helpers
string? contentEncoding = useOriginalHeaderValues ? requestResponse.OriginalContentEncoding : requestResponse.ContentEncoding; string? contentEncoding = useOriginalHeaderValues ? requestResponse.OriginalContentEncoding : requestResponse.ContentEncoding;
Stream s = limitedStream = new LimitedStream(this, bufferPool, isChunked, contentLength); Stream s = limitedStream = new LimitedStream(this, bufferPool, isChunked, contentLength);
if (transformation == TransformationMode.Uncompress && contentEncoding != null) if (transformation == TransformationMode.Uncompress && contentEncoding != null)
{ {
s = decompressStream = DecompressionFactory.Create(CompressionUtil.CompressionNameToEnum(contentEncoding), s); s = decompressStream = DecompressionFactory.Create(CompressionUtil.CompressionNameToEnum(contentEncoding), s);
......
...@@ -24,17 +24,5 @@ namespace Titanium.Web.Proxy.Http ...@@ -24,17 +24,5 @@ namespace Titanium.Web.Proxy.Http
headerCollection.AddHeader(headerName, headerValue); 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;
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment