jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs

159 lines
5.1 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2016-10-30 00:22:20 +02:00
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
2016-10-30 00:22:20 +02:00
using System.Text;
2019-01-13 20:16:19 +01:00
using System.Threading;
2016-10-30 00:22:20 +02:00
using System.Threading.Tasks;
using System.Xml.Linq;
2019-01-13 20:16:19 +01:00
using Emby.Dlna.Common;
using MediaBrowser.Common.Net;
2016-10-30 00:22:20 +02:00
2016-10-30 00:34:54 +02:00
namespace Emby.Dlna.PlayTo
2016-10-30 00:22:20 +02:00
{
public class SsdpHttpClient
{
private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
2018-12-13 10:18:29 +01:00
private const string FriendlyName = "Jellyfin";
2016-10-30 00:22:20 +02:00
2019-07-07 16:39:35 +02:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2016-10-30 00:22:20 +02:00
private readonly IHttpClient _httpClient;
public SsdpHttpClient(IHttpClient httpClient)
2016-10-30 00:22:20 +02:00
{
_httpClient = httpClient;
}
2019-07-07 16:39:35 +02:00
public async Task<XDocument> SendCommandAsync(
string baseUrl,
2019-01-08 00:27:46 +01:00
DeviceService service,
string command,
string postData,
2020-04-02 16:49:58 +02:00
string header = null,
CancellationToken cancellationToken = default)
2016-10-30 00:22:20 +02:00
{
2019-06-14 16:32:37 +02:00
var url = NormalizeServiceUrl(baseUrl, service.ControlUrl);
2019-07-07 16:39:35 +02:00
using (var response = await PostSoapDataAsync(
url,
$"\"{service.ServiceType}#{command}\"",
postData,
header,
cancellationToken)
2017-10-20 18:16:56 +02:00
.ConfigureAwait(false))
2019-06-14 16:32:37 +02:00
using (var stream = response.Content)
using (var reader = new StreamReader(stream, Encoding.UTF8))
2016-10-30 00:22:20 +02:00
{
2019-07-07 16:39:35 +02:00
return XDocument.Parse(
await reader.ReadToEndAsync().ConfigureAwait(false),
LoadOptions.PreserveWhitespace);
2016-10-30 00:22:20 +02:00
}
}
private static string NormalizeServiceUrl(string baseUrl, string serviceUrl)
2016-10-30 00:22:20 +02: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;
}
2020-04-02 16:49:58 +02:00
if (!serviceUrl.StartsWith("/", StringComparison.Ordinal))
{
2016-10-30 00:22:20 +02:00
serviceUrl = "/" + serviceUrl;
}
2016-10-30 00:22:20 +02:00
return baseUrl + serviceUrl;
}
2019-07-07 16:39:35 +02:00
public async Task SubscribeAsync(
string url,
2019-01-08 00:27:46 +01:00
string ip,
int port,
string localIp,
int eventport,
2016-10-30 00:22:20 +02:00
int timeOut = 3600)
{
var options = new HttpRequestOptions
{
Url = url,
UserAgent = USERAGENT,
LogErrorResponseBody = true,
BufferContent = false,
2016-10-30 00:22:20 +02: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);
using (await _httpClient.SendAsync(options, new HttpMethod("SUBSCRIBE")).ConfigureAwait(false))
2017-10-20 18:16:56 +02:00
{
}
2016-10-30 00:22:20 +02:00
}
2017-11-23 16:46:16 +01:00
public async Task<XDocument> GetDataAsync(string url, CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
var options = new HttpRequestOptions
{
Url = url,
UserAgent = USERAGENT,
LogErrorResponseBody = true,
BufferContent = false,
2017-11-23 16:46:16 +01:00
CancellationToken = cancellationToken
2016-10-30 00:22:20 +02:00
};
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
using (var response = await _httpClient.SendAsync(options, HttpMethod.Get).ConfigureAwait(false))
2019-07-07 16:39:35 +02:00
using (var stream = response.Content)
using (var reader = new StreamReader(stream, Encoding.UTF8))
2016-10-30 00:22:20 +02:00
{
2019-07-07 16:39:35 +02:00
return XDocument.Parse(
await reader.ReadToEndAsync().ConfigureAwait(false),
LoadOptions.PreserveWhitespace);
2016-10-30 00:22:20 +02:00
}
}
2019-06-14 16:32:37 +02:00
private Task<HttpResponseInfo> PostSoapDataAsync(
string url,
2019-01-08 00:27:46 +01:00
string soapAction,
string postData,
2016-10-30 00:22:20 +02:00
string header,
2018-09-12 19:26:21 +02:00
CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
2019-06-14 16:32:37 +02:00
if (soapAction[0] != '\"')
{
2019-07-07 16:39:35 +02:00
soapAction = $"\"{soapAction}\"";
2019-06-14 16:32:37 +02:00
}
2016-10-30 00:22:20 +02:00
var options = new HttpRequestOptions
{
Url = url,
UserAgent = USERAGENT,
LogErrorResponseBody = true,
BufferContent = false,
2018-09-12 19:26:21 +02:00
CancellationToken = cancellationToken
2016-10-30 00:22:20 +02:00
};
options.RequestHeaders["SOAPAction"] = soapAction;
options.RequestHeaders["Pragma"] = "no-cache";
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(header))
2016-10-30 00:22:20 +02:00
{
options.RequestHeaders["contentFeatures.dlna.org"] = header;
}
options.RequestContentType = "text/xml";
2016-10-30 00:22:20 +02:00
options.RequestContent = postData;
return _httpClient.Post(options);
}
}
}