Skip to content

Commit

Permalink
Simplify ContentHelper methods (PowerShell#19367)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloToso committed Mar 20, 2023
1 parent 688c7f3 commit 8f0cd65
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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))
{
Expand All @@ -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)
Expand Down Expand Up @@ -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))
{
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8f0cd65

Please sign in to comment.