C# Class Woopsa.WebServer

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
显示文件 Open project: woopsa-protocol/Woopsa Class Usage Examples

Public Methods

Method Description
Dispose ( ) : void
HTTPResponse_Error ( object sender, HTTPResponseErrorEventArgs e ) : void
Shutdown ( ) : void

Shutdowns the server and stops listening for TCP connexions. At this point, the server becomes completely unreachable. It cannot be restarted.

Start ( ) : void

Starts listening on the TCP socket and initiates the routing mechanism. At this point, the server become reachable by any client.

WebServer ( int port = DefaultPortHttp, int threadPoolSize = CustomThreadPool.DefaultThreadPoolSize, ThreadPriority priority = DefaultThreadPriority ) : System

Creates a WebServer that runs on the specified port and can be multithreaded

A server must be multithreaded in order to use the Keep-Alive HTTP mechanism.

Protected Methods

Method Description
Dispose ( bool disposing ) : void
OnError ( EventArgs args ) : void
OnLog ( HTTPRequest request, HTTPResponse response ) : void

Private Methods

Method Description
ExtractPOST ( HTTPRequest request, StreamReader reader, Encoding clientEncoding ) : void
ExtractQuery ( HTTPRequest request, Encoding clientEncoding ) : void
FillHeaders ( HTTPRequest request, StreamReader reader ) : void
HandleClient ( TcpClient client ) : void
InferEncoding ( HTTPRequest request ) : Encoding
Listen ( ) : void
_routeSolver_Error ( object sender, RoutingErrorEventArgs e ) : void

Method Details

Dispose() public method

public Dispose ( ) : void
return void

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void

HTTPResponse_Error() public method

public HTTPResponse_Error ( object sender, HTTPResponseErrorEventArgs e ) : void
sender object
e HTTPResponseErrorEventArgs
return void

OnError() protected method

protected OnError ( EventArgs args ) : void
args System.EventArgs
return void

OnLog() protected method

protected OnLog ( HTTPRequest request, HTTPResponse response ) : void
request HTTPRequest
response HTTPResponse
return void

Shutdown() public method

Shutdowns the server and stops listening for TCP connexions. At this point, the server becomes completely unreachable. It cannot be restarted.
public Shutdown ( ) : void
return void

Start() public method

Starts listening on the TCP socket and initiates the routing mechanism. At this point, the server become reachable by any client.
public Start ( ) : void
return void

WebServer() public method

Creates a WebServer that runs on the specified port and can be multithreaded
A server must be multithreaded in order to use the Keep-Alive HTTP mechanism.
public WebServer ( int port = DefaultPortHttp, int threadPoolSize = CustomThreadPool.DefaultThreadPoolSize, ThreadPriority priority = DefaultThreadPriority ) : System
port int /// The port on which to run the server (default 80) ///
threadPoolSize int /// The maximum number of threads to be created. /// CustomThreadPool.DefaultThreadPoolSize means use default operating system value. ///
priority ThreadPriority /// The priority of the web server threads. ///
return System