![]() |
HttpServerBoxed 0.0.11 alpha
A simple http server for C# and .NET
|
Here will be explained how to use the core library (HSB.dll)
This information is up to date until commit 1da2a6f (5 November 2023)
To include the library you must follow your IDE instruction
Although HSB is primarily intended for creating complete standalone web servers, it is possible to integrate the library into other types of projects
There are two fundamental components to make it run, the Configuration class and the Server class. The first holds any information about the server and the second effectively run the server itself. The Server class expects a configuration to be instantied and to run. So the minimal code to run the server is something like:
This will make the server listen to any address (IPv4 and IPv6) on port 8080 All parameters are documented here
There are two ways, one is like the nodejs library ExpressJS, the other is follows a more classical approach.
The configuration class provides 9 methods to handle the various HTTP methods : GET; POST; HEAD; PUT; DELETE; PATCH; TRACE; OPTIONS; CONNECT
These methods require two arguments, one is the path and the other one is a delegate that will handle the call. Example:
will route the index page to that delegate, printing 'Hello World' while visiting the root page
Note that custom HTTP Methods are not supported by this routing style, therefore the Servlet method must be used
To create a routing in the classic method you create a class that extends the Servlet base class, the constructor MUST have two parameters : Request and Response, and pass them to the base class The new class also needs the Binding Attribute, that requires a string (the path) and optionally a boolean indicating whether or not that servlet must handle all routes starting with the given path. Example:
This class maps to the root path and responds to the GET and POST methods, other methods reply, by default, with code 405 (Method not allowed)
Additionally the Servlet can have one more argument in is constructor, the configuration:
To handle a request with a non standard HTTP Request method, yuor server must map the handler function. For example:
This code will handle ONLY the requests that have the method "MyCustomHTTPMethod". Example request header:
Both the ExpressJS-like mapping and Servlet class contains a reference to the "Request" and "Response" class, optionally they can also hold the Configuration. Details about the Request class can be found here, while for Response here
All information about ExpressJS mapping and Servlet are available here
To make HSB use the SSL/TLS mode you must provide a valid certificate and modify the configuration. See set SSL for more information
HSB support the creation of WebSockets, these follow a structure similar to the servlets but with different functions for sending data A complete guide is available here