Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ protected void InitializeContent()
// Fill the Content buffer
string characterSet = WebResponseHelper.GetCharacterSet(BaseResponse);

if (string.IsNullOrEmpty(characterSet) && ContentHelper.IsJson(contentType))
{
characterSet = Encoding.UTF8.HeaderName;
}

Content = StreamHelper.DecodeStream(RawContentStream, characterSet, out Encoding encoding);
Encoding = encoding;
}
Expand Down Expand Up @@ -218,7 +213,7 @@ private void InitializeRawContent(HttpResponseMessage baseResponse)
{
StringBuilder raw = ContentHelper.GetRawContentHeader(baseResponse);
raw.Append(Content);
this.RawContent = raw.ToString();
RawContent = raw.ToString();
}

private static void ParseAttributes(string outerHtml, PSObject elementObject)
Expand All @@ -228,16 +223,16 @@ private static void ParseAttributes(string outerHtml, PSObject elementObject)
{
// Extract just the opening tag of the HTML element (omitting the closing tag and any contents,
// including contained HTML elements)
var match = s_tagRegex.Match(outerHtml);
Match match = s_tagRegex.Match(outerHtml);

// Extract all the attribute specifications within the HTML element opening tag
var attribMatches = s_attribsRegex.Matches(match.Value);
MatchCollection attribMatches = s_attribsRegex.Matches(match.Value);

foreach (Match attribMatch in attribMatches)
{
// Extract the name and value for this attribute (allowing for variations like single/double/no
// quotes, and no value at all)
var nvMatches = s_attribNameValueRegex.Match(attribMatch.Value);
Match nvMatches = s_attribNameValueRegex.Match(attribMatch.Value);
Debug.Assert(nvMatches.Groups.Count == 5);

// Name is always captured by group #1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal override void ProcessResponse(HttpResponseMessage response)
{
ArgumentNullException.ThrowIfNull(response);

var baseResponseStream = StreamHelper.GetResponseStream(response);
Stream baseResponseStream = StreamHelper.GetResponseStream(response);

if (ShouldWriteToPipeline)
{
Expand All @@ -89,22 +89,17 @@ internal override void ProcessResponse(HttpResponseMessage response)
}
else
{
// determine the response type
// Determine the response type
RestReturnType returnType = CheckReturnType(response);

// Try to get the response encoding from the ContentType header.
Encoding encoding = null;
string charSet = response.Content.Headers.ContentType?.CharSet;
if (!string.IsNullOrEmpty(charSet))
{
StreamHelper.TryGetEncoding(charSet, out encoding);
}
string charSet = WebResponseHelper.GetCharacterSet(response);

string str = StreamHelper.DecodeStream(responseStream, charSet, out Encoding encoding);

object obj = null;
Exception ex = null;

string str = StreamHelper.DecodeStream(responseStream, ref encoding);

string encodingVerboseName;
try
{
Expand Down Expand Up @@ -315,7 +310,7 @@ private static bool TryConvertToJson(string json, out object obj, ref Exception
}
catch (JsonException ex)
{
var msg = string.Format(System.Globalization.CultureInfo.CurrentCulture, WebCmdletStrings.JsonDeserializationFailed, ex.Message);
string msg = string.Format(System.Globalization.CultureInfo.CurrentCulture, WebCmdletStrings.JsonDeserializationFailed, ex.Message);
exRef = new ArgumentException(msg, ex);
obj = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,20 +609,15 @@ protected override void ProcessRecord()
HttpResponseException httpEx = new(message, response);
ErrorRecord er = new(httpEx, "WebCmdletWebResponseException", ErrorCategory.InvalidOperation, request);
string detailMsg = string.Empty;
StreamReader reader = null;
try
{
reader = new StreamReader(StreamHelper.GetResponseStream(response));
detailMsg = FormatErrorMessage(reader.ReadToEnd(), contentType);
string error = StreamHelper.GetResponseString(response);
detailMsg = FormatErrorMessage(error, contentType);
}
catch
{
// Catch all
}
finally
{
reader?.Dispose();
}

if (!string.IsNullOrEmpty(detailMsg))
{
Expand Down Expand Up @@ -1791,7 +1786,7 @@ private static string FormatErrorMessage(string error, string contentType)
if (string.IsNullOrEmpty(formattedError))
{
// Remove HTML tags making it easier to read
formattedError = System.Text.RegularExpressions.Regex.Replace(error, "<[^>]*>", string.Empty);
formattedError = Regex.Replace(error, "<[^>]*>", string.Empty);
}

return formattedError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands
{
internal static class WebResponseHelper
{
internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType.CharSet;
internal static string GetCharacterSet(HttpResponseMessage response) => response.Content.Headers.ContentType?.CharSet;

internal static Dictionary<string, IEnumerable<string>> GetHeadersDictionary(HttpResponseMessage response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down