More efficient preface parsing (#7406)
- Slice before turning the buffer into a Span - Use SequenceEqual instead of a loop
This commit is contained in:
parent
db7218b2fc
commit
8f49bdf195
|
|
@ -357,29 +357,25 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ParsePreface(ReadOnlySequence<byte> readableBuffer, out SequencePosition consumed, out SequencePosition examined)
|
private bool ParsePreface(in ReadOnlySequence<byte> buffer, out SequencePosition consumed, out SequencePosition examined)
|
||||||
{
|
{
|
||||||
consumed = readableBuffer.Start;
|
consumed = buffer.Start;
|
||||||
examined = readableBuffer.End;
|
examined = buffer.End;
|
||||||
|
|
||||||
if (readableBuffer.Length < ClientPreface.Length)
|
if (buffer.Length < ClientPreface.Length)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var span = readableBuffer.IsSingleSegment
|
var preface = buffer.Slice(0, ClientPreface.Length);
|
||||||
? readableBuffer.First.Span
|
var span = preface.ToSpan();
|
||||||
: readableBuffer.ToSpan();
|
|
||||||
|
|
||||||
for (var i = 0; i < ClientPreface.Length; i++)
|
if (!span.SequenceEqual(ClientPreface))
|
||||||
{
|
{
|
||||||
if (ClientPreface[i] != span[i])
|
throw new Http2ConnectionErrorException(CoreStrings.Http2ErrorInvalidPreface, Http2ErrorCode.PROTOCOL_ERROR);
|
||||||
{
|
|
||||||
throw new Http2ConnectionErrorException(CoreStrings.Http2ErrorInvalidPreface, Http2ErrorCode.PROTOCOL_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
consumed = examined = readableBuffer.GetPosition(ClientPreface.Length);
|
consumed = examined = preface.End;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue