mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-25 19:36:21 -10:00
Add CurlHandler.MaxResponseHeadersLength
Adds the MaxResponseHeadersLength property. Commit migrated from https://github.com/dotnet/corefx/commit/8840bc7b3961cbaae547da068bd47d452a8a5dfe
This commit is contained in:
1 parent
7557aa945c
commit
8b16b341a4
8 files changed
+147
-7
No files matched your search
@@ -11,6 +11,7 @@ namespace System.Net.Http
|
||||
{
|
||||
public const int DefaultMaxAutomaticRedirections = 50;
|
||||
public const int DefaultMaxConnectionsPerServer = int.MaxValue;
|
||||
public const int DefaultMaxResponseHeaderLength = 64 * 1024;
|
||||
public const DecompressionMethods DefaultAutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
|
||||
public const bool DefaultAutomaticRedirection = true;
|
||||
public const bool DefaultUseCookies = true;
|
||||
|
||||
@@ -354,4 +354,7 @@
|
||||
<data name="net_http_buffer_insufficient_length" xml:space="preserve">
|
||||
<value>The buffer was not long enough.</value>
|
||||
</data>
|
||||
<data name="net_http_response_headers_exceeded_length" xml:space="preserve">
|
||||
<value>The HTTP response headers length exceeded the set limit of {0} bytes.</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -131,6 +131,12 @@ namespace System.Net.Http
|
||||
set { throw new PlatformNotSupportedException(); }
|
||||
}
|
||||
|
||||
public int MaxResponseHeadersLength
|
||||
{
|
||||
get { return _curlHandler.MaxResponseHeadersLength; }
|
||||
set { _curlHandler.MaxResponseHeadersLength = value; }
|
||||
}
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region De/Constructors
|
||||
|
||||
+2
-1
@@ -15,8 +15,9 @@ namespace System.Net.Http
|
||||
{
|
||||
private sealed class CurlResponseMessage : HttpResponseMessage
|
||||
{
|
||||
internal readonly EasyRequest _easy;
|
||||
private readonly CurlResponseStream _responseStream;
|
||||
internal readonly EasyRequest _easy;
|
||||
internal uint _headerBytesReceived;
|
||||
|
||||
internal CurlResponseMessage(EasyRequest easy)
|
||||
{
|
||||
|
||||
@@ -641,28 +641,37 @@ namespace System.Net.Http
|
||||
// The callback is invoked once per header; multi-line headers get merged into a single line.
|
||||
|
||||
size *= nitems;
|
||||
Debug.Assert(size <= Interop.Http.CURL_MAX_HTTP_HEADER, $"Expected header size <= {Interop.Http.CURL_MAX_HTTP_HEADER}, got {size}");
|
||||
if (size == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Debug.Assert(size <= Interop.Http.CURL_MAX_HTTP_HEADER);
|
||||
|
||||
EasyRequest easy;
|
||||
if (TryGetEasyRequestFromContext(context, out easy))
|
||||
{
|
||||
CurlHandler.EventSourceTrace("Size: {0}", size, easy: easy);
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = easy._responseMessage;
|
||||
|
||||
CurlResponseMessage response = easy._responseMessage;
|
||||
CurlResponseHeaderReader reader = new CurlResponseHeaderReader(buffer, size);
|
||||
|
||||
// Validate that we haven't received too much header data.
|
||||
ulong headerBytesReceived = response._headerBytesReceived + size;
|
||||
if (headerBytesReceived > (ulong)easy._handler.MaxResponseHeadersLength)
|
||||
{
|
||||
throw new HttpRequestException(
|
||||
SR.Format(SR.net_http_response_headers_exceeded_length, easy._handler.MaxResponseHeadersLength));
|
||||
}
|
||||
response._headerBytesReceived = (uint)headerBytesReceived;
|
||||
|
||||
// Parse the header
|
||||
if (reader.ReadStatusLine(response))
|
||||
{
|
||||
// Clear the header if status line is received again. This signifies that there are multiple response headers (like in redirection).
|
||||
// Clear the headers when the status line is received. This may happen multiple times if there are multiple response headers (like in redirection).
|
||||
response.Headers.Clear();
|
||||
response.Content.Headers.Clear();
|
||||
response._headerBytesReceived = (uint)size;
|
||||
|
||||
easy._isRedirect = easy._handler.AutomaticRedirection &&
|
||||
(response.StatusCode == HttpStatusCode.Moved || // 301
|
||||
|
||||
@@ -89,6 +89,7 @@ namespace System.Net.Http
|
||||
private bool _automaticRedirection = HttpHandlerDefaults.DefaultAutomaticRedirection;
|
||||
private int _maxAutomaticRedirections = HttpHandlerDefaults.DefaultMaxAutomaticRedirections;
|
||||
private int _maxConnectionsPerServer = HttpHandlerDefaults.DefaultMaxConnectionsPerServer;
|
||||
private int _maxResponseHeadersLength = HttpHandlerDefaults.DefaultMaxResponseHeaderLength;
|
||||
private ClientCertificateOption _clientCertificateOption = HttpHandlerDefaults.DefaultClientCertificateOption;
|
||||
private X509Certificate2Collection _clientCertificates;
|
||||
private Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> _serverCertificateValidationCallback;
|
||||
@@ -293,7 +294,7 @@ namespace System.Net.Http
|
||||
}
|
||||
}
|
||||
|
||||
public int MaxConnectionsPerServer
|
||||
internal int MaxConnectionsPerServer
|
||||
{
|
||||
get { return _maxConnectionsPerServer; }
|
||||
set
|
||||
@@ -324,6 +325,21 @@ namespace System.Net.Http
|
||||
}
|
||||
}
|
||||
|
||||
internal int MaxResponseHeadersLength
|
||||
{
|
||||
get { return _maxResponseHeadersLength; }
|
||||
set
|
||||
{
|
||||
if (value <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(value), value, SR.Format(SR.net_http_value_must_be_greater_than, 0));
|
||||
}
|
||||
|
||||
CheckDisposedOrStarted();
|
||||
_maxResponseHeadersLength = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal bool UseDefaultCredentials
|
||||
{
|
||||
get { return false; }
|
||||
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
// 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.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Net.Test.Common;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace System.Net.Http.Functional.Tests
|
||||
{
|
||||
public class HttpClientHandler_MaxResponseHeadersLength_Test : RemoteExecutorTestBase
|
||||
{
|
||||
[Fact]
|
||||
public void Default_MaxResponseHeadersLength()
|
||||
{
|
||||
using (var handler = new HttpClientHandler())
|
||||
{
|
||||
Assert.Equal(64 * 1024, handler.MaxResponseHeadersLength);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public void InvalidValue_ThrowsException(int invalidValue)
|
||||
{
|
||||
using (var handler = new HttpClientHandler())
|
||||
{
|
||||
Assert.Throws<ArgumentOutOfRangeException>("value", () => handler.MaxResponseHeadersLength = invalidValue);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(65 * 1024)]
|
||||
[InlineData(int.MaxValue)]
|
||||
public void ValidValue_SetGet_Roundtrips(int validValue)
|
||||
{
|
||||
using (var handler = new HttpClientHandler())
|
||||
{
|
||||
handler.MaxResponseHeadersLength = validValue;
|
||||
Assert.Equal(validValue, handler.MaxResponseHeadersLength);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SetAfterUse_Throws()
|
||||
{
|
||||
using (var handler = new HttpClientHandler())
|
||||
using (var client = new HttpClient(handler))
|
||||
{
|
||||
handler.MaxResponseHeadersLength = int.MaxValue;
|
||||
await client.GetStreamAsync(HttpTestServers.RemoteEchoServer);
|
||||
Assert.Throws<InvalidOperationException>(() => handler.MaxResponseHeadersLength = int.MaxValue);
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", 37, false)]
|
||||
[InlineData("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", 38, true)]
|
||||
[InlineData("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n", 39, true)]
|
||||
public async Task ThresholdExceeded_ThrowsException(string responseHeaders, int maxResponseHeadersLength, bool shouldSucceed)
|
||||
{
|
||||
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||
{
|
||||
s.Bind(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
s.Listen(1);
|
||||
var ep = (IPEndPoint)s.LocalEndPoint;
|
||||
|
||||
using (var handler = new HttpClientHandler() { MaxResponseHeadersLength = maxResponseHeadersLength })
|
||||
using (var client = new HttpClient(handler))
|
||||
{
|
||||
Task<HttpResponseMessage> getAsync = client.GetAsync($"http://{ep.Address}:{ep.Port}", HttpCompletionOption.ResponseHeadersRead);
|
||||
|
||||
using (Socket server = s.Accept())
|
||||
using (Stream serverStream = new NetworkStream(server, ownsSocket: false))
|
||||
using (StreamReader reader = new StreamReader(serverStream, Encoding.ASCII))
|
||||
{
|
||||
string line;
|
||||
while ((line = reader.ReadLine()) != null && !string.IsNullOrEmpty(line)) ;
|
||||
|
||||
byte[] headerData = Encoding.ASCII.GetBytes(responseHeaders);
|
||||
serverStream.Write(headerData, 0, headerData.Length);
|
||||
}
|
||||
|
||||
if (shouldSucceed)
|
||||
{
|
||||
(await getAsync).Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
await Assert.ThrowsAsync<HttpRequestException>(() => getAsync);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+1
@@ -66,6 +66,7 @@
|
||||
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
|
||||
<Compile Include="HttpClientHandlerTest.DefaultProxyCredentials.cs" />
|
||||
<Compile Include="HttpClientHandlerTest.MaxConnectionsPerServer.cs" />
|
||||
<Compile Include="HttpClientHandlerTest.MaxResponseHeadersLength.cs" />
|
||||
<Compile Include="HttpClientHandlerTest.ServerCertificates.cs" />
|
||||
<Compile Include="HttpClientHandlerTest.SslProtocols.cs" />
|
||||
<Compile Include="HttpClientHandlerTest.Timeouts.cs" />
|
||||
|
||||
Reference in new issue
Block a user