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

Commit d2095c0

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

File tree

2 files changed

+32
-17
lines changed
  • src/Microsoft.AspNetCore.Server.Kestrel/Http
  • test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests

2 files changed

+32
-17
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);

test/Microsoft.AspNetCore.Server.Kestrel.FunctionalTests/project.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111
"xunit": "2.1.0"
1212
},
1313
"frameworks": {
14-
"netcoreapp1.0": {
15-
"dependencies": {
16-
"Microsoft.NETCore.App": {
17-
"version": "1.0.0-*",
18-
"type": "platform"
19-
},
20-
"System.Net.Http": "4.0.1-*",
21-
"System.Net.Http.WinHttpHandler": "4.0.0-*",
22-
"System.Runtime.Serialization.Primitives": "4.1.1-*"
23-
},
24-
"imports": [
25-
"portable-dnxcore50+net45+win8+wp8+wpa81",
26-
"dotnet",
27-
"portable-net45+win8"
28-
]
29-
},
14+
//"netcoreapp1.0": {
15+
// "dependencies": {
16+
// "Microsoft.NETCore.App": {
17+
// "version": "1.0.0-*",
18+
// "type": "platform"
19+
// },
20+
// "System.Net.Http": "4.0.1-*",
21+
// "System.Net.Http.WinHttpHandler": "4.0.0-*",
22+
// "System.Runtime.Serialization.Primitives": "4.1.1-*"
23+
// },
24+
// "imports": [
25+
// "portable-dnxcore50+net45+win8+wp8+wpa81",
26+
// "dotnet",
27+
// "portable-net45+win8"
28+
// ]
29+
//},
3030
"net451": {
3131
"dependencies": {
3232
"xunit.runner.console": "2.1.0"

0 commit comments

Comments
 (0)