tufao
1.3.0
An asynchronous web framework for C++ built on top of Qt
|
This class provides a robust and high performance HTTP request router. More...
Classes | |
struct | Mapping |
This class describes a request handler and a filter. More... | |
Public Types | |
typedef std::function< bool(HttpServerRequest &, HttpServerResponse &)> | Handler |
It's a simple typedef for the type of handler accepted by the HttpServerRequestRouter. More... | |
Public Slots | |
bool | handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response) override |
It will route the request to the right handler. More... | |
Public Member Functions | |
HttpServerRequestRouter (QObject *parent=0) | |
Constructs a HttpServerRequestRouter object. | |
HttpServerRequestRouter (std::initializer_list< Mapping > mappings, QObject *parent=0) | |
Constructs a HttpServerRequestRouter object initialized with mappings . More... | |
~HttpServerRequestRouter () | |
Destroys the object. | |
int | map (Mapping map) |
Chain map to the list of handlers available to handle requests. More... | |
int | map (std::initializer_list< Mapping > map) |
Chain map to the list of handlers available to handle requests. More... | |
void | unmap (int index) |
Removes the mapping at index . More... | |
void | clear () |
Removes all mappings. | |
Public Member Functions inherited from Tufao::AbstractHttpServerRequestHandler | |
operator std::function< bool (HttpServerRequest &, HttpServerResponse &)>() | |
Implicit conversion operator to std::function functor object. More... | |
virtual bool | handleRequest (Tufao::HttpServerRequest &request, Tufao::HttpServerResponse &response)=0 |
Handles the request using the response object. More... | |
This class provides a robust and high performance HTTP request router.
It allows register a chain of request handlers. This router uses mapping rules based on the url's path component and http method to determine the correct handlers.
The type of mapping rules used in this class provides a predictable behaviour that is simple to understand and allow the use of caching algorithms to improve the performance.
When the router finds one matching request handler, it will call it passing the request and response objects. If the found handler cannot handle the request (this is indicated by the return value), the router will continue its quest in the search of a worthy handler. If the router fails in its quest (when no handlers are found or when none of the found handlers are able to respond the request), the handleRequest method in the router returns false and the connection remains open. This mean that you should always create a handler that responds to any request with a 404 not found as the last handler in the most top-level request router.
The code below provides an example usage:
typedef std::function<bool(HttpServerRequest&, HttpServerResponse&)> Tufao::HttpServerRequestRouter::Handler |
It's a simple typedef for the type of handler accepted by the HttpServerRequestRouter.
|
explicit |
Constructs a HttpServerRequestRouter object initialized with mappings
.
|
overrideslot |
It will route the request to the right handler.
The handler will have access to the list of captured texts by the regular expression using HttpServerRequest::customData.
See example below:
If there is already an object set for this request, the router will do the following steps:
int Tufao::HttpServerRequestRouter::map | ( | Mapping | map | ) |
Chain map
to the list of handlers available to handle requests.
int Tufao::HttpServerRequestRouter::map | ( | std::initializer_list< Mapping > | map | ) |
Chain map
to the list of handlers available to handle requests.
void Tufao::HttpServerRequestRouter::unmap | ( | int | index | ) |
Removes the mapping at index
.