SsdpHttpClient.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = "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. using (var response = await PostSoapDataAsync(NormalizeServiceUrl(baseUrl, service.ControlUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header, logRequest, cancellationToken)
  33. .ConfigureAwait(false))
  34. {
  35. using (var stream = response.Content)
  36. {
  37. using (var reader = new StreamReader(stream, Encoding.UTF8))
  38. {
  39. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  40. }
  41. }
  42. }
  43. }
  44. private string NormalizeServiceUrl(string baseUrl, string serviceUrl)
  45. {
  46. // If it's already a complete url, don't stick anything onto the front of it
  47. if (serviceUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  48. {
  49. return serviceUrl;
  50. }
  51. if (!serviceUrl.StartsWith("/"))
  52. serviceUrl = "/" + serviceUrl;
  53. return baseUrl + serviceUrl;
  54. }
  55. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  56. public async Task SubscribeAsync(string url,
  57. string ip,
  58. int port,
  59. string localIp,
  60. int eventport,
  61. int timeOut = 3600)
  62. {
  63. var options = new HttpRequestOptions
  64. {
  65. Url = url,
  66. UserAgent = USERAGENT,
  67. LogErrorResponseBody = true,
  68. BufferContent = false,
  69. // The periodic requests may keep some devices awake
  70. LogRequestAsDebug = true
  71. };
  72. options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
  73. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
  74. options.RequestHeaders["NT"] = "upnp:event";
  75. options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
  76. using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
  77. {
  78. }
  79. }
  80. public async Task<XDocument> GetDataAsync(string url, CancellationToken cancellationToken)
  81. {
  82. var options = new HttpRequestOptions
  83. {
  84. Url = url,
  85. UserAgent = USERAGENT,
  86. LogErrorResponseBody = true,
  87. BufferContent = false,
  88. // The periodic requests may keep some devices awake
  89. LogRequestAsDebug = true,
  90. CancellationToken = cancellationToken
  91. };
  92. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  93. using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
  94. {
  95. using (var stream = response.Content)
  96. {
  97. using (var reader = new StreamReader(stream, Encoding.UTF8))
  98. {
  99. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  100. }
  101. }
  102. }
  103. }
  104. private Task<HttpResponseInfo> PostSoapDataAsync(string url,
  105. string soapAction,
  106. string postData,
  107. string header,
  108. bool logRequest,
  109. CancellationToken cancellationToken)
  110. {
  111. if (!soapAction.StartsWith("\""))
  112. soapAction = "\"" + soapAction + "\"";
  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.AppendCharsetToMimeType = true;
  133. options.RequestContent = postData;
  134. return _httpClient.Post(options);
  135. }
  136. }
  137. }