Cleanup WebCmdlets comments (#19124)

This commit is contained in:
CarloToso
2023-02-09 16:58:55 +05:00
committed by GitHub
parent 1ee8df5fe1
commit c4d688d6e3
7 changed files with 34 additions and 40 deletions
@@ -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;
}
}
@@ -232,7 +232,10 @@ namespace Microsoft.PowerShell.Commands
}
}
}
catch (XmlException) { }
catch (XmlException)
{
// Catch XmlException
}
finally
{
responseStream.Seek(0, SeekOrigin.Begin);
@@ -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);
@@ -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;
}
@@ -23,7 +23,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public InvokeWebRequestCommand() : base()
{
this._parseRelLink = true;
_parseRelLink = true;
}
/// <summary>
@@ -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;
@@ -11,22 +11,22 @@ namespace Microsoft.PowerShell.Commands
public class FormObject
{
/// <summary>
/// Gets or private sets the Id property.
/// Gets the Id property.
/// </summary>
public string Id { get; }
/// <summary>
/// Gets or private sets the Method property.
/// Gets the Method property.
/// </summary>
public string Method { get; }
/// <summary>
/// Gets or private sets the Action property.
/// Gets the Action property.
/// </summary>
public string Action { get; }
/// <summary>
/// Gets or private sets the Fields property.
/// Gets the Fields property.
/// </summary>
public Dictionary<string, string> 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;
}