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

134 lines
4.8 KiB
C#
Raw Normal View History

2014-02-26 21:31:47 +00:00
using MediaBrowser.Common.Net;
2014-03-14 17:09:22 +00:00
using MediaBrowser.Controller.Configuration;
2014-02-26 21:31:47 +00:00
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace MediaBrowser.Dlna.PlayTo
{
public class SsdpHttpClient
{
private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
private const string FriendlyName = "MediaBrowser";
private static readonly CookieContainer Container = new CookieContainer();
private readonly IHttpClient _httpClient;
2014-03-14 17:09:22 +00:00
private readonly IServerConfigurationManager _config;
2014-02-26 21:31:47 +00:00
2014-03-14 17:09:22 +00:00
public SsdpHttpClient(IHttpClient httpClient, IServerConfigurationManager config)
2014-02-26 21:31:47 +00:00
{
_httpClient = httpClient;
2014-03-14 17:09:22 +00:00
_config = config;
2014-02-26 21:31:47 +00:00
}
2014-03-13 19:08:02 +00:00
public async Task<XDocument> SendCommandAsync(string baseUrl, DeviceService service, string command, string postData, string header = null)
2014-02-26 21:31:47 +00:00
{
2014-03-13 19:08:02 +00:00
var serviceUrl = service.ControlUrl;
2014-02-26 21:31:47 +00:00
if (!serviceUrl.StartsWith("/"))
serviceUrl = "/" + serviceUrl;
var response = await PostSoapDataAsync(new Uri(baseUrl + serviceUrl), "\"" + service.ServiceType + "#" + command + "\"", postData, header)
.ConfigureAwait(false);
using (var stream = response.Content)
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
}
}
}
public async Task SubscribeAsync(Uri url, string ip, int port, string localIp, int eventport, int timeOut = 3600)
{
var options = new HttpRequestOptions
{
2014-02-27 18:00:49 +00:00
Url = url.ToString(),
2014-03-14 17:09:22 +00:00
UserAgent = USERAGENT,
2014-03-23 20:49:05 +00:00
LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
2014-02-26 21:31:47 +00:00
};
options.RequestHeaders["HOST"] = ip + ":" + port;
options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">";
options.RequestHeaders["NT"] = "upnp:event";
options.RequestHeaders["TIMEOUT"] = "Second - " + timeOut;
//request.CookieContainer = Container;
using (await _httpClient.Get(options).ConfigureAwait(false))
{
}
}
public async Task RespondAsync(Uri url, string ip, int port, string localIp, int eventport, int timeOut = 20000)
{
var options = new HttpRequestOptions
{
2014-02-27 18:00:49 +00:00
Url = url.ToString(),
UserAgent = USERAGENT
2014-02-26 21:31:47 +00:00
};
options.RequestHeaders["HOST"] = ip + ":" + port;
options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">";
options.RequestHeaders["NT"] = "upnp:event";
options.RequestHeaders["TIMEOUT"] = "Second - 3600";
//request.CookieContainer = Container;
using (await _httpClient.Get(options).ConfigureAwait(false))
{
}
}
public async Task<XDocument> GetDataAsync(Uri url)
{
var options = new HttpRequestOptions
{
2014-02-27 18:00:49 +00:00
Url = url.ToString(),
2014-03-14 17:09:22 +00:00
UserAgent = USERAGENT,
2014-03-23 20:49:05 +00:00
LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
2014-02-26 21:31:47 +00:00
};
options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName;
//request.CookieContainer = Container;
using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
return XDocument.Parse(reader.ReadToEnd(), LoadOptions.PreserveWhitespace);
}
}
}
2014-02-27 16:25:04 +00:00
private Task<HttpResponseInfo> PostSoapDataAsync(Uri url, string soapAction, string postData, string header = null, int timeOut = 20000)
2014-02-26 21:31:47 +00:00
{
if (!soapAction.StartsWith("\""))
soapAction = "\"" + soapAction + "\"";
var options = new HttpRequestOptions
{
2014-02-27 18:00:49 +00:00
Url = url.ToString(),
2014-03-14 17:09:22 +00:00
UserAgent = USERAGENT,
2014-03-23 20:49:05 +00:00
LogRequest = _config.Configuration.DlnaOptions.EnableDebugLogging
2014-02-26 21:31:47 +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; charset=\"utf-8\"";
options.RequestContent = postData;
return _httpClient.Post(options);
}
}
}