Provides a simple Web Server

A Web server can be single-threaded or multi-threaded. When it is single-threaded, only a single thread handles all incoming requests. This means that browsers will have to wait until the response is fully delivered before making the next request. This also means that the Connection: Keep-Alive HTTP mechanism cannot be used.

If the server is multi-threaded, then a ThreadPool is used to handle client requests and the Connection: Keep-Alive mechanism is thus supported. This makes page loading times significantly faster, as the browser doesn't need to close and re-open a TCP connection for every resource it fetches.

This Web Server is not meant to replace complex servers such as Apache, NGinx or ISS. It does not support more complex mechanisms such as: POST requests encoded with multipart/form-data (file uploads) HTTP Methods other than POST, GET, PUT, DELETE (must be handled manually)
Inheritance: IDisposable
Esempio n. 1
0
 /// <summary>
 /// Creates an instance of the Woopsa server without using the Reflector. You
 /// will have to create the object hierarchy yourself, using WoopsaObjects 
 /// or implementing IWoopsaContainer yourself.
 /// 
 /// If you are already using a <see cref="WebServer"/> somewhere else,
 /// this constructor will simply add the woopsa routes to that server.
 /// This allows you to serve static content and the Woopsa protocol
 /// on the same network interface and on the same port.
 /// </summary>
 /// <param name="root">The root object that will be published via Woopsa.</param>
 /// <param name="server">The server on which Woopsa routes will be added</param>
 /// <param name="routePrefix">
 /// The prefix to add to all routes for woopsa verbs. For example, specifying
 /// "myPrefix" will make the server available on http://server/myPrefix
 /// </param>
 public WoopsaServer(IWoopsaContainer root, WebServer server, string routePrefix = DefaultServerPrefix)
 {
     _root = root;
     WebServer = server;
     _routePrefix = routePrefix;
     AddRoutes();
 }
Esempio n. 2
0
        private void Listen()
        {
            _currentWebServer = this;
            while (!_aborted && _started)
            {
                try
                {
                    // The HandleClient method will loop in case of
                    // Keep-Alive connections.
                    // The HandleClient method should NEVER close the stream
                    // as this is done from the upper scope.
                    TcpClient client = _listener.AcceptTcpClient();

                    if (MultiThreaded)
                    {
                        try
                        {
                            _threadPool.StartUserWorkItem(
                                (o) =>
                                {
                                    _currentWebServer = this;
                                    try
                                    {
                                        HandleClient(client);
                                    }
                                    finally
                                    {
                                        client.Close();
                                    }
                                });
                        }
                        catch (Exception)
                        {
                            client.Close();
                            throw;
                        }
                    }
                    else
                    {
                        try
                        {
                            HandleClient(client);
                        }
                        finally
                        {
                            client.Close();
                        }
                    }
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.Interrupted)
                    {
                        _aborted = true;
                    }
                }
                catch (Exception)
                {
                    // TODO : mechanism to manage exceptions
                }
            }
            _listener.Stop();
        }
Esempio n. 3
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         ShutDown();
         RemoveRoutes();
         if (_subscriptionService != null)
         {
             _subscriptionService.Dispose();
             _subscriptionService = null;
         }
         if (_isWebServerEmbedded)
         {
             WebServer.Dispose();
             WebServer = null;
         }
     }
 }