SsdpHttpClient.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using Emby.Dlna.Common;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller.Configuration;
  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 = "Jellyfin";
  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. var cancellationToken = CancellationToken.None;
  32. var url = NormalizeServiceUrl(baseUrl, service.ControlUrl);
  33. using (var response = await PostSoapDataAsync(url, '\"' + service.ServiceType + '#' + command + '\"', postData, header, logRequest, cancellationToken)
  34. .ConfigureAwait(false))
  35. using (var stream = response.Content)
  36. using (var reader = new StreamReader(stream, Encoding.UTF8))
  37. {
  38. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  39. }
  40. }
  41. private static string NormalizeServiceUrl(string baseUrl, string serviceUrl)
  42. {
  43. // If it's already a complete url, don't stick anything onto the front of it
  44. if (serviceUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  45. {
  46. return serviceUrl;
  47. }
  48. if (!serviceUrl.StartsWith("/"))
  49. serviceUrl = "/" + serviceUrl;
  50. return baseUrl + serviceUrl;
  51. }
  52. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  53. public async Task SubscribeAsync(string url,
  54. string ip,
  55. int port,
  56. string localIp,
  57. int eventport,
  58. int timeOut = 3600)
  59. {
  60. var options = new HttpRequestOptions
  61. {
  62. Url = url,
  63. UserAgent = USERAGENT,
  64. LogErrorResponseBody = true,
  65. BufferContent = false,
  66. // The periodic requests may keep some devices awake
  67. LogRequestAsDebug = true
  68. };
  69. options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
  70. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
  71. options.RequestHeaders["NT"] = "upnp:event";
  72. options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
  73. using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
  74. {
  75. }
  76. }
  77. public async Task<XDocument> GetDataAsync(string url, CancellationToken cancellationToken)
  78. {
  79. var options = new HttpRequestOptions
  80. {
  81. Url = url,
  82. UserAgent = USERAGENT,
  83. LogErrorResponseBody = true,
  84. BufferContent = false,
  85. // The periodic requests may keep some devices awake
  86. LogRequestAsDebug = true,
  87. CancellationToken = cancellationToken
  88. };
  89. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  90. using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
  91. {
  92. using (var stream = response.Content)
  93. {
  94. using (var reader = new StreamReader(stream, Encoding.UTF8))
  95. {
  96. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  97. }
  98. }
  99. }
  100. }
  101. private Task<HttpResponseInfo> PostSoapDataAsync(
  102. string url,
  103. string soapAction,
  104. string postData,
  105. string header,
  106. bool logRequest,
  107. CancellationToken cancellationToken)
  108. {
  109. if (soapAction[0] != '\"')
  110. {
  111. soapAction = '\"' + soapAction + '\"';
  112. }
  113. var options = new HttpRequestOptions
  114. {
  115. Url = url,
  116. UserAgent = USERAGENT,
  117. LogRequest = logRequest || _config.GetDlnaConfiguration().EnableDebugLog,
  118. LogErrorResponseBody = true,
  119. BufferContent = false,
  120. // The periodic requests may keep some devices awake
  121. LogRequestAsDebug = true,
  122. CancellationToken = cancellationToken
  123. };
  124. options.RequestHeaders["SOAPAction"] = soapAction;
  125. options.RequestHeaders["Pragma"] = "no-cache";
  126. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  127. if (!string.IsNullOrEmpty(header))
  128. {
  129. options.RequestHeaders["contentFeatures.dlna.org"] = header;
  130. }
  131. options.RequestContentType = "text/xml";
  132. options.RequestContent = postData;
  133. return _httpClient.Post(options);
  134. }
  135. }
  136. }