DescriptionXmlBuilder.cs 13 KB

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