| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624 | using Funq;using MediaBrowser.Common;using MediaBrowser.Common.Extensions;using MediaBrowser.Common.Net;using MediaBrowser.Model.Logging;using ServiceStack.Api.Swagger;using ServiceStack.Common.Web;using ServiceStack.Configuration;using ServiceStack.Logging;using ServiceStack.ServiceHost;using ServiceStack.ServiceInterface.Cors;using ServiceStack.Text;using ServiceStack.WebHost.Endpoints;using ServiceStack.WebHost.Endpoints.Extensions;using ServiceStack.WebHost.Endpoints.Support;using System;using System.Collections.Concurrent;using System.Collections.Generic;using System.Globalization;using System.IO;using System.Linq;using System.Net;using System.Net.WebSockets;using System.Reactive.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace MediaBrowser.Server.Implementations.HttpServer{    /// <summary>    /// Class HttpServer    /// </summary>    public class HttpServer : HttpListenerBase, IHttpServer    {        /// <summary>        /// The logger        /// </summary>        private readonly ILogger _logger;        /// <summary>        /// Gets the URL prefix.        /// </summary>        /// <value>The URL prefix.</value>        public string UrlPrefix { get; private set; }        /// <summary>        /// The _rest services        /// </summary>        private readonly List<IRestfulService> _restServices = new List<IRestfulService>();        /// <summary>        /// This subscribes to HttpListener requests and finds the appropriate BaseHandler to process it        /// </summary>        /// <value>The HTTP listener.</value>        private IDisposable HttpListener { get; set; }        /// <summary>        /// Occurs when [web socket connected].        /// </summary>        public event EventHandler<WebSocketConnectEventArgs> WebSocketConnected;        /// <summary>        /// Gets the default redirect path.        /// </summary>        /// <value>The default redirect path.</value>        private string DefaultRedirectPath { get; set; }        /// <summary>        /// Gets or sets the name of the server.        /// </summary>        /// <value>The name of the server.</value>        private string ServerName { get; set; }        /// <summary>        /// The _container adapter        /// </summary>        private readonly ContainerAdapter _containerAdapter;        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 _localEndPoints.Keys.ToList(); }        }        /// <summary>        /// Initializes a new instance of the <see cref="HttpServer" /> class.        /// </summary>        /// <param name="applicationHost">The application host.</param>        /// <param name="logManager">The log manager.</param>        /// <param name="serverName">Name of the server.</param>        /// <param name="defaultRedirectpath">The default redirectpath.</param>        /// <exception cref="System.ArgumentNullException">urlPrefix</exception>        public HttpServer(IApplicationHost applicationHost, ILogManager logManager, string serverName, string defaultRedirectpath)            : base()        {            if (logManager == null)            {                throw new ArgumentNullException("logManager");            }            if (applicationHost == null)            {                throw new ArgumentNullException("applicationHost");            }            if (string.IsNullOrEmpty(serverName))            {                throw new ArgumentNullException("serverName");            }            if (string.IsNullOrEmpty(defaultRedirectpath))            {                throw new ArgumentNullException("defaultRedirectpath");            }            ServerName = serverName;            DefaultRedirectPath = defaultRedirectpath;            _logger = logManager.GetLogger("HttpServer");            LogManager.LogFactory = new ServerLogFactory(logManager);            EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath = null;            EndpointHostConfig.Instance.MetadataRedirectPath = "metadata";            _containerAdapter = new ContainerAdapter(applicationHost);        }        /// <summary>        /// The us culture        /// </summary>        protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");        /// <summary>        /// Configures the specified container.        /// </summary>        /// <param name="container">The container.</param>        public override void Configure(Container container)        {            JsConfig.DateHandler = JsonDateHandler.ISO8601;            JsConfig.ExcludeTypeInfo = true;            JsConfig.IncludeNullValues = false;            SetConfig(new EndpointHostConfig            {                DefaultRedirectPath = DefaultRedirectPath,                MapExceptionToStatusCode = {                    { typeof(InvalidOperationException), 422 },                    { typeof(ResourceNotFoundException), 404 },                    { typeof(FileNotFoundException), 404 },                    { typeof(DirectoryNotFoundException), 404 }                },                DebugMode = true,                ServiceName = ServerName,                LogFactory = LogManager.LogFactory,                // The Markdown feature causes slow startup times (5 mins+) on cold boots for some users                // Custom format allows images                EnableFeatures = Feature.Csv | Feature.Html | Feature.Json | Feature.Jsv | Feature.Metadata | Feature.Xml | Feature.CustomFormat            });            container.Adapter = _containerAdapter;            Plugins.Add(new SwaggerFeature());            Plugins.Add(new CorsFeature());            ResponseFilters.Add(FilterResponse);        }        /// <summary>        /// Filters the response.        /// </summary>        /// <param name="req">The req.</param>        /// <param name="res">The res.</param>        /// <param name="dto">The dto.</param>        private void FilterResponse(IHttpRequest req, IHttpResponse res, object dto)        {            var exception = dto as Exception;            if (exception != null)            {                _logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);                if (!string.IsNullOrEmpty(exception.Message))                {                    var error = exception.Message.Replace(Environment.NewLine, " ");                    error = RemoveControlCharacters(error);                    res.AddHeader("X-Application-Error-Code", error);                }            }            if (dto is CompressedResult)            {                // Per Google PageSpeed                // This instructs the proxies to cache two versions of the resource: one compressed, and one uncompressed.                 // The correct version of the resource is delivered based on the client request header.                 // This is a good choice for applications that are singly homed and depend on public proxies for user locality.                                        res.AddHeader("Vary", "Accept-Encoding");            }            var hasOptions = dto as IHasOptions;            if (hasOptions != null)            {                // Content length has to be explicitly set on on HttpListenerResponse or it won't be happy                string contentLength;                if (hasOptions.Options.TryGetValue("Content-Length", out contentLength) && !string.IsNullOrEmpty(contentLength))                {                    var length = long.Parse(contentLength, UsCulture);                    if (length > 0)                    {                        var response = (HttpListenerResponse)res.OriginalResponse;                        response.ContentLength64 = length;                        // Disable chunked encoding. Technically this is only needed when using Content-Range, but                        // anytime we know the content length there's no need for it                        response.SendChunked = false;                    }                }            }        }        /// <summary>        /// Removes the control characters.        /// </summary>        /// <param name="inString">The in string.</param>        /// <returns>System.String.</returns>        private static string RemoveControlCharacters(string inString)        {            if (inString == null) return null;            var newString = new StringBuilder();            foreach (var ch in inString)            {                if (!char.IsControl(ch))                {                    newString.Append(ch);                }            }            return newString.ToString();        }        /// <summary>        /// Starts the Web Service        /// </summary>        /// <param name="urlBase">A Uri that acts as the base that the server is listening on.        /// Format should be: http://127.0.0.1:8080/ or http://127.0.0.1:8080/somevirtual/        /// Note: the trailing slash is required! For more info see the        /// HttpListener.Prefixes property on MSDN.</param>        /// <exception cref="System.ArgumentNullException">urlBase</exception>        public override void Start(string urlBase)        {            if (string.IsNullOrEmpty(urlBase))            {                throw new ArgumentNullException("urlBase");            }            // *** Already running - just leave it in place            if (IsStarted)            {                return;            }            if (Listener == null)            {                _logger.Info("Creating HttpListner");                Listener = new HttpListener();            }            EndpointHost.Config.ServiceStackHandlerFactoryPath = HttpListenerRequestWrapper.GetHandlerPathIfAny(urlBase);            UrlPrefix = urlBase;            _logger.Info("Adding HttpListener Prefixes");            Listener.Prefixes.Add(urlBase);            IsStarted = true;            _logger.Info("Starting HttpListner");            Listener.Start();            _logger.Info("Creating HttpListner observable stream");            HttpListener = CreateObservableStream().Subscribe(ProcessHttpRequestAsync);        }        /// <summary>        /// Creates the observable stream.        /// </summary>        /// <returns>IObservable{HttpListenerContext}.</returns>        private IObservable<HttpListenerContext> CreateObservableStream()        {            return Observable.Create<HttpListenerContext>(obs =>                                Observable.FromAsync(() => Listener.GetContextAsync())                                          .Subscribe(obs))                             .Repeat()                             .Retry()                             .Publish()                             .RefCount();        }        /// <summary>        /// Processes incoming http requests by routing them to the appropiate handler        /// </summary>        /// <param name="context">The CTX.</param>        private async void ProcessHttpRequestAsync(HttpListenerContext context)        {            LogHttpRequest(context);            if (context.Request.IsWebSocketRequest)            {                await ProcessWebSocketRequest(context).ConfigureAwait(false);                return;            }            var localPath = context.Request.Url.LocalPath;            if (string.Equals(localPath, "/mediabrowser/", StringComparison.OrdinalIgnoreCase))            {                context.Response.Redirect(DefaultRedirectPath);                context.Response.Close();                return;            }            if (string.Equals(localPath, "/mediabrowser", StringComparison.OrdinalIgnoreCase))            {                context.Response.Redirect("mediabrowser/" + DefaultRedirectPath);                context.Response.Close();                return;            }            if (string.Equals(localPath, "/", StringComparison.OrdinalIgnoreCase))            {                context.Response.Redirect("mediabrowser/" + DefaultRedirectPath);                context.Response.Close();                return;            }            if (string.IsNullOrEmpty(localPath))            {                context.Response.Redirect("/mediabrowser/" + DefaultRedirectPath);                context.Response.Close();                return;            }            RaiseReceiveWebRequest(context);            await Task.Factory.StartNew(() =>            {                try                {                    ProcessRequest(context);                }                catch (Exception ex)                {                    _logger.ErrorException("ProcessRequest failure", ex);                }            }).ConfigureAwait(false);        }        /// <summary>        /// Processes the web socket request.        /// </summary>        /// <param name="ctx">The CTX.</param>        /// <returns>Task.</returns>        private async Task ProcessWebSocketRequest(HttpListenerContext ctx)        {            try            {                var webSocketContext = await ctx.AcceptWebSocketAsync(null).ConfigureAwait(false);                if (WebSocketConnected != null)                {                    WebSocketConnected(this, new WebSocketConnectEventArgs { WebSocket = new NativeWebSocket(webSocketContext.WebSocket, _logger), Endpoint = ctx.Request.RemoteEndPoint.ToString() });                }            }            catch (Exception ex)            {                _logger.ErrorException("AcceptWebSocketAsync error", ex);                ctx.Response.StatusCode = 500;                ctx.Response.Close();            }        }        /// <summary>        /// Logs the HTTP request.        /// </summary>        /// <param name="ctx">The CTX.</param>        private void LogHttpRequest(HttpListenerContext ctx)        {            var endpoint = ctx.Request.LocalEndPoint;            if (endpoint != null)            {                var address = endpoint.ToString();                _localEndPoints.GetOrAdd(address, address);            }            if (EnableHttpRequestLogging)            {                var log = new StringBuilder();                log.AppendLine("Url: " + ctx.Request.Url);                log.AppendLine("Headers: " + string.Join(",", ctx.Request.Headers.AllKeys.Select(k => k + "=" + ctx.Request.Headers[k])));                var type = ctx.Request.IsWebSocketRequest ? "Web Socket" : "HTTP " + ctx.Request.HttpMethod;                _logger.LogMultiline(type + " request received from " + ctx.Request.RemoteEndPoint, LogSeverity.Debug, log);            }        }        /// <summary>        /// Overridable method that can be used to implement a custom hnandler        /// </summary>        /// <param name="context">The context.</param>        /// <exception cref="System.NotImplementedException">Cannot execute handler:  + handler +  at PathInfo:  + httpReq.PathInfo</exception>        protected override void ProcessRequest(HttpListenerContext context)        {            if (string.IsNullOrEmpty(context.Request.RawUrl)) return;            var operationName = context.Request.GetOperationName();            var httpReq = new HttpListenerRequestWrapper(operationName, context.Request);            var httpRes = new HttpListenerResponseWrapper(context.Response);            var handler = ServiceStackHttpHandlerFactory.GetHandler(httpReq);            var url = context.Request.Url.ToString();            var endPoint = context.Request.RemoteEndPoint;            var serviceStackHandler = handler as IServiceStackHttpHandler;            if (serviceStackHandler != null)            {                var restHandler = serviceStackHandler as RestHandler;                if (restHandler != null)                {                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.Name;                }                serviceStackHandler.ProcessRequest(httpReq, httpRes, operationName);                LogResponse(context, url, endPoint);                return;            }            throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);        }        /// <summary>        /// Logs the response.        /// </summary>        /// <param name="ctx">The CTX.</param>        /// <param name="url">The URL.</param>        /// <param name="endPoint">The end point.</param>        private void LogResponse(HttpListenerContext ctx, string url, IPEndPoint endPoint)        {            if (!EnableHttpRequestLogging)            {                return;            }            var statusode = ctx.Response.StatusCode;            var log = new StringBuilder();            log.AppendLine(string.Format("Url: {0}", url));            log.AppendLine("Headers: " + string.Join(",", ctx.Response.Headers.AllKeys.Select(k => k + "=" + ctx.Response.Headers[k])));            var msg = "Http Response Sent (" + statusode + ") to " + endPoint;            _logger.LogMultiline(msg, LogSeverity.Debug, log);        }        /// <summary>        /// Creates the service manager.        /// </summary>        /// <param name="assembliesWithServices">The assemblies with services.</param>        /// <returns>ServiceManager.</returns>        protected override ServiceManager CreateServiceManager(params Assembly[] assembliesWithServices)        {            var types = _restServices.Select(r => r.GetType()).ToArray();            return new ServiceManager(new Container(), new ServiceController(() => types));        }        /// <summary>        /// Shut down the Web Service        /// </summary>        public override void Stop()        {            if (HttpListener != null)            {                HttpListener.Dispose();                HttpListener = null;            }            if (Listener != null)            {                Listener.Prefixes.Remove(UrlPrefix);            }            base.Stop();        }        /// <summary>        /// The _supports native web socket        /// </summary>        private bool? _supportsNativeWebSocket;        /// <summary>        /// Gets a value indicating whether [supports web sockets].        /// </summary>        /// <value><c>true</c> if [supports web sockets]; otherwise, <c>false</c>.</value>        public bool SupportsWebSockets        {            get            {                if (!_supportsNativeWebSocket.HasValue)                {                    try                    {                        new ClientWebSocket();                        _supportsNativeWebSocket = true;                    }                    catch (PlatformNotSupportedException)                    {                        _supportsNativeWebSocket = false;                    }                }                return _supportsNativeWebSocket.Value;            }        }        /// <summary>        /// Gets or sets a value indicating whether [enable HTTP request logging].        /// </summary>        /// <value><c>true</c> if [enable HTTP request logging]; otherwise, <c>false</c>.</value>        public bool EnableHttpRequestLogging { get; set; }        /// <summary>        /// Adds the rest handlers.        /// </summary>        /// <param name="services">The services.</param>        public void Init(IEnumerable<IRestfulService> services)        {            _restServices.AddRange(services);            _logger.Info("Calling EndpointHost.ConfigureHost");            EndpointHost.ConfigureHost(this, ServerName, CreateServiceManager());            _logger.Info("Calling ServiceStack AppHost.Init");            Init();        }        /// <summary>        /// Releases the specified instance.        /// </summary>        /// <param name="instance">The instance.</param>        public override void Release(object instance)        {            // Leave this empty so SS doesn't try to dispose our objects        }    }    /// <summary>    /// Class ContainerAdapter    /// </summary>    class ContainerAdapter : IContainerAdapter, IRelease    {        /// <summary>        /// The _app host        /// </summary>        private readonly IApplicationHost _appHost;        /// <summary>        /// Initializes a new instance of the <see cref="ContainerAdapter" /> class.        /// </summary>        /// <param name="appHost">The app host.</param>        public ContainerAdapter(IApplicationHost appHost)        {            _appHost = appHost;        }        /// <summary>        /// Resolves this instance.        /// </summary>        /// <typeparam name="T"></typeparam>        /// <returns>``0.</returns>        public T Resolve<T>()        {            return _appHost.Resolve<T>();        }        /// <summary>        /// Tries the resolve.        /// </summary>        /// <typeparam name="T"></typeparam>        /// <returns>``0.</returns>        public T TryResolve<T>()        {            return _appHost.TryResolve<T>();        }        /// <summary>        /// Releases the specified instance.        /// </summary>        /// <param name="instance">The instance.</param>        public void Release(object instance)        {            // Leave this empty so SS doesn't try to dispose our objects        }    }}
 |