SsdpHttpClient.cs 5.4 KB

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