SsdpHttpClient.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml.Linq;
  9. namespace MediaBrowser.Dlna.PlayTo
  10. {
  11. public class SsdpHttpClient
  12. {
  13. private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
  14. private const string FriendlyName = "MediaBrowser";
  15. private readonly IHttpClient _httpClient;
  16. private readonly IServerConfigurationManager _config;
  17. public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
  18. {
  19. _httpClient = httpClient;
  20. _config = config;
  21. }
  22. public async Task<XDocument> SendCommandAsync(string baseUrl,
  23. DeviceService service,
  24. string command,
  25. string postData,
  26. string header = null)
  27. {
  28. var serviceUrl = service.ControlUrl;
  29. if (!serviceUrl.StartsWith("/"))
  30. serviceUrl = "/" + serviceUrl;
  31. var response = await PostSoapDataAsync(baseUrl + serviceUrl, "\"" + service.ServiceType + "#" + command + "\"", postData, header)
  32. .ConfigureAwait(false);
  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. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  42. public async Task SubscribeAsync(string url,
  43. string ip,
  44. int port,
  45. string localIp,
  46. int eventport,
  47. int timeOut = 3600)
  48. {
  49. var options = new HttpRequestOptions
  50. {
  51. Url = url,
  52. UserAgent = USERAGENT,
  53. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging,
  54. LogErrorResponseBody = true
  55. };
  56. options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
  57. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
  58. options.RequestHeaders["NT"] = "upnp:event";
  59. options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
  60. // TODO: Method should be SUBSCRIBE
  61. // https://github.com/stormboy/node-upnp-controlpoint/blob/master/lib/upnp-service.js#L106
  62. using (await _httpClient.Get(options).ConfigureAwait(false))
  63. {
  64. }
  65. }
  66. public async Task RespondAsync(Uri url,
  67. string ip,
  68. int port,
  69. string localIp,
  70. int eventport,
  71. int timeOut = 3600)
  72. {
  73. var options = new HttpRequestOptions
  74. {
  75. Url = url.ToString(),
  76. UserAgent = USERAGENT
  77. };
  78. options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
  79. options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
  80. options.RequestHeaders["NT"] = "upnp:event";
  81. options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
  82. using (await _httpClient.Get(options).ConfigureAwait(false))
  83. {
  84. }
  85. }
  86. public async Task<XDocument> GetDataAsync(string url)
  87. {
  88. var options = new HttpRequestOptions
  89. {
  90. Url = url,
  91. UserAgent = USERAGENT,
  92. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging,
  93. LogErrorResponseBody = true
  94. };
  95. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  96. using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
  97. {
  98. using (var reader = new StreamReader(stream, Encoding.UTF8))
  99. {
  100. return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
  101. }
  102. }
  103. }
  104. private Task<HttpResponseInfo> PostSoapDataAsync(string url,
  105. string soapAction,
  106. string postData,
  107. string header = null)
  108. {
  109. if (!soapAction.StartsWith("\""))
  110. soapAction = "\"" + soapAction + "\"";
  111. var options = new HttpRequestOptions
  112. {
  113. Url = url,
  114. UserAgent = USERAGENT,
  115. LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging,
  116. LogErrorResponseBody = true
  117. };
  118. options.RequestHeaders["SOAPAction"] = soapAction;
  119. options.RequestHeaders["Pragma"] = "no-cache";
  120. options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
  121. if (!string.IsNullOrWhiteSpace(header))
  122. {
  123. options.RequestHeaders["contentFeatures.dlna.org"] = header;
  124. }
  125. options.RequestContentType = "text/xml; charset=\"utf-8\"";
  126. options.RequestContent = postData;
  127. return _httpClient.Post(options);
  128. }
  129. }
  130. }