2013-02-21 01:33:05 +00:00
|
|
|
using Funq;
|
2013-03-07 05:34:00 +00:00
|
|
|
using MediaBrowser.Common;
|
2013-02-21 01:33:05 +00:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2013-02-23 22:31:51 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2013-02-21 01:33:05 +00:00
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using ServiceStack.Api.Swagger;
|
|
|
|
using ServiceStack.Common.Web;
|
2013-02-23 07:57:11 +00:00
|
|
|
using ServiceStack.Configuration;
|
2013-04-17 19:16:36 +00:00
|
|
|
using ServiceStack.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
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;
|
2013-02-26 03:43:04 +00:00
|
|
|
using System.Collections.Generic;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2013-02-26 03:43:04 +00:00
|
|
|
using System.Net.WebSockets;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Reactive.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2013-03-07 05:34:00 +00:00
|
|
|
namespace MediaBrowser.Server.Implementations.HttpServer
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class HttpServer
|
|
|
|
/// </summary>
|
2013-02-23 22:31:51 +00:00
|
|
|
public class HttpServer : HttpListenerBase, IHttpServer
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The logger
|
|
|
|
/// </summary>
|
2013-02-21 21:06:23 +00:00
|
|
|
private readonly ILogger _logger;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the URL prefix.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The URL prefix.</value>
|
|
|
|
public string UrlPrefix { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-26 03:43:04 +00:00
|
|
|
/// The _rest services
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-03-25 02:41:27 +00:00
|
|
|
private readonly List<IRestfulService> _restServices = new List<IRestfulService>();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
/// <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>
|
2013-02-23 22:31:51 +00:00
|
|
|
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; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-17 19:16:36 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _container adapter
|
|
|
|
/// </summary>
|
2013-04-09 16:42:55 +00:00
|
|
|
private readonly ContainerAdapter _containerAdapter;
|
2013-04-08 15:55:53 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="HttpServer" /> class.
|
|
|
|
/// </summary>
|
2013-02-22 04:23:06 +00:00
|
|
|
/// <param name="applicationHost">The application host.</param>
|
2013-04-17 19:16:36 +00:00
|
|
|
/// <param name="logManager">The log manager.</param>
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <param name="serverName">Name of the server.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <param name="defaultRedirectpath">The default redirectpath.</param>
|
|
|
|
/// <exception cref="System.ArgumentNullException">urlPrefix</exception>
|
2013-04-17 19:16:36 +00:00
|
|
|
public HttpServer(IApplicationHost applicationHost, ILogManager logManager, string serverName, string defaultRedirectpath)
|
2013-02-21 01:33:05 +00:00
|
|
|
: base()
|
|
|
|
{
|
2013-04-17 19:16:36 +00:00
|
|
|
if (logManager == null)
|
2013-02-21 21:06:23 +00:00
|
|
|
{
|
2013-04-17 19:16:36 +00:00
|
|
|
throw new ArgumentNullException("logManager");
|
2013-02-21 21:06:23 +00:00
|
|
|
}
|
2013-02-22 04:23:06 +00:00
|
|
|
if (applicationHost == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("applicationHost");
|
|
|
|
}
|
2013-02-23 22:31:51 +00:00
|
|
|
if (string.IsNullOrEmpty(serverName))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("serverName");
|
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(defaultRedirectpath))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("defaultRedirectpath");
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
ServerName = serverName;
|
2013-02-21 01:33:05 +00:00
|
|
|
DefaultRedirectPath = defaultRedirectpath;
|
2013-04-17 19:16:36 +00:00
|
|
|
_logger = logManager.GetLogger("HttpServer");
|
|
|
|
|
|
|
|
ServiceStack.Logging.LogManager.LogFactory = new ServerLogFactory(logManager);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath = null;
|
|
|
|
EndpointHostConfig.Instance.MetadataRedirectPath = "metadata";
|
2013-04-08 15:55:53 +00:00
|
|
|
|
|
|
|
_containerAdapter = new ContainerAdapter(applicationHost);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2013-04-17 19:16:36 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The us culture
|
|
|
|
/// </summary>
|
2013-03-31 17:39:28 +00:00
|
|
|
protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2013-04-17 19:16:36 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Configures the specified container.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="container">The container.</param>
|
|
|
|
public override void Configure(Container container)
|
|
|
|
{
|
2013-02-24 21:53:54 +00:00
|
|
|
JsConfig.DateHandler = JsonDateHandler.ISO8601;
|
|
|
|
JsConfig.ExcludeTypeInfo = true;
|
|
|
|
JsConfig.IncludeNullValues = false;
|
2013-04-17 19:16:36 +00:00
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
SetConfig(new EndpointHostConfig
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-02-23 22:31:51 +00:00
|
|
|
DefaultRedirectPath = DefaultRedirectPath,
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
// Tell SS to bubble exceptions up to here
|
2013-03-12 02:38:27 +00:00
|
|
|
WriteErrorsToResponse = false
|
2013-02-23 22:31:51 +00:00
|
|
|
});
|
2013-02-23 07:57:11 +00:00
|
|
|
|
2013-04-08 15:55:53 +00:00
|
|
|
container.Adapter = _containerAdapter;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
Plugins.Add(new SwaggerFeature());
|
|
|
|
Plugins.Add(new CorsFeature());
|
|
|
|
|
2013-03-12 02:38:27 +00:00
|
|
|
ResponseFilters.Add((req, res, dto) =>
|
2013-04-13 20:20:04 +00:00
|
|
|
{
|
|
|
|
var exception = dto as Exception;
|
|
|
|
|
|
|
|
if (exception != null)
|
2013-03-12 02:38:27 +00:00
|
|
|
{
|
2013-04-13 20:20:04 +00:00
|
|
|
_logger.ErrorException("Error processing request for {0}", exception, req.RawUrl);
|
2013-03-12 02:38:27 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
if (!string.IsNullOrEmpty(exception.Message))
|
2013-03-12 02:38:27 +00:00
|
|
|
{
|
2013-04-13 20:20:04 +00:00
|
|
|
var error = exception.Message.Replace(Environment.NewLine, " ");
|
|
|
|
error = RemoveControlCharacters(error);
|
2013-04-09 16:42:55 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
res.AddHeader("X-Application-Error-Code", error);
|
2013-03-12 02:38:27 +00:00
|
|
|
}
|
2013-04-13 20:20:04 +00:00
|
|
|
}
|
2013-03-23 04:04:36 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
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");
|
|
|
|
}
|
2013-03-24 02:45:00 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
var hasOptions = dto as IHasOptions;
|
2013-03-24 02:45:00 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
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))
|
2013-03-24 02:45:00 +00:00
|
|
|
{
|
2013-04-13 20:20:04 +00:00
|
|
|
var length = long.Parse(contentLength, UsCulture);
|
2013-03-24 02:45:00 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
if (length > 0)
|
2013-03-24 02:45:00 +00:00
|
|
|
{
|
2013-04-13 20:20:04 +00:00
|
|
|
var response = (HttpListenerResponse)res.OriginalResponse;
|
2013-03-24 02:45:00 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
response.ContentLength64 = length;
|
2013-03-24 02:45:00 +00:00
|
|
|
|
2013-04-13 20:20:04 +00:00
|
|
|
// 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;
|
2013-03-24 02:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-13 20:20:04 +00:00
|
|
|
}
|
|
|
|
});
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2013-04-09 16:42:55 +00:00
|
|
|
|
|
|
|
/// <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();
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
/// <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>
|
2013-03-25 02:41:27 +00:00
|
|
|
/// <exception cref="System.ArgumentNullException">urlBase</exception>
|
2013-02-21 01:33:05 +00:00
|
|
|
public override void Start(string urlBase)
|
|
|
|
{
|
2013-02-23 22:31:51 +00:00
|
|
|
if (string.IsNullOrEmpty(urlBase))
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException("urlBase");
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
// *** Already running - just leave it in place
|
|
|
|
if (IsStarted)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Listener == null)
|
|
|
|
{
|
2013-04-08 15:55:53 +00:00
|
|
|
_logger.Info("Creating HttpListner");
|
2013-02-21 01:33:05 +00:00
|
|
|
Listener = new HttpListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
EndpointHost.Config.ServiceStackHandlerFactoryPath = HttpListenerRequestWrapper.GetHandlerPathIfAny(urlBase);
|
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
UrlPrefix = urlBase;
|
|
|
|
|
2013-04-08 15:55:53 +00:00
|
|
|
_logger.Info("Adding HttpListener Prefixes");
|
2013-02-21 01:33:05 +00:00
|
|
|
Listener.Prefixes.Add(urlBase);
|
|
|
|
|
|
|
|
IsStarted = true;
|
2013-04-08 15:55:53 +00:00
|
|
|
_logger.Info("Starting HttpListner");
|
2013-02-21 01:33:05 +00:00
|
|
|
Listener.Start();
|
|
|
|
|
2013-04-08 15:55:53 +00:00
|
|
|
_logger.Info("Creating HttpListner observable stream");
|
2013-02-21 01:33:05 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-15 19:09:27 +00:00
|
|
|
RaiseReceiveWebRequest(context);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-16 02:36:12 +00:00
|
|
|
await Task.Run(() =>
|
2013-04-15 19:09:27 +00:00
|
|
|
{
|
2013-04-16 02:36:12 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ProcessRequest(context);
|
|
|
|
}
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 422);
|
|
|
|
}
|
|
|
|
catch (ResourceNotFoundException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 404);
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 404);
|
|
|
|
}
|
|
|
|
catch (DirectoryNotFoundException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 404);
|
|
|
|
}
|
|
|
|
catch (UnauthorizedAccessException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 401);
|
|
|
|
}
|
|
|
|
catch (ArgumentException ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 400);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
HandleException(context.Response, ex, 500);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
context.Response.Close();
|
|
|
|
}
|
|
|
|
}).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
2013-02-23 22:31:51 +00:00
|
|
|
WebSocketConnected(this, new WebSocketConnectEventArgs { WebSocket = new NativeWebSocket(webSocketContext.WebSocket, _logger), Endpoint = ctx.Request.RemoteEndPoint.ToString() });
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
_logger.ErrorException("AcceptWebSocketAsync error", ex);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
ctx.Response.StatusCode = 500;
|
2013-04-15 23:38:08 +00:00
|
|
|
ctx.Response.Close();
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs the HTTP request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ctx">The CTX.</param>
|
|
|
|
private void LogHttpRequest(HttpListenerContext ctx)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
if (EnableHttpRequestLogging)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
_logger.LogMultiline(type + " request received from " + ctx.Request.RemoteEndPoint, LogSeverity.Debug, log);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Appends the error message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="response">The response.</param>
|
|
|
|
/// <param name="ex">The ex.</param>
|
|
|
|
/// <param name="statusCode">The status code.</param>
|
|
|
|
private void HandleException(HttpListenerResponse response, Exception ex, int statusCode)
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
_logger.ErrorException("Error processing request", ex);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-15 21:38:17 +00:00
|
|
|
// This could fail, but try to add the stack trace as the body content
|
|
|
|
try
|
|
|
|
{
|
2013-04-17 19:16:36 +00:00
|
|
|
//response.StatusCode = statusCode;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-15 21:38:17 +00:00
|
|
|
response.Headers.Add("Status", statusCode.ToString(new CultureInfo("en-US")));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-15 21:38:17 +00:00
|
|
|
response.Headers.Remove("Age");
|
|
|
|
response.Headers.Remove("Expires");
|
|
|
|
response.Headers.Remove("Cache-Control");
|
|
|
|
response.Headers.Remove("Etag");
|
|
|
|
response.Headers.Remove("Last-Modified");
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-04-15 21:38:17 +00:00
|
|
|
if (!string.IsNullOrEmpty(ex.Message))
|
|
|
|
{
|
|
|
|
response.AddHeader("X-Application-Error-Code", ex.Message);
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine("{");
|
|
|
|
sb.AppendLine("\"ResponseStatus\":{");
|
|
|
|
sb.AppendFormat(" \"ErrorCode\":{0},\n", ex.GetType().Name.EncodeJson());
|
|
|
|
sb.AppendFormat(" \"Message\":{0},\n", ex.Message.EncodeJson());
|
|
|
|
sb.AppendFormat(" \"StackTrace\":{0}\n", ex.StackTrace.EncodeJson());
|
|
|
|
sb.AppendLine("}");
|
|
|
|
sb.AppendLine("}");
|
|
|
|
|
|
|
|
var sbBytes = sb.ToString().ToUtf8Bytes();
|
|
|
|
response.OutputStream.Write(sbBytes, 0, sbBytes.Length);
|
|
|
|
}
|
|
|
|
catch (Exception errorEx)
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
_logger.ErrorException("Error processing failed request", errorEx);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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);
|
|
|
|
|
2013-02-26 21:57:53 +00:00
|
|
|
var url = context.Request.Url.ToString();
|
2013-02-26 21:53:23 +00:00
|
|
|
var endPoint = context.Request.RemoteEndPoint;
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
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);
|
2013-02-26 21:57:53 +00:00
|
|
|
LogResponse(context, url, endPoint);
|
2013-02-21 01:33:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs the response.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ctx">The CTX.</param>
|
2013-03-25 02:41:27 +00:00
|
|
|
/// <param name="url">The URL.</param>
|
|
|
|
/// <param name="endPoint">The end point.</param>
|
2013-02-26 21:57:53 +00:00
|
|
|
private void LogResponse(HttpListenerContext ctx, string url, IPEndPoint endPoint)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-02-26 21:53:23 +00:00
|
|
|
if (!EnableHttpRequestLogging)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-17 19:16:36 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
var statusode = ctx.Response.StatusCode;
|
|
|
|
|
|
|
|
var log = new StringBuilder();
|
|
|
|
|
2013-02-26 21:57:53 +00:00
|
|
|
log.AppendLine(string.Format("Url: {0}", url));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
log.AppendLine("Headers: " + string.Join(",", ctx.Response.Headers.AllKeys.Select(k => k + "=" + ctx.Response.Headers[k])));
|
|
|
|
|
2013-02-26 21:53:23 +00:00
|
|
|
var msg = "Http Response Sent (" + statusode + ") to " + endPoint;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-02-26 21:53:23 +00:00
|
|
|
_logger.LogMultiline(msg, LogSeverity.Debug, log);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates the service manager.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="assembliesWithServices">The assemblies with services.</param>
|
|
|
|
/// <returns>ServiceManager.</returns>
|
|
|
|
protected override ServiceManager CreateServiceManager(params Assembly[] assembliesWithServices)
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
var types = _restServices.Select(r => r.GetType()).ToArray();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
return new ServiceManager(new Container(), new ServiceController(() => types));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2013-02-23 22:31:51 +00:00
|
|
|
/// Shut down the Web Service
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-02-23 22:31:51 +00:00
|
|
|
public override void Stop()
|
|
|
|
{
|
|
|
|
if (HttpListener != null)
|
|
|
|
{
|
|
|
|
HttpListener.Dispose();
|
|
|
|
HttpListener = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Listener != null)
|
|
|
|
{
|
|
|
|
Listener.Prefixes.Remove(UrlPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Stop();
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
2013-02-23 22:31:51 +00:00
|
|
|
/// The _supports native web socket
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-02-23 22:31:51 +00:00
|
|
|
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; }
|
2013-02-26 03:43:04 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Adds the rest handlers.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="services">The services.</param>
|
|
|
|
public void Init(IEnumerable<IRestfulService> services)
|
|
|
|
{
|
|
|
|
_restServices.AddRange(services);
|
|
|
|
|
2013-03-25 02:41:27 +00:00
|
|
|
_logger.Info("Calling EndpointHost.ConfigureHost");
|
2013-04-08 15:55:53 +00:00
|
|
|
|
2013-02-26 03:43:04 +00:00
|
|
|
EndpointHost.ConfigureHost(this, ServerName, CreateServiceManager());
|
2013-03-25 02:41:27 +00:00
|
|
|
|
|
|
|
_logger.Info("Calling ServiceStack AppHost.Init");
|
2013-02-26 03:43:04 +00:00
|
|
|
Init();
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2013-02-23 07:57:11 +00:00
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Class ContainerAdapter
|
|
|
|
/// </summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
class ContainerAdapter : IContainerAdapter, IRelease
|
2013-02-23 07:57:11 +00:00
|
|
|
{
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The _app host
|
|
|
|
/// </summary>
|
2013-02-23 07:57:11 +00:00
|
|
|
private readonly IApplicationHost _appHost;
|
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ContainerAdapter" /> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appHost">The app host.</param>
|
2013-02-23 07:57:11 +00:00
|
|
|
public ContainerAdapter(IApplicationHost appHost)
|
|
|
|
{
|
|
|
|
_appHost = appHost;
|
|
|
|
}
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves this instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <returns>``0.</returns>
|
2013-02-23 07:57:11 +00:00
|
|
|
public T Resolve<T>()
|
|
|
|
{
|
|
|
|
return _appHost.Resolve<T>();
|
|
|
|
}
|
|
|
|
|
2013-02-23 22:31:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Tries the resolve.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <returns>``0.</returns>
|
2013-02-23 07:57:11 +00:00
|
|
|
public T TryResolve<T>()
|
|
|
|
{
|
|
|
|
return _appHost.TryResolve<T>();
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
|
|
|
/// <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
|
|
|
|
}
|
2013-02-23 07:57:11 +00:00
|
|
|
}
|
2013-04-17 19:16:36 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class ServerLogFactory
|
|
|
|
/// </summary>
|
|
|
|
public class ServerLogFactory : ILogFactory
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The _log manager
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogManager _logManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ServerLogFactory"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
public ServerLogFactory(ILogManager logManager)
|
|
|
|
{
|
|
|
|
_logManager = logManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the logger.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="typeName">Name of the type.</param>
|
|
|
|
/// <returns>ILog.</returns>
|
|
|
|
public ILog GetLogger(string typeName)
|
|
|
|
{
|
|
|
|
return new ServerLogger(_logManager.GetLogger(typeName));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the logger.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <returns>ILog.</returns>
|
|
|
|
public ILog GetLogger(Type type)
|
|
|
|
{
|
|
|
|
return GetLogger(type.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class ServerLogger
|
|
|
|
/// </summary>
|
|
|
|
public class ServerLogger : ILog
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The _logger
|
|
|
|
/// </summary>
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ServerLogger"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
public ServerLogger(ILogger logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Debug message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <param name="exception">The exception.</param>
|
|
|
|
public void Debug(object message, Exception exception)
|
|
|
|
{
|
|
|
|
_logger.ErrorException(GetMesssage(message), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Debug message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
public void Debug(object message)
|
|
|
|
{
|
|
|
|
_logger.Debug(GetMesssage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Debug format message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="format">The format.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
public void DebugFormat(string format, params object[] args)
|
|
|
|
{
|
|
|
|
_logger.Debug(format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Error message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <param name="exception">The exception.</param>
|
|
|
|
public void Error(object message, Exception exception)
|
|
|
|
{
|
|
|
|
_logger.ErrorException(GetMesssage(message), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Error message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
public void Error(object message)
|
|
|
|
{
|
|
|
|
_logger.Error(GetMesssage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Error format message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="format">The format.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
public void ErrorFormat(string format, params object[] args)
|
|
|
|
{
|
|
|
|
_logger.Error(format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Fatal message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <param name="exception">The exception.</param>
|
|
|
|
public void Fatal(object message, Exception exception)
|
|
|
|
{
|
|
|
|
_logger.FatalException(GetMesssage(message), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Fatal message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
public void Fatal(object message)
|
|
|
|
{
|
|
|
|
_logger.Fatal(GetMesssage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Error format message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="format">The format.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
public void FatalFormat(string format, params object[] args)
|
|
|
|
{
|
|
|
|
_logger.Fatal(format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs an Info message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <param name="exception">The exception.</param>
|
|
|
|
public void Info(object message, Exception exception)
|
|
|
|
{
|
|
|
|
_logger.ErrorException(GetMesssage(message), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs an Info message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
public void Info(object message)
|
|
|
|
{
|
|
|
|
_logger.Info(GetMesssage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs an Info format message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="format">The format.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
public void InfoFormat(string format, params object[] args)
|
|
|
|
{
|
|
|
|
_logger.Info(format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether this instance is debug enabled.
|
|
|
|
/// </summary>
|
|
|
|
/// <value><c>true</c> if this instance is debug enabled; otherwise, <c>false</c>.</value>
|
|
|
|
public bool IsDebugEnabled
|
|
|
|
{
|
|
|
|
get { return true; }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Warning message and exception.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <param name="exception">The exception.</param>
|
|
|
|
public void Warn(object message, Exception exception)
|
|
|
|
{
|
|
|
|
_logger.ErrorException(GetMesssage(message), exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Warning message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
public void Warn(object message)
|
|
|
|
{
|
|
|
|
_logger.Warn(GetMesssage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Logs a Warning format message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="format">The format.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
public void WarnFormat(string format, params object[] args)
|
|
|
|
{
|
|
|
|
_logger.Warn(format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the messsage.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="o">The o.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
private string GetMesssage(object o)
|
|
|
|
{
|
|
|
|
return o == null ? string.Empty : o.ToString();
|
|
|
|
}
|
|
|
|
}
|
2012-07-12 06:55:27 +00:00
|
|
|
}
|