SsdpHttpClient.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using Emby.Dlna.Common;
  4. using System;
  5. using System.Globalization;
  6. using System.IO;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml.Linq;
  10. namespace Emby.Dlna.PlayTo
  11. {
  12. public class SsdpHttpClient
  13. {
  14. private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
  15. private const string FriendlyName = "Emby";
  16. private readonly IHttpClient _httpClient;
  17. private readonly IServerConfigurationManager _config;
  18. public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
  19. {
  20. _httpClient = httpClient;
  21. _config = config;
  22. }
  23. public async Task<XDocument> SendCommandAsync(string baseUrl,
  24. DeviceService service,
  25. string command,
  26. string postData,
  27. bool logRequest = true,
  28. string header = null)
  29. {
  30. using (var response = await PostSoapDataAsync(NormalizeServiceUrl(baseUrl, service.ControlUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header, logRequest)
  31. .ConfigureAwait(false))
  32. {
  33. using (var stream = response.Content)
  34. {
  35. using (var reader = new StreamReader(stream, Encoding.UTF8))
  36. {
  37. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  38. }
  39. }
  40. }
  41. }
  42. private string NormalizeServiceUrl(string baseUrl, string serviceUrl)
  43. {
  44. // If it's already a complete url, don't stick anything onto the front of it
  45. if (serviceUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  46. {
  47. return serviceUrl;
  48. }
  49. if (!serviceUrl.StartsWith("/"))
  50. serviceUrl = "/" + serviceUrl;
  51. return baseUrl + serviceUrl;
  52. }
  53. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  54. public async Task SubscribeAsync(string url,
  55. string ip,
  56. int port,
  57. string localIp,
  58. int eventport,
  59. int timeOut = 3600)
  60. {
  61. var options = new HttpRequestOptions
  62. {
  63. Url = url,
  64. UserAgent = USERAGENT,
  65. LogErrorResponseBody = true,
  66. BufferContent = false
  67. };
  68. options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
  69. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
  70. options.RequestHeaders["NT"] = "upnp:event";
  71. options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
  72. using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
  73. {
  74. }
  75. }
  76. public async Task<XDocument> GetDataAsync(string url)
  77. {
  78. var options = new HttpRequestOptions
  79. {
  80. Url = url,
  81. UserAgent = USERAGENT,
  82. LogErrorResponseBody = true,
  83. BufferContent = false
  84. };
  85. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  86. using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
  87. {
  88. using (var stream = response.Content)
  89. {
  90. using (var reader = new StreamReader(stream, Encoding.UTF8))
  91. {
  92. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  93. }
  94. }
  95. }
  96. }
  97. private Task<HttpResponseInfo> PostSoapDataAsync(string url,
  98. string soapAction,
  99. string postData,
  100. string header,
  101. bool logRequest)
  102. {
  103. if (!soapAction.StartsWith("\""))
  104. soapAction = "\"" + soapAction + "\"";
  105. var options = new HttpRequestOptions
  106. {
  107. Url = url,
  108. UserAgent = USERAGENT,
  109. LogRequest = logRequest || _config.GetDlnaConfiguration().EnableDebugLog,
  110. LogErrorResponseBody = true,
  111. BufferContent = false
  112. };
  113. options.RequestHeaders["SOAPAction"] = soapAction;
  114. options.RequestHeaders["Pragma"] = "no-cache";
  115. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  116. if (!string.IsNullOrWhiteSpace(header))
  117. {
  118. options.RequestHeaders["contentFeatures.dlna.org"] = header;
  119. }
  120. options.RequestContentType = "text/xml; charset=\"utf-8\"";
  121. options.RequestContent = postData;
  122. return _httpClient.Post(options);
  123. }
  124. }
  125. }