diff --git a/src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs b/src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs index c3a8fc2e8f..5400a07e17 100644 --- a/src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs +++ b/src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs @@ -73,62 +73,51 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http private unsafe void ParseRequestLine(TRequestHandler handler, byte* data, int length) { // Get Method and set the offset - var method = HttpUtilities.GetKnownMethod(data, length, out var offset); + var method = HttpUtilities.GetKnownMethod(data, length, out var pathStartOffset); - Span customMethod = method == HttpMethod.Custom ? - GetUnknownMethod(data, length, out offset) : - default; + Span customMethod = default; + if (method == HttpMethod.Custom) + { + customMethod = GetUnknownMethod(data, length, out pathStartOffset); + } + // Use a new offset var as pathStartOffset needs to be on stack + // as its passed by reference above so can't be in register. // Skip space - offset++; + var offset = pathStartOffset + 1; + if (offset >= length) + { + // Start of path not found + RejectRequestLine(data, length); + } + + byte ch = data[offset]; + if (ch == ByteSpace || ch == ByteQuestionMark || ch == BytePercentage) + { + // Empty path is illegal, or path starting with percentage + RejectRequestLine(data, length); + } - byte ch = 0; // Target = Path and Query var pathEncoded = false; - var pathStart = -1; + var pathStart = offset; + + // Skip first char (just checked) + offset++; + + // Find end of path and if path is encoded for (; offset < length; offset++) { ch = data[offset]; - if (ch == ByteSpace) + if (ch == ByteSpace || ch == ByteQuestionMark) { - if (pathStart == -1) - { - // Empty path is illegal - RejectRequestLine(data, length); - } - - break; - } - else if (ch == ByteQuestionMark) - { - if (pathStart == -1) - { - // Empty path is illegal - RejectRequestLine(data, length); - } - + // End of path break; } else if (ch == BytePercentage) { - if (pathStart == -1) - { - // Path starting with % is illegal - RejectRequestLine(data, length); - } - pathEncoded = true; } - else if (pathStart == -1) - { - pathStart = offset; - } - } - - if (pathStart == -1) - { - // Start of path not found - RejectRequestLine(data, length); } var pathBuffer = new Span(data + pathStart, offset - pathStart);