jellyfin/Emby.Dlna/PlayTo/SsdpHttpClient.cs

138 lines
5.3 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
2016-10-29 18:22:20 -04:00
using System;
using System.Globalization;
using System.Net.Http;
using System.Net.Mime;
2016-10-29 18:22:20 -04:00
using System.Text;
2019-01-13 14:16:19 -05:00
using System.Threading;
2016-10-29 18:22:20 -04:00
using System.Threading.Tasks;
using System.Xml.Linq;
2019-01-13 14:16:19 -05:00
using Emby.Dlna.Common;
using MediaBrowser.Common.Net;
2016-10-29 18:22:20 -04:00
2016-10-29 18:34:54 -04:00
namespace Emby.Dlna.PlayTo
2016-10-29 18:22:20 -04:00
{
public class SsdpHttpClient
{
private const string USERAGENT = "Microsoft-Windows/6.2 UPnP/1.0 Microsoft-DLNA DLNADOC/1.50";
2018-12-13 04:18:29 -05:00
private const string FriendlyName = "Jellyfin";
2016-10-29 18:22:20 -04:00
2019-07-07 10:39:35 -04:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IHttpClientFactory _httpClientFactory;
2016-10-29 18:22:20 -04:00
public SsdpHttpClient(IHttpClientFactory httpClientFactory)
2016-10-29 18:22:20 -04:00
{
_httpClientFactory = httpClientFactory;
2016-10-29 18:22:20 -04:00
}
2019-07-07 10:39:35 -04:00
public async Task<XDocument> SendCommandAsync(
string baseUrl,
2019-01-07 18:27:46 -05:00
DeviceService service,
string command,
string postData,
2020-04-02 10:49:58 -04:00
string header = null,
CancellationToken cancellationToken = default)
2016-10-29 18:22:20 -04:00
{
2019-06-14 10:32:37 -04:00
var url = NormalizeServiceUrl(baseUrl, service.ControlUrl);
using var response = await PostSoapDataAsync(
url,
$"\"{service.ServiceType}#{command}\"",
postData,
header,
cancellationToken)
.ConfigureAwait(false);
2020-11-17 13:43:00 -05:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
return await XDocument.LoadAsync(
stream,
LoadOptions.PreserveWhitespace,
cancellationToken).ConfigureAwait(false);
2016-10-29 18:22:20 -04:00
}
private static string NormalizeServiceUrl(string baseUrl, string serviceUrl)
2016-10-29 18:22:20 -04: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 09:38:52 -05:00
if (!serviceUrl.StartsWith('/'))
{
2016-10-29 18:22:20 -04:00
serviceUrl = "/" + serviceUrl;
}
2016-10-29 18:22:20 -04:00
return baseUrl + serviceUrl;
}
2019-07-07 10:39:35 -04:00
public async Task SubscribeAsync(
string url,
2019-01-07 18:27:46 -05:00
string ip,
int port,
string localIp,
int eventport,
2016-10-29 18:22:20 -04:00
int timeOut = 3600)
{
using var options = new HttpRequestMessage(new HttpMethod("SUBSCRIBE"), url);
2020-09-01 15:16:19 -04: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 18:22:20 -04:00
}
2017-11-23 10:46:16 -05:00
public async Task<XDocument> GetDataAsync(string url, CancellationToken cancellationToken)
2016-10-29 18:22:20 -04:00
{
using var options = new HttpRequestMessage(HttpMethod.Get, url);
2020-09-01 15:16:19 -04: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 13:43:00 -05:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2021-03-22 13:21:12 -04:00
try
{
return await XDocument.LoadAsync(
stream,
LoadOptions.PreserveWhitespace,
cancellationToken).ConfigureAwait(false);
}
catch
{
return null;
}
2016-10-29 18:22:20 -04: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 06:22:33 -04:00
private async Task<HttpResponseMessage> PostSoapDataAsync(
2019-06-14 10:32:37 -04:00
string url,
2019-01-07 18:27:46 -05:00
string soapAction,
string postData,
2016-10-29 18:22:20 -04:00
string header,
2018-09-12 13:26:21 -04:00
CancellationToken cancellationToken)
2016-10-29 18:22:20 -04:00
{
2019-06-14 10:32:37 -04:00
if (soapAction[0] != '\"')
{
2019-07-07 10:39:35 -04:00
soapAction = $"\"{soapAction}\"";
2019-06-14 10:32:37 -04:00
}
2016-10-29 18:22:20 -04:00
using var options = new HttpRequestMessage(HttpMethod.Post, url);
2020-09-01 15:16:19 -04: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 18:22:20 -04:00
2018-09-12 13:26:21 -04:00
if (!string.IsNullOrEmpty(header))
2016-10-29 18:22:20 -04:00
{
options.Headers.TryAddWithoutValidation("contentFeatures.dlna.org", header);
2016-10-29 18:22:20 -04:00
}
options.Content = new StringContent(postData, Encoding.UTF8, MediaTypeNames.Text.Xml);
2016-10-29 18:22:20 -04: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 06:22:33 -04:00
return await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
2016-10-29 18:22:20 -04:00
}
}
}