The Free Software Media System - Server Backend & API
Merge branch 'master' into websocket
Bond_00915634a1913e8
diff --cc Emby.Server.Implementations/ApplicationHost.csindex e3e071af95,ffc916b980..8b387e195a--- a/Emby.Server.Implementations/ApplicationHost.cs+++ b/Emby.Server.Implementations/ApplicationHost.cs@@@ -44,7 -42,9 +42,8 @@@ using Emby.Server.Implementations.Playlusing Emby.Server.Implementations.ScheduledTasks;using Emby.Server.Implementations.Security;using Emby.Server.Implementations.Serialization;+ using Emby.Server.Implementations.Services;using Emby.Server.Implementations.Session;-using Emby.Server.Implementations.SocketSharp;using Emby.Server.Implementations.TV;using Emby.Server.Implementations.Updates;using MediaBrowser.Api;@@@ -103,8 -102,7 +101,8 @@@ using MediaBrowser.Providers.Subtitlesusing MediaBrowser.WebDashboard.Api;using MediaBrowser.XbmcMetadata.Providers;using Microsoft.AspNetCore.Http;+ using Microsoft.AspNetCore.Http.Extensions;+using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;@@@ -618,23 -500,43 +500,19 @@@ namespace Emby.Server.ImplementationDiscoverTypes();- await RegisterResources(serviceCollection).ConfigureAwait(false);-- ContentRoot = ServerConfigurationManager.Configuration.DashboardSourcePath;- if (string.IsNullOrEmpty(ContentRoot))- {- ContentRoot = ServerConfigurationManager.ApplicationPaths.WebPath;- }+ RegisterServices(serviceCollection);}- public async Task ExecuteWebsocketHandlerAsync(HttpContext context, Func<Task> next)- {- if (!context.WebSockets.IsWebSocketRequest)- {- await next().ConfigureAwait(false);- return;- }-- await _httpServer.ProcessWebSocketRequest(context).ConfigureAwait(false);- }-- public async Task ExecuteHttpHandlerAsync(HttpContext context, Func<Task> next)- {- if (context.WebSockets.IsWebSocketRequest)- {- await next().ConfigureAwait(false);- return;- }-- var request = context.Request;- var response = context.Response;- var localPath = context.Request.Path.ToString();-- var req = new WebSocketSharpRequest(request, response, request.Path, LoggerFactory.CreateLogger<WebSocketSharpRequest>());- await _httpServer.RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, context.RequestAborted).ConfigureAwait(false);- }+ public Task ExecuteHttpHandlerAsync(HttpContext context, Func<Task> next)+ => HttpServer.RequestHandler(context);/// <summary>- /// Registers resources that classes will depend on+ /// Registers services/resources with the service collection that will be available via DI./// </summary>- protected async Task RegisterResources(IServiceCollection serviceCollection)+ protected virtual void RegisterServices(IServiceCollection serviceCollection){+ serviceCollection.AddSingleton(_startupOptions);+serviceCollection.AddMemoryCache();serviceCollection.AddSingleton(ConfigurationManager);diff --cc Emby.Server.Implementations/HttpServer/HttpListenerHost.csindex 05dbad624b,211a0c1d99..2648b57d57--- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs+++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs@@@ -22,19 -22,23 +23,26 @@@ using MediaBrowser.Model.Globalizationusing MediaBrowser.Model.Serialization;using MediaBrowser.Model.Services;using Microsoft.AspNetCore.Http;+using Microsoft.AspNetCore.Http.Extensions;using Microsoft.AspNetCore.WebUtilities;using Microsoft.Extensions.Configuration;+ using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;+using Microsoft.Net.Http.Headers;using ServiceStack.Text.Jsv;namespace Emby.Server.Implementations.HttpServer{public class HttpListenerHost : IHttpServer, IDisposable{+ /// <summary>+ /// The key for a setting that specifies the default redirect path+ /// to use for requests where the URL base prefix is invalid or missing.+ /// </summary>+ public const string DefaultRedirectKey = "HttpListenerHost:DefaultRedirectPath";+private readonly ILogger _logger;+ private readonly ILoggerFactory _loggerFactory;private readonly IServerConfigurationManager _config;private readonly INetworkManager _networkManager;private readonly IServerApplicationHost _appHost;@@@ -43,7 -47,11 +51,11 @@@private readonly Func<Type, Func<string, object>> _funcParseFn;private readonly string _defaultRedirectPath;private readonly string _baseUrlPrefix;- private readonly Dictionary<Type, Type> ServiceOperationsMap = new Dictionary<Type, Type>();+++ private readonly Dictionary<Type, Type> _serviceOperationsMap = new Dictionary<Type, Type>();+ private readonly List<IWebSocketConnection> _webSocketConnections = new List<IWebSocketConnection>();+ private readonly IHostEnvironment _hostEnvironment;+private IWebSocketListener[] _webSocketListeners = Array.Empty<IWebSocketListener>();private bool _disposed = false;@@@ -236,13 -284,37 +256,11 @@@}// Strip any information we don't want to reveal-- msg = msg.Replace(_config.ApplicationPaths.ProgramSystemPath, string.Empty, StringComparison.OrdinalIgnoreCase);- msg = msg.Replace(_config.ApplicationPaths.ProgramDataPath, string.Empty, StringComparison.OrdinalIgnoreCase);-- return msg;+ return ex.Message+ ?.Replace(_config.ApplicationPaths.ProgramSystemPath, string.Empty, StringComparison.OrdinalIgnoreCase)+ .Replace(_config.ApplicationPaths.ProgramDataPath, string.Empty, StringComparison.OrdinalIgnoreCase);}- /// <summary>- /// Shut down the Web Service- /// </summary>- public void Stop()- {- List<IWebSocketConnection> connections;-- lock (_webSocketConnections)- {- connections = _webSocketConnections.ToList();- _webSocketConnections.Clear();- }-- foreach (var connection in connections)- {- try- {- connection.Dispose();- }- catch (Exception ex)- {- _logger.LogError(ex, "Error disposing connection");- }- }- }-public static string RemoveQueryStringByKey(string url, string key){var uri = new Uri(url);@@@ -375,26 -447,10 +393,26 @@@return true;}+ /// <inheritdoc />+ public Task RequestHandler(HttpContext context)+ {+ if (context.WebSockets.IsWebSocketRequest)+ {+ return WebSocketRequestHandler(context);+ }++ var request = context.Request;+ var response = context.Response;+ var localPath = context.Request.Path.ToString();++ var req = new WebSocketSharpRequest(request, response, request.Path, _logger);+ return RequestHandler(req, request.GetDisplayUrl(), request.Host.ToString(), localPath, context.RequestAborted);+ }+/// <summary>- /// Overridable method that can be used to implement a custom handler+ /// Overridable method that can be used to implement a custom handler./// </summary>- public async Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath, CancellationToken cancellationToken)+ private async Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath, CancellationToken cancellationToken){var stopWatch = new Stopwatch();stopWatch.Start();diff --cc MediaBrowser.Api/System/ActivityLogWebSocketListener.csindex 60b190a0e7,f8b6ee65d6..8e4860be4b--- a/MediaBrowser.Api/System/ActivityLogWebSocketListener.cs+++ b/MediaBrowser.Api/System/ActivityLogWebSocketListener.cs@@@ -23,13 -23,13 +23,13 @@@ namespace MediaBrowser.Api.Syste/// </summary>private readonly IActivityManager _activityManager;- public ActivityLogWebSocketListener(ILogger logger, IActivityManager activityManager) : base(logger)+ public ActivityLogWebSocketListener(ILogger<ActivityLogWebSocketListener> logger, IActivityManager activityManager) : base(logger){_activityManager = activityManager;- _activityManager.EntryCreated += _activityManager_EntryCreated;+ _activityManager.EntryCreated += OnEntryCreated;}- void _activityManager_EntryCreated(object sender, GenericEventArgs<ActivityLogEntry> e)+ private void OnEntryCreated(object sender, GenericEventArgs<ActivityLogEntry> e){SendData(true);}diff --cc MediaBrowser.Model/Net/WebSocketMessage.csindex 308032f833,7575224d4e..03f03e4ccd--- a/MediaBrowser.Model/Net/WebSocketMessage.cs+++ b/MediaBrowser.Model/Net/WebSocketMessage.cs@@@ -1,5 -1,5 +1,8 @@@+++ #pragma warning disable CS1591++using System;+namespace MediaBrowser.Model.Net{/// <summary>