2021-05-07 12:43:50 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-01-22 20:00:07 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2016-10-29 22:22:20 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
2020-01-22 20:00:07 +00:00
|
|
|
using System.Net.Http;
|
2020-08-31 17:26:42 +00:00
|
|
|
using System.Net.Mime;
|
2016-10-29 22:22:20 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
2021-09-19 18:53:31 +00:00
|
|
|
using Jellyfin.Extensions;
|
2019-01-13 19:16:19 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2016-10-29 22:34:54 +00:00
|
|
|
namespace Emby.Dlna.Eventing
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2020-08-13 19:47:31 +00:00
|
|
|
public class DlnaEventManager : IDlnaEventManager
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
|
|
|
private readonly ConcurrentDictionary<string, EventSubscription> _subscriptions =
|
|
|
|
new ConcurrentDictionary<string, EventSubscription>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
2020-08-31 17:26:42 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2020-08-20 15:01:04 +00:00
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
2020-08-31 17:26:42 +00:00
|
|
|
public DlnaEventManager(ILogger logger, IHttpClientFactory httpClientFactory)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2020-08-31 17:26:42 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-10-29 22:22:20 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2017-10-04 18:51:26 +00:00
|
|
|
public EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2017-10-04 18:51:26 +00:00
|
|
|
var subscription = GetSubscription(subscriptionId, false);
|
2020-06-15 09:10:52 +00:00
|
|
|
if (subscription != null)
|
|
|
|
{
|
|
|
|
subscription.TimeoutSeconds = ParseTimeout(requestedTimeoutString) ?? 300;
|
|
|
|
int timeoutSeconds = subscription.TimeoutSeconds;
|
|
|
|
subscription.SubscriptionTime = DateTime.UtcNow;
|
2017-10-04 18:51:26 +00:00
|
|
|
|
2020-06-15 09:10:52 +00:00
|
|
|
_logger.LogDebug(
|
|
|
|
"Renewing event subscription for {0} with timeout of {1} to {2}",
|
|
|
|
subscription.NotificationType,
|
|
|
|
timeoutSeconds,
|
|
|
|
subscription.CallbackUrl);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2020-06-15 09:10:52 +00:00
|
|
|
return GetEventSubscriptionResponse(subscriptionId, requestedTimeoutString, timeoutSeconds);
|
|
|
|
}
|
2017-10-04 18:51:26 +00:00
|
|
|
|
2021-07-26 21:02:32 +00:00
|
|
|
return new EventSubscriptionResponse(string.Empty, "text/plain");
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:37:13 +00:00
|
|
|
public EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2017-07-20 20:37:13 +00:00
|
|
|
var timeout = ParseTimeout(requestedTimeoutString) ?? 300;
|
2019-02-28 22:22:57 +00:00
|
|
|
var id = "uuid:" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2020-08-20 15:01:04 +00:00
|
|
|
_logger.LogDebug(
|
|
|
|
"Creating event subscription for {0} with timeout of {1} to {2}",
|
2019-12-30 15:03:20 +00:00
|
|
|
notificationType,
|
|
|
|
timeout,
|
|
|
|
callbackUrl);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
_subscriptions.TryAdd(id, new EventSubscription
|
|
|
|
{
|
|
|
|
Id = id,
|
|
|
|
CallbackUrl = callbackUrl,
|
|
|
|
SubscriptionTime = DateTime.UtcNow,
|
2020-11-28 23:44:19 +00:00
|
|
|
TimeoutSeconds = timeout,
|
2020-11-28 23:45:26 +00:00
|
|
|
NotificationType = notificationType
|
2016-10-29 22:22:20 +00:00
|
|
|
});
|
|
|
|
|
2017-07-20 20:37:13 +00:00
|
|
|
return GetEventSubscriptionResponse(id, requestedTimeoutString, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int? ParseTimeout(string header)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(header))
|
|
|
|
{
|
|
|
|
// Starts with SECOND-
|
2021-09-19 18:53:31 +00:00
|
|
|
if (int.TryParse(header.AsSpan().RightPart('-'), NumberStyles.Integer, _usCulture, out var val))
|
2017-07-20 20:37:13 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public EventSubscriptionResponse CancelEventSubscription(string subscriptionId)
|
|
|
|
{
|
2018-12-13 13:18:25 +00:00
|
|
|
_logger.LogDebug("Cancelling event subscription {0}", subscriptionId);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2020-08-20 15:01:04 +00:00
|
|
|
_subscriptions.TryRemove(subscriptionId, out _);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2021-07-26 21:02:32 +00:00
|
|
|
return new EventSubscriptionResponse(string.Empty, "text/plain");
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 20:37:13 +00:00
|
|
|
private EventSubscriptionResponse GetEventSubscriptionResponse(string subscriptionId, string requestedTimeoutString, int timeoutSeconds)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2021-07-26 21:02:32 +00:00
|
|
|
var response = new EventSubscriptionResponse(string.Empty, "text/plain");
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
response.Headers["SID"] = subscriptionId;
|
2018-09-12 17:26:21 +00:00
|
|
|
response.Headers["TIMEOUT"] = string.IsNullOrEmpty(requestedTimeoutString) ? ("SECOND-" + timeoutSeconds.ToString(_usCulture)) : requestedTimeoutString;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public EventSubscription GetSubscription(string id)
|
|
|
|
{
|
|
|
|
return GetSubscription(id, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private EventSubscription GetSubscription(string id, bool throwOnMissing)
|
|
|
|
{
|
2019-01-17 17:47:41 +00:00
|
|
|
if (!_subscriptions.TryGetValue(id, out EventSubscription e) && throwOnMissing)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
|
|
|
throw new ResourceNotFoundException("Event with Id " + id + " not found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task TriggerEvent(string notificationType, IDictionary<string, string> stateVariables)
|
|
|
|
{
|
|
|
|
var subs = _subscriptions.Values
|
|
|
|
.Where(i => !i.IsExpired && string.Equals(notificationType, i.NotificationType, StringComparison.OrdinalIgnoreCase))
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var tasks = subs.Select(i => TriggerEvent(i, stateVariables));
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task TriggerEvent(EventSubscription subscription, IDictionary<string, string> stateVariables)
|
|
|
|
{
|
|
|
|
var builder = new StringBuilder();
|
|
|
|
|
|
|
|
builder.Append("<?xml version=\"1.0\"?>");
|
|
|
|
builder.Append("<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">");
|
|
|
|
foreach (var key in stateVariables.Keys)
|
|
|
|
{
|
2020-07-22 12:34:51 +00:00
|
|
|
builder.Append("<e:property>")
|
|
|
|
.Append('<')
|
|
|
|
.Append(key)
|
|
|
|
.Append('>')
|
|
|
|
.Append(stateVariables[key])
|
|
|
|
.Append("</")
|
|
|
|
.Append(key)
|
|
|
|
.Append('>')
|
|
|
|
.Append("</e:property>");
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-29 22:22:20 +00:00
|
|
|
builder.Append("</e:propertyset>");
|
|
|
|
|
2020-11-06 15:15:30 +00:00
|
|
|
using var options = new HttpRequestMessage(new HttpMethod("NOTIFY"), subscription.CallbackUrl);
|
2020-08-31 17:26:42 +00:00
|
|
|
options.Content = new StringContent(builder.ToString(), Encoding.UTF8, MediaTypeNames.Text.Xml);
|
|
|
|
options.Headers.TryAddWithoutValidation("NT", subscription.NotificationType);
|
|
|
|
options.Headers.TryAddWithoutValidation("NTS", "upnp:propchange");
|
|
|
|
options.Headers.TryAddWithoutValidation("SID", subscription.Id);
|
|
|
|
options.Headers.TryAddWithoutValidation("SEQ", subscription.TriggerCount.ToString(_usCulture));
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-08-31 18:45:23 +00:00
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
2020-09-01 13:51:06 +00:00
|
|
|
.SendAsync(options, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// Already logged at lower levels
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
subscription.IncrementTriggerCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|