Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved HTTP/1 header parser #63295

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<WindowsRID>win</WindowsRID>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -80,6 +80,7 @@
<Compile Include="System\Net\Http\RequestRetryType.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\HttpMessageHandlerStage.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\RuntimeSettingParser.cs" />
<Compile Include="System\Net\Http\SocketsHttpHandler\SimdPaddedArray.cs" />
<Compile Include="System\Net\Http\StreamContent.cs" />
<Compile Include="System\Net\Http\StreamToStreamCopy.cs" />
<Compile Include="System\Net\Http\StringContent.cs" />
Expand Down Expand Up @@ -668,6 +669,7 @@
<Reference Include="System.Runtime.Extensions" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Runtime.Intrinsics" />
<Reference Include="System.Security.Claims" Condition="'$(TargetsWindows)' == 'true'" />
<Reference Include="System.Security.Cryptography" />
<Reference Include="System.Security.Cryptography.Algorithms" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ private sealed class ChunkedEncodingReadStream : HttpContentReadStream
/// infinite chunk length is sent. This value is arbitrary and can be changed as needed.
/// </remarks>
private const int MaxChunkBytesAllowed = 16 * 1024;
/// <summary>How long a trailing header can be. This value is arbitrary and can be changed as needed.</summary>
private const int MaxTrailingHeaderLength = 16 * 1024;
/// <summary>The number of bytes remaining in the chunk.</summary>
private ulong _chunkBytesRemaining;
/// <summary>The current state of the parsing state machine for the chunked response.</summary>
private ParsingState _state = ParsingState.ExpectChunkHeader;
private readonly HttpResponseMessage _response;
private readonly int _trailingHeaderBytesAllowed;

public ChunkedEncodingReadStream(HttpConnection connection, HttpResponseMessage response) : base(connection)
{
Debug.Assert(response != null, "The HttpResponseMessage cannot be null.");
_response = response;

// _allowedReadLineBytes is reset when reading chunk indicators.
// save the original here, which will be the total header bytes minus already received headers.
_trailingHeaderBytesAllowed = connection._allowedReadLineBytes;
}

public override int Read(Span<byte> buffer)
Expand Down Expand Up @@ -361,6 +364,7 @@ private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenR
}
else
{
_connection._allowedReadLineBytes = _trailingHeaderBytesAllowed;
_state = ParsingState.ConsumeTrailers;
goto case ParsingState.ConsumeTrailers;
}
Expand Down Expand Up @@ -406,39 +410,24 @@ private int ReadChunksFromConnectionBuffer(Span<byte> buffer, CancellationTokenR
case ParsingState.ConsumeTrailers:
Debug.Assert(_chunkBytesRemaining == 0, $"Expected {nameof(_chunkBytesRemaining)} == 0, got {_chunkBytesRemaining}");

while (true)
// Consume receive buffer. If the stream is disposed, pass a null response to avoid
// processing headers for a connection returned to the pool.

if (_connection!.ParseHeaders(IsDisposed ? null : _response, isFromTrailer: true))
{
_connection._allowedReadLineBytes = MaxTrailingHeaderLength;
if (!_connection.TryReadNextLine(out currentLine))
{
break;
}

if (currentLine.IsEmpty)
{
// Dispose of the registration and then check whether cancellation has been
// requested. This is necessary to make determinstic a race condition between
// cancellation being requested and unregistering from the token. Otherwise,
// it's possible cancellation could be requested just before we unregister and
// we then return a connection to the pool that has been or will be disposed
// (e.g. if a timer is used and has already queued its callback but the
// callback hasn't yet run).
cancellationRegistration.Dispose();
CancellationHelper.ThrowIfCancellationRequested(cancellationRegistration.Token);

_state = ParsingState.Done;
_connection.CompleteResponse();
_connection = null;

break;
}
// Parse the trailer.
else if (!IsDisposed)
{
// Make sure that we don't inadvertently consume trailing headers
// while draining a connection that's being returned back to the pool.
HttpConnection.ParseHeaderNameValue(_connection, currentLine, _response, isFromTrailer: true);
}
// Dispose of the registration and then check whether cancellation has been
// requested. This is necessary to make determinstic a race condition between
// cancellation being requested and unregistering from the token. Otherwise,
// it's possible cancellation could be requested just before we unregister and
// we then return a connection to the pool that has been or will be disposed
// (e.g. if a timer is used and has already queued its callback but the
// callback hasn't yet run).
cancellationRegistration.Dispose();
CancellationHelper.ThrowIfCancellationRequested(cancellationRegistration.Token);

_state = ParsingState.Done;
_connection.CompleteResponse();
_connection = null;
}

return default;
Expand Down
Loading