Пример #1
0
        /// <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="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 hostname, int port, bool ssl, Func <HttpRequest, HttpResponse> defaultRequestHandler)
        {
            if (String.IsNullOrEmpty(hostname))
            {
                hostname = "*";
            }
            if (port < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }
            if (defaultRequestHandler == null)
            {
                throw new ArgumentNullException(nameof(defaultRequestHandler));
            }

            _HttpListener = new HttpListener();

            _ListenerHostnames = new List <string>();
            _ListenerHostnames.Add(hostname);
            _ListenerPort = port;
            _ListenerSsl  = ssl;
            _DefaultRoute = defaultRequestHandler;

            Events = new EventCallbacks();
            InitializeRouteManagers();
            AccessControl = new AccessControlManager(AccessControlMode.DefaultPermit);
            Welcome();

            _TokenSource = new CancellationTokenSource();
            _Token       = _TokenSource.Token;
            Task.Run(() => StartServer(_Token), _Token);
        }
Пример #2
0
        internal HttpContext(HttpListenerContext ctx, EventCallbacks events)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException(nameof(ctx));
            }
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            _Context = ctx;
            _Events  = events;

            Request  = new HttpRequest(ctx);
            Response = new HttpResponse(Request, _Context, _Events, _StreamBufferSize);
        }
Пример #3
0
        internal HttpResponse(HttpRequest req, HttpListenerContext ctx, EventCallbacks events, int bufferSize)
        {
            if (req == null)
            {
                throw new ArgumentNullException(nameof(req));
            }
            if (ctx == null)
            {
                throw new ArgumentNullException(nameof(ctx));
            }
            if (events == null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            _Request          = req;
            _Context          = ctx;
            _Response         = _Context.Response;
            _Events           = events;
            _StreamBufferSize = bufferSize;
            _OutputStream     = _Response.OutputStream;
        }