Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal override void ProcessResponse(HttpResponseMessage response)

if (ShouldWriteToPipeline)
{
using var responseStream = new BufferingStreamReader(baseResponseStream, _cancelToken.Token);
using BufferingStreamReader responseStream = new(baseResponseStream, _cancelToken.Token);

// First see if it is an RSS / ATOM feed, in which case we can
// stream it - unless the user has overridden it with a return type of "XML"
Expand All @@ -94,45 +94,44 @@ internal override void ProcessResponse(HttpResponseMessage response)
}
else
{
// Determine the response type
RestReturnType returnType = CheckReturnType(response);

// Try to get the response encoding from the ContentType header.
string? characterSet = WebResponseHelper.GetCharacterSet(response);

string str = StreamHelper.DecodeStream(responseStream, characterSet, out Encoding encoding, _cancelToken.Token);

object? obj = null;
Exception? ex = null;

string encodingVerboseName;
try
{
encodingVerboseName = string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName;
encodingVerboseName = encoding.HeaderName;
}
catch (NotSupportedException)
catch
{
encodingVerboseName = encoding.EncodingName;
encodingVerboseName = string.Empty;
}

// NOTE: Tests use this verbose output to verify the encoding.
WriteVerbose(string.Create(System.Globalization.CultureInfo.InvariantCulture, $"Content encoding: {encodingVerboseName}"));

// Determine the response type
RestReturnType returnType = CheckReturnType(response);

bool convertSuccess = false;
object? obj = null;
Exception? ex = null;

if (returnType == RestReturnType.Json)
{
convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
}
// default to try xml first since it's more common
// Default to try xml first since it's more common
else
{
convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
}

if (!convertSuccess)
{
// fallback to string
// Fallback to string
obj = str;
}

Expand Down Expand Up @@ -171,11 +170,8 @@ private static RestReturnType CheckReturnType(HttpResponseMessage response)

RestReturnType rt = RestReturnType.Detect;
string? contentType = ContentHelper.GetContentType(response);
if (string.IsNullOrEmpty(contentType))
{
rt = RestReturnType.Detect;
}
else if (ContentHelper.IsJson(contentType))

if (ContentHelper.IsJson(contentType))
{
rt = RestReturnType.Json;
}
Expand Down Expand Up @@ -272,7 +268,7 @@ private static bool TryConvertToXml(string xml, [NotNullWhen(true)] out object?
XmlReaderSettings settings = GetSecureXmlReaderSettings();
XmlReader xmlReader = XmlReader.Create(new StringReader(xml), settings);

var xmlDoc = new XmlDocument();
XmlDocument xmlDoc = new();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(xmlReader);

Expand Down