Skip to content

HTTP2: Fix HttpRequestHeaders no longer being pooled #19329

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

Merged
merged 2 commits into from
Feb 25, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6105,7 +6105,7 @@ public unsafe void Append(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
}

// We didn't have a previous matching header value, or have already added a header, so get the string for this value.
var valueStr = value.GetRequestHeaderStringNonNullCharacters(_useLatin1);
var valueStr = value.GetRequestHeaderStringNonNullCharacters(UseLatin1);

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

if ((_bits & flag) == 0)
{
// We didn't already have a header set, so add a new one.
Expand All @@ -6123,7 +6123,7 @@ public unsafe void Append(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
// The header was not one of the "known" headers.
// Convert value to string first, because passing two spans causes 8 bytes stack zeroing in
// this method with rep stosd, which is slower than necessary.
var valueStr = value.GetRequestHeaderStringNonNullCharacters(_useLatin1);
var valueStr = value.GetRequestHeaderStringNonNullCharacters(UseLatin1);
AppendUnknownHeaders(name, valueStr);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO.Pipelines;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -81,14 +82,8 @@ public void Initialize(HttpConnectionContext context)

ServerOptions = ServiceContext.ServerOptions;

HttpRequestHeaders = new HttpRequestHeaders(
reuseHeaderValues: !ServerOptions.DisableStringReuse,
useLatin1: ServerOptions.Latin1RequestHeaders);

Reset();

HttpRequestHeaders.ReuseHeaderValues = !ServerOptions.DisableStringReuse;

HttpResponseControl = this;
}

Expand Down Expand Up @@ -371,6 +366,8 @@ public void Reset()
ConnectionIdFeature = ConnectionId;

HttpRequestHeaders.Reset();
HttpRequestHeaders.UseLatin1 = ServerOptions.Latin1RequestHeaders;
HttpRequestHeaders.ReuseHeaderValues = !ServerOptions.DisableStringReuse;
HttpResponseHeaders.Reset();
RequestHeaders = HttpRequestHeaders;
ResponseHeaders = HttpResponseHeaders;
Expand Down
10 changes: 5 additions & 5 deletions src/Servers/Kestrel/Core/src/Internal/Http/HttpRequestHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
{
internal sealed partial class HttpRequestHeaders : HttpHeaders
{
private readonly bool _useLatin1;
private long _previousBits = 0;

public bool ReuseHeaderValues { get; set; }
public bool UseLatin1 { get; set; }

public HttpRequestHeaders(bool reuseHeaderValues = true, bool useLatin1 = false)
{
ReuseHeaderValues = reuseHeaderValues;
_useLatin1 = useLatin1;
UseLatin1 = useLatin1;
}

public bool ReuseHeaderValues { get; set; }

public void OnHeadersComplete()
{
var bitsToClear = _previousBits & ~_bits;
Expand Down Expand Up @@ -83,7 +83,7 @@ private void AppendContentLength(ReadOnlySpan<byte> value)
parsed < 0 ||
consumed != value.Length)
{
BadHttpRequestException.Throw(RequestRejectionReason.InvalidContentLength, value.GetRequestHeaderStringNonNullCharacters(_useLatin1));
BadHttpRequestException.Throw(RequestRejectionReason.InvalidContentLength, value.GetRequestHeaderStringNonNullCharacters(UseLatin1));
}

_contentLength = parsed;
Expand Down
4 changes: 2 additions & 2 deletions src/Servers/Kestrel/shared/KnownHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ public unsafe void Append(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
}}

// We didn't have a previous matching header value, or have already added a header, so get the string for this value.
var valueStr = value.GetRequestHeaderStringNonNullCharacters(_useLatin1);
var valueStr = value.GetRequestHeaderStringNonNullCharacters(UseLatin1);
if ((_bits & flag) == 0)
{{
// We didn't already have a header set, so add a new one.
Expand All @@ -1004,7 +1004,7 @@ public unsafe void Append(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
// The header was not one of the ""known"" headers.
// Convert value to string first, because passing two spans causes 8 bytes stack zeroing in
// this method with rep stosd, which is slower than necessary.
var valueStr = value.GetRequestHeaderStringNonNullCharacters(_useLatin1);
var valueStr = value.GetRequestHeaderStringNonNullCharacters(UseLatin1);
AppendUnknownHeaders(name, valueStr);
}}
}}" : "")}
Expand Down