DlnaServerService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Emby.Dlna.Main;
  8. using MediaBrowser.Common.Extensions;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Dlna;
  11. using MediaBrowser.Controller.Net;
  12. using MediaBrowser.Model.Services;
  13. using Microsoft.AspNetCore.Http;
  14. namespace Emby.Dlna.Api
  15. {
  16. [Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
  17. [Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
  18. public class GetDescriptionXml
  19. {
  20. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  21. public string UuId { get; set; }
  22. }
  23. [Route("/Dlna/{UuId}/contentdirectory/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
  24. [Route("/Dlna/{UuId}/contentdirectory/contentdirectory", "GET", Summary = "Gets dlna content directory xml")]
  25. public class GetContentDirectory
  26. {
  27. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  28. public string UuId { get; set; }
  29. }
  30. [Route("/Dlna/{UuId}/connectionmanager/connectionmanager.xml", "GET", Summary = "Gets dlna connection manager xml")]
  31. [Route("/Dlna/{UuId}/connectionmanager/connectionmanager", "GET", Summary = "Gets dlna connection manager xml")]
  32. public class GetConnnectionManager
  33. {
  34. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  35. public string UuId { get; set; }
  36. }
  37. [Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar.xml", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
  38. [Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
  39. public class GetMediaReceiverRegistrar
  40. {
  41. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  42. public string UuId { get; set; }
  43. }
  44. [Route("/Dlna/{UuId}/contentdirectory/control", "POST", Summary = "Processes a control request")]
  45. public class ProcessContentDirectoryControlRequest : IRequiresRequestStream
  46. {
  47. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  48. public string UuId { get; set; }
  49. public Stream RequestStream { get; set; }
  50. }
  51. [Route("/Dlna/{UuId}/connectionmanager/control", "POST", Summary = "Processes a control request")]
  52. public class ProcessConnectionManagerControlRequest : IRequiresRequestStream
  53. {
  54. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  55. public string UuId { get; set; }
  56. public Stream RequestStream { get; set; }
  57. }
  58. [Route("/Dlna/{UuId}/mediareceiverregistrar/control", "POST", Summary = "Processes a control request")]
  59. public class ProcessMediaReceiverRegistrarControlRequest : IRequiresRequestStream
  60. {
  61. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  62. public string UuId { get; set; }
  63. public Stream RequestStream { get; set; }
  64. }
  65. [Route("/Dlna/{UuId}/mediareceiverregistrar/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  66. [Route("/Dlna/{UuId}/mediareceiverregistrar/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  67. public class ProcessMediaReceiverRegistrarEventRequest
  68. {
  69. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  70. public string UuId { get; set; }
  71. }
  72. [Route("/Dlna/{UuId}/contentdirectory/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  73. [Route("/Dlna/{UuId}/contentdirectory/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  74. public class ProcessContentDirectoryEventRequest
  75. {
  76. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  77. public string UuId { get; set; }
  78. }
  79. [Route("/Dlna/{UuId}/connectionmanager/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  80. [Route("/Dlna/{UuId}/connectionmanager/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  81. public class ProcessConnectionManagerEventRequest
  82. {
  83. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  84. public string UuId { get; set; }
  85. }
  86. [Route("/Dlna/{UuId}/icons/{Filename}", "GET", Summary = "Gets a server icon")]
  87. [Route("/Dlna/icons/{Filename}", "GET", Summary = "Gets a server icon")]
  88. public class GetIcon
  89. {
  90. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  91. public string UuId { get; set; }
  92. [ApiMember(Name = "Filename", Description = "The icon filename", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  93. public string Filename { get; set; }
  94. }
  95. public class DlnaServerService : IService
  96. {
  97. private const string XMLContentType = "text/xml; charset=UTF-8";
  98. private readonly IDlnaManager _dlnaManager;
  99. private readonly IHttpResultFactory _resultFactory;
  100. private readonly IServerConfigurationManager _configurationManager;
  101. public IRequest Request { get; set; }
  102. private IContentDirectory ContentDirectory => DlnaEntryPoint.Current.ContentDirectory;
  103. private IConnectionManager ConnectionManager => DlnaEntryPoint.Current.ConnectionManager;
  104. private IMediaReceiverRegistrar MediaReceiverRegistrar => DlnaEntryPoint.Current.MediaReceiverRegistrar;
  105. public DlnaServerService(
  106. IDlnaManager dlnaManager,
  107. IHttpResultFactory httpResultFactory,
  108. IServerConfigurationManager configurationManager,
  109. IHttpContextAccessor httpContextAccessor)
  110. {
  111. _dlnaManager = dlnaManager;
  112. _resultFactory = httpResultFactory;
  113. _configurationManager = configurationManager;
  114. Request = httpContextAccessor?.HttpContext.GetServiceStackRequest() ?? throw new ArgumentNullException(nameof(httpContextAccessor));
  115. }
  116. private string GetHeader(string name)
  117. {
  118. return Request.Headers[name];
  119. }
  120. public object Get(GetDescriptionXml request)
  121. {
  122. var url = Request.AbsoluteUri;
  123. var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
  124. var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, request.UuId, serverAddress);
  125. var cacheLength = TimeSpan.FromDays(1);
  126. var cacheKey = Request.RawUrl.GetMD5();
  127. var bytes = Encoding.UTF8.GetBytes(xml);
  128. return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, XMLContentType, () => Task.FromResult<Stream>(new MemoryStream(bytes)));
  129. }
  130. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  131. public object Get(GetContentDirectory request)
  132. {
  133. var xml = ContentDirectory.GetServiceXml();
  134. return _resultFactory.GetResult(Request, xml, XMLContentType);
  135. }
  136. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  137. public object Get(GetMediaReceiverRegistrar request)
  138. {
  139. var xml = MediaReceiverRegistrar.GetServiceXml();
  140. return _resultFactory.GetResult(Request, xml, XMLContentType);
  141. }
  142. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  143. public object Get(GetConnnectionManager request)
  144. {
  145. var xml = ConnectionManager.GetServiceXml();
  146. return _resultFactory.GetResult(Request, xml, XMLContentType);
  147. }
  148. public async Task<object> Post(ProcessMediaReceiverRegistrarControlRequest request)
  149. {
  150. var response = await PostAsync(request.RequestStream, MediaReceiverRegistrar).ConfigureAwait(false);
  151. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  152. }
  153. public async Task<object> Post(ProcessContentDirectoryControlRequest request)
  154. {
  155. var response = await PostAsync(request.RequestStream, ContentDirectory).ConfigureAwait(false);
  156. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  157. }
  158. public async Task<object> Post(ProcessConnectionManagerControlRequest request)
  159. {
  160. var response = await PostAsync(request.RequestStream, ConnectionManager).ConfigureAwait(false);
  161. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  162. }
  163. private Task<ControlResponse> PostAsync(Stream requestStream, IUpnpService service)
  164. {
  165. var id = GetPathValue(2).ToString();
  166. return service.ProcessControlRequestAsync(new ControlRequest
  167. {
  168. Headers = Request.Headers,
  169. InputXml = requestStream,
  170. TargetServerUuId = id,
  171. RequestedUrl = Request.AbsoluteUri
  172. });
  173. }
  174. // Copied from MediaBrowser.Api/BaseApiService.cs
  175. // TODO: Remove code duplication
  176. /// <summary>
  177. /// Gets the path segment at the specified index.
  178. /// </summary>
  179. /// <param name="index">The index of the path segment.</param>
  180. /// <returns>The path segment at the specified index.</returns>
  181. /// <exception cref="IndexOutOfRangeException" >Path doesn't contain enough segments.</exception>
  182. /// <exception cref="InvalidDataException" >Path doesn't start with the base url.</exception>
  183. protected internal ReadOnlySpan<char> GetPathValue(int index)
  184. {
  185. static void ThrowIndexOutOfRangeException()
  186. => throw new IndexOutOfRangeException("Path doesn't contain enough segments.");
  187. static void ThrowInvalidDataException()
  188. => throw new InvalidDataException("Path doesn't start with the base url.");
  189. ReadOnlySpan<char> path = Request.PathInfo;
  190. // Remove the protocol part from the url
  191. int pos = path.LastIndexOf("://");
  192. if (pos != -1)
  193. {
  194. path = path.Slice(pos + 3);
  195. }
  196. // Remove the query string
  197. pos = path.LastIndexOf('?');
  198. if (pos != -1)
  199. {
  200. path = path.Slice(0, pos);
  201. }
  202. // Remove the domain
  203. pos = path.IndexOf('/');
  204. if (pos != -1)
  205. {
  206. path = path.Slice(pos);
  207. }
  208. // Remove base url
  209. string baseUrl = _configurationManager.Configuration.BaseUrl;
  210. int baseUrlLen = baseUrl.Length;
  211. if (baseUrlLen != 0)
  212. {
  213. if (path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase))
  214. {
  215. path = path.Slice(baseUrlLen);
  216. }
  217. else
  218. {
  219. // The path doesn't start with the base url,
  220. // how did we get here?
  221. ThrowInvalidDataException();
  222. }
  223. }
  224. // Remove leading /
  225. path = path.Slice(1);
  226. // Backwards compatibility
  227. const string Emby = "emby/";
  228. if (path.StartsWith(Emby, StringComparison.OrdinalIgnoreCase))
  229. {
  230. path = path.Slice(Emby.Length);
  231. }
  232. const string MediaBrowser = "mediabrowser/";
  233. if (path.StartsWith(MediaBrowser, StringComparison.OrdinalIgnoreCase))
  234. {
  235. path = path.Slice(MediaBrowser.Length);
  236. }
  237. // Skip segments until we are at the right index
  238. for (int i = 0; i < index; i++)
  239. {
  240. pos = path.IndexOf('/');
  241. if (pos == -1)
  242. {
  243. ThrowIndexOutOfRangeException();
  244. }
  245. path = path.Slice(pos + 1);
  246. }
  247. // Remove the rest
  248. pos = path.IndexOf('/');
  249. if (pos != -1)
  250. {
  251. path = path.Slice(0, pos);
  252. }
  253. return path;
  254. }
  255. public object Get(GetIcon request)
  256. {
  257. var contentType = "image/" + Path.GetExtension(request.Filename)
  258. .TrimStart('.')
  259. .ToLowerInvariant();
  260. var cacheLength = TimeSpan.FromDays(365);
  261. var cacheKey = Request.RawUrl.GetMD5();
  262. return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, contentType, () => Task.FromResult(_dlnaManager.GetIcon(request.Filename).Stream));
  263. }
  264. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  265. public object Subscribe(ProcessContentDirectoryEventRequest request)
  266. {
  267. return ProcessEventRequest(ContentDirectory);
  268. }
  269. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  270. public object Subscribe(ProcessConnectionManagerEventRequest request)
  271. {
  272. return ProcessEventRequest(ConnectionManager);
  273. }
  274. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  275. public object Subscribe(ProcessMediaReceiverRegistrarEventRequest request)
  276. {
  277. return ProcessEventRequest(MediaReceiverRegistrar);
  278. }
  279. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  280. public object Unsubscribe(ProcessContentDirectoryEventRequest request)
  281. {
  282. return ProcessEventRequest(ContentDirectory);
  283. }
  284. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  285. public object Unsubscribe(ProcessConnectionManagerEventRequest request)
  286. {
  287. return ProcessEventRequest(ConnectionManager);
  288. }
  289. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  290. public object Unsubscribe(ProcessMediaReceiverRegistrarEventRequest request)
  291. {
  292. return ProcessEventRequest(MediaReceiverRegistrar);
  293. }
  294. private object ProcessEventRequest(IEventManager eventManager)
  295. {
  296. var subscriptionId = GetHeader("SID");
  297. if (string.Equals(Request.Verb, "SUBSCRIBE", StringComparison.OrdinalIgnoreCase))
  298. {
  299. var notificationType = GetHeader("NT");
  300. var callback = GetHeader("CALLBACK");
  301. var timeoutString = GetHeader("TIMEOUT");
  302. if (string.IsNullOrEmpty(notificationType))
  303. {
  304. return GetSubscriptionResponse(eventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callback));
  305. }
  306. return GetSubscriptionResponse(eventManager.CreateEventSubscription(notificationType, timeoutString, callback));
  307. }
  308. return GetSubscriptionResponse(eventManager.CancelEventSubscription(subscriptionId));
  309. }
  310. private object GetSubscriptionResponse(EventSubscriptionResponse response)
  311. {
  312. return _resultFactory.GetResult(Request, response.Content, response.ContentType, response.Headers);
  313. }
  314. }
  315. }