Add PreserveHttpMethodOnRedirect switch to Web cmdlets#18894
Add PreserveHttpMethodOnRedirect switch to Web cmdlets#18894daxian-dbw merged 33 commits intoPowerShell:masterfrom
Conversation
| switch (statusCode) | ||
| { | ||
| case HttpStatusCode.Moved: | ||
| case HttpStatusCode.Found: | ||
| case HttpStatusCode.MultipleChoices: | ||
| return requestMethod == WebRequestMethod.Post; | ||
| case HttpStatusCode.SeeOther: | ||
| return requestMethod != WebRequestMethod.Get && requestMethod != WebRequestMethod.Head; | ||
| default: | ||
| return false; |
There was a problem hiding this comment.
It is the same as https://source.dot.net/#System.Net.Http/System/Net/Http/SocketsHttpHandler/RedirectHandler.cs,141
This is used here https://source.dot.net/#System.Net.Http/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs,495
It seems SocketsHttpHandler already implemented the behavior, doesn't it?
There was a problem hiding this comment.
Yes, the only difference is SocketsHttpHandler uses HttpMethod requestMethod and we use WebRequestMethod requestMethod.
Maybe we could replace Method with httpMethod
There was a problem hiding this comment.
We have private HttpMethod GetHttpMethod(WebRequestMethod method) for the conversion.
There was a problem hiding this comment.
We need RequestRequiresForceGet for the cases when we set handler.AllowAutoRedirect = false and WebRequestPSCmdlet handles the redirect
There was a problem hiding this comment.
I see. Thanks for clarify!
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
| bool sessionRedirect = WebSession.MaximumRedirection > 0 || WebSession.MaximumRedirection == -1; | ||
|
|
||
| if (keepAuthorization && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null) | ||
| if ((keepAuthorization || (PreserveHTTPMethodOnRedirect && sessionRedirect)) && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null) |
There was a problem hiding this comment.
If we use sessionRedirect only here we can put PreserveHTTPMethodOnRedirect in line 1328 too.
Condition looks too complex :-) Is it correct?
There was a problem hiding this comment.
I think the current behaviour of if (keepAuthorization..) is wrong, I tested it by setting keepAuthorization = true --> this sets handleRetirect = true --> and then handler.AllowAutoRedirect = false. Without sessionRedirect it ignores MaximumRedirection.
Test: maunally set keepAuthorization = true before the if, then
Invoke-WebRequest "http://mockbin.org/redirect/308?to=https://mockbin.org/redirect/302?to=https://mockbin.org/status/200" -MaximumRedirection 0 -SkipHttpErrorCheck --> expected 308, result 200
Proposed fix --> if (keepAuthorization && sessionRedirect && IsRedirectCode(response.StatusCode) && response.Headers.Location is not null)
There was a problem hiding this comment.
If you think there is a bug in redirection please open new PR and keep the PR only for adding new parameter.
(I believe the new parameter shouldn't change redirection logic.)
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
|
|
||
| // This indicates GetResponse will handle redirects. | ||
| if (handleRedirect) | ||
| if (handleRedirect || PreserveHTTPMethodOnRedirect) |
There was a problem hiding this comment.
I suggest changing the code block as in your another PR to avoid merge conflict.
There was a problem hiding this comment.
I will change it after we merge the other PR
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Show resolved
Hide resolved
|
@SteveL-MSFT @PaulHigin Do you have any objections to this new parameter? |
|
@iSazonov I think the parameter name is fine |
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Outdated
Show resolved
Hide resolved
|
This PR has Quantification details
Why proper sizing of changes matters
Optimal pull request sizes drive a better predictable PR flow as they strike a
What can I do to optimize my changes
How to interpret the change counts in git diff output
Was this comment helpful? 👍 :ok_hand: :thumbsdown: (Email) |
|
🎉 Handy links: |
PR Summary
Add -PreserveHttpMethodOnRedirect switch to preserve the original HTTP method used in
Invoke-RestMethod,andInvoke-WebRequestupon redirect.Replace some code with switch expressions
PR Context
Fixes #14531
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.-PreserveHttpMethodOnRedirectparameter MicrosoftDocs/PowerShell-Docs#9690(which runs in a different PS Host).