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

Another fix for #638 #641

Merged
merged 1 commit into from
Oct 6, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/Titanium.Web.Proxy/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static async Task<int> startsWith(ICustomStreamReader clientStreamReader
{
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
if (peeked == 0)
return - 1;
return -1;

peeked += i;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,22 @@ public override int ReadByte()
/// <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];
}

Expand All @@ -279,24 +278,27 @@ public async Task<int> PeekByteAsync(int index, CancellationToken cancellationTo
/// <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;
}

Expand Down Expand Up @@ -516,19 +518,19 @@ public async Task<bool> 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
// move the remaining data to the beginning of the buffer
Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength);
}

int bytesToRead = streamBuffer.Length - bufferLength;
if (bytesToRead == 0)
{
return false;
}

bufferPos = 0;

bool result = false;
Expand Down