mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix decompression in web cmdlets (#17955)
This commit is contained in:
+1
-1
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 <DataEncoding>-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 <DataEncoding>-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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user