internal HttpResponse(HttpRequest req, HttpListenerContext ctx, WatsonWebserverSettings settings, WatsonWebserverEvents events) { if (req == null) { throw new ArgumentNullException(nameof(req)); } if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (events == null) { throw new ArgumentNullException(nameof(events)); } _Request = req; _Context = ctx; _Response = _Context.Response; _Settings = settings; _Events = events; _OutputStream = _Response.OutputStream; }
/// <summary> /// Creates a new instance of the Watson webserver. /// </summary> /// <param name="settings">Waton webserver settings.</param> /// <param name="defaultRoute">Method used when a request is received and no matching routes are found. Commonly used as the 404 handler when routes are used.</param> public Server(WatsonWebserverSettings settings = null, Func <HttpContext, Task> defaultRoute = null) { if (settings == null) { settings = new WatsonWebserverSettings(); } _Settings = settings; _Routes.Default = defaultRoute; }
/// <summary> /// Creates a new instance of the Watson webserver. /// If you do not provide a settings object, default settings will be used, which will cause Watson Webserver to listen on http://127.0.0.1:8000, and send events to the console. /// </summary> /// <param name="settings">Waton webserver settings.</param> /// <param name="defaultRoute">Method used when a request is received and no matching routes are found. Commonly used as the 404 handler when routes are used.</param> public Server(WatsonWebserverSettings settings = null, Func <HttpContext, Task> defaultRoute = null) { if (settings == null) { settings = new WatsonWebserverSettings(); settings.Prefixes.Add("http://127.0.0.1:8000/"); Events.Logger = Console.WriteLine; } _Settings = settings; _Routes.Default = defaultRoute; }
/// <summary> /// Creates a new instance of the Watson webserver. /// </summary> /// <param name="hostname">Hostname or IP address on which to listen.</param> /// <param name="port">TCP port on which to listen.</param> /// <param name="ssl">Specify whether or not SSL should be used (HTTPS).</param> /// <param name="defaultRoute">Method used when a request is received and no matching routes are found. Commonly used as the 404 handler when routes are used.</param> public Server(string hostname, int port, bool ssl = false, Func <HttpContext, Task> defaultRoute = null) { if (String.IsNullOrEmpty(hostname)) { hostname = "localhost"; } if (port < 1) { throw new ArgumentOutOfRangeException(nameof(port)); } _Settings = new WatsonWebserverSettings(hostname, port, ssl); _Routes.Default = defaultRoute; }
internal HttpContext(HttpListenerContext ctx, WatsonWebserverSettings settings, WatsonWebserverEvents events) { if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } if (events == null) { throw new ArgumentNullException(nameof(events)); } _Context = ctx; Request = new HttpRequest(ctx); Response = new HttpResponse(Request, ctx, settings, events); }
/// <summary> /// Instantiate the object using default settings and the specified default route. /// </summary> public WatsonWebserverRoutes(WatsonWebserverSettings settings, Func <HttpContext, Task> defaultRoute) { if (settings == null) { settings = new WatsonWebserverSettings(); } if (defaultRoute == null) { throw new ArgumentNullException(nameof(defaultRoute)); } _Settings = settings; _Preflight = PreflightInternal; _Default = defaultRoute; _ContentHandler = new ContentRouteHandler(_Content); }
/// <summary> /// Creates a new instance of the Watson webserver. /// </summary> /// <param name="hostnames">Hostnames or IP addresses on which to listen. Note: multiple listener endpoints are not supported on all platforms.</param> /// <param name="port">TCP port on which to listen.</param> /// <param name="ssl">Specify whether or not SSL should be used (HTTPS).</param> /// <param name="defaultRoute">Method used when a request is received and no matching routes are found. Commonly used as the 404 handler when routes are used.</param> public Server(List <string> hostnames, int port, bool ssl = false, Func <HttpContext, Task> defaultRoute = null) { if (hostnames == null || hostnames.Count < 1) { hostnames = new List <string> { "localhost" } } ; if (port < 1) { throw new ArgumentOutOfRangeException(nameof(port)); } _Settings = new WatsonWebserverSettings(hostnames, port, ssl); _Routes.Default = defaultRoute; }
/// <summary> /// Tear down the server and dispose of background workers. /// Do not use this object after disposal. /// </summary> protected virtual void Dispose(bool disposing) { if (disposing) { if (_HttpListener != null && _HttpListener.IsListening) { Stop(); _HttpListener.Close(); } Events.HandleServerDisposing(this, EventArgs.Empty); _HttpListener = null; _Settings = null; _Routes = null; _TokenSource = null; _AcceptConnections = null; Events = null; Statistics = null; } }