From 83d3b0e12b24a9005b6b730bbfc99a65a8c1d13e Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Tue, 22 Nov 2022 18:01:26 +0100 Subject: [PATCH] Webcmdlets add 308 to redirect codes and small cleanup (#18536) --- .../Common/WebRequestPSCmdlet.Common.cs | 51 +++++++------------ 1 file changed, 19 insertions(+), 32 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 85932f7eb..21be97d56 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 @@ -740,7 +740,7 @@ namespace Microsoft.PowerShell.Commands LanguagePrimitives.TryConvertTo(Body, out bodyAsDictionary); if ((bodyAsDictionary != null) && ((IsStandardMethodSet() && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get)) - || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET"))) + || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET"))) { UriBuilder uriBuilder = new(uri); if (uriBuilder.Query != null && uriBuilder.Query.Length > 1) @@ -1318,9 +1318,9 @@ namespace Microsoft.PowerShell.Commands int intCode = (int)code; return ( - (intCode >= 300 && intCode < 304) - || - intCode == 307 + (intCode >= 300 && intCode < 304) || + intCode == 307 || + intCode == 308 ); } @@ -1330,32 +1330,24 @@ namespace Microsoft.PowerShell.Commands { return ( - code == HttpStatusCode.Found - || - code == HttpStatusCode.Moved - || - code == HttpStatusCode.Redirect - || - code == HttpStatusCode.RedirectMethod - || - code == HttpStatusCode.SeeOther - || - code == HttpStatusCode.Ambiguous - || + code == HttpStatusCode.Found || + code == HttpStatusCode.Moved || + code == HttpStatusCode.Redirect || + code == HttpStatusCode.RedirectMethod || + code == HttpStatusCode.SeeOther || + code == HttpStatusCode.Ambiguous || code == HttpStatusCode.MultipleChoices ); } + // Returns true if the status code shows a server or client error and MaximumRetryCount > 0 private bool ShouldRetry(HttpStatusCode code) { int intCode = (int)code; - - if (((intCode == 304) || (intCode >= 400 && intCode <= 599)) && WebSession.MaximumRetryCount > 0) - { - return true; - } - - return false; + return + ( + (intCode == 304 || (intCode >= 400 && intCode <= 599)) && WebSession.MaximumRetryCount > 0 + ); } internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestMessage request, bool keepAuthorization) @@ -1601,16 +1593,11 @@ namespace Microsoft.PowerShell.Commands // Errors with redirection counts of greater than 0 are handled automatically by .NET, but are // impossible to detect programmatically when we hit this limit. By handling this ourselves // (and still writing out the result), users can debug actual HTTP redirect problems. - if (WebSession.MaximumRedirection == 0) // Indicate "HttpClientHandler.AllowAutoRedirect == false" + if (WebSession.MaximumRedirection == 0 && IsRedirectCode(response.StatusCode)) // Indicate "HttpClientHandler.AllowAutoRedirect == false" { - if (response.StatusCode == HttpStatusCode.Found || - response.StatusCode == HttpStatusCode.Moved || - response.StatusCode == HttpStatusCode.MovedPermanently) - { - ErrorRecord er = new(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); - er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); - WriteError(er); - } + ErrorRecord er = new(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); + er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); + WriteError(er); } } catch (HttpRequestException ex)