diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer')
12 files changed, 83 insertions, 116 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 56e2e5247..c3228db92 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -1,7 +1,6 @@ using Funq; using MediaBrowser.Common; using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.Net; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.HttpServer.NetListener; @@ -205,10 +204,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer { HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); - _listener = _supportsNativeWebSocket && NativeWebSocket.IsSupported - ? _listener = new HttpListenerServer(_logger, OnRequestReceived) - //? _listener = new WebSocketSharpListener(_logger, OnRequestReceived) - : _listener = new WebSocketSharpListener(_logger, OnRequestReceived); + _listener = GetListener(); _listener.WebSocketHandler = WebSocketHandler; _listener.ErrorHandler = ErrorHandler; @@ -217,6 +213,16 @@ namespace MediaBrowser.Server.Implementations.HttpServer _listener.Start(UrlPrefixes); } + private IHttpListener GetListener() + { + if (_supportsNativeWebSocket && NativeWebSocket.IsSupported) + { + return new HttpListenerServer(_logger, OnRequestReceived); + } + + return new WebSocketSharpListener(_logger, OnRequestReceived); + } + private void WebSocketHandler(WebSocketConnectEventArgs args) { if (WebSocketConnected != null) diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs index e13e27d5a..681d3ac5e 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -12,7 +12,7 @@ using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; -using MimeTypes = MediaBrowser.Common.Net.MimeTypes; +using MimeTypes = MediaBrowser.Model.Net.MimeTypes; namespace MediaBrowser.Server.Implementations.HttpServer { diff --git a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs index 86e8856cf..e77600e93 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs @@ -1,8 +1,8 @@ -using System.Threading.Tasks; -using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Net; using ServiceStack.Web; using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.HttpServer { diff --git a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs index 8cc614fe5..cac2f8e09 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/NativeWebSocket.cs @@ -1,9 +1,9 @@ -using System.Text; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Net; +using MediaBrowser.Common.Events; +using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using System; using System.Net.WebSockets; +using System.Text; using System.Threading; using System.Threading.Tasks; using WebSocketMessageType = MediaBrowser.Model.Net.WebSocketMessageType; diff --git a/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs index 2d41cc26f..31c0e87b3 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host.HttpListener; diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs index 57d87749c..1d17c641d 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs @@ -1,5 +1,6 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Connect; +using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; @@ -15,10 +16,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security { private readonly IServerConfigurationManager _config; - public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager, ISessionManager sessionManager) + public AuthService(IUserManager userManager, IAuthorizationContext authorizationContext, IServerConfigurationManager config, IConnectManager connectManager, ISessionManager sessionManager, IDeviceManager deviceManager) { AuthorizationContext = authorizationContext; _config = config; + DeviceManager = deviceManager; SessionManager = sessionManager; ConnectManager = connectManager; UserManager = userManager; @@ -28,6 +30,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security public IAuthorizationContext AuthorizationContext { get; private set; } public IConnectManager ConnectManager { get; private set; } public ISessionManager SessionManager { get; private set; } + public IDeviceManager DeviceManager { get; private set; } /// <summary> /// Redirect the client to a specific URL if authentication failed. @@ -68,24 +71,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security if (user != null) { - if (user.Configuration.IsDisabled) - { - throw new SecurityException("User account has been disabled.") - { - SecurityExceptionType = SecurityExceptionType.Unauthenticated - }; - } - - if (!user.Configuration.IsAdministrator && - !authAttribtues.EscapeParentalControl && - !user.IsParentalScheduleAllowed()) - { - request.AddResponseHeader("X-Application-Error-Code", "ParentalControl"); - throw new SecurityException("This user account is not allowed access at this time.") - { - SecurityExceptionType = SecurityExceptionType.ParentalControl - }; - } + ValidateUserAccess(user, request, authAttribtues, auth); } if (!IsExemptFromRoles(auth, authAttribtues)) @@ -108,6 +94,42 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security } } + private void ValidateUserAccess(User user, IServiceRequest request, + IAuthenticationAttributes authAttribtues, + AuthorizationInfo auth) + { + if (user.Policy.IsDisabled) + { + throw new SecurityException("User account has been disabled.") + { + SecurityExceptionType = SecurityExceptionType.Unauthenticated + }; + } + + if (!user.Policy.IsAdministrator && + !authAttribtues.EscapeParentalControl && + !user.IsParentalScheduleAllowed()) + { + request.AddResponseHeader("X-Application-Error-Code", "ParentalControl"); + + throw new SecurityException("This user account is not allowed access at this time.") + { + SecurityExceptionType = SecurityExceptionType.ParentalControl + }; + } + + if (!string.IsNullOrWhiteSpace(auth.DeviceId)) + { + if (!DeviceManager.CanAccessDevice(user.Id.ToString("N"), auth.DeviceId)) + { + throw new SecurityException("User is not allowed access from this device.") + { + SecurityExceptionType = SecurityExceptionType.ParentalControl + }; + } + } + } + private bool IsExemptFromAuthenticationToken(AuthorizationInfo auth, IAuthenticationAttributes authAttribtues) { if (!_config.Configuration.IsStartupWizardCompleted && @@ -135,7 +157,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security { if (roles.Contains("admin", StringComparer.OrdinalIgnoreCase)) { - if (user == null || !user.Configuration.IsAdministrator) + if (user == null || !user.Policy.IsAdministrator) { throw new SecurityException("User does not have admin access.") { @@ -145,7 +167,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security } if (roles.Contains("delete", StringComparer.OrdinalIgnoreCase)) { - if (user == null || !user.Configuration.EnableContentDeletion) + if (user == null || !user.Policy.EnableContentDeletion) { throw new SecurityException("User does not have delete access.") { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs index 63d57b6be..154313fb9 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/Extensions.cs @@ -1,6 +1,6 @@ -using System; -using MediaBrowser.Model.Logging; -using WebSocketSharp.Net; +using MediaBrowser.Model.Logging; +using SocketHttpListener.Net; +using System; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs index 7ff3a1247..9dcb679f4 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SharpWebSocket.cs @@ -1,11 +1,9 @@ -using System.Text; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Net; +using MediaBrowser.Common.Events; +using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using System; using System.Threading; using System.Threading.Tasks; -using WebSocketMessageType = MediaBrowser.Model.Net.WebSocketMessageType; using WebSocketState = MediaBrowser.Model.Net.WebSocketState; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp @@ -23,7 +21,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp /// Gets or sets the web socket. /// </summary> /// <value>The web socket.</value> - private WebSocketSharp.WebSocket WebSocket { get; set; } + private SocketHttpListener.WebSocket WebSocket { get; set; } private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); @@ -33,7 +31,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp /// <param name="socket">The socket.</param> /// <param name="logger">The logger.</param> /// <exception cref="System.ArgumentNullException">socket</exception> - public SharpWebSocket(WebSocketSharp.WebSocket socket, ILogger logger) + public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger) { if (socket == null) { @@ -55,17 +53,17 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp WebSocket.ConnectAsServer(); } - void socket_OnError(object sender, WebSocketSharp.ErrorEventArgs e) + void socket_OnError(object sender, SocketHttpListener.ErrorEventArgs e) { EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger); } - void socket_OnClose(object sender, WebSocketSharp.CloseEventArgs e) + void socket_OnClose(object sender, SocketHttpListener.CloseEventArgs e) { EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger); } - void socket_OnMessage(object sender, WebSocketSharp.MessageEventArgs e) + void socket_OnMessage(object sender, SocketHttpListener.MessageEventArgs e) { if (OnReceive != null) { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs index ffc99793e..427671b30 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/SocketSharpLogger.cs @@ -3,7 +3,7 @@ using System; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { - public class SocketSharpLogger : WebSocketSharp.Logging.ILogger + public class SocketSharpLogger : SocketHttpListener.Logging.ILogger { private readonly ILogger _logger; diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 9deb34e91..419d145bb 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Web; @@ -6,16 +6,14 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading; using System.Threading.Tasks; -using WebSocketSharp.Net; +using SocketHttpListener.Net; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { public class WebSocketSharpListener : IHttpListener { private HttpListener _listener; - private readonly ManualResetEventSlim _listenForNextRequest = new ManualResetEventSlim(false); private readonly ILogger _logger; private readonly Action<string> _endpointListener; @@ -43,70 +41,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp _listener.Prefixes.Add(prefix); } - _listener.Start(); - - Task.Factory.StartNew(Listen, TaskCreationOptions.LongRunning); - } - - private bool IsListening - { - get { return _listener != null && _listener.IsListening; } - } + _listener.OnContext = ProcessContext; - // Loop here to begin processing of new requests. - private void Listen() - { - while (IsListening) - { - if (_listener == null) return; - _listenForNextRequest.Reset(); - - try - { - _listener.BeginGetContext(ListenerCallback, _listener); - _listenForNextRequest.Wait(); - } - catch (Exception ex) - { - _logger.Error("Listen()", ex); - return; - } - if (_listener == null) return; - } + _listener.Start(); } - // Handle the processing of a request in here. - private void ListenerCallback(IAsyncResult asyncResult) + private void ProcessContext(HttpListenerContext context) { - _listenForNextRequest.Set(); - - 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; - } - Task.Factory.StartNew(() => InitTask(context)); } @@ -117,10 +58,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp var task = this.ProcessRequestAsync(context); task.ContinueWith(x => HandleError(x.Exception, context), TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent); - if (task.Status == TaskStatus.Created) - { - task.RunSynchronously(); - } + //if (task.Status == TaskStatus.Created) + //{ + // task.RunSynchronously(); + //} } catch (Exception ex) { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs index 7a5f6fbdc..54c27cf0a 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs @@ -7,7 +7,7 @@ using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host; using ServiceStack.Web; -using WebSocketSharp.Net; +using SocketHttpListener.Net; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs index b725af610..171dacb22 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs @@ -5,7 +5,7 @@ using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Host; using ServiceStack.Web; -using HttpListenerResponse = WebSocketSharp.Net.HttpListenerResponse; +using HttpListenerResponse = SocketHttpListener.Net.HttpListenerResponse; namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { |
