From 47255407d022279d2e467bd5132d198ed33d5e6c Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:10:40 +0100 Subject: [PATCH 1/2] remove GetContentTypeSignature() --- .../WebCmdlet/Common/ContentHelper.Common.cs | 43 +++---------------- 1 file changed, 5 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs index 41af8ebc6dc..bf127967ffe 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/ContentHelper.Common.cs @@ -60,28 +60,6 @@ internal static StringBuilder GetRawContentHeader(HttpResponseMessage response) } internal static bool IsJson(string contentType) - { - contentType = GetContentTypeSignature(contentType); - return CheckIsJson(contentType); - } - - internal static bool IsText(string contentType) - { - contentType = GetContentTypeSignature(contentType); - return CheckIsText(contentType); - } - - internal static bool IsXml(string contentType) - { - contentType = GetContentTypeSignature(contentType); - return CheckIsXml(contentType); - } - - #endregion Internal Methods - - #region Private Helper Methods - - private static bool CheckIsJson(string contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -102,7 +80,7 @@ private static bool CheckIsJson(string contentType) return isJson; } - private static bool CheckIsText(string contentType) + internal static bool IsText(string contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -111,8 +89,8 @@ private static bool CheckIsText(string contentType) // Any text, xml or json types are text bool isText = contentType.StartsWith("text/", StringComparison.OrdinalIgnoreCase) - || CheckIsXml(contentType) - || CheckIsJson(contentType); + || IsXml(contentType) + || IsJson(contentType); // Further content type analysis is available on Windows if (Platform.IsWindows && !isText) @@ -141,7 +119,7 @@ private static bool CheckIsText(string contentType) return isText; } - private static bool CheckIsXml(string contentType) + internal static bool IsXml(string contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -157,17 +135,6 @@ private static bool CheckIsXml(string contentType) return isXml; } - private static string GetContentTypeSignature(string contentType) - { - if (string.IsNullOrEmpty(contentType)) - { - return null; - } - - string sig = contentType.Split(';', 2)[0].ToUpperInvariant(); - return sig; - } - - #endregion Private Helper Methods + #endregion Internal Methods } } From f3629cea3a2100db4a824f8ab114ff2f1e635289 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:15:34 +0100 Subject: [PATCH 2/2] TryGetEncoding() --- .../commands/utility/WebCmdlet/StreamHelper.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs index 1ddaf5a5794..49cc7d43402 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/StreamHelper.cs @@ -383,13 +383,7 @@ private static string StreamToString(Stream stream, Encoding encoding) internal static string DecodeStream(Stream stream, string characterSet, out Encoding encoding) { - bool isDefaultEncoding = false; - if (!TryGetEncoding(characterSet, out encoding)) - { - // Use the default encoding if one wasn't provided - encoding = ContentHelper.GetDefaultEncoding(); - isDefaultEncoding = true; - } + bool isDefaultEncoding = !TryGetEncoding(characterSet, out encoding); string content = StreamToString(stream, encoding); if (isDefaultEncoding) @@ -433,7 +427,8 @@ internal static bool TryGetEncoding(string characterSet, out Encoding encoding) } catch (ArgumentException) { - encoding = null; + // Use the default encoding if one wasn't provided + encoding = ContentHelper.GetDefaultEncoding(); } return result;