Skip to content

Commit

Permalink
Allow retaining the original casing for known header values (#80754)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Feb 8, 2023
1 parent 4827704 commit 955f05e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ namespace System.Net.Http
/// </summary>
internal static class GlobalHttpSettings
{
// Switch to disable ignore-case matching for known header values. Enabled by default.
public static bool IgnoreCaseForKnownHeaderValues { get; } = RuntimeSettingParser.QueryRuntimeSettingSwitch(
"System.Net.Http.IgnoreCaseForKnownHeaderValues",
"DOTNET_SYSTEM_NET_HTTP_IGNORECASEFORKNOWNHEADERVALUES",
true);

internal static class DiagnosticsHandler
{
public static bool EnableActivityPropagation { get; } = RuntimeSettingParser.QueryRuntimeSettingSwitch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco
{
for (int i = 0; i < knownValues.Length; i++)
{
if (ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(knownValues[i], headerValue))
if (GlobalHttpSettings.IgnoreCaseForKnownHeaderValues
? ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(knownValues[i], headerValue)
: ByteArrayHelpers.EqualsOrdinalAscii(knownValues[i], headerValue))
{
return knownValues[i];
}
Expand Down Expand Up @@ -239,9 +241,17 @@ public string GetHeaderValue(ReadOnlySpan<byte> headerValue, Encoding? valueEnco

Debug.Assert(candidate is null || candidate.Length == contentTypeValue.Length);

return candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate, contentTypeValue) ?
candidate :
null;
if (candidate is not null)
{
if (GlobalHttpSettings.IgnoreCaseForKnownHeaderValues
? ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate, contentTypeValue)
: ByteArrayHelpers.EqualsOrdinalAscii(candidate, contentTypeValue))
{
return candidate;
}
}

return null;
}

private static bool TryDecodeUtf8(ReadOnlySpan<byte> input, [NotNullWhen(true)] out string? decoded)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;

namespace System.Net.Http.Tests
Expand Down Expand Up @@ -245,5 +247,28 @@ public void GetKnownHeaderValue_Unknown_NotFound(string name, string value)
Assert.Equal(value, v2);
Assert.NotSame(v1, v2);
}

[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[InlineData("Access-Control-Allow-Credentials", "true")]
[InlineData("Cache-Control", "no-cache")]
[InlineData("Content-Type", "text/plain")]
public void GetKnownHeaderValue_RespectsAppContextIgnoreCaseSwitch(string name, string value)
{
RemoteExecutor.Invoke(static (name, value) =>
{
AppContext.SetSwitch("System.Net.Http.IgnoreCaseForKnownHeaderValues", false);
KnownHeader knownHeader = KnownHeaders.TryGetKnownHeader(name);
Assert.NotNull(knownHeader);
string sameCase = knownHeader.Descriptor.GetHeaderValue(Encoding.ASCII.GetBytes(value), valueEncoding: null);
Assert.NotNull(sameCase);
Assert.Equal(value, sameCase);
string differentCase = knownHeader.Descriptor.GetHeaderValue(Encoding.ASCII.GetBytes(value.ToUpperInvariant()), valueEncoding: null);
Assert.NotNull(differentCase);
Assert.NotEqual(value, differentCase);
}, name, value).Dispose();
}
}
}
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>
<StringResourcesPath>../../src/Resources/Strings.resx</StringResourcesPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down Expand Up @@ -78,6 +78,8 @@
Link="ProductionCode\System\Net\Http\EmptyReadStream.cs" />
<Compile Include="..\..\src\System\Net\Http\FormUrlEncodedContent.cs"
Link="ProductionCode\System\Net\Http\FormUrlEncodedContent.cs" />
<Compile Include="..\..\src\System\Net\Http\GlobalHttpSettings.cs"
Link="ProductionCode\System\Net\Http\GlobalHttpSettings.cs" />
<Compile Include="..\..\src\System\Net\Http\HeaderEncodingSelector.cs"
Link="ProductionCode\System\Net\Http\HeaderEncodingSelector.cs" />
<Compile Include="..\..\src\System\Net\Http\Headers\AltSvcHeaderParser.cs"
Expand Down

0 comments on commit 955f05e

Please sign in to comment.