DescriptionXmlBuilder.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using Emby.Dlna.Common;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Extensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Security;
  9. using System.Text;
  10. namespace Emby.Dlna.Server
  11. {
  12. public class DescriptionXmlBuilder
  13. {
  14. private readonly DeviceProfile _profile;
  15. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  16. private readonly string _serverUdn;
  17. private readonly string _serverAddress;
  18. private readonly string _serverName;
  19. private readonly string _serverId;
  20. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
  21. {
  22. if (string.IsNullOrEmpty(serverUdn))
  23. {
  24. throw new ArgumentNullException("serverUdn");
  25. }
  26. if (string.IsNullOrEmpty(serverAddress))
  27. {
  28. throw new ArgumentNullException("serverAddress");
  29. }
  30. _profile = profile;
  31. _serverUdn = serverUdn;
  32. _serverAddress = serverAddress;
  33. _serverName = serverName;
  34. _serverId = serverId;
  35. }
  36. private bool EnableAbsoluteUrls
  37. {
  38. get { return false; }
  39. }
  40. public string GetXml()
  41. {
  42. var builder = new StringBuilder();
  43. builder.Append("<?xml version=\"1.0\"?>");
  44. builder.Append("<root");
  45. var attributes = _profile.XmlRootAttributes.ToList();
  46. attributes.Insert(0, new XmlAttribute
  47. {
  48. Name = "xmlns:dlna",
  49. Value = "urn:schemas-dlna-org:device-1-0"
  50. });
  51. attributes.Insert(0, new XmlAttribute
  52. {
  53. Name = "xmlns",
  54. Value = "urn:schemas-upnp-org:device-1-0"
  55. });
  56. foreach (var att in attributes)
  57. {
  58. builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
  59. }
  60. builder.Append(">");
  61. builder.Append("<specVersion>");
  62. builder.Append("<major>1</major>");
  63. builder.Append("<minor>0</minor>");
  64. builder.Append("</specVersion>");
  65. if (!EnableAbsoluteUrls)
  66. {
  67. builder.Append("<URLBase>" + Escape(_serverAddress) + "</URLBase>");
  68. }
  69. AppendDeviceInfo(builder);
  70. builder.Append("</root>");
  71. return builder.ToString();
  72. }
  73. private void AppendDeviceInfo(StringBuilder builder)
  74. {
  75. builder.Append("<device>");
  76. AppendDeviceProperties(builder);
  77. AppendIconList(builder);
  78. builder.Append("<presentationURL>" + Escape(_serverAddress) + "/web/index.html</presentationURL>");
  79. AppendServiceList(builder);
  80. builder.Append("</device>");
  81. }
  82. private static readonly char[] s_escapeChars = new char[]
  83. {
  84. '<',
  85. '>',
  86. '"',
  87. '\'',
  88. '&'
  89. };
  90. private static readonly string[] s_escapeStringPairs = new string[]
  91. {
  92. "<",
  93. "&lt;",
  94. ">",
  95. "&gt;",
  96. "\"",
  97. "&quot;",
  98. "'",
  99. "&apos;",
  100. "&",
  101. "&amp;"
  102. };
  103. private static string GetEscapeSequence(char c)
  104. {
  105. int num = s_escapeStringPairs.Length;
  106. for (int i = 0; i < num; i += 2)
  107. {
  108. string text = s_escapeStringPairs[i];
  109. string result = s_escapeStringPairs[i + 1];
  110. if (text[0] == c)
  111. {
  112. return result;
  113. }
  114. }
  115. return c.ToString();
  116. }
  117. /// <summary>Replaces invalid XML characters in a string with their valid XML equivalent.</summary>
  118. /// <returns>The input string with invalid characters replaced.</returns>
  119. /// <param name="str">The string within which to escape invalid characters. </param>
  120. public static string Escape(string str)
  121. {
  122. if (str == null)
  123. {
  124. return null;
  125. }
  126. StringBuilder stringBuilder = null;
  127. int length = str.Length;
  128. int num = 0;
  129. while (true)
  130. {
  131. int num2 = str.IndexOfAny(s_escapeChars, num);
  132. if (num2 == -1)
  133. {
  134. break;
  135. }
  136. if (stringBuilder == null)
  137. {
  138. stringBuilder = new StringBuilder();
  139. }
  140. stringBuilder.Append(str, num, num2 - num);
  141. stringBuilder.Append(GetEscapeSequence(str[num2]));
  142. num = num2 + 1;
  143. }
  144. if (stringBuilder == null)
  145. {
  146. return str;
  147. }
  148. stringBuilder.Append(str, num, length - num);
  149. return stringBuilder.ToString();
  150. }
  151. private void AppendDeviceProperties(StringBuilder builder)
  152. {
  153. builder.Append("<dlna:X_DLNACAP/>");
  154. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">DMS-1.50</dlna:X_DLNADOC>");
  155. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
  156. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  157. builder.Append("<friendlyName>" + Escape(GetFriendlyName()) + "</friendlyName>");
  158. builder.Append("<manufacturer>" + Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  159. builder.Append("<manufacturerURL>" + Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  160. builder.Append("<modelDescription>" + Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  161. builder.Append("<modelName>" + Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  162. builder.Append("<modelNumber>" + Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  163. builder.Append("<modelURL>" + Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  164. if (string.IsNullOrEmpty(_profile.SerialNumber))
  165. {
  166. builder.Append("<serialNumber>" + Escape(_serverId) + "</serialNumber>");
  167. }
  168. else
  169. {
  170. builder.Append("<serialNumber>" + Escape(_profile.SerialNumber) + "</serialNumber>");
  171. }
  172. builder.Append("<UPC/>");
  173. builder.Append("<UDN>uuid:" + Escape(_serverUdn) + "</UDN>");
  174. if (!string.IsNullOrEmpty(_profile.SonyAggregationFlags))
  175. {
  176. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  177. }
  178. }
  179. private string GetFriendlyName()
  180. {
  181. if (string.IsNullOrEmpty(_profile.FriendlyName))
  182. {
  183. return "Jellyfin - " + _serverName;
  184. }
  185. var characterList = new List<char>();
  186. foreach (var c in _serverName)
  187. {
  188. if (char.IsLetterOrDigit(c) || c == '-')
  189. {
  190. characterList.Add(c);
  191. }
  192. }
  193. var characters = characterList.ToArray();
  194. var serverName = new string(characters);
  195. var name = (_profile.FriendlyName ?? string.Empty).Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
  196. return name;
  197. }
  198. private void AppendIconList(StringBuilder builder)
  199. {
  200. builder.Append("<iconList>");
  201. foreach (var icon in GetIcons())
  202. {
  203. builder.Append("<icon>");
  204. builder.Append("<mimetype>" + Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  205. builder.Append("<width>" + Escape(icon.Width.ToString(_usCulture)) + "</width>");
  206. builder.Append("<height>" + Escape(icon.Height.ToString(_usCulture)) + "</height>");
  207. builder.Append("<depth>" + Escape(icon.Depth ?? string.Empty) + "</depth>");
  208. builder.Append("<url>" + BuildUrl(icon.Url) + "</url>");
  209. builder.Append("</icon>");
  210. }
  211. builder.Append("</iconList>");
  212. }
  213. private void AppendServiceList(StringBuilder builder)
  214. {
  215. builder.Append("<serviceList>");
  216. foreach (var service in GetServices())
  217. {
  218. builder.Append("<service>");
  219. builder.Append("<serviceType>" + Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  220. builder.Append("<serviceId>" + Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  221. builder.Append("<SCPDURL>" + BuildUrl(service.ScpdUrl) + "</SCPDURL>");
  222. builder.Append("<controlURL>" + BuildUrl(service.ControlUrl) + "</controlURL>");
  223. builder.Append("<eventSubURL>" + BuildUrl(service.EventSubUrl) + "</eventSubURL>");
  224. builder.Append("</service>");
  225. }
  226. builder.Append("</serviceList>");
  227. }
  228. private string BuildUrl(string url)
  229. {
  230. if (string.IsNullOrEmpty(url))
  231. {
  232. return string.Empty;
  233. }
  234. url = url.TrimStart('/');
  235. url = "/dlna/" + _serverUdn + "/" + url;
  236. if (EnableAbsoluteUrls)
  237. {
  238. url = _serverAddress.TrimEnd('/') + url;
  239. }
  240. return Escape(url);
  241. }
  242. private IEnumerable<DeviceIcon> GetIcons()
  243. {
  244. var list = new List<DeviceIcon>();
  245. list.Add(new DeviceIcon
  246. {
  247. MimeType = "image/png",
  248. Depth = "24",
  249. Width = 240,
  250. Height = 240,
  251. Url = "icons/logo240.png"
  252. });
  253. list.Add(new DeviceIcon
  254. {
  255. MimeType = "image/jpeg",
  256. Depth = "24",
  257. Width = 240,
  258. Height = 240,
  259. Url = "icons/logo240.jpg"
  260. });
  261. list.Add(new DeviceIcon
  262. {
  263. MimeType = "image/png",
  264. Depth = "24",
  265. Width = 120,
  266. Height = 120,
  267. Url = "icons/logo120.png"
  268. });
  269. list.Add(new DeviceIcon
  270. {
  271. MimeType = "image/jpeg",
  272. Depth = "24",
  273. Width = 120,
  274. Height = 120,
  275. Url = "icons/logo120.jpg"
  276. });
  277. list.Add(new DeviceIcon
  278. {
  279. MimeType = "image/png",
  280. Depth = "24",
  281. Width = 48,
  282. Height = 48,
  283. Url = "icons/logo48.png"
  284. });
  285. list.Add(new DeviceIcon
  286. {
  287. MimeType = "image/jpeg",
  288. Depth = "24",
  289. Width = 48,
  290. Height = 48,
  291. Url = "icons/logo48.jpg"
  292. });
  293. return list;
  294. }
  295. private IEnumerable<DeviceService> GetServices()
  296. {
  297. var list = new List<DeviceService>();
  298. list.Add(new DeviceService
  299. {
  300. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  301. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  302. ScpdUrl = "contentdirectory/contentdirectory.xml",
  303. ControlUrl = "contentdirectory/control",
  304. EventSubUrl = "contentdirectory/events"
  305. });
  306. list.Add(new DeviceService
  307. {
  308. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  309. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  310. ScpdUrl = "connectionmanager/connectionmanager.xml",
  311. ControlUrl = "connectionmanager/control",
  312. EventSubUrl = "connectionmanager/events"
  313. });
  314. if (_profile.EnableMSMediaReceiverRegistrar)
  315. {
  316. list.Add(new DeviceService
  317. {
  318. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  319. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  320. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  321. ControlUrl = "mediareceiverregistrar/control",
  322. EventSubUrl = "mediareceiverregistrar/events"
  323. });
  324. }
  325. return list;
  326. }
  327. public override string ToString()
  328. {
  329. return GetXml();
  330. }
  331. }
  332. }