jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs

165 lines
5.5 KiB
C#
Raw Normal View History

2016-10-30 00:22:20 +02:00
using System;
using System.Globalization;
using System.IO;
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;
using MediaBrowser.Controller.Configuration;
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
private readonly IHttpClient _httpClient;
private readonly IServerConfigurationManager _config;
public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
{
_httpClient = httpClient;
_config = config;
}
2019-01-08 00:27:46 +01:00
public async Task<XDocument> SendCommandAsync(string baseUrl,
DeviceService service,
string command,
string postData,
2016-10-30 00:22:20 +02:00
bool logRequest = true,
string header = null)
{
2018-09-12 19:26:21 +02:00
var cancellationToken = CancellationToken.None;
using (var response = await PostSoapDataAsync(NormalizeServiceUrl(baseUrl, service.ControlUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header, logRequest, cancellationToken)
2017-10-20 18:16:56 +02:00
.ConfigureAwait(false))
2016-10-30 00:22:20 +02:00
{
2017-10-20 18:16:56 +02:00
using (var stream = response.Content)
2016-10-30 00:22:20 +02:00
{
2017-10-20 18:16:56 +02:00
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), 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;
}
if (!serviceUrl.StartsWith("/"))
serviceUrl = "/" + serviceUrl;
return baseUrl + serviceUrl;
}
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
2019-01-08 00:27:46 +01:00
public async Task SubscribeAsync(string url,
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,
// The periodic requests may keep some devices awake
LogRequestAsDebug = true
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);
2017-10-20 18:16:56 +02:00
using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
{
}
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,
// The periodic requests may keep some devices awake
2017-11-23 16:46:16 +01:00
LogRequestAsDebug = true,
CancellationToken = cancellationToken
2016-10-30 00:22:20 +02:00
};
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
2017-10-20 18:16:56 +02:00
using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
2016-10-30 00:22:20 +02:00
{
2017-10-20 18:16:56 +02:00
using (var stream = response.Content)
2016-10-30 00:22:20 +02:00
{
2017-10-20 18:16:56 +02:00
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
}
2016-10-30 00:22:20 +02:00
}
}
}
2019-01-08 00:27:46 +01:00
private Task<HttpResponseInfo> PostSoapDataAsync(string url,
string soapAction,
string postData,
2016-10-30 00:22:20 +02:00
string header,
2018-09-12 19:26:21 +02:00
bool logRequest,
CancellationToken cancellationToken)
2016-10-30 00:22:20 +02:00
{
if (!soapAction.StartsWith("\""))
soapAction = "\"" + soapAction + "\"";
var options = new HttpRequestOptions
{
Url = url,
UserAgent = USERAGENT,
LogRequest = logRequest || _config.GetDlnaConfiguration().EnableDebugLog,
LogErrorResponseBody = true,
BufferContent = false,
// The periodic requests may keep some devices awake
2018-09-12 19:26:21 +02:00
LogRequestAsDebug = true,
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";
2017-10-23 01:36:22 +02:00
options.AppendCharsetToMimeType = true;
2016-10-30 00:22:20 +02:00
options.RequestContent = postData;
return _httpClient.Post(options);
}
}
}