Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
76e40aa
simplify switch (ParameterSetName)
CarloToso Dec 23, 2022
2c19cd6
Simplify CustomMehod.ToUpperInvariant()
CarloToso Dec 23, 2022
a82141a
WebRequestMethod.Default->WebRequestMethod.Get
CarloToso Dec 23, 2022
d2ee34b
Simplify GetHttpMethod()
CarloToso Dec 23, 2022
4a0cab6
simplify setting httpMethod
CarloToso Dec 23, 2022
3e296d0
remove IsStandardMethodSet(); IsCustomMethodSet()
CarloToso Dec 23, 2022
b627f22
Cleanup IRM and CLR
CarloToso Dec 23, 2022
2b6d084
move _custommethod
CarloToso Dec 24, 2022
a978a8a
Add errors to ValidateParameters(); fix some comments
CarloToso Dec 28, 2022
a851663
Revert ParameterSetName = StandardMethod-CustomMethod and connected e…
CarloToso Dec 28, 2022
95c37ff
fix WebCmdlets.Tests.ps1
CarloToso Dec 28, 2022
0828454
remove useless ()
CarloToso Dec 28, 2022
7c4a8d9
Method -> method (Bug)
CarloToso Jan 6, 2023
a502140
replace Method with request.Method
CarloToso Jan 6, 2023
feeb284
remove useless CustomMethod
CarloToso Jan 6, 2023
622fef3
Merge branch 'master' into Investigation-on-removing-WebRequestMethod
CarloToso Jan 6, 2023
3e77ad5
Re-add removed Parameters
CarloToso Jan 6, 2023
5d82b45
remove leftover [Parameter]
CarloToso Jan 6, 2023
bd603f5
WebRequestMethod.Default
CarloToso Jan 7, 2023
5a98c78
GetHttpMethod static
CarloToso Jan 7, 2023
c7ad35c
Method -> method
CarloToso Jan 7, 2023
5ff3608
GetHttpMethod switch expression
CarloToso Jan 9, 2023
a8c8e97
GetHttpMethod alphabetical order
CarloToso Jan 9, 2023
2c191c8
switch remove last ,
CarloToso Jan 9, 2023
b55dfff
cosmetic reorder &&
CarloToso Jan 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override string CustomMethod
{
get => base.CustomMethod;

set => base.CustomMethod = value;
set => base.CustomMethod = value.ToUpperInvariant();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,14 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet
[Parameter(Mandatory = true, ParameterSetName = "CustomMethodNoProxy")]
[Alias("CM")]
[ValidateNotNullOrEmpty]
public virtual string CustomMethod { get; set; }
public virtual string CustomMethod
{
get => _custommethod;

set => _custommethod = value.ToUpperInvariant();
}

private string _custommethod;

#endregion

Expand Down Expand Up @@ -388,7 +395,7 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet

internal virtual void ValidateParameters()
{
// sessions
// Sessions
if (WebSession is not null && SessionVariable is not null)
{
ErrorRecord error = GetValidationError(WebCmdletStrings.SessionConflict, "WebCmdletSessionConflictException");
Expand Down Expand Up @@ -432,7 +439,7 @@ internal virtual void ValidateParameters()
ThrowTerminatingError(error);
}

// credentials
// Credentials
if (UseDefaultCredentials && Credential is not null)
{
ErrorRecord error = GetValidationError(WebCmdletStrings.CredentialConflict, "WebCmdletCredentialConflictException");
Expand All @@ -451,7 +458,7 @@ internal virtual void ValidateParameters()
ThrowTerminatingError(error);
}

// request body content
// Request body content
if (Body is not null && InFile is not null)
{
ErrorRecord error = GetValidationError(WebCmdletStrings.BodyConflict, "WebCmdletBodyConflictException");
Expand All @@ -470,7 +477,7 @@ internal virtual void ValidateParameters()
ThrowTerminatingError(error);
}

// validate InFile path
// Validate InFile path
if (InFile is not null)
{
ProviderInfo provider = null;
Expand Down Expand Up @@ -525,7 +532,7 @@ internal virtual void ValidateParameters()
}
}

// output ??
// Output ??
if (PassThru && OutFile is null)
{
ErrorRecord error = GetValidationError(WebCmdletStrings.OutFileMissing, "WebCmdletOutFileMissingException", nameof(PassThru));
Expand Down Expand Up @@ -679,9 +686,7 @@ private Uri PrepareUri(Uri uri)
// preprocess Body if content is a dictionary and method is GET (set as query)
IDictionary bodyAsDictionary;
LanguagePrimitives.TryConvertTo<IDictionary>(Body, out bodyAsDictionary);
if (bodyAsDictionary is not null
&& ((IsStandardMethodSet() && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get))
|| (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET")))
if (bodyAsDictionary is not null && (Method == WebRequestMethod.Default || Method == WebRequestMethod.Get || CustomMethod == "GET"))
{
UriBuilder uriBuilder = new(uri);
if (uriBuilder.Query is not null && uriBuilder.Query.Length > 1)
Expand Down Expand Up @@ -762,16 +767,6 @@ private ErrorRecord GetValidationError(string msg, string errorId, params object
return error;
}

private bool IsStandardMethodSet()
{
return (ParameterSetName == "StandardMethod" || ParameterSetName == "StandardMethodNoProxy");
}

private bool IsCustomMethodSet()
{
return (ParameterSetName == "CustomMethod" || ParameterSetName == "CustomMethodNoProxy");
}

private string GetBasicAuthorizationHeader()
{
var password = new NetworkCredential(null, Credential.Password).Password;
Expand Down Expand Up @@ -901,30 +896,18 @@ public abstract partial class WebRequestPSCmdlet : PSCmdlet
/// </summary>
private long _resumeFileSize = 0;

private HttpMethod GetHttpMethod(WebRequestMethod method)
private static HttpMethod GetHttpMethod(WebRequestMethod method) => method switch
{
switch (Method)
{
case WebRequestMethod.Default:
case WebRequestMethod.Get:
return HttpMethod.Get;
case WebRequestMethod.Head:
return HttpMethod.Head;
case WebRequestMethod.Post:
return HttpMethod.Post;
case WebRequestMethod.Put:
return HttpMethod.Put;
case WebRequestMethod.Delete:
return HttpMethod.Delete;
case WebRequestMethod.Trace:
return HttpMethod.Trace;
case WebRequestMethod.Options:
return HttpMethod.Options;
default:
// Merge and Patch
return new HttpMethod(Method.ToString().ToUpperInvariant());
}
}
WebRequestMethod.Default or WebRequestMethod.Get => HttpMethod.Get,
WebRequestMethod.Delete => HttpMethod.Delete,
WebRequestMethod.Head => HttpMethod.Head,
WebRequestMethod.Patch => HttpMethod.Patch,
WebRequestMethod.Post => HttpMethod.Post,
WebRequestMethod.Put => HttpMethod.Put,
WebRequestMethod.Options => HttpMethod.Options,
WebRequestMethod.Trace => HttpMethod.Trace,
_ => new HttpMethod(method.ToString().ToUpperInvariant())
};

#region Virtual Methods

Expand Down Expand Up @@ -1005,27 +988,7 @@ internal virtual HttpClient GetHttpClient(bool handleRedirect)
internal virtual HttpRequestMessage GetRequest(Uri uri)
{
Uri requestUri = PrepareUri(uri);
HttpMethod httpMethod = null;

switch (ParameterSetName)
{
case "StandardMethodNoProxy":
goto case "StandardMethod";
case "StandardMethod":
// set the method if the parameter was provided
httpMethod = GetHttpMethod(Method);
break;
case "CustomMethodNoProxy":
goto case "CustomMethod";
case "CustomMethod":
if (!string.IsNullOrEmpty(CustomMethod))
{
// set the method if the parameter was provided
httpMethod = new HttpMethod(CustomMethod.ToUpperInvariant());
}

break;
}
HttpMethod httpMethod = string.IsNullOrEmpty(CustomMethod) ? GetHttpMethod(Method) : new HttpMethod(CustomMethod);

// create the base WebRequest object
var request = new HttpRequestMessage(httpMethod, requestUri);
Expand Down Expand Up @@ -1130,7 +1093,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
// request
}
// ContentType is null
else if (Method == WebRequestMethod.Post || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "POST"))
else if (request.Method == HttpMethod.Post)
{
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
string contentType = null;
Expand Down Expand Up @@ -1221,7 +1184,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
if (request.Content is null)
{
// If this is a Get request and there is no content, then don't fill in the content as empty content gets rejected by some web services per RFC7230
if ((IsStandardMethodSet() && request.Method == HttpMethod.Get && ContentType is null) || (IsCustomMethodSet() && CustomMethod.ToUpperInvariant() == "GET"))
if (request.Method == HttpMethod.Get && ContentType is null)
{
return;
}
Expand Down Expand Up @@ -1344,10 +1307,10 @@ internal virtual HttpResponseMessage GetResponse(HttpClient client, HttpRequestM
// Request again without the Range header because the server indicated the range was not satisfiable.
// This happens when the local file is larger than the remote file.
// If the size of the remote file is the same as the local file, there is nothing to resume.
if (Resume.IsPresent &&
response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable &&
(response.Content.Headers.ContentRange.HasLength &&
response.Content.Headers.ContentRange.Length != _resumeFileSize))
if (Resume.IsPresent
&& response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable
&& (response.Content.Headers.ContentRange.HasLength
&& response.Content.Headers.ContentRange.Length != _resumeFileSize))
{
_cancelToken.Cancel();

Expand Down Expand Up @@ -1451,10 +1414,10 @@ protected override void ProcessRecord()

// if the request contains an authorization header and PreserveAuthorizationOnRedirect is not set,
// it needs to be stripped on the first redirect.
bool keepAuthorization = WebSession is not null &&
WebSession.Headers is not null &&
PreserveAuthorizationOnRedirect.IsPresent &&
WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization);
bool keepAuthorization = WebSession is not null
&& WebSession.Headers is not null
&& PreserveAuthorizationOnRedirect.IsPresent
&& WebSession.Headers.ContainsKey(HttpKnownHeaderNames.Authorization);

using (HttpClient client = GetHttpClient(keepAuthorization))
{
Expand Down Expand Up @@ -1505,10 +1468,10 @@ WebSession.Headers is not null &&

// Check if the Resume range was not satisfiable because the file already completed downloading.
// This happens when the local file is the same size as the remote file.
if (Resume.IsPresent &&
response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable &&
response.Content.Headers.ContentRange.HasLength &&
response.Content.Headers.ContentRange.Length == _resumeFileSize)
if (Resume.IsPresent
&& response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable
&& response.Content.Headers.ContentRange.HasLength
&& response.Content.Headers.ContentRange.Length == _resumeFileSize)
{
_isSuccess = true;
WriteVerbose(string.Format(
Expand Down