diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 692c895d21..85932f7eb8 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -992,9 +992,9 @@ namespace Microsoft.PowerShell.Commands // and PreserveAuthorizationOnRedirect is NOT set. internal virtual HttpClient GetHttpClient(bool handleRedirect) { - // By default the HttpClientHandler will automatically decompress GZip and Deflate content HttpClientHandler handler = new(); handler.CookieContainer = WebSession.Cookies; + handler.AutomaticDecompression = DecompressionMethods.All; // set the credentials used by this request if (WebSession.UseDefaultCredentials) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index b9345ff260..9314cfd1a5 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -3,7 +3,6 @@ using System; using System.IO; -using System.IO.Compression; using System.Management.Automation; using System.Management.Automation.Internal; using System.Net.Http; @@ -502,21 +501,6 @@ namespace Microsoft.PowerShell.Commands internal static Stream GetResponseStream(HttpResponseMessage response) { Stream responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); - var contentEncoding = response.Content.Headers.ContentEncoding; - - // HttpClient by default will automatically decompress GZip and Deflate content. - // We keep this decompression logic here just in case. - if (contentEncoding != null && contentEncoding.Count > 0) - { - if (contentEncoding.Contains("gzip")) - { - responseStream = new GZipStream(responseStream, CompressionMode.Decompress); - } - else if (contentEncoding.Contains("deflate")) - { - responseStream = new DeflateStream(responseStream, CompressionMode.Decompress); - } - } return responseStream; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 7a5ad26d54..c7d75bc75e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -587,8 +587,8 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { # $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity") # Note: These are the supported options, but we do not have a web service to test them all. It "Invoke-WebRequest supports request that returns -encoded data." -TestCases @( - @{ DataEncoding = "gzip"} - @{ DataEncoding = "deflate"} + @{ DataEncoding = "gzip" } + @{ DataEncoding = "deflate" } ) { param($dataEncoding) $uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding @@ -598,7 +598,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" { ValidateResponse -response $result # Validate response content - $result.Output.Headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding + # The content should be de-compressed, and otherwise converting from JSON will fail. $jsonContent = $result.Output.Content | ConvertFrom-Json $jsonContent.Headers.Host | Should -BeExactly $uri.Authority } @@ -2246,15 +2246,15 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" { # $dataEncodings = @("Chunked", "Compress", "Deflate", "GZip", "Identity") # Note: These are the supported options, but we do not have a web service to test them all. It "Invoke-RestMethod supports request that returns -encoded data." -TestCases @( - @{ DataEncoding = "gzip"} - @{ DataEncoding = "deflate"} + @{ DataEncoding = "gzip" } + @{ DataEncoding = "deflate" } ) { param($dataEncoding) $uri = Get-WebListenerUrl -Test 'Compression' -TestValue $dataEncoding - $result = Invoke-RestMethod -Uri $uri -ResponseHeadersVariable 'headers' + $result = Invoke-RestMethod -Uri $uri # Validate response content - $headers.'Content-Encoding'[0] | Should -BeExactly $dataEncoding + # The content should be de-compressed. Otherwise, the above 'Invoke-RestMethod' would have thrown because converting to JSON internally would fail. $result.Headers.Host | Should -BeExactly $uri.Authority }