aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2017-09-20 13:22:39 -0400
committerGitHub <noreply@github.com>2017-09-20 13:22:39 -0400
commiteb2a1330045d802bfe0366df7105c220a36f111f (patch)
tree2c1638c424ee9c0837c5de6d6e08a2398da69cdb /Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
parentec426d5c92875639ceac64477ce10fab3e639335 (diff)
parenta015e1208885bc6a8788db683c4fe47e93dc26b7 (diff)
Merge pull request #2897 from MediaBrowser/beta
Beta
Diffstat (limited to 'Emby.Server.Implementations/HttpServer/HttpListenerHost.cs')
-rw-r--r--Emby.Server.Implementations/HttpServer/HttpListenerHost.cs138
1 files changed, 55 insertions, 83 deletions
diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
index f1fea2085..86df798d0 100644
--- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
+++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
@@ -7,10 +7,10 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
+using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
-using Emby.Server.Implementations.HttpServer;
using Emby.Server.Implementations.HttpServer.SocketSharp;
using Emby.Server.Implementations.Services;
using MediaBrowser.Common.Net;
@@ -24,8 +24,6 @@ using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Services;
using MediaBrowser.Model.System;
using MediaBrowser.Model.Text;
-using SocketHttpListener.Net;
-using SocketHttpListener.Primitives;
namespace Emby.Server.Implementations.HttpServer
{
@@ -34,7 +32,7 @@ namespace Emby.Server.Implementations.HttpServer
private string DefaultRedirectPath { get; set; }
private readonly ILogger _logger;
- public IEnumerable<string> UrlPrefixes { get; private set; }
+ public string[] UrlPrefixes { get; private set; }
private readonly List<IService> _restServices = new List<IService>();
@@ -56,14 +54,13 @@ namespace Emby.Server.Implementations.HttpServer
private readonly IFileSystem _fileSystem;
private readonly IJsonSerializer _jsonSerializer;
private readonly IXmlSerializer _xmlSerializer;
- private readonly ICertificate _certificate;
+ private readonly X509Certificate _certificate;
private readonly IEnvironmentInfo _environment;
- private readonly IStreamFactory _streamFactory;
private readonly Func<Type, Func<string, object>> _funcParseFn;
private readonly bool _enableDualModeSockets;
- public List<Action<IRequest, IResponse, object>> RequestFilters { get; set; }
- public List<Action<IRequest, IResponse, object>> ResponseFilters { get; set; }
+ public Action<IRequest, IResponse, object>[] RequestFilters { get; set; }
+ public Action<IRequest, IResponse, object>[] ResponseFilters { get; set; }
private readonly Dictionary<Type, Type> ServiceOperationsMap = new Dictionary<Type, Type>();
public static HttpListenerHost Instance { get; protected set; }
@@ -72,7 +69,7 @@ namespace Emby.Server.Implementations.HttpServer
ILogger logger,
IServerConfigurationManager config,
string serviceName,
- string defaultRedirectPath, INetworkManager networkManager, IMemoryStreamFactory memoryStreamProvider, ITextEncoding textEncoding, ISocketFactory socketFactory, ICryptoProvider cryptoProvider, IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IEnvironmentInfo environment, ICertificate certificate, IStreamFactory streamFactory, Func<Type, Func<string, object>> funcParseFn, bool enableDualModeSockets, IFileSystem fileSystem)
+ string defaultRedirectPath, INetworkManager networkManager, IMemoryStreamFactory memoryStreamProvider, ITextEncoding textEncoding, ISocketFactory socketFactory, ICryptoProvider cryptoProvider, IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IEnvironmentInfo environment, X509Certificate certificate, Func<Type, Func<string, object>> funcParseFn, bool enableDualModeSockets, IFileSystem fileSystem)
{
Instance = this;
@@ -87,7 +84,6 @@ namespace Emby.Server.Implementations.HttpServer
_xmlSerializer = xmlSerializer;
_environment = environment;
_certificate = certificate;
- _streamFactory = streamFactory;
_funcParseFn = funcParseFn;
_enableDualModeSockets = enableDualModeSockets;
_fileSystem = fileSystem;
@@ -95,8 +91,8 @@ namespace Emby.Server.Implementations.HttpServer
_logger = logger;
- RequestFilters = new List<Action<IRequest, IResponse, object>>();
- ResponseFilters = new List<Action<IRequest, IResponse, object>>();
+ RequestFilters = new Action<IRequest, IResponse, object>[] { };
+ ResponseFilters = new Action<IRequest, IResponse, object>[] { };
}
public string GlobalResponse { get; set; }
@@ -135,7 +131,9 @@ namespace Emby.Server.Implementations.HttpServer
//Exec all RequestFilter attributes with Priority < 0
var attributes = GetRequestFilterAttributes(requestDto.GetType());
var i = 0;
- for (; i < attributes.Length && attributes[i].Priority < 0; i++)
+ var count = attributes.Count;
+
+ for (; i < count && attributes[i].Priority < 0; i++)
{
var attribute = attributes[i];
attribute.RequestFilter(req, res, requestDto);
@@ -148,7 +146,7 @@ namespace Emby.Server.Implementations.HttpServer
}
//Exec remaining RequestFilter attributes with Priority >= 0
- for (; i < attributes.Length && attributes[i].Priority >= 0; i++)
+ for (; i < count && attributes[i].Priority >= 0; i++)
{
var attribute = attributes[i];
attribute.RequestFilter(req, res, requestDto);
@@ -167,7 +165,7 @@ namespace Emby.Server.Implementations.HttpServer
ServiceOperationsMap[requestType] = serviceType;
}
- private IHasRequestFilter[] GetRequestFilterAttributes(Type requestDtoType)
+ private List<IHasRequestFilter> GetRequestFilterAttributes(Type requestDtoType)
{
var attributes = requestDtoType.GetTypeInfo().GetCustomAttributes(true).OfType<IHasRequestFilter>().ToList();
@@ -179,40 +177,13 @@ namespace Emby.Server.Implementations.HttpServer
attributes.Sort((x, y) => x.Priority - y.Priority);
- return attributes.ToArray(attributes.Count);
- }
-
- /// <summary>
- /// Starts the Web Service
- /// </summary>
- private void StartListener()
- {
- WebSocketSharpRequest.HandlerFactoryPath = GetHandlerPathIfAny(UrlPrefixes.First());
-
- _listener = GetListener();
-
- _listener.WebSocketConnected = OnWebSocketConnected;
- _listener.WebSocketConnecting = OnWebSocketConnecting;
- _listener.ErrorHandler = ErrorHandler;
- _listener.RequestHandler = RequestHandler;
-
- _listener.Start(UrlPrefixes);
- }
-
- public static string GetHandlerPathIfAny(string listenerUrl)
- {
- if (listenerUrl == null) return null;
- var pos = listenerUrl.IndexOf("://", StringComparison.OrdinalIgnoreCase);
- if (pos == -1) return null;
- var startHostUrl = listenerUrl.Substring(pos + "://".Length);
- var endPos = startHostUrl.IndexOf('/');
- if (endPos == -1) return null;
- var endHostUrl = startHostUrl.Substring(endPos + 1);
- return string.IsNullOrEmpty(endHostUrl) ? null : endHostUrl.TrimEnd('/');
+ return attributes;
}
private IHttpListener GetListener()
{
+ //return new KestrelHost.KestrelListener(_logger, _environment, _fileSystem);
+
return new WebSocketSharpListener(_logger,
_certificate,
_memoryStreamProvider,
@@ -220,22 +191,11 @@ namespace Emby.Server.Implementations.HttpServer
_networkManager,
_socketFactory,
_cryptoProvider,
- _streamFactory,
_enableDualModeSockets,
- GetRequest,
_fileSystem,
_environment);
}
- private IHttpRequest GetRequest(HttpListenerContext httpContext)
- {
- var operationName = httpContext.Request.GetOperationName();
-
- var req = new WebSocketSharpRequest(httpContext, operationName, _logger, _memoryStreamProvider);
-
- return req;
- }
-
private void OnWebSocketConnecting(WebSocketConnectingEventArgs args)
{
if (_disposed)
@@ -348,7 +308,8 @@ namespace Emby.Server.Implementations.HttpServer
if (_listener != null)
{
_logger.Info("Stopping HttpListener...");
- _listener.Stop();
+ var task = _listener.Stop();
+ Task.WaitAll(task);
_logger.Info("HttpListener stopped");
}
}
@@ -434,7 +395,7 @@ namespace Emby.Server.Implementations.HttpServer
return address.Trim('/');
}
- private bool ValidateHost(Uri url)
+ private bool ValidateHost(string host)
{
var hosts = _config
.Configuration
@@ -447,7 +408,7 @@ namespace Emby.Server.Implementations.HttpServer
return true;
}
- var host = url.Host ?? string.Empty;
+ host = host ?? string.Empty;
_logger.Debug("Validating host {0}", host);
@@ -465,7 +426,7 @@ namespace Emby.Server.Implementations.HttpServer
/// <summary>
/// Overridable method that can be used to implement a custom hnandler
/// </summary>
- protected async Task RequestHandler(IHttpRequest httpReq, Uri url, CancellationToken cancellationToken)
+ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, string host, string localPath, CancellationToken cancellationToken)
{
var date = DateTime.Now;
var httpRes = httpReq.Response;
@@ -484,7 +445,7 @@ namespace Emby.Server.Implementations.HttpServer
return;
}
- if (!ValidateHost(url))
+ if (!ValidateHost(host))
{
httpRes.StatusCode = 400;
httpRes.ContentType = "text/plain";
@@ -504,9 +465,7 @@ namespace Emby.Server.Implementations.HttpServer
}
var operationName = httpReq.OperationName;
- var localPath = url.LocalPath;
- var urlString = url.OriginalString;
enableLog = EnableLogging(urlString, localPath);
urlToLog = urlString;
logHeaders = enableLog && urlToLog.IndexOf("/videos/", StringComparison.OrdinalIgnoreCase) != -1;
@@ -698,13 +657,18 @@ namespace Emby.Server.Implementations.HttpServer
ServiceController.Init(this, types);
- var requestFilters = _appHost.GetExports<IRequestFilter>().ToList();
- foreach (var filter in requestFilters)
+ var list = new List<Action<IRequest, IResponse, object>>();
+ foreach (var filter in _appHost.GetExports<IRequestFilter>())
{
- RequestFilters.Add(filter.Filter);
+ list.Add(filter.Filter);
}
- ResponseFilters.Add(new ResponseFilter(_logger).FilterResponse);
+ RequestFilters = list.ToArray();
+
+ ResponseFilters = new Action<IRequest, IResponse, object>[]
+ {
+ new ResponseFilter(_logger).FilterResponse
+ };
}
public RouteAttribute[] GetRouteAttributes(Type requestType)
@@ -721,19 +685,19 @@ namespace Emby.Server.Implementations.HttpServer
Summary = route.Summary
});
- routes.Add(new RouteAttribute(NormalizeRoutePath(route.Path), route.Verbs)
+ routes.Add(new RouteAttribute(NormalizeMediaBrowserRoutePath(route.Path), route.Verbs)
{
Notes = route.Notes,
Priority = route.Priority,
Summary = route.Summary
});
- routes.Add(new RouteAttribute(DoubleNormalizeEmbyRoutePath(route.Path), route.Verbs)
- {
- Notes = route.Notes,
- Priority = route.Priority,
- Summary = route.Summary
- });
+ //routes.Add(new RouteAttribute(DoubleNormalizeEmbyRoutePath(route.Path), route.Verbs)
+ //{
+ // Notes = route.Notes,
+ // Priority = route.Priority,
+ // Summary = route.Summary
+ //});
}
return routes.ToArray(routes.Count);
@@ -774,24 +738,24 @@ namespace Emby.Server.Implementations.HttpServer
return "emby/" + path;
}
- private string DoubleNormalizeEmbyRoutePath(string path)
+ private string NormalizeMediaBrowserRoutePath(string path)
{
if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
{
- return "/emby/emby" + path;
+ return "/mediabrowser" + path;
}
- return "emby/emby/" + path;
+ return "mediabrowser/" + path;
}
- private string NormalizeRoutePath(string path)
+ private string DoubleNormalizeEmbyRoutePath(string path)
{
if (path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
{
- return "/mediabrowser" + path;
+ return "/emby/emby" + path;
}
- return "mediabrowser/" + path;
+ return "emby/emby/" + path;
}
private bool _disposed;
@@ -819,10 +783,18 @@ namespace Emby.Server.Implementations.HttpServer
GC.SuppressFinalize(this);
}
- public void StartServer(IEnumerable<string> urlPrefixes)
+ public void StartServer(string[] urlPrefixes)
{
- UrlPrefixes = urlPrefixes.ToList();
- StartListener();
+ UrlPrefixes = urlPrefixes;
+
+ _listener = GetListener();
+
+ _listener.WebSocketConnected = OnWebSocketConnected;
+ _listener.WebSocketConnecting = OnWebSocketConnecting;
+ _listener.ErrorHandler = ErrorHandler;
+ _listener.RequestHandler = RequestHandler;
+
+ _listener.Start(UrlPrefixes);
}
}
} \ No newline at end of file