/// <summary> /// Determines whether the stream starts with the given string. /// </summary> /// <returns> /// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise /// </returns> private static async Task <int> startsWith(ICustomStreamReader clientStreamReader, IBufferPool bufferPool, string expectedStart, CancellationToken cancellationToken = default) { const int lengthToCheck = 10; byte[] buffer = null; try { if (bufferPool.BufferSize < lengthToCheck) { throw new Exception($"Buffer is too small. Minimum size is {lengthToCheck} bytes"); } buffer = bufferPool.GetBuffer(bufferPool.BufferSize); 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); } peeked += i; while (i < peeked) { int b = buffer[i]; if (b == ' ' && i > 2) { return(isExpected ? 1 : 0); } else { char ch = (char)b; if (ch < 'A' || ch > 'z' || (ch > 'Z' && ch < 'a')) // ASCII letter { return(-1); } else if (i >= expectedStart.Length || ch != expectedStart[i]) { isExpected = false; } } i++; } } // only letters return(0); } finally { bufferPool.ReturnBuffer(buffer); } }
/// <summary> /// Determines whether the stream starts with the given string. /// </summary> /// <param name="clientStreamReader">The client stream reader.</param> /// <param name="expectedStart">The expected start.</param> /// <returns> /// 1: when starts with the given string, 0: when valid HTTP method, -1: otherwise /// </returns> private static async Task <int> startsWith(ICustomStreamReader clientStreamReader, IBufferPool bufferPool, int bufferSize, string expectedStart, CancellationToken cancellationToken = default) { int iRet = -1; const int lengthToCheck = 10; byte[] buffer = null; try { buffer = bufferPool.GetBuffer(Math.Max(bufferSize, lengthToCheck)); int peeked = await clientStreamReader.PeekBytesAsync(buffer, 0, 0, lengthToCheck, cancellationToken); if (peeked > 0) { bool isExpected = true; for (int i = 0; i < lengthToCheck; i++) { int b = buffer[i]; if (b == ' ' && i > 2) { return(isExpected ? 1 : 0); } else { char ch = (char)b; if (!char.IsLetter(ch)) { return(-1); } else if (i >= expectedStart.Length || ch != expectedStart[i]) { isExpected = false; } } } // only letters iRet = isExpected ? 1 : 0; } } finally { bufferPool.ReturnBuffer(buffer); buffer = null; } return(iRet); }
/// <summary> /// Peeks bytes asynchronous. /// </summary> /// <param name="index">The index.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> Task <byte[]> ICustomStreamReader.PeekBytesAsync(int index, int size, CancellationToken cancellationToken) { return(baseStream.PeekBytesAsync(index, size, cancellationToken)); }
/// <summary> /// Peeks bytes asynchronous. /// </summary> /// <param name="buffer">The buffer to copy.</param> /// <param name="offset">The offset where copying.</param> /// <param name="index">The index.</param> /// <param name="count">The count.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> Task <int> ICustomStreamReader.PeekBytesAsync(byte[] buffer, int offset, int index, int count, CancellationToken cancellationToken) { return(baseStream.PeekBytesAsync(buffer, offset, index, count, cancellationToken)); }
public Task <int> PeekBytesAsync(byte[] buffer, int offset, int index, int size, CancellationToken cancellationToken = default) { return(reader.PeekBytesAsync(buffer, offset, index, size, cancellationToken)); }
public Task <byte[]> PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)) { return(reader.PeekBytesAsync(index, size, cancellationToken)); }