From 6d5baf39af69ae460f260201984b2ce1e1fc8bca Mon Sep 17 00:00:00 2001 From: Honfika Date: Sun, 6 Oct 2019 09:29:32 +0200 Subject: [PATCH] Another fix for #638 --- src/Titanium.Web.Proxy/Helpers/HttpHelper.cs | 2 +- .../Network/CustomBufferedStream.cs | 48 ++++++++++--------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/Titanium.Web.Proxy/Helpers/HttpHelper.cs b/src/Titanium.Web.Proxy/Helpers/HttpHelper.cs index a649f91bd..71b1d74aa 100644 --- a/src/Titanium.Web.Proxy/Helpers/HttpHelper.cs +++ b/src/Titanium.Web.Proxy/Helpers/HttpHelper.cs @@ -168,7 +168,7 @@ private static async Task startsWith(ICustomStreamReader clientStreamReader { int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken); if (peeked == 0) - return - 1; + return -1; peeked += i; diff --git a/src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs b/src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs index fedbd25fd..9f004e458 100644 --- a/src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs +++ b/src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs @@ -248,23 +248,22 @@ public override int ReadByte() /// public async Task PeekByteAsync(int index, CancellationToken cancellationToken = default) { - if (Available <= index) - { - await FillBufferAsync(cancellationToken); - } - // When index is greater than the buffer size if (streamBuffer.Length <= index) { throw new Exception("Requested Peek index exceeds the buffer size. Consider increasing the buffer size."); } - // When index is greater than the buffer size - if (Available <= index) + while (Available <= index) { - return -1; + // When index is greater than the buffer size + bool fillResult = await FillBufferAsync(cancellationToken); + if (!fillResult) + { + return -1; + } } - + return streamBuffer[bufferPos + index]; } @@ -279,24 +278,27 @@ public async Task PeekByteAsync(int index, CancellationToken cancellationTo /// public async Task PeekBytesAsync(byte[] buffer, int offset, int index, int count, CancellationToken cancellationToken = default) { - if (Available <= index) + // When index is greater than the buffer size + if (streamBuffer.Length <= index + count) { - await FillBufferAsync(cancellationToken); + throw new Exception("Requested Peek index and size exceeds the buffer size. Consider increasing the buffer size."); } - // When index is greater than the buffer size - if (streamBuffer.Length <= (index + count)) + while (Available <= index) { - throw new Exception("Requested Peek index and size exceeds the buffer size. Consider increasing the buffer size."); + bool fillResult = await FillBufferAsync(cancellationToken); + if (!fillResult) + { + return 0; + } } - if (Available <= (index + count)) + if (Available - index < count) { - return -1; + count = Available - index; } Buffer.BlockCopy(streamBuffer, index, buffer, offset, count); - return count; } @@ -516,6 +518,12 @@ public async Task FillBufferAsync(CancellationToken cancellationToken = de throw new Exception("Stream is already closed"); } + int bytesToRead = streamBuffer.Length - bufferLength; + if (bytesToRead == 0) + { + return false; + } + if (bufferLength > 0) { // normally we fill the buffer only when it is empty, but sometimes we need more data @@ -523,12 +531,6 @@ public async Task FillBufferAsync(CancellationToken cancellationToken = de Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength); } - int bytesToRead = streamBuffer.Length - bufferLength; - if (bytesToRead == 0) - { - return false; - } - bufferPos = 0; bool result = false;