jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs

132 lines
5.2 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;
using System.Net.Mime;
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");
private readonly IHttpClientFactory _httpClientFactory;
2016-10-29 22:22:20 +00:00
public SsdpHttpClient(IHttpClientFactory httpClientFactory)
2016-10-29 22:22:20 +00:00
{
_httpClientFactory = httpClientFactory;
2016-10-29 22:22:20 +00:00
}
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);
using var response = await PostSoapDataAsync(
url,
$"\"{service.ServiceType}#{command}\"",
postData,
header,
cancellationToken)
.ConfigureAwait(false);
2020-11-17 18:43:00 +00:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var reader = new StreamReader(stream, Encoding.UTF8);
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-12-02 14:38:52 +00:00
if (!serviceUrl.StartsWith('/'))
{
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)
{
using var options = new HttpRequestMessage(new HttpMethod("SUBSCRIBE"), url);
2020-09-01 19:16:19 +00:00
options.Headers.UserAgent.ParseAdd(USERAGENT);
options.Headers.TryAddWithoutValidation("HOST", ip + ":" + port.ToString(_usCulture));
options.Headers.TryAddWithoutValidation("CALLBACK", "<" + localIp + ":" + eventport.ToString(_usCulture) + ">");
options.Headers.TryAddWithoutValidation("NT", "upnp:event");
options.Headers.TryAddWithoutValidation("TIMEOUT", "Second-" + timeOut.ToString(_usCulture));
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.SendAsync(options, HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false);
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
{
using var options = new HttpRequestMessage(HttpMethod.Get, url);
2020-09-01 19:16:19 +00:00
options.Headers.UserAgent.ParseAdd(USERAGENT);
options.Headers.TryAddWithoutValidation("FriendlyName.DLNA.ORG", FriendlyName);
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
2020-11-17 18:43:00 +00:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var reader = new StreamReader(stream, Encoding.UTF8);
return XDocument.Parse(
await reader.ReadToEndAsync().ConfigureAwait(false),
LoadOptions.PreserveWhitespace);
2016-10-29 22:22:20 +00:00
}
Fix ObjectDisposedException ``` System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Http.StringContent'. at System.Net.Http.HttpContent.CheckDisposed() at System.Net.Http.HttpContent.CopyToAsync(Stream stream, TransportContext context, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Emby.Dlna.PlayTo.SsdpHttpClient.SendCommandAsync(String baseUrl, DeviceService service, String command, String postData, String header, CancellationToken cancellationToken) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs:line 41 at Emby.Dlna.PlayTo.Device.GetTransportInfo(TransportCommands avCommands, CancellationToken cancellationToken) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/Device.cs:line 629 at Emby.Dlna.PlayTo.Device.TimerCallback(Object sender) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/Device.cs:line 445 ```
2020-09-07 10:22:33 +00:00
private async Task<HttpResponseMessage> PostSoapDataAsync(
2019-06-14 14:32:37 +00:00
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
using var options = new HttpRequestMessage(HttpMethod.Post, url);
2020-09-01 19:16:19 +00:00
options.Headers.UserAgent.ParseAdd(USERAGENT);
options.Headers.TryAddWithoutValidation("SOAPACTION", soapAction);
options.Headers.TryAddWithoutValidation("Pragma", "no-cache");
options.Headers.TryAddWithoutValidation("FriendlyName.DLNA.ORG", FriendlyName);
2016-10-29 22:22:20 +00:00
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(header))
2016-10-29 22:22:20 +00:00
{
options.Headers.TryAddWithoutValidation("contentFeatures.dlna.org", header);
2016-10-29 22:22:20 +00:00
}
options.Content = new StringContent(postData, Encoding.UTF8, MediaTypeNames.Text.Xml);
2016-10-29 22:22:20 +00:00
Fix ObjectDisposedException ``` System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Http.StringContent'. at System.Net.Http.HttpContent.CheckDisposed() at System.Net.Http.HttpContent.CopyToAsync(Stream stream, TransportContext context, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Emby.Dlna.PlayTo.SsdpHttpClient.SendCommandAsync(String baseUrl, DeviceService service, String command, String postData, String header, CancellationToken cancellationToken) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs:line 41 at Emby.Dlna.PlayTo.Device.GetTransportInfo(TransportCommands avCommands, CancellationToken cancellationToken) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/Device.cs:line 629 at Emby.Dlna.PlayTo.Device.TimerCallback(Object sender) in /home/loma/dev/jellyfin/Emby.Dlna/PlayTo/Device.cs:line 445 ```
2020-09-07 10:22:33 +00:00
return await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
2016-10-29 22:22:20 +00:00
}
}
}