Commit 299d4bad authored by Honfika's avatar Honfika

Fix for #638

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