Shrink ParseRequestLine inner loop (#6594)

This commit is contained in:
Ben Adams 2019-01-12 05:34:30 +01:00 committed by David Fowler
parent c7f05c614a
commit b27739ad3f
1 changed files with 29 additions and 40 deletions

View File

@ -73,62 +73,51 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
private unsafe void ParseRequestLine(TRequestHandler handler, byte* data, int length) private unsafe void ParseRequestLine(TRequestHandler handler, byte* data, int length)
{ {
// Get Method and set the offset // 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<byte> customMethod = method == HttpMethod.Custom ? Span<byte> customMethod = default;
GetUnknownMethod(data, length, out offset) : if (method == HttpMethod.Custom)
default; {
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 // 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 // Target = Path and Query
var pathEncoded = false; 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++) for (; offset < length; offset++)
{ {
ch = data[offset]; ch = data[offset];
if (ch == ByteSpace) if (ch == ByteSpace || ch == ByteQuestionMark)
{ {
if (pathStart == -1) // End of path
{
// Empty path is illegal
RejectRequestLine(data, length);
}
break;
}
else if (ch == ByteQuestionMark)
{
if (pathStart == -1)
{
// Empty path is illegal
RejectRequestLine(data, length);
}
break; break;
} }
else if (ch == BytePercentage) else if (ch == BytePercentage)
{ {
if (pathStart == -1)
{
// Path starting with % is illegal
RejectRequestLine(data, length);
}
pathEncoded = true; pathEncoded = true;
} }
else if (pathStart == -1)
{
pathStart = offset;
}
}
if (pathStart == -1)
{
// Start of path not found
RejectRequestLine(data, length);
} }
var pathBuffer = new Span<byte>(data + pathStart, offset - pathStart); var pathBuffer = new Span<byte>(data + pathStart, offset - pathStart);