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

Commit e951ce9

Browse files
authored
Merge pull request #641 from justcoding121/master
Another fix for #638
2 parents ab36896 + 6d5baf3 commit e951ce9

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/Titanium.Web.Proxy/Helpers/HttpHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private static async Task<int> startsWith(ICustomStreamReader clientStreamReader
168168
{
169169
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
170170
if (peeked == 0)
171-
return - 1;
171+
return -1;
172172

173173
peeked += i;
174174

src/Titanium.Web.Proxy/StreamExtended/Network/CustomBufferedStream.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -248,23 +248,22 @@ public override int ReadByte()
248248
/// <returns></returns>
249249
public async Task<int> PeekByteAsync(int index, CancellationToken cancellationToken = default)
250250
{
251-
if (Available <= index)
252-
{
253-
await FillBufferAsync(cancellationToken);
254-
}
255-
256251
// When index is greater than the buffer size
257252
if (streamBuffer.Length <= index)
258253
{
259254
throw new Exception("Requested Peek index exceeds the buffer size. Consider increasing the buffer size.");
260255
}
261256

262-
// When index is greater than the buffer size
263-
if (Available <= index)
257+
while (Available <= index)
264258
{
265-
return -1;
259+
// When index is greater than the buffer size
260+
bool fillResult = await FillBufferAsync(cancellationToken);
261+
if (!fillResult)
262+
{
263+
return -1;
264+
}
266265
}
267-
266+
268267
return streamBuffer[bufferPos + index];
269268
}
270269

@@ -279,24 +278,27 @@ public async Task<int> PeekByteAsync(int index, CancellationToken cancellationTo
279278
/// <returns></returns>
280279
public async Task<int> PeekBytesAsync(byte[] buffer, int offset, int index, int count, CancellationToken cancellationToken = default)
281280
{
282-
if (Available <= index)
281+
// When index is greater than the buffer size
282+
if (streamBuffer.Length <= index + count)
283283
{
284-
await FillBufferAsync(cancellationToken);
284+
throw new Exception("Requested Peek index and size exceeds the buffer size. Consider increasing the buffer size.");
285285
}
286286

287-
// When index is greater than the buffer size
288-
if (streamBuffer.Length <= (index + count))
287+
while (Available <= index)
289288
{
290-
throw new Exception("Requested Peek index and size exceeds the buffer size. Consider increasing the buffer size.");
289+
bool fillResult = await FillBufferAsync(cancellationToken);
290+
if (!fillResult)
291+
{
292+
return 0;
293+
}
291294
}
292295

293-
if (Available <= (index + count))
296+
if (Available - index < count)
294297
{
295-
return -1;
298+
count = Available - index;
296299
}
297300

298301
Buffer.BlockCopy(streamBuffer, index, buffer, offset, count);
299-
300302
return count;
301303
}
302304

@@ -516,19 +518,19 @@ public async Task<bool> FillBufferAsync(CancellationToken cancellationToken = de
516518
throw new Exception("Stream is already closed");
517519
}
518520

521+
int bytesToRead = streamBuffer.Length - bufferLength;
522+
if (bytesToRead == 0)
523+
{
524+
return false;
525+
}
526+
519527
if (bufferLength > 0)
520528
{
521529
// normally we fill the buffer only when it is empty, but sometimes we need more data
522530
// move the remaining data to the beginning of the buffer
523531
Buffer.BlockCopy(streamBuffer, bufferPos, streamBuffer, 0, bufferLength);
524532
}
525533

526-
int bytesToRead = streamBuffer.Length - bufferLength;
527-
if (bytesToRead == 0)
528-
{
529-
return false;
530-
}
531-
532534
bufferPos = 0;
533535

534536
bool result = false;

0 commit comments

Comments
 (0)