diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs index de624276a2..86d4c4dfbb 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs @@ -164,8 +164,7 @@ namespace Microsoft.PowerShell.Commands string contentType = ContentHelper.GetContentType(BaseResponse); if (ContentHelper.IsText(contentType)) { - Encoding encoding = null; - // fill the Content buffer + // Fill the Content buffer string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse); if (string.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType)) @@ -173,12 +172,12 @@ namespace Microsoft.PowerShell.Commands characterSet = Encoding.UTF8.HeaderName; } - this.Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out encoding); - this.Encoding = encoding; + Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding); + Encoding = encoding; } else { - this.Content = string.Empty; + Content = string.Empty; } } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs index 242f380d94..a27e4e7000 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs @@ -232,7 +232,10 @@ namespace Microsoft.PowerShell.Commands } } } - catch (XmlException) { } + catch (XmlException) + { + // Catch XmlException + } finally { responseStream.Seek(0, SeekOrigin.Begin); 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 bd4e762f5a..7779295c84 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 @@ -270,7 +270,7 @@ namespace Microsoft.PowerShell.Commands /// This property overrides compatibility with web requests on Windows. /// On FullCLR (WebRequest), authorization headers are stripped during redirect. /// CoreCLR (HTTPClient) does not have this behavior so web requests that work on - /// PowerShell/FullCLR can fail with PowerShell/CoreCLR. To provide compatibility, + /// PowerShell/FullCLR can fail with PowerShell/CoreCLR. To provide compatibility, /// we'll detect requests with an Authorization header and automatically strip /// the header when the first redirect occurs. This switch turns off this logic for /// edge cases where the authorization header needs to be preserved across redirects. @@ -576,24 +576,24 @@ namespace Microsoft.PowerShell.Commands internal virtual void PrepareSession() { - // make sure we have a valid WebRequestSession object to work with + // Make sure we have a valid WebRequestSession object to work with WebSession ??= new WebRequestSession(); if (SessionVariable is not null) { - // save the session back to the PS environment if requested + // Save the session back to the PS environment if requested PSVariableIntrinsics vi = SessionState.PSVariable; vi.Set(SessionVariable, WebSession); } - // handle credentials + // Handle credentials if (Credential is not null && Authentication == WebAuthenticationType.None) { - // get the relevant NetworkCredential + // Get the relevant NetworkCredential NetworkCredential netCred = Credential.GetNetworkCredential(); WebSession.Credentials = netCred; - // supplying a credential overrides the UseDefaultCredentials setting + // Supplying a credential overrides the UseDefaultCredentials setting WebSession.UseDefaultCredentials = false; } else if ((Credential is not null || Token is not null) && Authentication != WebAuthenticationType.None) @@ -629,10 +629,10 @@ namespace Microsoft.PowerShell.Commands WebSession.AddCertificate(Certificate); } - // handle the user agent + // Handle the user agent if (UserAgent is not null) { - // store the UserAgent string + // Store the UserAgent string WebSession.UserAgent = UserAgent; } @@ -659,7 +659,7 @@ namespace Microsoft.PowerShell.Commands WebSession.MaximumRedirection = MaximumRedirection; } - // store the other supplied headers + // Store the other supplied headers if (Headers is not null) { foreach (string key in Headers.Keys) @@ -725,7 +725,8 @@ namespace Microsoft.PowerShell.Commands } uri = uriBuilder.Uri; - // set body to null to prevent later FillRequestStream + + // Set body to null to prevent later FillRequestStream Body = null; } @@ -913,18 +914,16 @@ namespace Microsoft.PowerShell.Commands #region Virtual Methods - // NOTE: Only pass true for handleRedirect if the original request has an authorization header - // and PreserveAuthorizationOnRedirect is NOT set. internal virtual HttpClient GetHttpClient(bool handleRedirect) { HttpClientHandler handler = new(); handler.CookieContainer = WebSession.Cookies; handler.AutomaticDecompression = DecompressionMethods.All; - // set the credentials used by this request + // Set the credentials used by this request if (WebSession.UseDefaultCredentials) { - // the UseDefaultCredentials flag overrides other supplied credentials + // The UseDefaultCredentials flag overrides other supplied credentials handler.UseDefaultCredentials = true; } else if (WebSession.Credentials is not null) @@ -977,7 +976,7 @@ namespace Microsoft.PowerShell.Commands Uri requestUri = PrepareUri(uri); HttpMethod httpMethod = string.IsNullOrEmpty(CustomMethod) ? GetHttpMethod(Method) : new HttpMethod(CustomMethod); - // create the base WebRequest object + // Create the base WebRequest object var request = new HttpRequestMessage(httpMethod, requestUri); if (HttpVersion is not null) @@ -985,7 +984,7 @@ namespace Microsoft.PowerShell.Commands request.Version = HttpVersion; } - // pull in session data + // Pull in session data if (WebSession.Headers.Count > 0) { WebSession.ContentHeaders.Clear(); @@ -1072,13 +1071,11 @@ namespace Microsoft.PowerShell.Commands { ArgumentNullException.ThrowIfNull(request); - // set the content type + // Set the request content type if (ContentType is not null) { WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType; - // request } - // ContentType is null else if (request.Method == HttpMethod.Post) { // Win8:545310 Invoke-WebRequest does not properly set MIME type for POST @@ -1499,7 +1496,7 @@ 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 && IsRedirectCode(response.StatusCode)) // Indicate "HttpClientHandler.AllowAutoRedirect is false" + if (WebSession.MaximumRedirection == 0 && IsRedirectCode(response.StatusCode)) { ErrorRecord er = new(new InvalidOperationException(), "MaximumRedirectExceeded", ErrorCategory.InvalidOperation, request); er.ErrorDetails = new ErrorDetails(WebCmdletStrings.MaximumRedirectionCountExceeded); diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs index 8db9064022..5a55ccc2e7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebResponseObject.Common.cs @@ -150,7 +150,8 @@ namespace Microsoft.PowerShell.Commands int initialCapacity = (int)Math.Min(contentLength, StreamHelper.DefaultReadBuffer); RawContentStream = new WebResponseContentMemoryStream(st, initialCapacity, cmdlet: null, response.Content.Headers.ContentLength.GetValueOrDefault()); } - // set the position of the content stream to the beginning + + // Set the position of the content stream to the beginning RawContentStream.Position = 0; } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs index d449001977..e101bcc65b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/InvokeWebRequestCommand.CoreClr.cs @@ -23,7 +23,7 @@ namespace Microsoft.PowerShell.Commands /// public InvokeWebRequestCommand() : base() { - this._parseRelLink = true; + _parseRelLink = true; } /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs index 29e9da1f28..0f46827bd6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/CoreCLR/WebProxy.cs @@ -38,12 +38,7 @@ namespace Microsoft.PowerShell.Commands { ArgumentNullException.ThrowIfNull(destination); - if (destination.IsLoopback) - { - return destination; - } - - return _proxyAddress; + return destination.IsLoopback ? destination : _proxyAddress; } public bool IsBypassed(Uri host) => host.IsLoopback; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs index b8f7d25271..b496a12032 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/FormObject.cs @@ -11,22 +11,22 @@ namespace Microsoft.PowerShell.Commands public class FormObject { /// - /// Gets or private sets the Id property. + /// Gets the Id property. /// public string Id { get; } /// - /// Gets or private sets the Method property. + /// Gets the Method property. /// public string Method { get; } /// - /// Gets or private sets the Action property. + /// Gets the Action property. /// public string Action { get; } /// - /// Gets or private sets the Fields property. + /// Gets the Fields property. /// public Dictionary Fields { get; } @@ -46,8 +46,7 @@ namespace Microsoft.PowerShell.Commands internal void AddField(string key, string value) { - string test; - if (key != null && !Fields.TryGetValue(key, out test)) + if (key is not null && !Fields.TryGetValue(key, out string test)) { Fields[key] = value; }