mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-23 03:39:35 -10:00
Merge pull request dotnet/corefx#3199 from justinvp/winhttphandler_parseresponseheaders
Reduce allocations when parsing response headers in WinHttpHandler Commit migrated from https://github.com/dotnet/corefx/commit/35f7ef05a8022358ec435152e7099d8263a4422a
This commit is contained in:
commit
52476494de
19 files changed
+1177
-247
No files matched your search
@@ -258,7 +258,7 @@ internal static partial class Interop
|
||||
public static bool WinHttpQueryHeaders(
|
||||
SafeWinHttpHandle requestHandle,
|
||||
uint infoLevel, string name,
|
||||
StringBuilder buffer,
|
||||
IntPtr buffer,
|
||||
ref uint bufferLength,
|
||||
ref uint index)
|
||||
{
|
||||
@@ -273,42 +273,20 @@ internal static partial class Interop
|
||||
|
||||
if (infoLevel == Interop.WinHttp.WINHTTP_QUERY_VERSION)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
bufferLength = ((uint)httpVersion.Length + 1) * 2;
|
||||
TestControl.LastWin32Error = (int)Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER;
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.Append(httpVersion);
|
||||
return true;
|
||||
return CopyToBufferOrFailIfInsufficientBufferLength(httpVersion, buffer, ref bufferLength);
|
||||
}
|
||||
|
||||
if (infoLevel == Interop.WinHttp.WINHTTP_QUERY_STATUS_TEXT)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
bufferLength = ((uint)statusText.Length + 1) * 2;
|
||||
TestControl.LastWin32Error = (int)Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER;
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.Append(statusText);
|
||||
return true;
|
||||
return CopyToBufferOrFailIfInsufficientBufferLength(statusText, buffer, ref bufferLength);
|
||||
}
|
||||
|
||||
if (infoLevel == Interop.WinHttp.WINHTTP_QUERY_CONTENT_ENCODING)
|
||||
{
|
||||
string compression = null;
|
||||
|
||||
if (TestServer.ResponseHeaders.Contains("Content-Encoding: deflate"))
|
||||
{
|
||||
compression = "deflate";
|
||||
}
|
||||
else if (TestServer.ResponseHeaders.Contains("Content-Encoding: gzip"))
|
||||
{
|
||||
compression = "gzip";
|
||||
}
|
||||
string compression =
|
||||
TestServer.ResponseHeaders.Contains("Content-Encoding: deflate") ? "deflate" :
|
||||
TestServer.ResponseHeaders.Contains("Content-Encoding: gzip") ? "gzip" :
|
||||
null;
|
||||
|
||||
if (compression == null)
|
||||
{
|
||||
@@ -316,33 +294,39 @@ internal static partial class Interop
|
||||
return false;
|
||||
}
|
||||
|
||||
if (buffer == null)
|
||||
{
|
||||
bufferLength = ((uint)compression.Length + 1) * 2;
|
||||
TestControl.LastWin32Error = (int)Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER;
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.Append(compression);
|
||||
return true;
|
||||
return CopyToBufferOrFailIfInsufficientBufferLength(compression, buffer, ref bufferLength);
|
||||
}
|
||||
|
||||
if (infoLevel == Interop.WinHttp.WINHTTP_QUERY_RAW_HEADERS_CRLF)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
bufferLength = ((uint)TestServer.ResponseHeaders.Length + 1) * 2;
|
||||
TestControl.LastWin32Error = (int)Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER;
|
||||
return false;
|
||||
}
|
||||
|
||||
buffer.Append(TestServer.ResponseHeaders);
|
||||
return true;
|
||||
return CopyToBufferOrFailIfInsufficientBufferLength(TestServer.ResponseHeaders, buffer, ref bufferLength);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool CopyToBufferOrFailIfInsufficientBufferLength(string value, IntPtr buffer, ref uint bufferLength)
|
||||
{
|
||||
// The length of the string (plus terminating null char) in bytes.
|
||||
uint bufferLengthNeeded = ((uint)value.Length + 1) * sizeof(char);
|
||||
|
||||
if (buffer == IntPtr.Zero || bufferLength < bufferLengthNeeded)
|
||||
{
|
||||
bufferLength = bufferLengthNeeded;
|
||||
TestControl.LastWin32Error = (int)Interop.WinHttp.ERROR_INSUFFICIENT_BUFFER;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy the string to the buffer.
|
||||
char[] temp = new char[value.Length + 1]; // null terminated.
|
||||
value.CopyTo(0, temp, 0, value.Length);
|
||||
Marshal.Copy(temp, 0, buffer, temp.Length);
|
||||
|
||||
// The length in bytes, minus the length of the null char at the end.
|
||||
bufferLength = (uint)value.Length * sizeof(char);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool WinHttpQueryHeaders(
|
||||
SafeWinHttpHandle requestHandle,
|
||||
uint infoLevel,
|
||||
|
||||
+16
@@ -41,9 +41,21 @@
|
||||
<Compile Include="$(CommonPath)\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs">
|
||||
<Link>Common\Microsoft\Win32\SafeHandles\SafeHandleZeroOrMinusOneIsInvalid.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\CharArrayHelpers.cs">
|
||||
<Link>Common\System\CharArrayHelpers.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\StringExtensions.cs">
|
||||
<Link>Common\System\StringExtensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpKnownHeaderNames.cs">
|
||||
<Link>Common\System\Net\HttpKnownHeaderNames.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpKnownHeaderNames.TryGetHeaderName.cs">
|
||||
<Link>Common\System\Net\HttpKnownHeaderNames.TryGetHeaderName.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpStatusDescription.cs">
|
||||
<Link>Common\System\Net\HttpStatusDescription.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="$(CommonPath)\System\Net\HttpVersion.cs">
|
||||
<Link>Common\System\Net\HttpVersion.cs</Link>
|
||||
</Compile>
|
||||
@@ -89,6 +101,9 @@
|
||||
<Compile Include="..\..\src\System\Net\Http\WinHttpRequestStream.cs">
|
||||
<Link>ProductionCode\WinHttpRequestStream.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\System\Net\Http\WinHttpResponseHeaderReader.cs">
|
||||
<Link>ProductionCode\WinHttpResponseHeaderReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\..\src\System\Net\Http\WinHttpResponseParser.cs">
|
||||
<Link>ProductionCode\WinHttpResponseParser.cs</Link>
|
||||
</Compile>
|
||||
@@ -118,6 +133,7 @@
|
||||
<Compile Include="TestControl.cs" />
|
||||
<Compile Include="WinHttpHandlerTest.cs" />
|
||||
<Compile Include="WinHttpRequestStreamTest.cs" />
|
||||
<Compile Include="WinHttpResponseHeaderReaderTest.cs" />
|
||||
<Compile Include="WinHttpResponseStreamTest.cs" />
|
||||
<Compile Include="XunitTestAssemblyAtrributes.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Http.WinHttpHandlerUnitTests
|
||||
{
|
||||
public class WinHttpResponseHeaderReaderTest
|
||||
{
|
||||
private static readonly KeyValuePair<string, string>[] s_emptyHeaders = Array.Empty<KeyValuePair<string, string>>();
|
||||
|
||||
[Fact]
|
||||
public void ReadHeader_WithStatusLine_CanSkipStatusLineAndReadHeader()
|
||||
{
|
||||
char[] array = "HTTP/1.0 200 OK\r\nServer: Apache".ToCharArray();
|
||||
var reader = new WinHttpResponseHeaderReader(array, 0, array.Length);
|
||||
|
||||
Assert.True(reader.ReadLine());
|
||||
|
||||
string name;
|
||||
string value;
|
||||
Assert.True(reader.ReadHeader(out name, out value));
|
||||
Assert.Equal("Server", name);
|
||||
Assert.Equal("Apache", value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadHeader_KnownHeaderName_SameKnownHeaderNameObjectReturned()
|
||||
{
|
||||
char[] array = "Server: Apache".ToCharArray();
|
||||
var reader = new WinHttpResponseHeaderReader(array, 0, array.Length);
|
||||
|
||||
string name;
|
||||
string value;
|
||||
Assert.True(reader.ReadHeader(out name, out value));
|
||||
Assert.Same(HttpKnownHeaderNames.Server, name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Content-Encoding: gzip")]
|
||||
[InlineData("Content-Encoding: deflate")]
|
||||
public void ReadHeader_KnownHeaderValue_SameHeaderValueObjectReturned(string header)
|
||||
{
|
||||
char[] array = (header + "\r\n" + header).ToCharArray();
|
||||
var reader = new WinHttpResponseHeaderReader(array, 0, array.Length);
|
||||
|
||||
string name1;
|
||||
string value1;
|
||||
Assert.True(reader.ReadHeader(out name1, out value1));
|
||||
|
||||
string name2;
|
||||
string value2;
|
||||
Assert.True(reader.ReadHeader(out name2, out value2));
|
||||
|
||||
Assert.Same(value1, value2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData("HeaderData")]
|
||||
public void ReadHeader_VariousInputs_MatchesExpectedBehavior(string raw, KeyValuePair<string, string>[] expectedHeaders)
|
||||
{
|
||||
char[] array = raw.ToCharArray();
|
||||
var reader = new WinHttpResponseHeaderReader(array, 0, array.Length);
|
||||
|
||||
string name;
|
||||
string value;
|
||||
|
||||
for (int i = 0; i < expectedHeaders.Length; i++)
|
||||
{
|
||||
string expectedName = expectedHeaders[i].Key;
|
||||
string expectedValue = expectedHeaders[i].Value;
|
||||
|
||||
Assert.True(reader.ReadHeader(out name, out value));
|
||||
Assert.Equal(expectedName, name);
|
||||
Assert.Equal(expectedValue, value);
|
||||
}
|
||||
|
||||
Assert.False(reader.ReadHeader(out name, out value));
|
||||
Assert.Null(name);
|
||||
Assert.Null(value);
|
||||
}
|
||||
|
||||
public static object[][] HeaderData =
|
||||
{
|
||||
new object[] { "", s_emptyHeaders },
|
||||
new object[] { " ", s_emptyHeaders },
|
||||
new object[] { "\t", s_emptyHeaders },
|
||||
new object[] { "\r\n", s_emptyHeaders },
|
||||
new object[] { "\r\n\r\n", s_emptyHeaders },
|
||||
new object[] { "\r\n\r\n\r\n", s_emptyHeaders },
|
||||
|
||||
new object[] { "Content-Length: 50", new[] { CreateHeader("Content-Length", "50") } },
|
||||
|
||||
new object[] { "X-Custom-Header: Foo", new[] { CreateHeader("X-Custom-Header", "Foo") } },
|
||||
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50\r\n" +
|
||||
"Content-Encoding: gzip\r\n" +
|
||||
"X-Powered-By: .NET",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("Content-Encoding", "gzip"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
},
|
||||
|
||||
// No colon in a line, should be skipped
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50\r\n" +
|
||||
"no colon, should be skipped\r\n" +
|
||||
"X-Powered-By: .NET",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
},
|
||||
|
||||
// Empty lines (middle line) should be skipped
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50\r\n" +
|
||||
"\r\n" +
|
||||
"X-Powered-By: .NET",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50\r\n" +
|
||||
"" +
|
||||
"X-Powered-By: .NET",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
},
|
||||
|
||||
// Empty lines (last line) should be skipped
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50\r\n" +
|
||||
"Content-Encoding: deflate\r\n" +
|
||||
"X-Powered-By: .NET\r\n",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("Content-Encoding", "deflate"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
},
|
||||
|
||||
// Values should be trimmed
|
||||
new object[]
|
||||
{
|
||||
"Content-Length: 50 \r\n" +
|
||||
"Content-Encoding: brotli \r\n" +
|
||||
"X-Powered-By: .NET ",
|
||||
new[]
|
||||
{
|
||||
CreateHeader("Content-Length", "50"),
|
||||
CreateHeader("Content-Encoding", "brotli"),
|
||||
CreateHeader("X-Powered-By", ".NET")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static KeyValuePair<string, string> CreateHeader(string name, string value)
|
||||
{
|
||||
return new KeyValuePair<string, string>(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user