Rescue PlayTo function in case of malformed Xml response

(port from 10.8 fix)
This commit is contained in:
SeaEagle1 2023-05-12 17:53:27 +02:00
parent 47290a8c36
commit 9352a24374
No known key found for this signature in database
GPG Key ID: 93B2DF7C2289CC9C

View File

@ -2,9 +2,11 @@
using System; using System;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Net.Mime; using System.Net.Mime;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
@ -54,15 +56,34 @@ namespace Emby.Dlna.PlayTo
LoadOptions.None, LoadOptions.None,
cancellationToken).ConfigureAwait(false); cancellationToken).ConfigureAwait(false);
} }
catch (XmlException ex) catch (XmlException)
{ {
_logger.LogError(ex, "Failed to parse response"); // try correcting the Xml response with common errors
if (_logger.IsEnabled(LogLevel.Debug)) var xmlString = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
{
_logger.LogDebug("Malformed response: {Content}\n", await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false));
}
return null; // find and replace unescaped ampersands (&)
Regex regex = new Regex(@"(&(?![a-z]*;))");
xmlString = regex.Replace(xmlString, @"&");
try
{
// retry reading Xml
var xmlReader = new StringReader(xmlString);
return await XDocument.LoadAsync(
xmlReader,
LoadOptions.None,
cancellationToken).ConfigureAwait(false);
}
catch (XmlException ex)
{
_logger.LogError(ex, "Failed to parse response");
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogDebug("Malformed response: {Content}\n", xmlString);
}
return null;
}
} }
} }