DlnaServerService.cs 17 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. namespace Emby.Dlna.Api
  14. {
  15. [Route("/Dlna/{UuId}/description.xml", "GET", Summary = "Gets dlna server info")]
  16. [Route("/Dlna/{UuId}/description", "GET", Summary = "Gets dlna server info")]
  17. public class GetDescriptionXml
  18. {
  19. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  20. public string UuId { get; set; }
  21. }
  22. [Route("/Dlna/{UuId}/contentdirectory/contentdirectory.xml", "GET", Summary = "Gets dlna content directory xml")]
  23. [Route("/Dlna/{UuId}/contentdirectory/contentdirectory", "GET", Summary = "Gets dlna content directory xml")]
  24. public class GetContentDirectory
  25. {
  26. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  27. public string UuId { get; set; }
  28. }
  29. [Route("/Dlna/{UuId}/connectionmanager/connectionmanager.xml", "GET", Summary = "Gets dlna connection manager xml")]
  30. [Route("/Dlna/{UuId}/connectionmanager/connectionmanager", "GET", Summary = "Gets dlna connection manager xml")]
  31. public class GetConnnectionManager
  32. {
  33. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  34. public string UuId { get; set; }
  35. }
  36. [Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar.xml", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
  37. [Route("/Dlna/{UuId}/mediareceiverregistrar/mediareceiverregistrar", "GET", Summary = "Gets dlna mediareceiverregistrar xml")]
  38. public class GetMediaReceiverRegistrar
  39. {
  40. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  41. public string UuId { get; set; }
  42. }
  43. [Route("/Dlna/{UuId}/contentdirectory/control", "POST", Summary = "Processes a control request")]
  44. public class ProcessContentDirectoryControlRequest : IRequiresRequestStream
  45. {
  46. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  47. public string UuId { get; set; }
  48. public Stream RequestStream { get; set; }
  49. }
  50. [Route("/Dlna/{UuId}/connectionmanager/control", "POST", Summary = "Processes a control request")]
  51. public class ProcessConnectionManagerControlRequest : IRequiresRequestStream
  52. {
  53. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  54. public string UuId { get; set; }
  55. public Stream RequestStream { get; set; }
  56. }
  57. [Route("/Dlna/{UuId}/mediareceiverregistrar/control", "POST", Summary = "Processes a control request")]
  58. public class ProcessMediaReceiverRegistrarControlRequest : IRequiresRequestStream
  59. {
  60. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
  61. public string UuId { get; set; }
  62. public Stream RequestStream { get; set; }
  63. }
  64. [Route("/Dlna/{UuId}/mediareceiverregistrar/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  65. [Route("/Dlna/{UuId}/mediareceiverregistrar/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  66. public class ProcessMediaReceiverRegistrarEventRequest
  67. {
  68. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  69. public string UuId { get; set; }
  70. }
  71. [Route("/Dlna/{UuId}/contentdirectory/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  72. [Route("/Dlna/{UuId}/contentdirectory/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  73. public class ProcessContentDirectoryEventRequest
  74. {
  75. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  76. public string UuId { get; set; }
  77. }
  78. [Route("/Dlna/{UuId}/connectionmanager/events", "SUBSCRIBE", Summary = "Processes an event subscription request")]
  79. [Route("/Dlna/{UuId}/connectionmanager/events", "UNSUBSCRIBE", Summary = "Processes an event subscription request")]
  80. public class ProcessConnectionManagerEventRequest
  81. {
  82. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "SUBSCRIBE,UNSUBSCRIBE")]
  83. public string UuId { get; set; }
  84. }
  85. [Route("/Dlna/{UuId}/icons/{Filename}", "GET", Summary = "Gets a server icon")]
  86. [Route("/Dlna/icons/{Filename}", "GET", Summary = "Gets a server icon")]
  87. public class GetIcon
  88. {
  89. [ApiMember(Name = "UuId", Description = "Server UuId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  90. public string UuId { get; set; }
  91. [ApiMember(Name = "Filename", Description = "The icon filename", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  92. public string Filename { get; set; }
  93. }
  94. public class DlnaServerService : IService, IRequiresRequest
  95. {
  96. private const string XMLContentType = "text/xml; charset=UTF-8";
  97. private readonly IDlnaManager _dlnaManager;
  98. private readonly IHttpResultFactory _resultFactory;
  99. private readonly IServerConfigurationManager _configurationManager;
  100. public IRequest Request { get; set; }
  101. private IContentDirectory ContentDirectory => DlnaEntryPoint.Current.ContentDirectory;
  102. private IConnectionManager ConnectionManager => DlnaEntryPoint.Current.ConnectionManager;
  103. private IMediaReceiverRegistrar MediaReceiverRegistrar => DlnaEntryPoint.Current.MediaReceiverRegistrar;
  104. public DlnaServerService(
  105. IDlnaManager dlnaManager,
  106. IHttpResultFactory httpResultFactory,
  107. IServerConfigurationManager configurationManager,
  108. IHttpContextAccessor httpContextAccessor)
  109. {
  110. _dlnaManager = dlnaManager;
  111. _resultFactory = httpResultFactory;
  112. _configurationManager = configurationManager;
  113. object request = httpContextAccessor?.HttpContext.Items["ServiceStackRequest"] ?? throw new ArgumentNullException(nameof(httpContextAccessor));
  114. Request = (IRequest)request;
  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. }