DlnaServerService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. {
  109. _dlnaManager = dlnaManager;
  110. _resultFactory = httpResultFactory;
  111. _configurationManager = configurationManager;
  112. }
  113. private string GetHeader(string name)
  114. {
  115. return Request.Headers[name];
  116. }
  117. public object Get(GetDescriptionXml request)
  118. {
  119. var url = Request.AbsoluteUri;
  120. var serverAddress = url.Substring(0, url.IndexOf("/dlna/", StringComparison.OrdinalIgnoreCase));
  121. var xml = _dlnaManager.GetServerDescriptionXml(Request.Headers, request.UuId, serverAddress);
  122. var cacheLength = TimeSpan.FromDays(1);
  123. var cacheKey = Request.RawUrl.GetMD5();
  124. var bytes = Encoding.UTF8.GetBytes(xml);
  125. return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, XMLContentType, () => Task.FromResult<Stream>(new MemoryStream(bytes)));
  126. }
  127. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  128. public object Get(GetContentDirectory request)
  129. {
  130. var xml = ContentDirectory.GetServiceXml();
  131. return _resultFactory.GetResult(Request, xml, XMLContentType);
  132. }
  133. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  134. public object Get(GetMediaReceiverRegistrar request)
  135. {
  136. var xml = MediaReceiverRegistrar.GetServiceXml();
  137. return _resultFactory.GetResult(Request, xml, XMLContentType);
  138. }
  139. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  140. public object Get(GetConnnectionManager request)
  141. {
  142. var xml = ConnectionManager.GetServiceXml();
  143. return _resultFactory.GetResult(Request, xml, XMLContentType);
  144. }
  145. public async Task<object> Post(ProcessMediaReceiverRegistrarControlRequest request)
  146. {
  147. var response = await PostAsync(request.RequestStream, MediaReceiverRegistrar).ConfigureAwait(false);
  148. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  149. }
  150. public async Task<object> Post(ProcessContentDirectoryControlRequest request)
  151. {
  152. var response = await PostAsync(request.RequestStream, ContentDirectory).ConfigureAwait(false);
  153. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  154. }
  155. public async Task<object> Post(ProcessConnectionManagerControlRequest request)
  156. {
  157. var response = await PostAsync(request.RequestStream, ConnectionManager).ConfigureAwait(false);
  158. return _resultFactory.GetResult(Request, response.Xml, XMLContentType);
  159. }
  160. private Task<ControlResponse> PostAsync(Stream requestStream, IUpnpService service)
  161. {
  162. var id = GetPathValue(2).ToString();
  163. return service.ProcessControlRequestAsync(new ControlRequest
  164. {
  165. Headers = Request.Headers,
  166. InputXml = requestStream,
  167. TargetServerUuId = id,
  168. RequestedUrl = Request.AbsoluteUri
  169. });
  170. }
  171. // Copied from MediaBrowser.Api/BaseApiService.cs
  172. // TODO: Remove code duplication
  173. /// <summary>
  174. /// Gets the path segment at the specified index.
  175. /// </summary>
  176. /// <param name="index">The index of the path segment.</param>
  177. /// <returns>The path segment at the specified index.</returns>
  178. /// <exception cref="IndexOutOfRangeException" >Path doesn't contain enough segments.</exception>
  179. /// <exception cref="InvalidDataException" >Path doesn't start with the base url.</exception>
  180. protected internal ReadOnlySpan<char> GetPathValue(int index)
  181. {
  182. static void ThrowIndexOutOfRangeException()
  183. => throw new IndexOutOfRangeException("Path doesn't contain enough segments.");
  184. static void ThrowInvalidDataException()
  185. => throw new InvalidDataException("Path doesn't start with the base url.");
  186. ReadOnlySpan<char> path = Request.PathInfo;
  187. // Remove the protocol part from the url
  188. int pos = path.LastIndexOf("://");
  189. if (pos != -1)
  190. {
  191. path = path.Slice(pos + 3);
  192. }
  193. // Remove the query string
  194. pos = path.LastIndexOf('?');
  195. if (pos != -1)
  196. {
  197. path = path.Slice(0, pos);
  198. }
  199. // Remove the domain
  200. pos = path.IndexOf('/');
  201. if (pos != -1)
  202. {
  203. path = path.Slice(pos);
  204. }
  205. // Remove base url
  206. string baseUrl = _configurationManager.Configuration.BaseUrl;
  207. int baseUrlLen = baseUrl.Length;
  208. if (baseUrlLen != 0)
  209. {
  210. if (path.StartsWith(baseUrl, StringComparison.OrdinalIgnoreCase))
  211. {
  212. path = path.Slice(baseUrlLen);
  213. }
  214. else
  215. {
  216. // The path doesn't start with the base url,
  217. // how did we get here?
  218. ThrowInvalidDataException();
  219. }
  220. }
  221. // Remove leading /
  222. path = path.Slice(1);
  223. // Backwards compatibility
  224. const string Emby = "emby/";
  225. if (path.StartsWith(Emby, StringComparison.OrdinalIgnoreCase))
  226. {
  227. path = path.Slice(Emby.Length);
  228. }
  229. const string MediaBrowser = "mediabrowser/";
  230. if (path.StartsWith(MediaBrowser, StringComparison.OrdinalIgnoreCase))
  231. {
  232. path = path.Slice(MediaBrowser.Length);
  233. }
  234. // Skip segments until we are at the right index
  235. for (int i = 0; i < index; i++)
  236. {
  237. pos = path.IndexOf('/');
  238. if (pos == -1)
  239. {
  240. ThrowIndexOutOfRangeException();
  241. }
  242. path = path.Slice(pos + 1);
  243. }
  244. // Remove the rest
  245. pos = path.IndexOf('/');
  246. if (pos != -1)
  247. {
  248. path = path.Slice(0, pos);
  249. }
  250. return path;
  251. }
  252. public object Get(GetIcon request)
  253. {
  254. var contentType = "image/" + Path.GetExtension(request.Filename)
  255. .TrimStart('.')
  256. .ToLowerInvariant();
  257. var cacheLength = TimeSpan.FromDays(365);
  258. var cacheKey = Request.RawUrl.GetMD5();
  259. return _resultFactory.GetStaticResult(Request, cacheKey, null, cacheLength, contentType, () => Task.FromResult(_dlnaManager.GetIcon(request.Filename).Stream));
  260. }
  261. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  262. public object Subscribe(ProcessContentDirectoryEventRequest request)
  263. {
  264. return ProcessEventRequest(ContentDirectory);
  265. }
  266. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  267. public object Subscribe(ProcessConnectionManagerEventRequest request)
  268. {
  269. return ProcessEventRequest(ConnectionManager);
  270. }
  271. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  272. public object Subscribe(ProcessMediaReceiverRegistrarEventRequest request)
  273. {
  274. return ProcessEventRequest(MediaReceiverRegistrar);
  275. }
  276. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  277. public object Unsubscribe(ProcessContentDirectoryEventRequest request)
  278. {
  279. return ProcessEventRequest(ContentDirectory);
  280. }
  281. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  282. public object Unsubscribe(ProcessConnectionManagerEventRequest request)
  283. {
  284. return ProcessEventRequest(ConnectionManager);
  285. }
  286. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
  287. public object Unsubscribe(ProcessMediaReceiverRegistrarEventRequest request)
  288. {
  289. return ProcessEventRequest(MediaReceiverRegistrar);
  290. }
  291. private object ProcessEventRequest(IEventManager eventManager)
  292. {
  293. var subscriptionId = GetHeader("SID");
  294. if (string.Equals(Request.Verb, "SUBSCRIBE", StringComparison.OrdinalIgnoreCase))
  295. {
  296. var notificationType = GetHeader("NT");
  297. var callback = GetHeader("CALLBACK");
  298. var timeoutString = GetHeader("TIMEOUT");
  299. if (string.IsNullOrEmpty(notificationType))
  300. {
  301. return GetSubscriptionResponse(eventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callback));
  302. }
  303. return GetSubscriptionResponse(eventManager.CreateEventSubscription(notificationType, timeoutString, callback));
  304. }
  305. return GetSubscriptionResponse(eventManager.CancelEventSubscription(subscriptionId));
  306. }
  307. private object GetSubscriptionResponse(EventSubscriptionResponse response)
  308. {
  309. return _resultFactory.GetResult(Request, response.Content, response.ContentType, response.Headers);
  310. }
  311. }
  312. }