Skip to content

Commit 2c87db6

Browse files
authored
Allow known values for HttpHeaderType.Custom in KnownHeaders (#35003)
1 parent 3b4c5a5 commit 2c87db6

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ await LoopbackServer.CreateClientAndServerAsync(async uri =>
989989
Assert.Contains(new ViaHeaderValue("1.1", "example.com", null, "(Apache/1.1)"), resp.Headers.Via);
990990
Assert.Contains(new WarningHeaderValue(199, "-", "\"Miscellaneous warning\"", DateTimeOffset.Parse("Wed, 21 Oct 2015 07:28:00 GMT")), resp.Headers.Warning);
991991
Assert.Contains(new AuthenticationHeaderValue("Basic"), resp.Headers.WwwAuthenticate);
992-
Assert.Contains("deny", resp.Headers.GetValues("X-Frame-Options"));
992+
Assert.Contains("deny", resp.Headers.GetValues("X-Frame-Options"), StringComparer.OrdinalIgnoreCase);
993993
Assert.Contains("default-src 'self'", resp.Headers.GetValues("X-WebKit-CSP"));
994994
Assert.Contains("5; url=http://www.w3.org/pub/WWW/People.html", resp.Headers.GetValues("Refresh"));
995995
Assert.Contains("200 OK", resp.Headers.GetValues("Status"));

src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue)
126126
// If it's a known header value, use the known value instead of allocating a new string.
127127
if (_knownHeader != null)
128128
{
129-
if (_knownHeader.KnownValues != null)
129+
string[]? knownValues = _knownHeader.KnownValues;
130+
if (knownValues != null)
130131
{
131-
string[] knownValues = _knownHeader.KnownValues;
132132
for (int i = 0; i < knownValues.Length; i++)
133133
{
134134
if (ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(knownValues[i], headerValue))

src/libraries/System.Net.Http/src/System/Net/Http/Headers/KnownHeader.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public KnownHeader(string name, HttpHeaderType headerType, HttpHeaderParser? par
2121
{
2222
Debug.Assert(!string.IsNullOrEmpty(name));
2323
Debug.Assert(name[0] == ':' || HttpRuleParser.GetTokenLength(name, 0) == name.Length);
24-
Debug.Assert(knownValues == null || (headerType & HttpHeaderType.Custom) != HttpHeaderType.Custom);
2524

2625
Name = name;
2726
HeaderType = headerType;

src/libraries/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ internal static class KnownHeaders
7070
public static readonly KnownHeader PublicKeyPins = new KnownHeader("Public-Key-Pins");
7171
public static readonly KnownHeader Range = new KnownHeader("Range", HttpHeaderType.Request | HttpHeaderType.NonTrailing, GenericHeaderParser.RangeParser, null, H2StaticTable.Range, H3StaticTable.RangeBytes0ToAll);
7272
public static readonly KnownHeader Referer = new KnownHeader("Referer", HttpHeaderType.Request, UriHeaderParser.RelativeOrAbsoluteUriParser, null, H2StaticTable.Referer, H3StaticTable.Referer); // NB: The spelling-mistake "Referer" for "Referrer" must be matched.
73-
public static readonly KnownHeader ReferrerPolicy = new KnownHeader("Referrer-Policy");
73+
public static readonly KnownHeader ReferrerPolicy = new KnownHeader("Referrer-Policy", HttpHeaderType.Custom, null, new string[] { "strict-origin-when-cross-origin", "origin-when-cross-origin", "strict-origin", "origin", "same-origin", "no-referrer-when-downgrade", "no-referrer", "unsafe-url" });
7474
public static readonly KnownHeader Refresh = new KnownHeader("Refresh", H2StaticTable.Refresh);
7575
public static readonly KnownHeader RetryAfter = new KnownHeader("Retry-After", HttpHeaderType.Response | HttpHeaderType.NonTrailing, GenericHeaderParser.RetryConditionParser, null, H2StaticTable.RetryAfter);
7676
public static readonly KnownHeader SecWebSocketAccept = new KnownHeader("Sec-WebSocket-Accept");
@@ -88,22 +88,22 @@ internal static class KnownHeaders
8888
public static readonly KnownHeader Trailer = new KnownHeader("Trailer", HttpHeaderType.General | HttpHeaderType.NonTrailing, GenericHeaderParser.TokenListParser);
8989
public static readonly KnownHeader TransferEncoding = new KnownHeader("Transfer-Encoding", HttpHeaderType.General | HttpHeaderType.NonTrailing, TransferCodingHeaderParser.MultipleValueParser, new string[] { "chunked", "compress", "deflate", "gzip", "identity" }, H2StaticTable.TransferEncoding);
9090
public static readonly KnownHeader Upgrade = new KnownHeader("Upgrade", HttpHeaderType.General, GenericHeaderParser.MultipleValueProductParser);
91-
public static readonly KnownHeader UpgradeInsecureRequests = new KnownHeader("Upgrade-Insecure-Requests", http3StaticTableIndex: H3StaticTable.UpgradeInsecureRequests1);
91+
public static readonly KnownHeader UpgradeInsecureRequests = new KnownHeader("Upgrade-Insecure-Requests", HttpHeaderType.Custom, null, new string[] { "1" }, http3StaticTableIndex: H3StaticTable.UpgradeInsecureRequests1);
9292
public static readonly KnownHeader UserAgent = new KnownHeader("User-Agent", HttpHeaderType.Request, ProductInfoHeaderParser.MultipleValueParser, null, H2StaticTable.UserAgent, H3StaticTable.UserAgent);
93-
public static readonly KnownHeader Vary = new KnownHeader("Vary", HttpHeaderType.Response | HttpHeaderType.NonTrailing, GenericHeaderParser.TokenListParser, null, H2StaticTable.Vary, H3StaticTable.VaryAcceptEncoding);
93+
public static readonly KnownHeader Vary = new KnownHeader("Vary", HttpHeaderType.Response | HttpHeaderType.NonTrailing, GenericHeaderParser.TokenListParser, new string[] { "*" }, H2StaticTable.Vary, H3StaticTable.VaryAcceptEncoding);
9494
public static readonly KnownHeader Via = new KnownHeader("Via", HttpHeaderType.General, GenericHeaderParser.MultipleValueViaParser, null, H2StaticTable.Via);
9595
public static readonly KnownHeader WWWAuthenticate = new KnownHeader("WWW-Authenticate", HttpHeaderType.Response | HttpHeaderType.NonTrailing, GenericHeaderParser.MultipleValueAuthenticationParser, null, H2StaticTable.WwwAuthenticate);
9696
public static readonly KnownHeader Warning = new KnownHeader("Warning", HttpHeaderType.General | HttpHeaderType.NonTrailing, GenericHeaderParser.MultipleValueWarningParser);
9797
public static readonly KnownHeader XAspNetVersion = new KnownHeader("X-AspNet-Version");
9898
public static readonly KnownHeader XCache = new KnownHeader("X-Cache");
9999
public static readonly KnownHeader XContentDuration = new KnownHeader("X-Content-Duration");
100-
public static readonly KnownHeader XContentTypeOptions = new KnownHeader("X-Content-Type-Options", http3StaticTableIndex: H3StaticTable.XContentTypeOptionsNoSniff);
101-
public static readonly KnownHeader XFrameOptions = new KnownHeader("X-Frame-Options", http3StaticTableIndex: H3StaticTable.XFrameOptionsDeny);
100+
public static readonly KnownHeader XContentTypeOptions = new KnownHeader("X-Content-Type-Options", HttpHeaderType.Custom, null, new string[] { "nosniff" }, http3StaticTableIndex: H3StaticTable.XContentTypeOptionsNoSniff);
101+
public static readonly KnownHeader XFrameOptions = new KnownHeader("X-Frame-Options", HttpHeaderType.Custom, null, new string[] { "DENY", "SAMEORIGIN" }, http3StaticTableIndex: H3StaticTable.XFrameOptionsDeny);
102102
public static readonly KnownHeader XMSEdgeRef = new KnownHeader("X-MSEdge-Ref");
103103
public static readonly KnownHeader XPoweredBy = new KnownHeader("X-Powered-By");
104104
public static readonly KnownHeader XRequestID = new KnownHeader("X-Request-ID");
105105
public static readonly KnownHeader XUACompatible = new KnownHeader("X-UA-Compatible");
106-
public static readonly KnownHeader XXssProtection = new KnownHeader("X-XSS-Protection");
106+
public static readonly KnownHeader XXssProtection = new KnownHeader("X-XSS-Protection", HttpHeaderType.Custom, null, new string[] { "0", "1", "1; mode=block" });
107107

108108
// Helper interface for making GetCandidate generic over strings, utf8, etc
109109
private interface IHeaderNameAccessor

0 commit comments

Comments
 (0)