2021-05-07 14:43:50 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2020-01-22 21:00:07 +01:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2016-10-30 00:22:20 +02:00
|
|
|
using System;
|
|
|
|
using System.Globalization;
|
2020-01-22 21:00:07 +01:00
|
|
|
using System.Net.Http;
|
2020-08-31 19:26:42 +02:00
|
|
|
using System.Net.Mime;
|
2016-10-30 00:22:20 +02:00
|
|
|
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;
|
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
|
|
|
|
2019-07-07 16:39:35 +02:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
2020-08-31 19:26:42 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2016-10-30 00:22:20 +02:00
|
|
|
|
2020-08-31 19:26:42 +02:00
|
|
|
public SsdpHttpClient(IHttpClientFactory httpClientFactory)
|
2016-10-30 00:22:20 +02:00
|
|
|
{
|
2020-08-31 19:26:42 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-10-30 00:22:20 +02:00
|
|
|
}
|
|
|
|
|
2019-07-07 16:39:35 +02:00
|
|
|
public async Task<XDocument> SendCommandAsync(
|
|
|
|
string baseUrl,
|
2019-01-08 00:27:46 +01:00
|
|
|
DeviceService service,
|
|
|
|
string command,
|
|
|
|
string postData,
|
2020-04-02 16:49:58 +02:00
|
|
|
string header = null,
|
|
|
|
CancellationToken cancellationToken = default)
|
2016-10-30 00:22:20 +02:00
|
|
|
{
|
2019-06-14 16:32:37 +02:00
|
|
|
var url = NormalizeServiceUrl(baseUrl, service.ControlUrl);
|
2020-08-31 19:26:42 +02:00
|
|
|
using var response = await PostSoapDataAsync(
|
|
|
|
url,
|
|
|
|
$"\"{service.ServiceType}#{command}\"",
|
|
|
|
postData,
|
|
|
|
header,
|
|
|
|
cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2021-09-21 22:32:14 +02:00
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
2020-11-17 19:43:00 +01:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2021-03-06 03:33:52 +01:00
|
|
|
return await XDocument.LoadAsync(
|
|
|
|
stream,
|
2021-09-21 22:32:14 +02:00
|
|
|
LoadOptions.None,
|
2021-03-06 03:33:52 +01:00
|
|
|
cancellationToken).ConfigureAwait(false);
|
2016-10-30 00:22:20 +02:00
|
|
|
}
|
|
|
|
|
2019-01-06 21:50:43 +01: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;
|
|
|
|
}
|
|
|
|
|
2020-12-02 15:38:52 +01:00
|
|
|
if (!serviceUrl.StartsWith('/'))
|
2020-01-22 21:00:07 +01:00
|
|
|
{
|
2016-10-30 00:22:20 +02:00
|
|
|
serviceUrl = "/" + serviceUrl;
|
2020-01-22 21:00:07 +01:00
|
|
|
}
|
2016-10-30 00:22:20 +02:00
|
|
|
|
|
|
|
return baseUrl + serviceUrl;
|
|
|
|
}
|
|
|
|
|
2019-07-07 16:39:35 +02:00
|
|
|
public async Task SubscribeAsync(
|
|
|
|
string url,
|
2019-01-08 00:27:46 +01:00
|
|
|
string ip,
|
|
|
|
int port,
|
|
|
|
string localIp,
|
|
|
|
int eventport,
|
2016-10-30 00:22:20 +02:00
|
|
|
int timeOut = 3600)
|
|
|
|
{
|
2020-08-31 19:26:42 +02:00
|
|
|
using var options = new HttpRequestMessage(new HttpMethod("SUBSCRIBE"), url);
|
2020-09-01 21:16:19 +02:00
|
|
|
options.Headers.UserAgent.ParseAdd(USERAGENT);
|
2020-08-31 19:26:42 +02:00
|
|
|
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)
|
2020-09-01 15:51:06 +02:00
|
|
|
.SendAsync(options, HttpCompletionOption.ResponseHeadersRead)
|
2020-08-31 19:26:42 +02:00
|
|
|
.ConfigureAwait(false);
|
2021-09-21 22:32:14 +02:00
|
|
|
response.EnsureSuccessStatusCode();
|
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
|
|
|
{
|
2020-08-31 19:26:42 +02:00
|
|
|
using var options = new HttpRequestMessage(HttpMethod.Get, url);
|
2020-09-01 21:16:19 +02:00
|
|
|
options.Headers.UserAgent.ParseAdd(USERAGENT);
|
2020-08-31 19:26:42 +02:00
|
|
|
options.Headers.TryAddWithoutValidation("FriendlyName.DLNA.ORG", FriendlyName);
|
2020-09-01 15:51:06 +02:00
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
2021-09-21 22:32:14 +02:00
|
|
|
response.EnsureSuccessStatusCode();
|
2020-11-17 19:43:00 +01:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2021-03-22 18:21:12 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return await XDocument.LoadAsync(
|
|
|
|
stream,
|
2021-09-21 22:32:14 +02:00
|
|
|
LoadOptions.None,
|
2021-03-22 18:21:12 +01:00
|
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2016-10-30 00:22:20 +02: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 12:22:33 +02:00
|
|
|
private async Task<HttpResponseMessage> PostSoapDataAsync(
|
2019-06-14 16:32:37 +02:00
|
|
|
string url,
|
2019-01-08 00:27:46 +01:00
|
|
|
string soapAction,
|
|
|
|
string postData,
|
2016-10-30 00:22:20 +02:00
|
|
|
string header,
|
2018-09-12 19:26:21 +02:00
|
|
|
CancellationToken cancellationToken)
|
2016-10-30 00:22:20 +02:00
|
|
|
{
|
2019-06-14 16:32:37 +02:00
|
|
|
if (soapAction[0] != '\"')
|
|
|
|
{
|
2019-07-07 16:39:35 +02:00
|
|
|
soapAction = $"\"{soapAction}\"";
|
2019-06-14 16:32:37 +02:00
|
|
|
}
|
2016-10-30 00:22:20 +02:00
|
|
|
|
2020-08-31 19:26:42 +02:00
|
|
|
using var options = new HttpRequestMessage(HttpMethod.Post, url);
|
2020-09-01 21:16:19 +02:00
|
|
|
options.Headers.UserAgent.ParseAdd(USERAGENT);
|
2020-08-31 19:26:42 +02:00
|
|
|
options.Headers.TryAddWithoutValidation("SOAPACTION", soapAction);
|
|
|
|
options.Headers.TryAddWithoutValidation("Pragma", "no-cache");
|
|
|
|
options.Headers.TryAddWithoutValidation("FriendlyName.DLNA.ORG", FriendlyName);
|
2016-10-30 00:22:20 +02:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
if (!string.IsNullOrEmpty(header))
|
2016-10-30 00:22:20 +02:00
|
|
|
{
|
2020-08-31 19:26:42 +02:00
|
|
|
options.Headers.TryAddWithoutValidation("contentFeatures.dlna.org", header);
|
2016-10-30 00:22:20 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 19:26:42 +02:00
|
|
|
options.Content = new StringContent(postData, Encoding.UTF8, MediaTypeNames.Text.Xml);
|
2016-10-30 00:22:20 +02: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 12:22:33 +02:00
|
|
|
return await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
2016-10-30 00:22:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|