jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs

157 lines
5.3 KiB
C#
Raw Normal View History

2016-10-29 22:22:20 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
2016-10-29 22:34:54 +00:00
using Emby.Dlna.Common;
2016-10-29 22:22:20 +00:00
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
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";
private const string FriendlyName = "Emby";
private readonly IHttpClient _httpClient;
private readonly IServerConfigurationManager _config;
public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
{
_httpClient = httpClient;
_config = config;
}
public async Task<XDocument> SendCommandAsync(string baseUrl,
DeviceService service,
string command,
string postData,
bool logRequest = true,
string header = null)
{
2017-10-20 16:16:56 +00:00
using (var response = await PostSoapDataAsync(NormalizeServiceUrl(baseUrl, service.ControlUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header, logRequest)
.ConfigureAwait(false))
2016-10-29 22:22:20 +00:00
{
2017-10-20 16:16:56 +00:00
using (var stream = response.Content)
2016-10-29 22:22:20 +00:00
{
2017-10-20 16:16:56 +00:00
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
}
2016-10-29 22:22:20 +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;
}
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
public async Task SubscribeAsync(string url,
string ip,
int port,
string localIp,
int eventport,
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-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);
2017-10-20 16:16:56 +00:00
using (await _httpClient.SendAsync(options, "SUBSCRIBE").ConfigureAwait(false))
{
}
2016-10-29 22:22:20 +00:00
}
public async Task<XDocument> GetDataAsync(string url)
{
var options = new HttpRequestOptions
{
Url = url,
UserAgent = USERAGENT,
LogErrorResponseBody = true,
BufferContent = false,
// The periodic requests may keep some devices awake
LogRequestAsDebug = true
2016-10-29 22:22:20 +00:00
};
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
2017-10-20 16:16:56 +00:00
using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
2016-10-29 22:22:20 +00:00
{
2017-10-20 16:16:56 +00:00
using (var stream = response.Content)
2016-10-29 22:22:20 +00:00
{
2017-10-20 16:16:56 +00:00
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
}
2016-10-29 22:22:20 +00:00
}
}
}
private Task<HttpResponseInfo> PostSoapDataAsync(string url,
string soapAction,
string postData,
string header,
bool logRequest)
{
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
LogRequestAsDebug = true
2016-10-29 22:22:20 +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";
2017-10-22 23:36:22 +00:00
options.AppendCharsetToMimeType = true;
2016-10-29 22:22:20 +00:00
options.RequestContent = postData;
return _httpClient.Post(options);
}
}
}