Unverified Commit e951ce9c authored by honfika's avatar honfika Committed by GitHub

Merge pull request #641 from justcoding121/master

Another fix for  #638
parents ab36896e 6d5baf39
......@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy.Helpers
{
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
if (peeked == 0)
return - 1;
return -1;
peeked += i;
......
......@@ -248,23 +248,22 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns></returns>
public async Task<int> 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 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns></returns>
public async Task<int> 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 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
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 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength);
}
int bytesToRead = streamBuffer.Length - bufferLength;
if (bytesToRead == 0)
{
return false;
}
bufferPos = 0;
bool result = false;
......
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