-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Add HttpVersion parameter to Invoke-WebRequest cmdlet #15853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daxian-dbw
merged 20 commits into
PowerShell:master
from
hayhay27:add-http-version-parameter
Nov 2, 2021
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0e33ca5
Implement HttpVersion parameter for Invoke-RestMethod and Invoke-WebR…
hayhay27 a61cb24
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 c4fdc68
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 7335a4c
Apply suggestions from code review
hayhay27 fc17e44
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 0c708d7
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 886809a
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 364b459
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 6951148
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 1c01483
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 5078cdb
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 40a5e8f
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 0452c71
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 fa4c937
Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/Web…
iSazonov 1662308
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 546ca85
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 2bca829
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 83ce173
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 27b46b3
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 2fb1df8
Add HttpVersion parameter to Invoke-WebRequest
hayhay27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...ell.Commands.Utility/commands/utility/WebCmdlet/Common/HttpVersionCompletionsAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Management.Automation; | ||
| using System.Net; | ||
| using System.Reflection; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
| /// <summary> | ||
| /// A completer for HTTP version names. | ||
| /// </summary> | ||
| internal sealed class HttpVersionCompletionsAttribute : ArgumentCompletionsAttribute | ||
| { | ||
| public static readonly string[] AllowedVersions; | ||
|
|
||
| static HttpVersionCompletionsAttribute() | ||
| { | ||
| FieldInfo[] fields = typeof(HttpVersion).GetFields(BindingFlags.Static | BindingFlags.Public); | ||
|
|
||
| var versions = new List<string>(fields.Length - 1); | ||
|
|
||
| for (int i = 0; i < fields.Length; i++) | ||
| { | ||
| // skip field Unknown and not Version type | ||
| if (fields[i].Name == nameof(HttpVersion.Unknown) || fields[i].FieldType != typeof(Version)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var version = (Version?)fields[i].GetValue(null); | ||
|
|
||
| if (version is not null) | ||
| { | ||
| versions.Add(version.ToString()); | ||
| } | ||
| } | ||
|
|
||
| AllowedVersions = versions.ToArray(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public HttpVersionCompletionsAttribute() : base(AllowedVersions) | ||
| { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/System.Management.Automation/engine/ArgumentToVersionTransformationAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace System.Management.Automation | ||
| { | ||
| /// <summary> | ||
| /// To make it easier to specify a version, we add some conversions that wouldn't happen otherwise: | ||
| /// * A simple integer, i.e. 2; | ||
| /// * A string without a dot, i.e. "2". | ||
| /// </summary> | ||
| internal class ArgumentToVersionTransformationAttribute : ArgumentTransformationAttribute | ||
| { | ||
| /// <inheritdoc/> | ||
| public override object Transform(EngineIntrinsics engineIntrinsics, object inputData) | ||
| { | ||
| object version = PSObject.Base(inputData); | ||
|
|
||
| if (version is string versionStr) | ||
| { | ||
| if (TryConvertFromString(versionStr, out var convertedVersion)) | ||
| { | ||
| return convertedVersion; | ||
| } | ||
|
|
||
| if (versionStr.Contains('.')) | ||
| { | ||
| // If the string contains a '.', let the Version constructor handle the conversion. | ||
| return inputData; | ||
| } | ||
| } | ||
|
|
||
| if (version is double) | ||
| { | ||
| // The conversion to int below is wrong, but the usual conversions will turn | ||
| // the double into a string, so just return the original object. | ||
| return inputData; | ||
| } | ||
|
|
||
| if (LanguagePrimitives.TryConvertTo<int>(version, out var majorVersion)) | ||
| { | ||
| return new Version(majorVersion, 0); | ||
| } | ||
|
|
||
| return inputData; | ||
| } | ||
|
|
||
| protected virtual bool TryConvertFromString(string versionString, [NotNullWhen(true)] out Version? version) | ||
| { | ||
| version = null; | ||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.