jellyfin/Emby.Dlna/Api/DlnaServerService.cs

384 lines
16 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2020-04-02 16:49:58 +02:00
using System.Diagnostics.CodeAnalysis;
2014-04-10 17:06:54 +02:00
using System.IO;
2019-01-13 20:16:19 +01:00
using System.Text;
2014-04-10 17:06:54 +02:00
using System.Threading.Tasks;
2019-01-13 20:16:19 +01:00
using Emby.Dlna.Main;
2018-09-12 19:26:21 +02:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
2019-01-13 20:16:19 +01:00
using MediaBrowser.Controller.Dlna;
2018-09-12 19:26:21 +02:00
using MediaBrowser.Controller.Net;
2019-01-13 20:16:19 +01:00
using MediaBrowser.Model.Services;
2014-04-10 17:06:54 +02:00
2018-09-12 19:26:21 +02:00
namespace Emby.Dlna.Api
2014-04-10 17:06:54 +02:00
{
[Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
[Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
public class GetDescriptionXml
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
[Route("/Dlna/{UuId}/contentdirectory/contentdirectory", "GET", Summary = "Gets dlna content directory xml")]
2014-04-10 17:06:54 +02:00
public class GetContentDirectory
{
2015-01-30 22:14:08 +01:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-04-10 17:06:54 +02:00
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager.xml", "GET", Summary = "Gets dlna connection manager xml")]
[Route("/Dlna/{UuId}/connectionmanager/connectionmanager", "GET", Summary = "Gets dlna connection manager xml")]
2014-05-21 02:56:24 +02:00
public class GetConnnectionManager
{
2015-01-30 22:14:08 +01:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-05-21 02:56:24 +02:00
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar.xml", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
[Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
2015-01-30 18:58:38 +01:00
public class GetMediaReceiverRegistrar
{
2015-01-30 22:14:08 +01:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2015-01-30 18:58:38 +01:00
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/contentdirectory/control", "POST", Summary = "Processes a control request")]
2014-05-21 02:56:24 +02:00
public class ProcessContentDirectoryControlRequest : IRequiresRequestStream
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
public Stream RequestStream { get; set; }
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/connectionmanager/control", "POST", Summary = "Processes a control request")]
2014-05-21 02:56:24 +02:00
public class ProcessConnectionManagerControlRequest : IRequiresRequestStream
2014-04-10 17:06:54 +02:00
{
2014-04-18 07:03:01 +02:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
2014-04-20 07:21:08 +02:00
2014-04-10 17:06:54 +02:00
public Stream RequestStream { get; set; }
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/control", "POST", Summary = "Processes a control request")]
2015-01-30 18:58:38 +01:00
public class ProcessMediaReceiverRegistrarControlRequest : IRequiresRequestStream
{
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UuId { get; set; }
public Stream RequestStream { get; set; }
}
2017-08-30 20:52:29 +02:00
[Route("/Dlna/{UuId}/mediareceiverregistrar/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/mediareceiverregistrar/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2015-01-30 18:58:38 +01:00
public class ProcessMediaReceiverRegistrarEventRequest
{
2017-08-30 20:52:29 +02:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2015-01-30 18:58:38 +01:00
public string UuId { get; set; }
}
2017-08-30 20:52:29 +02:00
[Route("/Dlna/{UuId}/contentdirectory/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/contentdirectory/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2014-05-21 03:15:46 +02:00
public class ProcessContentDirectoryEventRequest
{
2017-08-30 20:52:29 +02:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2014-05-21 03:15:46 +02:00
public string UuId { get; set; }
}
2017-08-30 20:52:29 +02:00
[Route("/Dlna/{UuId}/connectionmanager/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
[Route("/Dlna/{UuId}/connectionmanager/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
2014-05-21 03:15:46 +02:00
public class ProcessConnectionManagerEventRequest
2014-04-20 07:21:08 +02:00
{
2017-08-30 20:52:29 +02:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
2014-04-20 07:21:08 +02:00
public string UuId { get; set; }
}
2015-01-30 22:14:08 +01:00
[Route("/Dlna/{UuId}/icons/{Filename}", "GET", Summary = "Gets a server icon")]
2014-04-16 07:08:12 +02:00
[Route("/Dlna/icons/{Filename}", "GET", Summary = "Gets a server icon")]
public class GetIcon
{
2018-09-12 19:26:21 +02:00
[ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-01-30 22:14:08 +01:00
public string UuId { get; set; }
2016-11-04 09:31:05 +01:00
2014-04-16 07:08:12 +02:00
[ApiMember(Name = "Filename", Description = "The icon filename", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Filename { get; set; }
}
2018-09-12 19:26:21 +02:00
public class DlnaServerService : IService, IRequiresRequest
2014-04-10 17:06:54 +02:00
{
2015-05-13 06:16:33 +02:00
private const string XMLContentType = "text/xml; charset=UTF-8";
2015-05-12 01:04:16 +02:00
private readonly IDlnaManager _dlnaManager;
private readonly IHttpResultFactory _resultFactory;
private readonly IServerConfigurationManager _configurationManager;
2018-09-12 19:26:21 +02:00
public IRequest Request { get; set; }
private IContentDirectory ContentDirectory => DlnaEntryPoint.Current.ContentDirectory;
2018-09-12 19:26:21 +02:00
private IConnectionManager ConnectionManager => DlnaEntryPoint.Current.ConnectionManager;
2018-09-12 19:26:21 +02:00
private IMediaReceiverRegistrar MediaReceiverRegistrar => DlnaEntryPoint.Current.MediaReceiverRegistrar;
2018-09-12 19:26:21 +02:00
public DlnaServerService(
IDlnaManager dlnaManager,
IHttpResultFactory httpResultFactory,
IServerConfigurationManager configurationManager)
2014-04-10 17:06:54 +02:00
{
_dlnaManager = dlnaManager;
2018-09-12 19:26:21 +02:00
_resultFactory = httpResultFactory;
_configurationManager = configurationManager;
2018-09-12 19:26:21 +02:00
}
private string GetHeader(string name)
{
return Request.Headers[name];
2014-04-10 17:06:54 +02:00
}
public object Get(GetDescriptionXml request)
{
2015-01-30 22:14:08 +01:00
var url = Request.AbsoluteUri;
var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, request.UuId, serverAddress);
2014-04-10 17:06:54 +02:00
2018-09-12 19:26:21 +02:00
var cacheLength = TimeSpan.FromDays(1);
var cacheKey = Request.RawUrl.GetMD5();
var bytes = Encoding.UTF8.GetBytes(xml);
return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, XMLContentType, () => Task.FromResult<Stream>(new MemoryStream(bytes)));
2014-04-10 17:06:54 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2014-04-10 17:06:54 +02:00
public object Get(GetContentDirectory request)
{
var xml = ContentDirectory.GetServiceXml();
2014-04-10 17:06:54 +02:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2014-04-10 17:06:54 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2015-01-30 18:58:38 +01:00
public object Get(GetMediaReceiverRegistrar request)
{
var xml = MediaReceiverRegistrar.GetServiceXml();
2015-01-30 18:58:38 +01:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2015-01-30 18:58:38 +01:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2014-05-21 02:56:24 +02:00
public object Get(GetConnnectionManager request)
{
var xml = ConnectionManager.GetServiceXml();
2014-05-21 02:56:24 +02:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, xml, XMLContentType);
2014-05-21 02:56:24 +02:00
}
2020-01-21 17:59:41 +01:00
public async Task<object> Post(ProcessMediaReceiverRegistrarControlRequest request)
2014-05-21 02:56:24 +02:00
{
2020-01-21 17:59:41 +01:00
var response = await PostAsync(request.RequestStream, MediaReceiverRegistrar).ConfigureAwait(false);
2014-05-21 02:56:24 +02:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2014-05-21 02:56:24 +02:00
}
2020-01-21 17:59:41 +01:00
public async Task<object> Post(ProcessContentDirectoryControlRequest request)
2015-01-30 18:58:38 +01:00
{
2020-01-21 17:59:41 +01:00
var response = await PostAsync(request.RequestStream, ContentDirectory).ConfigureAwait(false);
2015-01-30 18:58:38 +01:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2015-01-30 18:58:38 +01:00
}
2020-01-21 17:59:41 +01:00
public async Task<object> Post(ProcessConnectionManagerControlRequest request)
2014-04-10 17:06:54 +02:00
{
2020-01-21 17:59:41 +01:00
var response = await PostAsync(request.RequestStream, ConnectionManager).ConfigureAwait(false);
2014-04-10 17:06:54 +02:00
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
2014-04-10 17:06:54 +02:00
}
2020-01-21 17:59:41 +01:00
private Task<ControlResponse> PostAsync(Stream requestStream, IUpnpService service)
2014-04-10 17:06:54 +02:00
{
var id = GetPathValue(2).ToString();
2014-04-20 07:21:08 +02:00
2020-01-21 17:59:41 +01:00
return service.ProcessControlRequestAsync(new ControlRequest
2014-04-10 17:06:54 +02:00
{
Headers = Request.Headers,
2016-11-04 09:31:05 +01:00
InputXml = requestStream,
TargetServerUuId = id,
RequestedUrl = Request.AbsoluteUri
});
2014-04-16 07:08:12 +02:00
}
2014-04-10 17:06:54 +02:00
// Copied from MediaBrowser.Api/BaseApiService.cs
// TODO: Remove code duplication
/// <summary>
/// Gets the path segment at the specified index.
/// </summary>
/// <param name="index">The index of the path segment.</param>
/// <returns>The path segment at the specified index.</returns>
/// <exception cref="IndexOutOfRangeException" >Path doesn't contain enough segments.</exception>
/// <exception cref="InvalidDataException" >Path doesn't start with the base url.</exception>
protected internal ReadOnlySpan<char> GetPathValue(int index)
2014-04-16 07:08:12 +02:00
{
static void ThrowIndexOutOfRangeException()
2019-12-05 17:44:46 +01:00
=> throw new IndexOutOfRangeException("Path doesn't contain enough segments.");
2018-09-12 19:26:21 +02:00
static void ThrowInvalidDataException()
2019-12-05 17:44:46 +01:00
=> throw new InvalidDataException("Path doesn't start with the base url.");
ReadOnlySpan<char> path = Request.PathInfo;
// Remove the protocol part from the url
int pos = path.LastIndexOf("://");
if (pos != -1)
{
path = path.Slice(pos + 3);
}
// Remove the query string
pos = path.LastIndexOf('?');
if (pos != -1)
{
path = path.Slice(0, pos);
}
// Remove the domain
pos = path.IndexOf('/');
if (pos != -1)
{
path = path.Slice(pos);
}
// Remove base url
string baseUrl = _configurationManager.Configuration.BaseUrl;
int baseUrlLen = baseUrl.Length;
if (baseUrlLen != 0)
2014-04-16 07:08:12 +02:00
{
if (path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase))
{
path = path.Slice(baseUrlLen);
}
else
{
// The path doesn't start with the base url,
// how did we get here?
ThrowInvalidDataException();
}
2018-09-12 19:26:21 +02:00
}
2014-04-16 07:08:12 +02:00
// Remove leading /
path = path.Slice(1);
2018-09-12 19:26:21 +02:00
// Backwards compatibility
const string Emby = "emby/";
if (path.StartsWith(Emby, StringComparison.OrdinalIgnoreCase))
{
path = path.Slice(Emby.Length);
}
2018-09-12 19:26:21 +02:00
const string MediaBrowser = "mediabrowser/";
if (path.StartsWith(MediaBrowser, StringComparison.OrdinalIgnoreCase))
{
path = path.Slice(MediaBrowser.Length);
}
2018-09-12 19:26:21 +02:00
// Skip segments until we are at the right index
for (int i = 0; i < index; i++)
2018-09-12 19:26:21 +02:00
{
pos = path.IndexOf('/');
if (pos == -1)
{
ThrowIndexOutOfRangeException();
}
path = path.Slice(pos + 1);
2014-04-16 07:08:12 +02:00
}
2018-09-12 19:26:21 +02:00
// Remove the rest
pos = path.IndexOf('/');
if (pos != -1)
{
path = path.Slice(0, pos);
}
2018-09-12 19:26:21 +02:00
return path;
2018-09-12 19:26:21 +02:00
}
public object Get(GetIcon request)
{
2019-01-27 12:03:43 +01:00
var contentType = "image/" + Path.GetExtension(request.Filename)
.TrimStart('.')
.ToLowerInvariant();
2018-09-12 19:26:21 +02:00
var cacheLength = TimeSpan.FromDays(365);
var cacheKey = Request.RawUrl.GetMD5();
2019-01-13 21:37:13 +01:00
return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, contentType, () => Task.FromResult(_dlnaManager.GetIcon(request.Filename).Stream));
2014-04-16 07:08:12 +02:00
}
2014-04-20 07:21:08 +02:00
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Subscribe(ProcessContentDirectoryEventRequest request)
2014-05-21 03:15:46 +02:00
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(ContentDirectory);
2014-05-21 03:15:46 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Subscribe(ProcessConnectionManagerEventRequest request)
2014-05-21 03:15:46 +02:00
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(ConnectionManager);
2014-05-21 03:15:46 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Subscribe(ProcessMediaReceiverRegistrarEventRequest request)
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(MediaReceiverRegistrar);
2017-08-30 20:52:29 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Unsubscribe(ProcessContentDirectoryEventRequest request)
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(ContentDirectory);
2017-08-30 20:52:29 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Unsubscribe(ProcessConnectionManagerEventRequest request)
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(ConnectionManager);
2017-08-30 20:52:29 +02:00
}
2020-04-02 16:49:58 +02:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2017-08-30 20:52:29 +02:00
public object Unsubscribe(ProcessMediaReceiverRegistrarEventRequest request)
2015-01-30 18:58:38 +01:00
{
2018-09-12 19:26:21 +02:00
return ProcessEventRequest(MediaReceiverRegistrar);
2015-01-30 18:58:38 +01:00
}
2014-05-21 03:15:46 +02:00
private object ProcessEventRequest(IEventManager eventManager)
2014-04-20 07:21:08 +02:00
{
var subscriptionId = GetHeader("SID");
if (string.Equals(Request.Verb, "SUBSCRIBE", StringComparison.OrdinalIgnoreCase))
{
2017-07-20 22:37:13 +02:00
var notificationType = GetHeader("NT");
var callback = GetHeader("CALLBACK");
var timeoutString = GetHeader("TIMEOUT");
2014-04-20 07:21:08 +02:00
if (string.IsNullOrEmpty(notificationType))
{
2017-10-04 20:51:26 +02:00
return GetSubscriptionResponse(eventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callback));
2014-04-20 07:21:08 +02:00
}
2017-07-20 22:37:13 +02:00
return GetSubscriptionResponse(eventManager.CreateEventSubscription(notificationType, timeoutString, callback));
2014-04-20 07:21:08 +02:00
}
2014-05-21 03:15:46 +02:00
return GetSubscriptionResponse(eventManager.CancelEventSubscription(subscriptionId));
2014-04-20 07:21:08 +02:00
}
2014-04-21 18:02:30 +02:00
private object GetSubscriptionResponse(EventSubscriptionResponse response)
2014-04-20 07:21:08 +02:00
{
2018-09-12 19:26:21 +02:00
return _resultFactory.GetResult(Request, response.Content, response.ContentType, response.Headers);
2014-04-20 07:21:08 +02:00
}
2014-04-10 17:06:54 +02:00
}
}