diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs index e76515b042..5e3aa7097e 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Http/KestrelHttpParser.cs @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http // Prepare the first span var span = buffer.First.Span; - var lineIndex = span.SequentialIndexOf(ByteLF); + var lineIndex = span.IndexOf(ByteLF); if (lineIndex >= 0) { consumed = buffer.Move(consumed, lineIndex + 1); @@ -255,7 +255,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http index = reader.Index; } - var endIndex = new Span(pBuffer + index, remaining).SequentialIndexOf(ByteLF); + var endIndex = new Span(pBuffer + index, remaining).IndexOf(ByteLF); var length = 0; if (endIndex != -1) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs index 003c8ccc8e..b4dafbd3a7 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Infrastructure/HttpUtilities.cs @@ -270,23 +270,32 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Infrastructure /// A reference to the known scheme, if the input matches any /// True when memory starts with known http or https schema [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool GetKnownHttpScheme(this Span span, out HttpScheme knownScheme) + public static unsafe bool GetKnownHttpScheme(this Span span, out HttpScheme knownScheme) { - if (span.TryRead(out var value)) + fixed (byte* data = &span.DangerousGetPinnableReference()) { - if ((value & _mask7Chars) == _httpSchemeLong) + return GetKnownHttpScheme(data, span.Length, out knownScheme); + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static unsafe bool GetKnownHttpScheme(byte* location, int length, out HttpScheme knownScheme) + { + if (length >= sizeof(ulong)) + { + var scheme = *(ulong*)location; + if ((scheme & _mask7Chars) == _httpSchemeLong) { knownScheme = HttpScheme.Http; return true; } - if (value == _httpsSchemeLong) + if (scheme == _httpsSchemeLong) { knownScheme = HttpScheme.Https; return true; } } - knownScheme = HttpScheme.Unknown; return false; }