-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Small cleanup WebCmdlets #19299
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
Merged
Small cleanup WebCmdlets #19299
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b3dc569
Small cleanup
CarloToso 1333c50
replace var
CarloToso cf4b994
add ?
CarloToso c820546
changes from #19308
CarloToso df7ab30
merge DecodeStream
CarloToso 57cbd6e
remove StreamReader
CarloToso 43ed05d
Merge branch 'PowerShell:master' into cleanup-restmethod
CarloToso 3af2a03
revert following suggestions @daxian-dbw
CarloToso 788f719
revert
CarloToso 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
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
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 |
|---|---|---|
|
|
@@ -382,49 +382,9 @@ private static string StreamToString(Stream stream, Encoding encoding) | |
| } | ||
|
|
||
| internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) | ||
| { | ||
| try | ||
| { | ||
| encoding = Encoding.GetEncoding(characterSet); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| encoding = null; | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same. |
||
|
|
||
| return DecodeStream(stream, ref encoding); | ||
| } | ||
|
|
||
| internal static bool TryGetEncoding(string characterSet, out Encoding encoding) | ||
| { | ||
| bool result = false; | ||
| try | ||
| { | ||
| encoding = Encoding.GetEncoding(characterSet); | ||
| result = true; | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| encoding = null; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static readonly Regex s_metaRegex = new( | ||
| @"<meta\s.*[^.><]*charset\s*=\s*[""'\n]?(?<charset>[A-Za-z].[^\s""'\n<>]*)[\s""'\n>]", | ||
| RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.NonBacktracking | ||
| ); | ||
|
|
||
| private static readonly Regex s_xmlRegex = new( | ||
| @"<\?xml\s.*[^.><]*encoding\s*=\s*[""'\n]?(?<charset>[A-Za-z].[^\s""'\n<>]*)[\s""'\n>]", | ||
| RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.NonBacktracking | ||
| ); | ||
|
|
||
| internal static string DecodeStream(Stream stream, ref Encoding encoding) | ||
| { | ||
| bool isDefaultEncoding = false; | ||
| if (encoding is null) | ||
| if (!TryGetEncoding(characterSet, out encoding)) | ||
| { | ||
| // Use the default encoding if one wasn't provided | ||
| encoding = ContentHelper.GetDefaultEncoding(); | ||
|
|
@@ -449,10 +409,9 @@ internal static string DecodeStream(Stream stream, ref Encoding encoding) | |
|
|
||
| if (match.Success) | ||
| { | ||
| Encoding localEncoding = null; | ||
| string characterSet = match.Groups["charset"].Value; | ||
| characterSet = match.Groups["charset"].Value; | ||
|
|
||
| if (TryGetEncoding(characterSet, out localEncoding)) | ||
| if (TryGetEncoding(characterSet, out Encoding localEncoding)) | ||
| { | ||
| stream.Seek(0, SeekOrigin.Begin); | ||
| content = StreamToString(stream, localEncoding); | ||
|
|
@@ -464,6 +423,32 @@ internal static string DecodeStream(Stream stream, ref Encoding encoding) | |
| return content; | ||
| } | ||
|
|
||
| internal static bool TryGetEncoding(string characterSet, out Encoding encoding) | ||
| { | ||
| bool result = false; | ||
| try | ||
| { | ||
| encoding = Encoding.GetEncoding(characterSet); | ||
| result = true; | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| encoding = null; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static readonly Regex s_metaRegex = new( | ||
| @"<meta\s.*[^.><]*charset\s*=\s*[""'\n]?(?<charset>[A-Za-z].[^\s""'\n<>]*)[\s""'\n>]", | ||
| RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.NonBacktracking | ||
| ); | ||
|
|
||
| private static readonly Regex s_xmlRegex = new( | ||
| @"<\?xml\s.*[^.><]*encoding\s*=\s*[""'\n]?(?<charset>[A-Za-z].[^\s""'\n<>]*)[\s""'\n>]", | ||
| RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.NonBacktracking | ||
| ); | ||
|
|
||
| internal static byte[] EncodeToBytes(string str, Encoding encoding) | ||
| { | ||
| // Just use the default encoding if one wasn't provided | ||
|
|
@@ -472,6 +457,8 @@ internal static byte[] EncodeToBytes(string str, Encoding encoding) | |
| return encoding.GetBytes(str); | ||
| } | ||
|
|
||
| internal static string GetResponseString(HttpResponseMessage response) => response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); | ||
|
|
||
| internal static Stream GetResponseStream(HttpResponseMessage response) => response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); | ||
|
|
||
| #endregion Static Methods | ||
|
|
||
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.