diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer')
5 files changed, 30 insertions, 64 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 1cec4461b..16ca8b099 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -14,6 +14,7 @@ using ServiceStack.Host.HttpListener; using ServiceStack.Logging; using ServiceStack.Web; using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; @@ -39,13 +40,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer public event EventHandler<WebSocketConnectEventArgs> WebSocketConnected; + private readonly ConcurrentDictionary<string, string> _localEndPoints = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase); + /// <summary> /// Gets the local end points. /// </summary> /// <value>The local end points.</value> public IEnumerable<string> LocalEndPoints { - get { return _listener == null ? new List<string>() : _listener.LocalEndPoints; } + get { return _listener == null ? new List<string>() : _localEndPoints.Keys.ToList(); } } public HttpListenerHost(IApplicationHost applicationHost, ILogManager logManager, string serviceName, string handlerPath, string defaultRedirectPath, params Assembly[] assembliesWithServices) @@ -151,6 +154,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer return this; } + private void OnRequestReceived(string localEndPoint) + { + _localEndPoints.GetOrAdd(localEndPoint, localEndPoint); + } + /// <summary> /// Starts the Web Service /// </summary> @@ -159,9 +167,9 @@ namespace MediaBrowser.Server.Implementations.HttpServer HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); _listener = NativeWebSocket.IsSupported - ? _listener = new HttpListenerServer(_logger) + ? _listener = new HttpListenerServer(_logger, OnRequestReceived) //? _listener = new WebSocketSharpListener(_logger) - : _listener = new WebSocketSharpListener(_logger); + : _listener = new WebSocketSharpListener(_logger, OnRequestReceived); _listener.WebSocketHandler = WebSocketHandler; _listener.ErrorHandler = ErrorHandler; diff --git a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs index 1d80a263c..86e8856cf 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/IHttpListener.cs @@ -8,8 +8,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer { public interface IHttpListener : IDisposable { - IEnumerable<string> LocalEndPoints { get; } - /// <summary> /// Gets or sets the error handler. /// </summary> diff --git a/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs index bdc2750fb..118bec60e 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/NetListener/HttpListenerServer.cs @@ -4,7 +4,6 @@ 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; @@ -20,26 +19,18 @@ namespace MediaBrowser.Server.Implementations.HttpServer.NetListener private HttpListener _listener; private readonly AutoResetEvent _listenForNextRequest = new AutoResetEvent(false); - public System.Action<Exception, IRequest> ErrorHandler { get; set; } + public Action<Exception, IRequest> ErrorHandler { get; set; } public Action<WebSocketConnectEventArgs> WebSocketHandler { get; set; } - public System.Func<IHttpRequest, Uri, Task> RequestHandler { get; set; } + public Func<IHttpRequest, Uri, Task> RequestHandler { get; set; } - private readonly ConcurrentDictionary<string, string> _localEndPoints = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase); + private readonly Action<string> _endpointListener; - public HttpListenerServer(ILogger logger) + public HttpListenerServer(ILogger logger, Action<string> endpointListener) { _logger = logger; + _endpointListener = endpointListener; } - /// <summary> - /// Gets the local end points. - /// </summary> - /// <value>The local end points.</value> - public IEnumerable<string> LocalEndPoints - { - get { return _localEndPoints.Keys.ToList(); } - } - private List<string> UrlPrefixes { get; set; } public void Start(IEnumerable<string> urlPrefixes) @@ -47,7 +38,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.NetListener UrlPrefixes = urlPrefixes.ToList(); if (_listener == null) - _listener = new System.Net.HttpListener(); + _listener = new HttpListener(); //HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); @@ -229,7 +220,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.NetListener { var address = endpoint.ToString(); - _localEndPoints.GetOrAdd(address, address); + _endpointListener(address); } LogRequest(_logger, request); diff --git a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs index 19870c435..2b9ae7d09 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/Security/AuthService.cs @@ -42,7 +42,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security /// </summary> public string HtmlRedirect { get; set; } - public void Authenticate(IRequest req, IResponse res, object requestDto) + public void Authenticate(IRequest req, IResponse res, object requestDto, bool allowLocal) { if (HostContext.HasValidAuthSecret(req)) return; @@ -50,13 +50,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security //ExecuteBasic(req, res, requestDto); //first check if session is authenticated //if (res.IsClosed) return; //AuthenticateAttribute already closed the request (ie auth failed) - ValidateUser(req); + ValidateUser(req, allowLocal); } // TODO: Remove this when all clients have supported the new sescurity - private readonly List<string> _updatedClients = new List<string>(){"Dashboard"}; + private readonly List<string> _updatedClients = new List<string>() { "Dashboard", "Chromecast" }; - private void ValidateUser(IRequest req) + private void ValidateUser(IRequest req, bool allowLocal) { //This code is executed before the service var auth = AuthorizationContext.GetAuthorizationInfo(req); @@ -65,7 +65,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security || _config.Configuration.EnableTokenAuthentication || _updatedClients.Contains(auth.Client ?? string.Empty, StringComparer.OrdinalIgnoreCase)) { - SessionManager.ValidateSecurityToken(auth.Token); + if (!allowLocal || !req.IsLocal) + { + SessionManager.ValidateSecurityToken(auth.Token); + } } var user = string.IsNullOrWhiteSpace(auth.UserId) @@ -96,35 +99,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer.Security } } - private void ExecuteBasic(IRequest req, IResponse res, object requestDto) - { - if (AuthenticateService.AuthProviders == null) - throw new InvalidOperationException( - "The AuthService must be initialized by calling AuthService.Init to use an authenticate attribute"); - - var matchingOAuthConfigs = AuthenticateService.AuthProviders.Where(x => - this.Provider.IsNullOrEmpty() - || x.Provider == this.Provider).ToList(); - - if (matchingOAuthConfigs.Count == 0) - { - res.WriteError(req, requestDto, "No OAuth Configs found matching {0} provider" - .Fmt(this.Provider ?? "any")); - res.EndRequest(); - } - - matchingOAuthConfigs.OfType<IAuthWithRequest>() - .Each(x => x.PreAuthenticate(req, res)); - - var session = req.GetSession(); - if (session == null || !matchingOAuthConfigs.Any(x => session.IsAuthorized(x.Provider))) - { - if (this.DoHtmlRedirectIfConfigured(req, res, true)) return; - - AuthProvider.HandleFailedAuth(matchingOAuthConfigs[0], session, req, res); - } - } - protected bool DoHtmlRedirectIfConfigured(IRequest req, IResponse res, bool includeRedirectParam = false) { var htmlRedirect = this.HtmlRedirect ?? AuthenticateService.HtmlRedirect; diff --git a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 477aa3878..f2fae9e90 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -3,7 +3,6 @@ using MediaBrowser.Model.Logging; using ServiceStack; using ServiceStack.Web; using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; @@ -15,20 +14,16 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { public class WebSocketSharpListener : IHttpListener { - private readonly ConcurrentDictionary<string, string> _localEndPoints = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase); private WebSocketSharp.Net.HttpListener _listener; private readonly AutoResetEvent _listenForNextRequest = new AutoResetEvent(false); private readonly ILogger _logger; + private readonly Action<string> _endpointListener; - public WebSocketSharpListener(ILogger logger) + public WebSocketSharpListener(ILogger logger, Action<string> endpointListener) { _logger = logger; - } - - public IEnumerable<string> LocalEndPoints - { - get { return _localEndPoints.Keys.ToList(); } + _endpointListener = endpointListener; } public Action<Exception, IRequest> ErrorHandler { get; set; } @@ -170,7 +165,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer.SocketSharp { var address = endpoint.ToString(); - _localEndPoints.GetOrAdd(address, address); + _endpointListener(address); } LogRequest(_logger, request); |
