Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 52980f6

Browse files
author
Cesar Blum Silveira
committed
Avoid unecessary allocation for RawTarget.
1 parent 5f51316 commit 52980f6

File tree

1 file changed

+16
-1
lines changed
  • src/Microsoft.AspNetCore.Server.Kestrel/Http

1 file changed

+16
-1
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Http/Frame.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ protected RequestLineStatus TakeStartLine(SocketInput input)
851851
queryString = begin.GetAsciiString(scan);
852852
}
853853

854-
var rawTarget = pathBegin.GetAsciiString(scan);
854+
var queryEnd = scan;
855855

856856
if (pathBegin.Peek() == ' ')
857857
{
@@ -900,8 +900,12 @@ protected RequestLineStatus TakeStartLine(SocketInput input)
900900
// Multibyte Internationalized Resource Identifiers (IRIs) are first converted to utf8;
901901
// then encoded/escaped to ASCII https://www.ietf.org/rfc/rfc3987.txt "Mapping of IRIs to URIs"
902902
string requestUrlPath;
903+
string rawTarget;
903904
if (needDecode)
904905
{
906+
// Read raw target before mutating memory.
907+
rawTarget = pathBegin.GetAsciiString(queryEnd);
908+
905909
// URI was encoded, unescape and then parse as utf8
906910
pathEnd = UrlPathDecoder.Unescape(pathBegin, pathEnd);
907911
requestUrlPath = pathBegin.GetUtf8String(pathEnd);
@@ -911,6 +915,17 @@ protected RequestLineStatus TakeStartLine(SocketInput input)
911915
{
912916
// URI wasn't encoded, parse as ASCII
913917
requestUrlPath = pathBegin.GetAsciiString(pathEnd);
918+
919+
if (queryString.Length == 0)
920+
{
921+
// No need to allocate an extra string if the path didn't need
922+
// decoding and there's no query string following it.
923+
rawTarget = requestUrlPath;
924+
}
925+
else
926+
{
927+
rawTarget = pathBegin.GetAsciiString(queryEnd);
928+
}
914929
}
915930

916931
requestUrlPath = PathNormalizer.RemoveDotSegments(requestUrlPath);

0 commit comments

Comments
 (0)