Skip to content

Commit 3a506b9

Browse files
authored
Fix decompression in web cmdlets (PowerShell#17955)
1 parent fd94bea commit 3a506b9

File tree

3 files changed

+8
-24
lines changed

3 files changed

+8
-24
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,9 @@ private HttpMethod GetHttpMethod(WebRequestMethod method)
992992
// and PreserveAuthorizationOnRedirect is NOT set.
993993
internal virtual HttpClient GetHttpClient(bool handleRedirect)
994994
{
995-
// By default the HttpClientHandler will automatically decompress GZip and Deflate content
996995
HttpClientHandler handler = new();
997996
handler.CookieContainer = WebSession.Cookies;
997+
handler.AutomaticDecompression = DecompressionMethods.All;
998998

999999
// set the credentials used by this request
10001000
if (WebSession.UseDefaultCredentials)

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.IO;
6-
using System.IO.Compression;
76
using System.Management.Automation;
87
using System.Management.Automation.Internal;
98
using System.Net.Http;
@@ -502,21 +501,6 @@ internal static byte[] EncodeToBytes(string str)
502501
internal static Stream GetResponseStream(HttpResponseMessage response)
503502
{
504503
Stream responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
505-
var contentEncoding = response.Content.Headers.ContentEncoding;
506-
507-
// HttpClient by default will automatically decompress GZip and Deflate content.
508-
// We keep this decompression logic here just in case.
509-
if (contentEncoding != null && contentEncoding.Count > 0)
510-
{
511-
if (contentEncoding.Contains("gzip"))
512-
{
513-
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
514-
}
515-
else if (contentEncoding.Contains("deflate"))
516-
{
517-
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
518-
}
519-
}
520504

521505
return responseStream;
522506
}

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
587587
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
588588
# Note: These are the supported options, but we do not have a web service to test them all.
589589
It "Invoke-WebRequest supports request that returns <DataEncoding>-encoded data." -TestCases @(
590-
@{ DataEncoding = "gzip"}
591-
@{ DataEncoding = "deflate"}
590+
@{ DataEncoding = "gzip" }
591+
@{ DataEncoding = "deflate" }
592592
) {
593593
param($dataEncoding)
594594
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
@@ -598,7 +598,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
598598
ValidateResponse -response $result
599599

600600
# Validate response content
601-
$result.Output.Headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding
601+
# The content should be de-compressed, and otherwise converting from JSON will fail.
602602
$jsonContent = $result.Output.Content | ConvertFrom-Json
603603
$jsonContent.Headers.Host | Should -BeExactly $uri.Authority
604604
}
@@ -2246,15 +2246,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
22462246
# $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity")
22472247
# Note: These are the supported options, but we do not have a web service to test them all.
22482248
It "Invoke-RestMethod supports request that returns <DataEncoding>-encoded data." -TestCases @(
2249-
@{ DataEncoding = "gzip"}
2250-
@{ DataEncoding = "deflate"}
2249+
@{ DataEncoding = "gzip" }
2250+
@{ DataEncoding = "deflate" }
22512251
) {
22522252
param($dataEncoding)
22532253
$uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding
2254-
$result = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers'
2254+
$result = Invoke-RestMethod -Uri $uri
22552255

22562256
# Validate response content
2257-
$headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding
2257+
# The content should be de-compressed. Otherwise, the above 'Invoke-RestMethod' would have thrown because converting to JSON internally would fail.
22582258
$result.Headers.Host | Should -BeExactly $uri.Authority
22592259
}
22602260

0 commit comments

Comments
 (0)