/// <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 is 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, Func <HttpContext, Task> defaultRoute) { if (port < 1) { throw new ArgumentOutOfRangeException(nameof(port)); } if (defaultRoute == null) { throw new ArgumentNullException(nameof(defaultRoute)); } _HttpListener = new HttpListener(); _ListenerHostnames = new List <string>(); if (hostnames == null || hostnames.Count < 1) { _ListenerHostnames.Add("*"); } else { foreach (string curr in hostnames) { _ListenerHostnames.Add(curr); } } _ListenerPort = port; _ListenerSsl = ssl; _DefaultRoute = defaultRoute; _Token = _TokenSource.Token; _ContentRouteProcessor = new ContentRouteProcessor(ContentRoutes); }
/// <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, Func <HttpContext, Task> defaultRoute) { if (String.IsNullOrEmpty(hostname)) { hostname = "*"; } if (port < 1) { throw new ArgumentOutOfRangeException(nameof(port)); } if (defaultRoute == null) { throw new ArgumentNullException(nameof(defaultRoute)); } _HttpListener = new HttpListener(); _ListenerHostnames = new List <string>(); _ListenerHostnames.Add(hostname); _ListenerPort = port; _ListenerSsl = ssl; _DefaultRoute = defaultRoute; _Token = _TokenSource.Token; _ContentRouteProcessor = new ContentRouteProcessor(ContentRoutes); }
private void InitializeRouteManagers() { DynamicRoutes = new DynamicRouteManager(); StaticRoutes = new StaticRouteManager(); ContentRoutes = new ContentRouteManager(); _ContentRouteProcessor = new ContentRouteProcessor(ContentRoutes); OptionsRoute = null; }
/// <summary> /// Creates a new instance of the Watson Webserver. /// </summary> /// <param name="ip">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="defaultRequestHandler">Method used when a request is received and no routes are defined. Commonly used as the 404 handler when routes are used.</param> public Server(string ip, int port, bool ssl, Func <HttpRequest, HttpResponse> defaultRequestHandler, bool debug) { if (String.IsNullOrEmpty(ip)) { ip = "*"; } if (port < 1) { throw new ArgumentOutOfRangeException(nameof(port)); } if (defaultRequestHandler == null) { throw new ArgumentNullException(nameof(defaultRequestHandler)); } Http = new HttpListener(); ListenerIp = ip; ListenerPort = port; ListenerSsl = ssl; Logging = new LoggingManager(debug); if (debug) { DebugRestRequests = true; DebugRestResponses = true; } DynamicRoutes = new DynamicRouteManager(Logging, debug); StaticRoutes = new StaticRouteManager(Logging, debug); ContentRoutes = new ContentRouteManager(Logging, debug); ContentProcessor = new ContentRouteProcessor(Logging, debug, ContentRoutes); DefaultRoute = defaultRequestHandler; OptionsRoute = null; Console.Write("Starting Watson Webserver at "); if (ListenerSsl) { Console.WriteLine("https://" + ListenerIp + ":" + ListenerPort); } else { Console.WriteLine("http://" + ListenerIp + ":" + ListenerPort); } TokenSource = new CancellationTokenSource(); Token = TokenSource.Token; Task.Run(() => StartServer(Token), Token); }
/// <summary> /// Creates a new instance of the Watson Webserver. /// </summary> /// <param name="uris">URIs on which to listen. /// URIs should be of the form: http://[hostname]:[port]/[url] /// Note: multiple listener endpoints is not supported on all platforms.</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> uris, Func <HttpContext, Task> defaultRoute) { if (defaultRoute == null) { throw new ArgumentNullException(nameof(defaultRoute)); } if (uris == null) { throw new ArgumentNullException(nameof(uris)); } if (uris.Count < 1) { throw new ArgumentException("At least one URI must be supplied."); } _HttpListener = new HttpListener(); _ListenerUris = new List <string>(uris); _DefaultRoute = defaultRoute; _Token = _TokenSource.Token; _ContentRouteProcessor = new ContentRouteProcessor(ContentRoutes); }
/// <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?.ServerDisposed?.Invoke(); _HeaderValues = null; _HttpListener = null; _ListenerUris = null; _ListenerHostnames = null; _ContentRouteProcessor = null; _DefaultRoute = null; _TokenSource = null; _AcceptConnections = null; _Stats = null; } }