From 1cea5bcbd8360fa7c5c9bfd273f27f472809a178 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 5 Apr 2016 22:18:56 -0400 Subject: improve identify feature --- .../HttpServer/HttpListenerHost.cs | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 3e4f4a70c..25463b660 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -344,6 +344,19 @@ namespace MediaBrowser.Server.Implementations.HttpServer LoggerUtils.LogRequest(_logger, urlToLog, httpReq.HttpMethod, httpReq.UserAgent); } + if (string.Equals(localPath, "/emby/", StringComparison.OrdinalIgnoreCase) || + string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase)) + { + httpRes.RedirectToUrl(DefaultRedirectPath); + return Task.FromResult(true); + } + if (string.Equals(localPath, "/emby", StringComparison.OrdinalIgnoreCase) || + string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase)) + { + httpRes.RedirectToUrl("emby/" + DefaultRedirectPath); + return Task.FromResult(true); + } + if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase) || localPath.IndexOf("mediabrowser/web", StringComparison.OrdinalIgnoreCase) != -1 || @@ -363,16 +376,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - if (string.Equals(localPath, "/emby/", StringComparison.OrdinalIgnoreCase)) - { - httpRes.RedirectToUrl(DefaultRedirectPath); - return Task.FromResult(true); - } - if (string.Equals(localPath, "/emby", StringComparison.OrdinalIgnoreCase)) - { - httpRes.RedirectToUrl("emby/" + DefaultRedirectPath); - return Task.FromResult(true); - } if (string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl(DefaultRedirectPath); -- cgit v1.2.3 From c94706d6eec63f705eb69ec87a4887f314986dc0 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 22 Apr 2016 12:12:20 -0400 Subject: update shutdown --- .../HttpServer/HttpListenerHost.cs | 17 +++++++++++++++++ .../ServerManager/ServerManager.cs | 19 +++++++++++++++++++ MediaBrowser.ServerApplication/MainStartup.cs | 5 +++-- 3 files changed, 39 insertions(+), 2 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 25463b660..c5cb810e5 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -179,6 +179,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer private void OnWebSocketConnecting(WebSocketConnectingEventArgs args) { + if (_disposed) + { + return; + } + if (WebSocketConnecting != null) { WebSocketConnecting(this, args); @@ -187,6 +192,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer private void OnWebSocketConnected(WebSocketConnectEventArgs args) { + if (_disposed) + { + return; + } + if (WebSocketConnected != null) { WebSocketConnected(this, args); @@ -331,6 +341,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer var httpRes = httpReq.Response; + if (_disposed) + { + httpRes.StatusCode = 503; + httpRes.Close(); + return Task.FromResult(true); + } + var operationName = httpReq.OperationName; var localPath = url.LocalPath; diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index 8719f5448..33d106916 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -71,6 +71,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The web socket listeners. private readonly List _webSocketListeners = new List(); + private bool _disposed; + /// /// Initializes a new instance of the class. /// @@ -143,6 +145,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The instance containing the event data. void HttpServer_WebSocketConnected(object sender, WebSocketConnectEventArgs e) { + if (_disposed) + { + return; + } + var connection = new WebSocketConnection(e.WebSocket, e.Endpoint, _jsonSerializer, _logger) { OnReceive = ProcessWebSocketMessageReceived, @@ -164,6 +171,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The result. private async void ProcessWebSocketMessageReceived(WebSocketMessageInfo result) { + if (_disposed) + { + return; + } + //_logger.Debug("Websocket message received: {0}", result.MessageType); var tasks = _webSocketListeners.Select(i => Task.Run(async () => @@ -244,6 +256,11 @@ namespace MediaBrowser.Server.Implementations.ServerManager throw new ArgumentNullException("dataFunction"); } + if (_disposed) + { + throw new ObjectDisposedException(GetType().Name); + } + cancellationToken.ThrowIfCancellationRequested(); var connectionsList = connections.Where(s => s.State == WebSocketState.Open).ToList(); @@ -301,6 +318,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// public void Dispose() { + _disposed = true; + Dispose(true); GC.SuppressFinalize(this); } diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index ac1b7ca99..f83c7d545 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -555,9 +555,10 @@ namespace MediaBrowser.ServerApplication private static void ShutdownWindowsApplication() { - _logger.Info("Calling Application.Exit"); - Application.Exit(); + //_logger.Info("Calling Application.Exit"); + //Application.Exit(); + _logger.Info("Calling Environment.Exit"); Environment.Exit(0); _logger.Info("Calling ApplicationTaskCompletionSource.SetResult"); -- cgit v1.2.3 From b6b6b85bf4d8bb6f58ea860a622dbc8902721b68 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 29 May 2016 17:04:49 -0400 Subject: remove x-frame-options --- MediaBrowser.Model/Configuration/ServerConfiguration.cs | 3 --- .../HttpServer/HttpListenerHost.cs | 2 +- MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs | 9 +-------- 3 files changed, 2 insertions(+), 12 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs') diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 1971b812e..df0e42869 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -184,8 +184,6 @@ namespace MediaBrowser.Model.Configuration public bool EnableVideoArchiveFiles { get; set; } public int RemoteClientBitrateLimit { get; set; } - public bool DenyIFrameEmbedding { get; set; } - public AutoOnOff EnableLibraryMonitor { get; set; } public int SharingExpirationDays { get; set; } @@ -222,7 +220,6 @@ namespace MediaBrowser.Model.Configuration EnableAnonymousUsageReporting = true; EnableAutomaticRestart = true; - DenyIFrameEmbedding = true; EnableUPnP = true; SharingExpirationDays = 30; diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index c5cb810e5..f091f0f1f 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -106,7 +106,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer } }); - HostContext.GlobalResponseFilters.Add(new ResponseFilter(_logger, () => _config.Configuration.DenyIFrameEmbedding).FilterResponse); + HostContext.GlobalResponseFilters.Add(new ResponseFilter(_logger).FilterResponse); } public override void OnAfterInit() diff --git a/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs b/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs index f993d4437..ee05702f4 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/ResponseFilter.cs @@ -12,12 +12,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer { private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); private readonly ILogger _logger; - private readonly Func _denyIframeEmbedding; - public ResponseFilter(ILogger logger, Func denyIframeEmbedding) + public ResponseFilter(ILogger logger) { _logger = logger; - _denyIframeEmbedding = denyIframeEmbedding; } /// @@ -31,11 +29,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer // Try to prevent compatibility view res.AddHeader("X-UA-Compatible", "IE=Edge"); - if (_denyIframeEmbedding()) - { - res.AddHeader("X-Frame-Options", "SAMEORIGIN"); - } - var exception = dto as Exception; if (exception != null) -- cgit v1.2.3 From 1900afb311aeb394ee6663e834cb2a25cd44c375 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 14 Jul 2016 15:13:52 -0400 Subject: update components --- .../HttpServer/HttpListenerHost.cs | 41 +++++++++---------- .../HttpServer/StreamWriter.cs | 47 ++++++++++++++++------ .../MediaBrowser.Server.Implementations.csproj | 4 +- .../packages.config | 2 +- 4 files changed, 56 insertions(+), 38 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index f091f0f1f..17e4793cb 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -335,7 +335,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// The HTTP req. /// The URL. /// Task. - protected Task RequestHandler(IHttpRequest httpReq, Uri url) + protected async Task RequestHandler(IHttpRequest httpReq, Uri url) { var date = DateTime.Now; @@ -345,7 +345,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer { httpRes.StatusCode = 503; httpRes.Close(); - return Task.FromResult(true); + return ; } var operationName = httpReq.OperationName; @@ -365,13 +365,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl(DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.Equals(localPath, "/emby", StringComparison.OrdinalIgnoreCase) || string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl("emby/" + DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase) || @@ -389,35 +389,35 @@ namespace MediaBrowser.Server.Implementations.HttpServer httpRes.Write("EmbyPlease update your Emby bookmark to " + newUrl + ""); httpRes.Close(); - return Task.FromResult(true); + return; } } if (string.Equals(localPath, "/web", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl(DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.Equals(localPath, "/web/", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl("../" + DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl(DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.IsNullOrEmpty(localPath)) { httpRes.RedirectToUrl("/" + DefaultRedirectPath); - return Task.FromResult(true); + return; } if (string.Equals(localPath, "/emby/pin", StringComparison.OrdinalIgnoreCase)) { httpRes.RedirectToUrl("web/pin.html"); - return Task.FromResult(true); + return; } if (!string.IsNullOrWhiteSpace(GlobalResponse)) @@ -427,7 +427,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer httpRes.Write(GlobalResponse); httpRes.Close(); - return Task.FromResult(true); + return; } var handler = HttpHandlerFactory.GetHandler(httpReq); @@ -443,13 +443,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName(); } - var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName); - - task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent); - //Matches Exceptions handled in HttpListenerBase.InitTask() - - task.ContinueWith(x => + try + { + await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName).ConfigureAwait(false); + } + finally { + httpRes.Close(); var statusCode = httpRes.StatusCode; var duration = DateTime.Now - date; @@ -458,13 +458,10 @@ namespace MediaBrowser.Server.Implementations.HttpServer { LoggerUtils.LogResponse(_logger, statusCode, urlToLog, remoteIp, duration); } - - }, TaskContinuationOptions.None); - return task; + } } - return new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo) - .AsTaskException(); + throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo); } /// diff --git a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs index a756f4aa8..e38e39322 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/StreamWriter.cs @@ -4,13 +4,15 @@ using System; using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Threading.Tasks; +using ServiceStack; namespace MediaBrowser.Server.Implementations.HttpServer { /// /// Class StreamWriter /// - public class StreamWriter : IStreamWriter, IHasOptions + public class StreamWriter : IStreamWriter, /*IAsyncStreamWriter,*/ IHasOptions { private ILogger Logger { get; set; } @@ -73,30 +75,49 @@ namespace MediaBrowser.Server.Implementations.HttpServer { } + // 256k + private const int BufferSize = 262144; + /// /// Writes to. /// /// The response stream. public void WriteTo(Stream responseStream) { - WriteToInternal(responseStream); + try + { + using (var src = SourceStream) + { + src.CopyTo(responseStream, BufferSize); + } + } + catch (Exception ex) + { + Logger.ErrorException("Error streaming data", ex); + + if (OnError != null) + { + OnError(); + } + + throw; + } + finally + { + if (OnComplete != null) + { + OnComplete(); + } + } } - // 256k - private const int BufferSize = 262144; - - /// - /// Writes to async. - /// - /// The response stream. - /// Task. - private void WriteToInternal(Stream responseStream) + public async Task WriteToAsync(Stream responseStream) { try { using (var src = SourceStream) { - src.CopyTo(responseStream, BufferSize); + await src.CopyToAsync(responseStream, BufferSize).ConfigureAwait(false); } } catch (Exception ex) @@ -107,7 +128,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer { OnError(); } - + throw; } finally diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index c78306911..2102eef82 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -73,8 +73,8 @@ ..\packages\SimpleInjector.3.2.0\lib\net45\SimpleInjector.dll True - - ..\packages\SocketHttpListener.1.0.0.30\lib\net45\SocketHttpListener.dll + + ..\packages\SocketHttpListener.1.0.0.32\lib\net45\SocketHttpListener.dll True diff --git a/MediaBrowser.Server.Implementations/packages.config b/MediaBrowser.Server.Implementations/packages.config index 9592ecb16..c4b46481e 100644 --- a/MediaBrowser.Server.Implementations/packages.config +++ b/MediaBrowser.Server.Implementations/packages.config @@ -9,5 +9,5 @@ - + \ No newline at end of file -- cgit v1.2.3