2016-10-29 18:22:20 -04:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
2019-01-13 14:16:19 -05:00
|
|
|
using System.Threading;
|
2016-10-29 18:22:20 -04:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml.Linq;
|
2019-01-13 14:16:19 -05:00
|
|
|
using Emby.Dlna.Common;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2016-10-29 18:34:54 -04:00
|
|
|
namespace Emby.Dlna.PlayTo
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
public class SsdpHttpClient
|
|
|
|
{
|
|
|
|
private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
|
2018-12-13 04:18:29 -05:00
|
|
|
private const string FriendlyName = "Jellyfin";
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
|
|
public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
|
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_config = config;
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:27:46 -05:00
|
|
|
public async Task<XDocument> SendCommandAsync(string baseUrl,
|
|
|
|
DeviceService service,
|
|
|
|
string command,
|
|
|
|
string postData,
|
2016-10-29 18:22:20 -04:00
|
|
|
bool logRequest = true,
|
|
|
|
string header = null)
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var cancellationToken = CancellationToken.None;
|
|
|
|
|
2019-06-14 10:32:37 -04:00
|
|
|
var url = NormalizeServiceUrl(baseUrl, service.ControlUrl);
|
|
|
|
using (var response = await PostSoapDataAsync(url, '\"' + service.ServiceType + '#' + command + '\"', postData, header, logRequest, cancellationToken)
|
2017-10-20 12:16:56 -04:00
|
|
|
.ConfigureAwait(false))
|
2019-06-14 10:32:37 -04:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2019-06-14 10:32:37 -04:00
|
|
|
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
private static string NormalizeServiceUrl(string baseUrl, string serviceUrl)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2019-01-07 18:27:46 -05:00
|
|
|
|
|
|
|
public async Task SubscribeAsync(string url,
|
|
|
|
string ip,
|
|
|
|
int port,
|
|
|
|
string localIp,
|
|
|
|
int eventport,
|
2016-10-29 18:22:20 -04:00
|
|
|
int timeOut = 3600)
|
|
|
|
{
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
UserAgent = USERAGENT,
|
|
|
|
LogErrorResponseBody = true,
|
2017-10-22 19:27:09 -04:00
|
|
|
BufferContent = false,
|
2016-10-29 18:22:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture);
|
|
|
|
options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport.ToString(_usCulture) + ">";
|
|
|
|
options.RequestHeaders["NT"] = "upnp:event";
|
|
|
|
options.RequestHeaders["TIMEOUT"] = "Second-" + timeOut.ToString(_usCulture);
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
public async Task<XDocument> GetDataAsync(string url, CancellationToken cancellationToken)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
UserAgent = USERAGENT,
|
|
|
|
LogErrorResponseBody = true,
|
2017-10-22 19:27:09 -04:00
|
|
|
BufferContent = false,
|
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
CancellationToken = cancellationToken
|
2016-10-29 18:22:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var stream = response.Content)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var reader = new StreamReader(stream, Encoding.UTF8))
|
|
|
|
{
|
|
|
|
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 10:32:37 -04:00
|
|
|
private Task<HttpResponseInfo> PostSoapDataAsync(
|
|
|
|
string url,
|
2019-01-07 18:27:46 -05:00
|
|
|
string soapAction,
|
|
|
|
string postData,
|
2016-10-29 18:22:20 -04:00
|
|
|
string header,
|
2018-09-12 13:26:21 -04:00
|
|
|
bool logRequest,
|
|
|
|
CancellationToken cancellationToken)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2019-06-14 10:32:37 -04:00
|
|
|
if (soapAction[0] != '\"')
|
|
|
|
{
|
|
|
|
soapAction = '\"' + soapAction + '\"';
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
UserAgent = USERAGENT,
|
|
|
|
LogErrorResponseBody = true,
|
2017-10-22 19:27:09 -04:00
|
|
|
BufferContent = false,
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
CancellationToken = cancellationToken
|
2016-10-29 18:22:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
options.RequestHeaders["SOAPAction"] = soapAction;
|
|
|
|
options.RequestHeaders["Pragma"] = "no-cache";
|
|
|
|
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!string.IsNullOrEmpty(header))
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
options.RequestHeaders["contentFeatures.dlna.org"] = header;
|
|
|
|
}
|
|
|
|
|
2017-10-22 19:27:09 -04:00
|
|
|
options.RequestContentType = "text/xml";
|
2016-10-29 18:22:20 -04:00
|
|
|
options.RequestContent = postData;
|
|
|
|
|
|
|
|
return _httpClient.Post(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|