From 462f82574320cbb0a104ab29cff4196980935d17 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Wed, 8 Feb 2023 11:54:53 +0100 Subject: [PATCH] Small changes in Webcmdlets (#19109) --- .../Common/WebRequestPSCmdlet.Common.cs | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) 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 7e991eece0..38b4e645ec 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 @@ -605,7 +605,7 @@ namespace Microsoft.PowerShell.Commands if (CertificateThumbprint is not null) { - X509Store store = new(StoreName.My, StoreLocation.CurrentUser); + using X509Store store = new(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection tbCollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false); @@ -1230,7 +1230,6 @@ namespace Microsoft.PowerShell.Commands internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestMessage request, bool handleRedirect) { ArgumentNullException.ThrowIfNull(client); - ArgumentNullException.ThrowIfNull(request); // Add 1 to account for the first request. @@ -1241,7 +1240,7 @@ namespace Microsoft.PowerShell.Commands do { // Track the current URI being used by various requests and re-requests. - var currentUri = req.RequestUri; + Uri currentUri = req.RequestUri; _cancelToken = new CancellationTokenSource(); response = client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, _cancelToken.Token).GetAwaiter().GetResult(); @@ -1259,6 +1258,7 @@ namespace Microsoft.PowerShell.Commands { WebSession.MaximumRedirection--; } + // For selected redirects that used POST, GET must be used with the // redirected Location. // Since GET is the default; POST only occurs when -Method POST is used. @@ -1269,12 +1269,10 @@ namespace Microsoft.PowerShell.Commands } currentUri = new Uri(request.RequestUri, response.Headers.Location); + // Continue to handle redirection - using (client = GetHttpClient(handleRedirect)) - using (HttpRequestMessage redirectRequest = GetRequest(currentUri)) - { - response = GetResponse(client, redirectRequest, handleRedirect); - } + using HttpRequestMessage redirectRequest = GetRequest(currentUri); + response = GetResponse(client, redirectRequest, handleRedirect); } // Request again without the Range header because the server indicated the range was not satisfiable. @@ -1296,11 +1294,8 @@ namespace Microsoft.PowerShell.Commands using (HttpRequestMessage requestWithoutRange = GetRequest(currentUri)) { FillRequestStream(requestWithoutRange); - long requestContentLength = 0; - if (requestWithoutRange.Content is not null) - { - requestContentLength = requestWithoutRange.Content.Headers.ContentLength.Value; - } + + long requestContentLength = requestWithoutRange.Content is null ? 0 : requestWithoutRange.Content.Headers.ContentLength.Value; string reqVerboseMsg = string.Format( CultureInfo.CurrentCulture, @@ -1413,11 +1408,7 @@ namespace Microsoft.PowerShell.Commands FillRequestStream(request); try { - long requestContentLength = 0; - if (request.Content is not null) - { - requestContentLength = request.Content.Headers.ContentLength.Value; - } + long requestContentLength = request.Content is null ? 0 : request.Content.Headers.ContentLength.Value; string reqVerboseMsg = string.Format( CultureInfo.CurrentCulture, @@ -1472,13 +1463,14 @@ namespace Microsoft.PowerShell.Commands StreamReader reader = null; try { - reader = new StreamReader(StreamHelper.GetResponseStream(response)); - // remove HTML tags making it easier to read + reader = new(StreamHelper.GetResponseStream(response)); + + // Remove HTML tags making it easier to read detailMsg = System.Text.RegularExpressions.Regex.Replace(reader.ReadToEnd(), "<[^>]*>", string.Empty); } - catch (Exception) + catch { - // catch all + // Catch all } finally { @@ -1551,7 +1543,7 @@ namespace Microsoft.PowerShell.Commands } /// - /// Implementing ^C, after start the BeginGetResponse. + /// To implement ^C. /// protected override void StopProcessing() => _cancelToken?.Cancel();