Skip to content

Commit

Permalink
Make SocketsHttpHandler's content encoding comparison case-insensitive (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Feb 15, 2024
1 parent bccbf2e commit 64204a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public abstract class HttpClientHandler_Decompression_Test : HttpClientHandlerTe
public HttpClientHandler_Decompression_Test(ITestOutputHelper output) : base(output) { }

public static IEnumerable<object[]> DecompressedResponse_MethodSpecified_DecompressedContentReturned_MemberData() =>
from compressionName in new[] { "gzip", "zlib", "deflate", "br" }
from compressionName in new[] { "gzip", "GZIP", "zlib", "ZLIB", "deflate", "DEFLATE", "br", "BR" }
from all in new[] { false, true }
from copyTo in new[] { false, true }
from contentLength in new[] { 0, 1, 12345 }
Expand All @@ -40,7 +40,7 @@ public static IEnumerable<object[]> DecompressedResponse_MethodSpecified_Decompr
public async Task DecompressedResponse_MethodSpecified_DecompressedContentReturned(string compressionName, bool all, bool useCopyTo, int contentLength)
{
if (IsWinHttpHandler &&
(compressionName == "br" || compressionName == "zlib"))
(compressionName is "br" or "BR" or "zlib" or "ZLIB"))
{
// brotli and zlib not supported on WinHttpHandler
return;
Expand All @@ -52,24 +52,28 @@ public async Task DecompressedResponse_MethodSpecified_DecompressedContentReturn
switch (compressionName)
{
case "gzip":
case "GZIP":
compress = s => new GZipStream(s, CompressionLevel.Optimal, leaveOpen: true);
methods = all ? DecompressionMethods.GZip : _all;
break;

#if !NETFRAMEWORK
case "br":
case "BR":
compress = s => new BrotliStream(s, CompressionLevel.Optimal, leaveOpen: true);
methods = all ? DecompressionMethods.Brotli : _all;
break;

case "zlib":
case "ZLIB":
compress = s => new ZLibStream(s, CompressionLevel.Optimal, leaveOpen: true);
methods = all ? DecompressionMethods.Deflate : _all;
encodingName = "deflate";
break;
#endif

case "deflate":
case "DEFLATE":
compress = s => new DeflateStream(s, CompressionLevel.Optimal, leaveOpen: true);
methods = all ? DecompressionMethods.Deflate : _all;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ internal override async ValueTask<HttpResponseMessage> SendAsync(HttpRequestMess
last = encoding;
}

if (GZipEnabled && last == Gzip)
if (GZipEnabled && string.Equals(last, Gzip, StringComparison.OrdinalIgnoreCase))
{
response.Content = new GZipDecompressedContent(response.Content);
}
else if (DeflateEnabled && last == Deflate)
else if (DeflateEnabled && string.Equals(last, Deflate, StringComparison.OrdinalIgnoreCase))
{
response.Content = new DeflateDecompressedContent(response.Content);
}
else if (BrotliEnabled && last == Brotli)
else if (BrotliEnabled && string.Equals(last, Brotli, StringComparison.OrdinalIgnoreCase))
{
response.Content = new BrotliDecompressedContent(response.Content);
}
Expand Down

0 comments on commit 64204a5

Please sign in to comment.