jellyfin-server/Emby.Dlna/Service/BaseControlHandler.cs

249 lines
7.7 KiB
C#
Raw Normal View History

2016-10-29 22:22:20 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dlna;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.Server;
2016-10-29 22:22:20 +00:00
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
2016-11-04 08:31:05 +00:00
using System.IO;
2016-10-29 22:22:20 +00:00
using System.Linq;
using System.Text;
using System.Xml;
2016-11-04 08:31:05 +00:00
using Emby.Dlna.Didl;
using MediaBrowser.Model.Xml;
2016-10-29 22:22:20 +00:00
2016-10-29 22:34:54 +00:00
namespace Emby.Dlna.Service
2016-10-29 22:22:20 +00:00
{
public abstract class BaseControlHandler
{
private const string NS_SOAPENV = "http://schemas.xmlsoap.org/soap/envelope/";
protected readonly IServerConfigurationManager Config;
protected readonly ILogger Logger;
2016-11-04 08:31:05 +00:00
protected readonly IXmlReaderSettingsFactory XmlReaderSettingsFactory;
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
protected BaseControlHandler(IServerConfigurationManager config, ILogger logger, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
2016-10-29 22:22:20 +00:00
{
Config = config;
Logger = logger;
2016-11-04 08:31:05 +00:00
XmlReaderSettingsFactory = xmlReaderSettingsFactory;
2016-10-29 22:22:20 +00:00
}
public ControlResponse ProcessControlRequest(ControlRequest request)
{
try
{
var enableDebugLogging = Config.GetDlnaConfiguration().EnableDebugLog;
if (enableDebugLogging)
{
LogRequest(request);
}
var response = ProcessControlRequestInternal(request);
if (enableDebugLogging)
{
LogResponse(response);
}
return response;
}
catch (Exception ex)
{
Logger.ErrorException("Error processing control request", ex);
return new ControlErrorHandler().GetResponse(ex);
}
}
private ControlResponse ProcessControlRequestInternal(ControlRequest request)
{
2016-11-04 08:31:05 +00:00
ControlRequestInfo requestInfo = null;
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
using (var streamReader = new StreamReader(request.InputXml))
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
var readerSettings = XmlReaderSettingsFactory.Create(false);
readerSettings.CheckCharacters = false;
readerSettings.IgnoreProcessingInstructions = true;
readerSettings.IgnoreComments = true;
using (var reader = XmlReader.Create(streamReader, readerSettings))
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
requestInfo = ParseRequest(reader);
2016-10-29 22:22:20 +00:00
}
}
2016-11-04 08:31:05 +00:00
Logger.Debug("Received control request {0}", requestInfo.LocalName);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
var result = GetResult(requestInfo.LocalName, requestInfo.Headers);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
CloseOutput = false
};
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
StringWriter builder = new StringWriterWithEncoding(Encoding.UTF8);
2016-10-29 22:22:20 +00:00
2016-11-04 08:31:05 +00:00
using (XmlWriter writer = XmlWriter.Create(builder, settings))
2016-10-29 22:22:20 +00:00
{
2016-11-04 08:31:05 +00:00
writer.WriteStartDocument(true);
writer.WriteStartElement("SOAP-ENV", "Envelope", NS_SOAPENV);
writer.WriteAttributeString(string.Empty, "encodingStyle", NS_SOAPENV, "http://schemas.xmlsoap.org/soap/encoding/");
writer.WriteStartElement("SOAP-ENV", "Body", NS_SOAPENV);
writer.WriteStartElement("u", requestInfo.LocalName + "Response", requestInfo.NamespaceURI);
foreach (var i in result)
{
writer.WriteStartElement(i.Key);
writer.WriteString(i.Value);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
2016-10-29 22:22:20 +00:00
}
2016-11-04 08:31:05 +00:00
var xml = builder.ToString().Replace("xmlns:m=", "xmlns:u=");
2016-10-29 22:22:20 +00:00
var controlResponse = new ControlResponse
{
Xml = xml,
IsSuccessful = true
};
//Logger.Debug(xml);
controlResponse.Headers.Add("EXT", string.Empty);
return controlResponse;
}
2016-11-04 08:31:05 +00:00
private ControlRequestInfo ParseRequest(XmlReader reader)
{
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.LocalName)
{
case "Body":
{
using (var subReader = reader.ReadSubtree())
{
return ParseBodyTag(subReader);
}
}
default:
{
reader.Skip();
break;
}
}
}
else
{
reader.Read();
}
}
return new ControlRequestInfo();
}
private ControlRequestInfo ParseBodyTag(XmlReader reader)
{
var result = new ControlRequestInfo();
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{
result.LocalName = reader.LocalName;
result.NamespaceURI = reader.NamespaceURI;
using (var subReader = reader.ReadSubtree())
{
result.Headers = ParseFirstBodyChild(subReader);
return result;
}
}
else
{
reader.Read();
}
}
return result;
}
private Headers ParseFirstBodyChild(XmlReader reader)
{
var result = new Headers();
reader.MoveToContent();
reader.Read();
// Loop through each element
while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{
result.Add(reader.LocalName, reader.ReadElementContentAsString());
}
else
{
reader.Read();
}
}
return result;
}
private class ControlRequestInfo
{
public string LocalName;
public string NamespaceURI;
public Headers Headers = new Headers();
}
2016-10-29 22:22:20 +00:00
protected abstract IEnumerable<KeyValuePair<string, string>> GetResult(string methodName, Headers methodParams);
private void LogRequest(ControlRequest request)
{
var builder = new StringBuilder();
var headers = string.Join(", ", request.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value)).ToArray());
builder.AppendFormat("Headers: {0}", headers);
builder.AppendLine();
2016-11-04 23:57:21 +00:00
//builder.Append(request.InputXml);
2016-10-29 22:22:20 +00:00
Logger.LogMultiline("Control request", LogSeverity.Debug, builder);
}
private void LogResponse(ControlResponse response)
{
var builder = new StringBuilder();
var headers = string.Join(", ", response.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value)).ToArray());
builder.AppendFormat("Headers: {0}", headers);
builder.AppendLine();
builder.Append(response.Xml);
Logger.LogMultiline("Control response", LogSeverity.Debug, builder);
}
}
}