From 8f0cd65257ab8959dd16a424a5973fbf2fdb7651 Mon Sep 17 00:00:00 2001 From: CarloToso <105941898+CarloToso@users.noreply.github.com> Date: Mon, 20 Mar 2023 17:14:49 +0100 Subject: [PATCH] Simplify ContentHelper methods (#19367) --- .../WebCmdlet/Common/ContentHelper.Common.cs | 43 +++---------------- .../utility/WebCmdlet/StreamHelper.cs | 11 ++--- 2 files changed, 8 insertions(+), 46 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 41af8ebc6d..bf127967ff 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 @@ namespace Microsoft.PowerShell.Commands } 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 @@ namespace Microsoft.PowerShell.Commands return isJson; } - private static bool CheckIsText(string contentType) + internal static bool IsText(string contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -111,8 +89,8 @@ namespace Microsoft.PowerShell.Commands // 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 @@ namespace Microsoft.PowerShell.Commands return isText; } - private static bool CheckIsXml(string contentType) + internal static bool IsXml(string contentType) { if (string.IsNullOrEmpty(contentType)) { @@ -157,17 +135,6 @@ namespace Microsoft.PowerShell.Commands 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 } } 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 1ddaf5a579..49cc7d4340 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 @@ namespace Microsoft.PowerShell.Commands 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 @@ namespace Microsoft.PowerShell.Commands } catch (ArgumentException) { - encoding = null; + // Use the default encoding if one wasn't provided + encoding = ContentHelper.GetDefaultEncoding(); } return result;