diff --git a/MediaBrowser.Common/Net/IServerManager.cs b/MediaBrowser.Common/Net/IServerManager.cs index f81c99ac6..7c14f98ce 100644 --- a/MediaBrowser.Common/Net/IServerManager.cs +++ b/MediaBrowser.Common/Net/IServerManager.cs @@ -26,8 +26,7 @@ namespace MediaBrowser.Common.Net /// Starts this instance. /// /// The URL prefixes. - /// if set to true [enable HTTP logging]. - void Start(IEnumerable urlPrefixes, bool enableHttpLogging); + void Start(IEnumerable urlPrefixes); /// /// Starts the web socket server. diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs index 20f07c74d..9a3cc6cb5 100644 --- a/MediaBrowser.Controller/Net/IHttpServer.cs +++ b/MediaBrowser.Controller/Net/IHttpServer.cs @@ -38,12 +38,6 @@ namespace MediaBrowser.Controller.Net /// void Stop(); - /// - /// Gets or sets a value indicating whether [enable HTTP request logging]. - /// - /// true if [enable HTTP request logging]; otherwise, false. - bool EnableHttpRequestLogging { get; set; } - /// /// Occurs when [web socket connected]. /// diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 064ac234f..904c7bcdd 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -22,12 +22,6 @@ namespace MediaBrowser.Model.Configuration /// The weather unit. public WeatherUnits WeatherUnit { get; set; } - /// - /// Gets or sets a value indicating whether [enable HTTP level logging]. - /// - /// true if [enable HTTP level logging]; otherwise, false. - public bool EnableHttpLevelLogging { get; set; } - /// /// Gets or sets a value indicating whether [enable u pn p]. /// @@ -223,7 +217,6 @@ namespace MediaBrowser.Model.Configuration ImageSavingConvention = ImageSavingConvention.Compatible; HttpServerPortNumber = 8096; LegacyWebSocketPortNumber = 8945; - EnableHttpLevelLogging = true; EnableDashboardResponseCaching = true; EnableAutomaticRestart = true; diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 340149182..68b69cdf0 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -5,6 +5,7 @@ using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; +using MediaBrowser.Server.Implementations.HttpServer.NetListener; using ServiceStack; using ServiceStack.Api.Swagger; using ServiceStack.Host; @@ -34,14 +35,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer private readonly List _restServices = new List(); - private HttpListener Listener { get; set; } - protected bool IsStarted = false; + private IHttpListener _listener; - private readonly AutoResetEvent _listenForNextRequest = new AutoResetEvent(false); private readonly SmartThreadPool _threadPoolManager; + private const int IdleTimeout = 300; - private const int IdleTimeout = 300; - private readonly ContainerAdapter _containerAdapter; private readonly ConcurrentDictionary _localEndPoints = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); @@ -53,7 +51,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// The local end points. public IEnumerable LocalEndPoints { - get { return _localEndPoints.Keys.ToList(); } + get { return _listener == null ? new List() : _listener.LocalEndPoints; } } public HttpListenerHost(IApplicationHost applicationHost, ILogManager logManager, string serviceName, string handlerPath, string defaultRedirectPath, params Assembly[] assembliesWithServices) @@ -148,120 +146,32 @@ namespace MediaBrowser.Server.Implementations.HttpServer public override ServiceStackHost Start(string listeningAtUrlBase) { - StartListener(Listen); + StartListener(); return this; } /// /// Starts the Web Service /// - private void StartListener(WaitCallback listenCallback) + private void StartListener() { - // *** Already running - just leave it in place - if (IsStarted) - return; - - if (Listener == null) - Listener = new HttpListener(); - HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); - foreach (var prefix in UrlPrefixes) + _listener = new HttpListenerServer(_logger, _threadPoolManager) { - _logger.Info("Adding HttpListener prefix " + prefix); - Listener.Prefixes.Add(prefix); - } + WebSocketHandler = WebSocketHandler, + ErrorHandler = ErrorHandler, + RequestHandler = RequestHandler + }; - IsStarted = true; - _logger.Info("Starting HttpListner"); - Listener.Start(); - _logger.Info("HttpListener started"); - - ThreadPool.QueueUserWorkItem(listenCallback); + _listener.Start(UrlPrefixes); } - private bool IsListening + private void WebSocketHandler(WebSocketConnectEventArgs args) { - get { return this.IsStarted && this.Listener != null && this.Listener.IsListening; } - } - - // Loop here to begin processing of new requests. - private void Listen(object state) - { - while (IsListening) + if (WebSocketConnected != null) { - if (Listener == null) return; - - try - { - Listener.BeginGetContext(ListenerCallback, Listener); - _listenForNextRequest.WaitOne(); - } - catch (Exception ex) - { - _logger.Error("Listen()", ex); - return; - } - if (Listener == null) return; - } - } - - // Handle the processing of a request in here. - private void ListenerCallback(IAsyncResult asyncResult) - { - var listener = asyncResult.AsyncState as HttpListener; - HttpListenerContext context; - - if (listener == null) return; - var isListening = listener.IsListening; - - try - { - if (!isListening) - { - _logger.Debug("Ignoring ListenerCallback() as HttpListener is no longer listening"); return; - } - // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework, - // blocks until there is a request to be processed or some type of data is available. - context = listener.EndGetContext(asyncResult); - } - catch (Exception ex) - { - // You will get an exception when httpListener.Stop() is called - // because there will be a thread stopped waiting on the .EndGetContext() - // method, and again, that is just the way most Begin/End asynchronous - // methods of the .NET Framework work. - var errMsg = ex + ": " + IsListening; - _logger.Warn(errMsg); - return; - } - finally - { - // Once we know we have a request (or exception), we signal the other thread - // so that it calls the BeginGetContext() (or possibly exits if we're not - // listening any more) method to start handling the next incoming request - // while we continue to process this request on a different thread. - _listenForNextRequest.Set(); - } - - _threadPoolManager.QueueWorkItem(() => InitTask(context)); - } - - public virtual void InitTask(HttpListenerContext context) - { - try - { - var task = this.ProcessRequestAsync(context); - task.ContinueWith(x => HandleError(x.Exception, context), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent); - - if (task.Status == TaskStatus.Created) - { - task.RunSynchronously(); - } - } - catch (Exception ex) - { - HandleError(ex, context); + WebSocketConnected(this, args); } } @@ -280,43 +190,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer _localEndPoints.GetOrAdd(address, address); } - if (EnableHttpRequestLogging) - { - LoggerUtils.LogRequest(_logger, request); - } + LoggerUtils.LogRequest(_logger, request); } - /// - /// Processes the web socket request. - /// - /// The CTX. - /// Task. - private async Task ProcessWebSocketRequest(HttpListenerContext ctx) - { -#if !__MonoCS__ - try - { - var webSocketContext = await ctx.AcceptWebSocketAsync(null).ConfigureAwait(false); - if (WebSocketConnected != null) - { - WebSocketConnected(this, new WebSocketConnectEventArgs { WebSocket = new NativeWebSocket(webSocketContext.WebSocket, _logger), Endpoint = ctx.Request.RemoteEndPoint.ToString() }); - } - } - catch (Exception ex) - { - _logger.ErrorException("AcceptWebSocketAsync error", ex); - ctx.Response.StatusCode = 500; - ctx.Response.Close(); - } -#endif - } - - private void HandleError(Exception ex, HttpListenerContext context) + private void ErrorHandler(Exception ex, IRequest httpReq) { try { - var operationName = context.Request.GetOperationName(); - var httpReq = GetRequest(context, operationName); var httpRes = httpReq.Response; if (httpRes.IsClosed) @@ -366,84 +246,54 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - private static ListenerRequest GetRequest(HttpListenerContext httpContext, string operationName) - { - var req = new ListenerRequest(httpContext, operationName, RequestAttributes.None); - req.RequestAttributes = req.GetAttributes(); - - return req; - } - /// /// Shut down the Web Service /// public void Stop() { - if (Listener != null) + if (_listener != null) { - foreach (var prefix in UrlPrefixes) - { - Listener.Prefixes.Remove(prefix); - } - - Listener.Close(); + _listener.Stop(); } } /// /// Overridable method that can be used to implement a custom hnandler /// - /// - protected Task ProcessRequestAsync(HttpListenerContext context) + /// The HTTP req. + /// Task. + protected Task RequestHandler(IHttpRequest httpReq, Uri url) { - var request = context.Request; + var date = DateTime.Now; - LogHttpRequest(request); + var httpRes = httpReq.Response; - if (request.IsWebSocketRequest) - { - return ProcessWebSocketRequest(context); - } - - var localPath = request.Url.LocalPath; + var operationName = httpReq.OperationName; + var localPath = url.LocalPath; if (string.Equals(localPath, "/" + HandlerPath + "/", StringComparison.OrdinalIgnoreCase)) { - context.Response.Redirect(DefaultRedirectPath); - context.Response.Close(); + httpRes.RedirectToUrl(DefaultRedirectPath); return Task.FromResult(true); } if (string.Equals(localPath, "/" + HandlerPath, StringComparison.OrdinalIgnoreCase)) { - context.Response.Redirect(HandlerPath + "/" + DefaultRedirectPath); - context.Response.Close(); + httpRes.RedirectToUrl(HandlerPath + "/" + DefaultRedirectPath); return Task.FromResult(true); } if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase)) { - context.Response.Redirect(HandlerPath + "/" + DefaultRedirectPath); - context.Response.Close(); + httpRes.RedirectToUrl(HandlerPath + "/" + DefaultRedirectPath); return Task.FromResult(true); } if (string.IsNullOrEmpty(localPath)) { - context.Response.Redirect("/" + HandlerPath + "/" + DefaultRedirectPath); - context.Response.Close(); + httpRes.RedirectToUrl("/" + HandlerPath + "/" + DefaultRedirectPath); return Task.FromResult(true); } - var date = DateTime.Now; - - if (string.IsNullOrEmpty(context.Request.RawUrl)) - return ((object)null).AsTaskResult(); - - var operationName = context.Request.GetOperationName(); - - var httpReq = GetRequest(context, operationName); - var httpRes = httpReq.Response; var handler = HttpHandlerFactory.GetHandler(httpReq); - var url = request.Url.ToString(); var remoteIp = httpReq.RemoteIp; var serviceStackHandler = handler as IServiceStackHandler; @@ -461,15 +311,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer //Matches Exceptions handled in HttpListenerBase.InitTask() var statusCode = httpRes.StatusCode; + var urlString = url.ToString(); task.ContinueWith(x => { var duration = DateTime.Now - date; - if (EnableHttpRequestLogging) - { - LoggerUtils.LogResponse(_logger, statusCode, url, remoteIp, duration); - } + LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration); }, TaskContinuationOptions.None); @@ -480,12 +328,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer .AsTaskException(); } - /// - /// Gets or sets a value indicating whether [enable HTTP request logging]. - /// - /// true if [enable HTTP request logging]; otherwise, false. - public bool EnableHttpRequestLogging { get; set; } - /// /// Adds the rest handlers. /// @@ -530,8 +372,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer if (disposing) { - _threadPoolManager.Dispose(); - + _threadPoolManager.Dispose(); + Stop(); } diff --git a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs new file mode 100644 index 000000000..1d80a263c --- /dev/null +++ b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs @@ -0,0 +1,42 @@ +using System.Threading.Tasks; +using MediaBrowser.Common.Net; +using ServiceStack.Web; +using System; +using System.Collections.Generic; + +namespace MediaBrowser.Server.Implementations.HttpServer +{ + public interface IHttpListener : IDisposable + { + IEnumerable LocalEndPoints { get; } + + /// + /// Gets or sets the error handler. + /// + /// The error handler. + Action ErrorHandler { get; set; } + + /// + /// Gets or sets the request handler. + /// + /// The request handler. + Func RequestHandler { get; set; } + + /// + /// Gets or sets the web socket handler. + /// + /// The web socket handler. + Action WebSocketHandler { get; set; } + + /// + /// Starts this instance. + /// + /// The URL prefixes. + void Start(IEnumerable urlPrefixes); + + /// + /// Stops this instance. + /// + void Stop(); + } +} diff --git a/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs new file mode 100644 index 000000000..51f0554d7 --- /dev/null +++ b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs @@ -0,0 +1,281 @@ +using Amib.Threading; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Logging; +using ServiceStack; +using ServiceStack.Host.HttpListener; +using ServiceStack.Web; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Server.Implementations.HttpServer.NetListener +{ + public class HttpListenerServer : IHttpListener + { + private readonly ILogger _logger; + private HttpListener _listener; + private readonly AutoResetEvent _listenForNextRequest = new AutoResetEvent(false); + private readonly SmartThreadPool _threadPoolManager; + + public System.Action ErrorHandler { get; set; } + public Action WebSocketHandler { get; set; } + public System.Func RequestHandler { get; set; } + + private readonly ConcurrentDictionary _localEndPoints = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + + public HttpListenerServer(ILogger logger, SmartThreadPool threadPoolManager) + { + _logger = logger; + + _threadPoolManager = threadPoolManager; + } + + /// + /// Gets the local end points. + /// + /// The local end points. + public IEnumerable LocalEndPoints + { + get { return _localEndPoints.Keys.ToList(); } + } + + private List UrlPrefixes { get; set; } + + public void Start(IEnumerable urlPrefixes) + { + UrlPrefixes = urlPrefixes.ToList(); + + if (_listener == null) + _listener = new System.Net.HttpListener(); + + //HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); + + foreach (var prefix in UrlPrefixes) + { + _logger.Info("Adding HttpListener prefix " + prefix); + _listener.Prefixes.Add(prefix); + } + + _listener.Start(); + + ThreadPool.QueueUserWorkItem(Listen); + } + + private bool IsListening + { + get { return _listener != null && _listener.IsListening; } + } + + // Loop here to begin processing of new requests. + private void Listen(object state) + { + while (IsListening) + { + if (_listener == null) return; + + try + { + _listener.BeginGetContext(ListenerCallback, _listener); + _listenForNextRequest.WaitOne(); + } + catch (Exception ex) + { + _logger.Error("Listen()", ex); + return; + } + if (_listener == null) return; + } + } + + // Handle the processing of a request in here. + private void ListenerCallback(IAsyncResult asyncResult) + { + var listener = asyncResult.AsyncState as HttpListener; + HttpListenerContext context; + + if (listener == null) return; + var isListening = listener.IsListening; + + try + { + if (!isListening) + { + _logger.Debug("Ignoring ListenerCallback() as HttpListener is no longer listening"); return; + } + // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework, + // blocks until there is a request to be processed or some type of data is available. + context = listener.EndGetContext(asyncResult); + } + catch (Exception ex) + { + // You will get an exception when httpListener.Stop() is called + // because there will be a thread stopped waiting on the .EndGetContext() + // method, and again, that is just the way most Begin/End asynchronous + // methods of the .NET Framework work. + var errMsg = ex + ": " + IsListening; + _logger.Warn(errMsg); + return; + } + finally + { + // Once we know we have a request (or exception), we signal the other thread + // so that it calls the BeginGetContext() (or possibly exits if we're not + // listening any more) method to start handling the next incoming request + // while we continue to process this request on a different thread. + _listenForNextRequest.Set(); + } + + _threadPoolManager.QueueWorkItem(() => InitTask(context)); + } + + public virtual void InitTask(HttpListenerContext context) + { + try + { + var task = this.ProcessRequestAsync(context); + task.ContinueWith(x => HandleError(x.Exception, context), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent); + + if (task.Status == TaskStatus.Created) + { + task.RunSynchronously(); + } + } + catch (Exception ex) + { + HandleError(ex, context); + } + } + + protected Task ProcessRequestAsync(HttpListenerContext context) + { + var request = context.Request; + + LogHttpRequest(request); + + if (request.IsWebSocketRequest) + { + return ProcessWebSocketRequest(context); + } + + if (string.IsNullOrEmpty(context.Request.RawUrl)) + return ((object)null).AsTaskResult(); + + var operationName = context.Request.GetOperationName(); + + var httpReq = GetRequest(context, operationName); + + return RequestHandler(httpReq, request.Url); + } + + /// + /// Processes the web socket request. + /// + /// The CTX. + /// Task. + private async Task ProcessWebSocketRequest(HttpListenerContext ctx) + { +#if !__MonoCS__ + try + { + var webSocketContext = await ctx.AcceptWebSocketAsync(null).ConfigureAwait(false); + + if (WebSocketHandler != null) + { + WebSocketHandler(new WebSocketConnectEventArgs + { + WebSocket = new NativeWebSocket(webSocketContext.WebSocket, _logger), + Endpoint = ctx.Request.RemoteEndPoint.ToString() + }); + } + } + catch (Exception ex) + { + _logger.ErrorException("AcceptWebSocketAsync error", ex); + ctx.Response.StatusCode = 500; + ctx.Response.Close(); + } +#endif + } + + private void HandleError(Exception ex, HttpListenerContext context) + { + var operationName = context.Request.GetOperationName(); + var httpReq = GetRequest(context, operationName); + + if (ErrorHandler != null) + { + ErrorHandler(ex, httpReq); + } + } + + private static ListenerRequest GetRequest(HttpListenerContext httpContext, string operationName) + { + var req = new ListenerRequest(httpContext, operationName, RequestAttributes.None); + req.RequestAttributes = req.GetAttributes(); + + return req; + } + + /// + /// Logs the HTTP request. + /// + /// The request. + private void LogHttpRequest(HttpListenerRequest request) + { + var endpoint = request.LocalEndPoint; + + if (endpoint != null) + { + var address = endpoint.ToString(); + + _localEndPoints.GetOrAdd(address, address); + } + + LoggerUtils.LogRequest(_logger, request); + } + + public void Stop() + { + if (_listener != null) + { + foreach (var prefix in UrlPrefixes) + { + _listener.Prefixes.Remove(prefix); + } + + _listener.Close(); + } + } + + public void Dispose() + { + Dispose(true); + } + + private bool _disposed; + private readonly object _disposeLock = new object(); + protected virtual void Dispose(bool disposing) + { + if (_disposed) return; + + lock (_disposeLock) + { + if (_disposed) return; + + if (disposing) + { + _threadPoolManager.Dispose(); + + Stop(); + } + + //release unmanaged resources here... + _disposed = true; + } + } + } +} diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 8d4eec29e..c24e9574f 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -96,6 +96,9 @@ ..\ThirdParty\ServiceStack.Text\ServiceStack.Text.dll + + ..\ThirdParty\WebsocketSharp\websocket-sharp.dll + @@ -134,6 +137,8 @@ + + diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index e66b87b0c..5931d7718 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -123,9 +123,9 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// Starts this instance. /// - public void Start(IEnumerable urlPrefixes, bool enableHttpLogging) + public void Start(IEnumerable urlPrefixes) { - ReloadHttpServer(urlPrefixes, enableHttpLogging); + ReloadHttpServer(urlPrefixes); } public void StartWebSocketServer() @@ -152,14 +152,13 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// Restarts the Http Server, or starts it if not currently running /// - private void ReloadHttpServer(IEnumerable urlPrefixes, bool enableHttpLogging) + private void ReloadHttpServer(IEnumerable urlPrefixes) { _logger.Info("Loading Http Server"); try { HttpServer = _applicationHost.Resolve(); - HttpServer.EnableHttpRequestLogging = enableHttpLogging; HttpServer.StartServer(urlPrefixes); } catch (SocketException ex) diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 60d1c92b4..da7f8631b 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -851,7 +851,7 @@ namespace MediaBrowser.ServerApplication { try { - ServerManager.Start(HttpServerUrlPrefixes, ServerConfigurationManager.Configuration.EnableHttpLevelLogging); + ServerManager.Start(HttpServerUrlPrefixes); } catch (Exception ex) { @@ -881,8 +881,6 @@ namespace MediaBrowser.ServerApplication { base.OnConfigurationUpdated(sender, e); - HttpServer.EnableHttpRequestLogging = ServerConfigurationManager.Configuration.EnableHttpLevelLogging; - if (!HttpServer.UrlPrefixes.SequenceEqual(HttpServerUrlPrefixes, StringComparer.OrdinalIgnoreCase)) { NotifyPendingRestart();