diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-05-31 15:40:34 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-31 15:40:34 -0400 |
| commit | 91176d9ccc1dde8155c10411c70e62a9f4b059d5 (patch) | |
| tree | 21365f5a8dd09534a53d9f88d2a7a3116f3f3f98 /Emby.Server.Implementations/HttpServer | |
| parent | c37c9a75073b1b9caa3af2c3bc62abd837bd630e (diff) | |
| parent | 4e10daf646e0788409f2bc52ef70effa2616e3f3 (diff) | |
Merge pull request #2677 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/HttpServer')
7 files changed, 38 insertions, 26 deletions
diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 5e96eda94..79209d438 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer.SocketSharp; @@ -445,10 +446,7 @@ namespace Emby.Server.Implementations.HttpServer /// <summary> /// Overridable method that can be used to implement a custom hnandler /// </summary> - /// <param name="httpReq">The HTTP req.</param> - /// <param name="url">The URL.</param> - /// <returns>Task.</returns> - protected async Task RequestHandler(IHttpRequest httpReq, Uri url) + protected async Task RequestHandler(IHttpRequest httpReq, Uri url, CancellationToken cancellationToken) { var date = DateTime.Now; var httpRes = httpReq.Response; @@ -589,7 +587,7 @@ namespace Emby.Server.Implementations.HttpServer if (handler != null) { - await handler.ProcessRequestAsync(this, httpReq, httpRes, Logger, operationName).ConfigureAwait(false); + await handler.ProcessRequestAsync(this, httpReq, httpRes, Logger, operationName, cancellationToken).ConfigureAwait(false); } else { diff --git a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs index 0af88595f..6c37d5f7a 100644 --- a/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs +++ b/Emby.Server.Implementations/HttpServer/HttpResultFactory.cs @@ -58,6 +58,18 @@ namespace Emby.Server.Implementations.HttpServer return GetHttpResult(content, contentType, true, responseHeaders); } + public object GetRedirectResult(string url) + { + var responseHeaders = new Dictionary<string, string>(); + responseHeaders["Location"] = url; + + var result = new HttpResult(new byte[] { }, "text/plain", HttpStatusCode.Redirect); + + AddResponseHeaders(result, responseHeaders); + + return result; + } + /// <summary> /// Gets the HTTP result. /// </summary> @@ -599,9 +611,9 @@ namespace Emby.Server.Implementations.HttpServer } } - private async Task<IHasHeaders> GetCompressedResult(Stream stream, - string requestedCompressionType, - IDictionary<string,string> responseHeaders, + private async Task<IHasHeaders> GetCompressedResult(Stream stream, + string requestedCompressionType, + IDictionary<string, string> responseHeaders, bool isHeadRequest, string contentType) { diff --git a/Emby.Server.Implementations/HttpServer/IHttpListener.cs b/Emby.Server.Implementations/HttpServer/IHttpListener.cs index 18df5682d..82175dbed 100644 --- a/Emby.Server.Implementations/HttpServer/IHttpListener.cs +++ b/Emby.Server.Implementations/HttpServer/IHttpListener.cs @@ -1,6 +1,7 @@ using MediaBrowser.Controller.Net; using System; using System.Collections.Generic; +using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.Services; @@ -18,7 +19,7 @@ namespace Emby.Server.Implementations.HttpServer /// Gets or sets the request handler. /// </summary> /// <value>The request handler.</value> - Func<IHttpRequest, Uri, Task> RequestHandler { get; set; } + Func<IHttpRequest, Uri, CancellationToken, Task> RequestHandler { get; set; } /// <summary> /// Gets or sets the web socket handler. diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs index 682fa7a0b..e648838b2 100644 --- a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs +++ b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpListener.cs @@ -4,6 +4,7 @@ using SocketHttpListener.Net; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Net; using MediaBrowser.Model.Cryptography; @@ -33,6 +34,9 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp private readonly bool _enableDualMode; private readonly IEnvironmentInfo _environment; + private CancellationTokenSource _disposeCancellationTokenSource = new CancellationTokenSource(); + private CancellationToken _disposeCancellationToken; + public WebSocketSharpListener(ILogger logger, ICertificate certificate, IMemoryStreamFactory memoryStreamProvider, ITextEncoding textEncoding, INetworkManager networkManager, ISocketFactory socketFactory, ICryptoProvider cryptoProvider, IStreamFactory streamFactory, bool enableDualMode, Func<HttpListenerContext, IHttpRequest> httpRequestFactory, IFileSystem fileSystem, IEnvironmentInfo environment) { _logger = logger; @@ -47,10 +51,12 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp _httpRequestFactory = httpRequestFactory; _fileSystem = fileSystem; _environment = environment; + + _disposeCancellationToken = _disposeCancellationTokenSource.Token; } public Action<Exception, IRequest, bool> ErrorHandler { get; set; } - public Func<IHttpRequest, Uri, Task> RequestHandler { get; set; } + public Func<IHttpRequest, Uri, CancellationToken, Task> RequestHandler { get; set; } public Action<WebSocketConnectingEventArgs> WebSocketConnecting { get; set; } @@ -81,11 +87,11 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp private void ProcessContext(HttpListenerContext context) { - //Task.Factory.StartNew(() => InitTask(context), TaskCreationOptions.DenyChildAttach | TaskCreationOptions.PreferFairness); - Task.Run(() => InitTask(context)); + InitTask(context, _disposeCancellationToken); + //Task.Run(() => InitTask(context, _disposeCancellationToken)); } - private Task InitTask(HttpListenerContext context) + private Task InitTask(HttpListenerContext context, CancellationToken cancellationToken) { IHttpRequest httpReq = null; var request = context.Request; @@ -111,7 +117,7 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp return Task.FromResult(true); } - return RequestHandler(httpReq, request.Url); + return RequestHandler(httpReq, request.Url, cancellationToken); } private void ProcessWebSocketRequest(HttpListenerContext ctx) @@ -172,6 +178,8 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp public void Stop() { + _disposeCancellationTokenSource.Cancel(); + if (_listener != null) { foreach (var prefix in _listener.Prefixes.ToList()) diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs index b3fcde745..2dfe6a9e3 100644 --- a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs +++ b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpRequest.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Net; using System.Text; using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer.SocketSharp; @@ -374,7 +373,7 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp this.pathInfo = request.RawUrl; } - this.pathInfo = WebUtility.UrlDecode(pathInfo); + this.pathInfo = System.Net.WebUtility.UrlDecode(pathInfo); this.pathInfo = NormalizePathInfo(pathInfo, mode); } return this.pathInfo; @@ -440,7 +439,7 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp cookies = new Dictionary<string, System.Net.Cookie>(); foreach (var cookie in this.request.Cookies) { - var httpCookie = (Cookie) cookie; + var httpCookie = (System.Net.Cookie) cookie; cookies[httpCookie.Name] = new System.Net.Cookie(httpCookie.Name, httpCookie.Value, httpCookie.Path, httpCookie.Domain); } } diff --git a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs index 9e58ee57c..d6762d94b 100644 --- a/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs +++ b/Emby.Server.Implementations/HttpServer/SocketSharp/WebSocketSharpResponse.cs @@ -114,15 +114,9 @@ namespace Emby.Server.Implementations.HttpServer.SocketSharp var outputStream = response.OutputStream; // This is needed with compression - if (outputStream is ResponseStream) - { - //if (!string.IsNullOrWhiteSpace(GetHeader("Content-Encoding"))) - { - outputStream.Flush(); - } + outputStream.Flush(); + outputStream.Dispose(); - outputStream.Dispose(); - } response.Close(); } catch (Exception ex) diff --git a/Emby.Server.Implementations/HttpServer/StreamWriter.cs b/Emby.Server.Implementations/HttpServer/StreamWriter.cs index 33378949c..5d42f42fa 100644 --- a/Emby.Server.Implementations/HttpServer/StreamWriter.cs +++ b/Emby.Server.Implementations/HttpServer/StreamWriter.cs @@ -5,7 +5,7 @@ using System.Globalization; using System.IO; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Common.IO; + using MediaBrowser.Model.Services; namespace Emby.Server.Implementations.HttpServer |
