Small cleanup WebCmdlets (#19030)

This commit is contained in:
CarloToso
2023-02-01 21:42:37 +05:00
committed by GitHub
parent 049a1f29d5
commit 3e74bd0ea1
4 changed files with 59 additions and 74 deletions
@@ -15,11 +15,8 @@ namespace Microsoft.PowerShell.Commands
{
#region Internal Methods
internal static string GetContentType(HttpResponseMessage response)
{
// ContentType may not exist in response header. Return null if not.
return response.Content.Headers.ContentType?.MediaType;
}
// ContentType may not exist in response header. Return null if not.
internal static string GetContentType(HttpResponseMessage response) => response.Content.Headers.ContentType?.MediaType;
internal static Encoding GetDefaultEncoding() => Encoding.UTF8;
@@ -32,8 +29,7 @@ namespace Microsoft.PowerShell.Commands
{
int statusCode = WebResponseHelper.GetStatusCode(response);
string statusDescription = WebResponseHelper.GetStatusDescription(response);
raw.Append($"{protocol} {statusCode} {statusDescription}");
raw.AppendLine();
raw.AppendLine($"{protocol} {statusCode} {statusDescription}");
}
HttpHeaders[] headerCollections =
@@ -52,12 +48,9 @@ namespace Microsoft.PowerShell.Commands
foreach (var header in headerCollection)
{
// Headers may have multiple entries with different values
foreach (var headerValue in header.Value)
foreach (string headerValue in header.Value)
{
raw.Append(header.Key);
raw.Append(": ");
raw.Append(headerValue);
raw.AppendLine();
raw.AppendLine($"{header.Key}: {headerValue}");
}
}
}
@@ -101,10 +94,10 @@ namespace Microsoft.PowerShell.Commands
// Add in these other "javascript" related types that
// sometimes get sent down as the mime type for JSON content
isJson |= contentType.Equals("text/json", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/javascript", StringComparison.OrdinalIgnoreCase);
|| contentType.Equals("application/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/x-javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/javascript", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("text/javascript", StringComparison.OrdinalIgnoreCase);
return isJson;
}
@@ -118,8 +111,8 @@ namespace Microsoft.PowerShell.Commands
// Any text, xml or json types are text
bool isText = contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase)
|| CheckIsXml(contentType)
|| CheckIsJson(contentType);
|| CheckIsXml(contentType)
|| CheckIsJson(contentType);
// Further content type analysis is available on Windows
if (Platform.IsWindows && !isText)
@@ -156,11 +149,11 @@ namespace Microsoft.PowerShell.Commands
}
// RFC 3023: Media types with the suffix "+xml" are XML
bool isXml = (contentType.Equals("application/xml", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/xml-external-parsed-entity", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/xml-dtd", StringComparison.OrdinalIgnoreCase));
isXml |= contentType.EndsWith("+xml", StringComparison.OrdinalIgnoreCase);
bool isXml = contentType.Equals("application/xml", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/xml-external-parsed-entity", StringComparison.OrdinalIgnoreCase)
|| contentType.Equals("application/xml-dtd", StringComparison.OrdinalIgnoreCase)
|| contentType.EndsWith("+xml", StringComparison.OrdinalIgnoreCase);
return isXml;
}
@@ -105,7 +105,7 @@ namespace Microsoft.PowerShell.Commands
[ValidateNotNullOrEmpty]
public virtual Uri Uri { get; set; }
#endregion
#endregion URI
#region HTTP Version
@@ -117,7 +117,7 @@ namespace Microsoft.PowerShell.Commands
[HttpVersionCompletions]
public virtual Version HttpVersion { get; set; }
#endregion
#endregion HTTP Version
#region Session
/// <summary>
@@ -133,7 +133,7 @@ namespace Microsoft.PowerShell.Commands
[Alias("SV")]
public virtual string SessionVariable { get; set; }
#endregion
#endregion Session
#region Authorization and Credentials
@@ -198,13 +198,7 @@ namespace Microsoft.PowerShell.Commands
[Parameter]
public virtual SecureString Token { get; set; }
/// <summary>
/// Gets or sets the AllowInsecureRedirect property used to follow HTTP redirects from HTTPS.
/// </summary>
[Parameter]
public virtual SwitchParameter AllowInsecureRedirect { get; set; }
#endregion
#endregion Authorization and Credentials
#region Headers
@@ -234,10 +228,25 @@ namespace Microsoft.PowerShell.Commands
[Parameter]
public virtual IDictionary Headers { get; set; }
#endregion
/// <summary>
/// Gets or sets the SkipHeaderValidation property.
/// </summary>
/// <remarks>
/// This property adds headers to the request's header collection without validation.
/// </remarks>
[Parameter]
public virtual SwitchParameter SkipHeaderValidation { get; set; }
#endregion Headers
#region Redirect
/// <summary>
/// Gets or sets the AllowInsecureRedirect property used to follow HTTP redirects from HTTPS.
/// </summary>
[Parameter]
public virtual SwitchParameter AllowInsecureRedirect { get; set; }
/// <summary>
/// Gets or sets the RedirectMax property.
/// </summary>
@@ -252,6 +261,21 @@ namespace Microsoft.PowerShell.Commands
[ValidateRange(0, int.MaxValue)]
public virtual int MaximumRetryCount { get; set; }
/// <summary>
/// Gets or sets the PreserveAuthorizationOnRedirect property.
/// </summary>
/// <remarks>
/// 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,
/// 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.
/// </remarks>
[Parameter]
public virtual SwitchParameter PreserveAuthorizationOnRedirect { get; set; }
/// <summary>
/// Gets or sets the RetryIntervalSec property, which determines the number seconds between retries.
/// </summary>
@@ -259,7 +283,7 @@ namespace Microsoft.PowerShell.Commands
[ValidateRange(1, int.MaxValue)]
public virtual int RetryIntervalSec { get; set; } = 5;
#endregion
#endregion Redirect
#region Method
@@ -286,7 +310,7 @@ namespace Microsoft.PowerShell.Commands
private string _custommethod;
#endregion
#endregion Method
#region NoProxy
@@ -297,7 +321,7 @@ namespace Microsoft.PowerShell.Commands
[Parameter(Mandatory = true, ParameterSetName = "StandardMethodNoProxy")]
public virtual SwitchParameter NoProxy { get; set; }
#endregion
#endregion NoProxy
#region Proxy
@@ -323,7 +347,7 @@ namespace Microsoft.PowerShell.Commands
[Parameter(ParameterSetName = "CustomMethod")]
public virtual SwitchParameter ProxyUseDefaultCredentials { get; set; }
#endregion
#endregion Proxy
#region Input
@@ -365,7 +389,7 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
private string _originalFilePath;
#endregion
#endregion Input
#region Output
@@ -393,7 +417,7 @@ namespace Microsoft.PowerShell.Commands
[Parameter]
public virtual SwitchParameter SkipHttpErrorCheck { get; set; }
#endregion
#endregion Output
#endregion Virtual Properties
@@ -825,30 +849,6 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public abstract partial class WebRequestPSCmdlet : PSCmdlet
{
/// <summary>
/// Gets or sets the PreserveAuthorizationOnRedirect property.
/// </summary>
/// <remarks>
/// 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,
/// 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.
/// </remarks>
[Parameter]
public virtual SwitchParameter PreserveAuthorizationOnRedirect { get; set; }
/// <summary>
/// Gets or sets the SkipHeaderValidation property.
/// </summary>
/// <remarks>
/// This property adds headers to the request's header collection without validation.
/// </remarks>
[Parameter]
public virtual SwitchParameter SkipHeaderValidation { get; set; }
#region Abstract Methods
/// <summary>
@@ -43,7 +43,7 @@ namespace Microsoft.PowerShell.Commands
internal static bool IsText(HttpResponseMessage response)
{
// ContentType may not exist in response header.
string contentType = response.Content.Headers.ContentType?.MediaType;
string contentType = ContentHelper.GetContentType(response);
return ContentHelper.IsText(contentType);
}
}
@@ -11,15 +11,7 @@ namespace Microsoft.PowerShell.Commands
{
internal static WebResponseObject GetResponseObject(HttpResponseMessage response, Stream responseStream, ExecutionContext executionContext)
{
WebResponseObject output;
if (WebResponseHelper.IsText(response))
{
output = new BasicHtmlWebResponseObject(response, responseStream);
}
else
{
output = new WebResponseObject(response, responseStream);
}
WebResponseObject output = WebResponseHelper.IsText(response) ? new BasicHtmlWebResponseObject(response, responseStream) : new WebResponseObject(response, responseStream);
return output;
}