Commit 6d5baf39 authored by Honfika's avatar Honfika

Another fix for #638

parent b88a11b3
...@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -168,7 +168,7 @@ namespace Titanium.Web.Proxy.Helpers
{ {
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken); int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
if (peeked == 0) if (peeked == 0)
return - 1; return -1;
peeked += i; peeked += i;
......
...@@ -248,23 +248,22 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -248,23 +248,22 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns></returns> /// <returns></returns>
public async Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default) public async Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default)
{ {
if (Available <= index)
{
await FillBufferAsync(cancellationToken);
}
// When index is greater than the buffer size // When index is greater than the buffer size
if (streamBuffer.Length <= index) if (streamBuffer.Length <= index)
{ {
throw new Exception("Requested Peek index exceeds the buffer size. Consider increasing the buffer size."); throw new Exception("Requested Peek index exceeds the buffer size. Consider increasing the buffer size.");
} }
// When index is greater than the buffer size while (Available <= index)
if (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]; return streamBuffer[bufferPos + index];
} }
...@@ -279,24 +278,27 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -279,24 +278,27 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns></returns> /// <returns></returns>
public async Task<int> PeekBytesAsync(byte[] buffer, int offset, int index, int count, CancellationToken cancellationToken = default) 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 while (Available <= index)
if (streamBuffer.Length <= (index + count))
{ {
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); Buffer.BlockCopy(streamBuffer, index, buffer, offset, count);
return count; return count;
} }
...@@ -516,6 +518,12 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -516,6 +518,12 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
throw new Exception("Stream is already closed"); throw new Exception("Stream is already closed");
} }
int bytesToRead = streamBuffer.Length - bufferLength;
if (bytesToRead == 0)
{
return false;
}
if (bufferLength > 0) if (bufferLength > 0)
{ {
// normally we fill the buffer only when it is empty, but sometimes we need more data // 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 ...@@ -523,12 +531,6 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength); Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength);
} }
int bytesToRead = streamBuffer.Length - bufferLength;
if (bytesToRead == 0)
{
return false;
}
bufferPos = 0; bufferPos = 0;
bool result = false; 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