Commit 299d4bad authored by Honfika's avatar Honfika

Fix for #638

parent b6d73ca5
......@@ -151,20 +151,28 @@ namespace Titanium.Web.Proxy.Helpers
/// </returns>
private static async Task<int> startsWith(ICustomStreamReader clientStreamReader, IBufferPool bufferPool, string expectedStart, CancellationToken cancellationToken = default)
{
int iRet = -1;
const int lengthToCheck = 10;
byte[] buffer = null;
try
{
buffer = bufferPool.GetBuffer(Math.Max(bufferPool.BufferSize, lengthToCheck));
if (bufferPool.BufferSize < lengthToCheck)
{
throw new Exception($"Buffer is too small. Minimum size is {lengthToCheck} bytes");
}
int peeked = await clientStreamReader.PeekBytesAsync(buffer, 0, 0, lengthToCheck, cancellationToken);
buffer = bufferPool.GetBuffer(bufferPool.BufferSize);
if (peeked > 0)
bool isExpected = true;
int i = 0;
while (i < lengthToCheck)
{
bool isExpected = true;
int peeked = await clientStreamReader.PeekBytesAsync(buffer, i, i, lengthToCheck - i, cancellationToken);
if (peeked == 0)
return - 1;
peeked += i;
for (int i = 0; i < lengthToCheck; i++)
while (i < peeked)
{
int b = buffer[i];
......@@ -173,23 +181,23 @@ namespace Titanium.Web.Proxy.Helpers
else
{
char ch = (char)b;
if (!char.IsLetter(ch))
if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter
return -1;
else if (i >= expectedStart.Length || ch != expectedStart[i])
isExpected = false;
isExpected = false;
}
}
// only letters
iRet = isExpected ? 1 : 0;
i++;
}
}
// only letters
return 0;
}
finally
{
bufferPool.ReturnBuffer(buffer);
buffer = null;
}
return iRet;
}
}
}
......@@ -235,6 +235,7 @@ namespace Titanium.Web.Proxy
/// The buffer pool used throughout this proxy instance.
/// Set custom implementations by implementing this interface.
/// By default this uses DefaultBufferPool implementation available in StreamExtended library package.
/// Buffer size should be at least 10 bytes.
/// </summary>
public IBufferPool BufferPool { get; set; }
......
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