2015-01-26 22:47:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using MediaBrowser.Dlna.Common;
|
2014-04-06 17:53:23 +00:00
|
|
|
|
using System.Globalization;
|
2014-02-26 21:31:47 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.PlayTo
|
|
|
|
|
{
|
|
|
|
|
public class SsdpHttpClient
|
|
|
|
|
{
|
|
|
|
|
private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
|
|
|
|
|
private const string FriendlyName = "MediaBrowser";
|
|
|
|
|
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-26 21:31:47 +00:00
|
|
|
|
|
2014-03-14 17:09:22 +00:00
|
|
|
|
public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
2014-03-14 17:09:22 +00:00
|
|
|
|
_config = config;
|
2014-02-26 21:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 21:13:55 +00:00
|
|
|
|
public async Task<XDocument> SendCommandAsync(string baseUrl,
|
|
|
|
|
DeviceService service,
|
|
|
|
|
string command,
|
|
|
|
|
string postData,
|
|
|
|
|
string header = null)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
{
|
2015-01-26 22:47:16 +00:00
|
|
|
|
var response = await PostSoapDataAsync(NormalizeServiceUrl(baseUrl, service.ControlUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
using (var stream = response.Content)
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-26 22:47:16 +00:00
|
|
|
|
private string NormalizeServiceUrl(string baseUrl, string serviceUrl)
|
|
|
|
|
{
|
|
|
|
|
// If it's already a complete url, don't stick anything onto the front of it
|
|
|
|
|
if (serviceUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return serviceUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!serviceUrl.StartsWith("/"))
|
|
|
|
|
serviceUrl = "/" + serviceUrl;
|
|
|
|
|
|
|
|
|
|
return baseUrl + serviceUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-03-25 21:13:55 +00:00
|
|
|
|
public async Task SubscribeAsync(string url,
|
|
|
|
|
string ip,
|
|
|
|
|
int port,
|
|
|
|
|
string localIp,
|
|
|
|
|
int eventport,
|
|
|
|
|
int timeOut = 3600)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
2014-03-25 05:25:03 +00:00
|
|
|
|
Url = url,
|
2014-03-14 17:09:22 +00:00
|
|
|
|
UserAgent = USERAGENT,
|
2014-06-29 17:35:05 +00:00
|
|
|
|
LogRequest = _config.GetDlnaConfiguration().EnableDebugLogging,
|
2014-04-08 04:17:18 +00:00
|
|
|
|
LogErrorResponseBody = true
|
2014-02-26 21:31:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-04-06 17:53:23 +00:00
|
|
|
|
options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
|
|
|
|
|
options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
|
2014-02-26 21:31:47 +00:00
|
|
|
|
options.RequestHeaders["NT"] = "upnp:event";
|
2014-04-06 17:53:23 +00:00
|
|
|
|
options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
|
2014-02-26 21:31:47 +00:00
|
|
|
|
|
2014-06-22 05:52:31 +00:00
|
|
|
|
await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false);
|
2014-02-26 21:31:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 05:25:03 +00:00
|
|
|
|
public async Task<XDocument> GetDataAsync(string url)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
2014-03-25 05:25:03 +00:00
|
|
|
|
Url = url,
|
2014-03-14 17:09:22 +00:00
|
|
|
|
UserAgent = USERAGENT,
|
2014-06-29 17:35:05 +00:00
|
|
|
|
LogRequest = _config.GetDlnaConfiguration().EnableDebugLogging,
|
2014-04-08 04:17:18 +00:00
|
|
|
|
LogErrorResponseBody = true
|
2014-02-26 21:31:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
|
|
|
|
|
|
|
|
|
|
using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 21:13:55 +00:00
|
|
|
|
private Task<HttpResponseInfo> PostSoapDataAsync(string url,
|
|
|
|
|
string soapAction,
|
|
|
|
|
string postData,
|
|
|
|
|
string header = null)
|
2014-02-26 21:31:47 +00:00
|
|
|
|
{
|
|
|
|
|
if (!soapAction.StartsWith("\""))
|
|
|
|
|
soapAction = "\"" + soapAction + "\"";
|
|
|
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
2014-03-25 05:25:03 +00:00
|
|
|
|
Url = url,
|
2014-03-14 17:09:22 +00:00
|
|
|
|
UserAgent = USERAGENT,
|
2014-06-29 17:35:05 +00:00
|
|
|
|
LogRequest = _config.GetDlnaConfiguration().EnableDebugLogging,
|
2014-04-08 04:17:18 +00:00
|
|
|
|
LogErrorResponseBody = true
|
2014-02-26 21:31:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.RequestHeaders["SOAPAction"] = soapAction;
|
|
|
|
|
options.RequestHeaders["Pragma"] = "no-cache";
|
|
|
|
|
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(header))
|
|
|
|
|
{
|
|
|
|
|
options.RequestHeaders["contentFeatures.dlna.org"] = header;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options.RequestContentType = "text/xml; charset=\"utf-8\"";
|
|
|
|
|
options.RequestContent = postData;
|
|
|
|
|
|
|
|
|
|
return _httpClient.Post(options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|