jellyfin-server/Emby.Dlna/PlayTo/SsdpHttpClient.cs

160 lines
5.1 KiB
C#
Raw Normal View History

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