Skip to content

Commit 6de7549

Browse files
authored
Respect ResponseHeaderEncodingSelector for the Location header (#95810)
1 parent f0a6dbd commit 6de7549

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
161161
else if (knownHeader == KnownHeaders.Location)
162162
{
163163
// Normally Location should be in ISO-8859-1 but occasionally some servers respond with UTF-8.
164-
if (TryDecodeUtf8(headerValue, out string? decoded))
164+
// If the user set the ResponseHeaderEncodingSelector, we give that priority instead.
165+
if (valueEncoding is null && TryDecodeUtf8(headerValue, out string? decoded))
165166
{
166167
return decoded;
167168
}

src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Headers.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ private static readonly (string Name, Encoding ValueEncoding, string Separator,
441441
("Cookie", Encoding.UTF8, "; ", new[] { "Cookies", "\uD83C\uDF6A", "everywhere" }),
442442
("Set-Cookie", Encoding.UTF8, ", ", new[] { "\uD83C\uDDF8\uD83C\uDDEE" }),
443443
("header-5", Encoding.UTF8, ", ", new[] { "\uD83D\uDE48\uD83D\uDE49\uD83D\uDE4A", "foo", "\uD83D\uDE03", "bar" }),
444-
("bar", Encoding.UTF8, ", ", new[] { "foo" })
444+
("bar", Encoding.UTF8, ", ", new[] { "foo" }),
445+
("Location", Encoding.Latin1, ", ", new[] { "\u00D0\u00A4" })
445446
};
446447

447448
[Fact]
@@ -538,7 +539,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(
538539
foreach ((string name, Encoding valueEncoding, string separator, string[] values) in s_nonAsciiHeaders)
539540
{
540541
Assert.Contains(name, seenHeaderNames);
541-
IEnumerable<string> receivedValues = Assert.Single(response.Headers, h => h.Key.Equals(name, StringComparison.OrdinalIgnoreCase)).Value;
542+
IEnumerable<string> receivedValues = Assert.Single(response.Headers.NonValidated, h => h.Key.Equals(name, StringComparison.OrdinalIgnoreCase)).Value;
542543
string value = Assert.Single(receivedValues);
543544

544545
string expected = valueEncoding.GetString(valueEncoding.GetBytes(string.Join(separator, values)));

0 commit comments

Comments
 (0)