tufao
0.8.1
An asynchronous web framework for C++ built on top of Qt
|
This class provides a robust and high performance HTTP request router. More...
Public Slots | |
bool | handleRequest (Tufao::HttpServerRequest *request, Tufao::HttpServerResponse *response, const QStringList &args=QStringList()) |
It will route the request to the right handler. More... | |
Public Slots inherited from Tufao::AbstractHttpServerRequestHandler | |
virtual bool | handleRequest (Tufao::HttpServerRequest *request, Tufao::HttpServerResponse *response, const QStringList &args=QStringList())=0 |
Handles the request using the response object. More... | |
Public Member Functions | |
HttpServerRequestRouter (QObject *parent=0) | |
Constructs a HttpServerRequestRouter object. | |
~HttpServerRequestRouter () | |
Destroys the object. | |
HttpServerRequestRouter & | map (const QRegExp &path, AbstractHttpServerRequestHandler *handler) |
This method maps requests which path components in the url matches the regular expression path to the given handler . More... | |
HttpServerRequestRouter & | map (const QRegExp &path, const QByteArray &method, AbstractHttpServerRequestHandler *handler) |
This method maps requests which path components in the url matches the regular expression path and uses the HTTP verb method to the given handler . More... | |
HttpServerRequestRouter & | unmap (const QRegExp &path, AbstractHttpServerRequestHandler *handler) |
Removes all mappings that correspond to path and handler rules and aren't specific to a particular HTTP method. | |
HttpServerRequestRouter & | unmap (const QRegExp &path, const QByteArray &method, AbstractHttpServerRequestHandler *handler) |
Removes all mappings that correspond to path , method and handler rules. | |
HttpServerRequestRouter & | unmap (const QRegExp &path) |
Removes all mappings that correspond to path rule. | |
HttpServerRequestRouter & | unmap (const QRegExp &path, const QByteArray &method) |
Removes all mappings that correspond to path and method rules. | |
HttpServerRequestRouter & | unmap (AbstractHttpServerRequestHandler *handler) |
Removes all mappings that correspond to handler rule. | |
void | clear () |
Removes all mappings. | |
Public Member Functions inherited from Tufao::AbstractHttpServerRequestHandler | |
AbstractHttpServerRequestHandler (QObject *parent=0) | |
Constructs an AbstractHttpServerRequestHandler object. | |
This class provides a robust and high performance HTTP request router.
It allows register AbstractHttpServerRequestHandler objects to handle requests that matches the mapping rules. The mapping rules are limited to use regular expressions to filter the url path component and, optionally, specify which method the object can handle.
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.
Did you note that HttpServerRequestRouter is a subclass of AbstractHttpServerRequestHandler? This design choice allows you to nest routes. But the nicest thing (for you) may not be the nested routing possibility, but the interface that this class is implementing. This design allows you to use the same request routing object to handle the requests coming from a HttpServer and a HttpsServer objects (or how many of them you need).
When the router finds one matching request handler, it will call its handleRequest method passing the request and response objects and also the captured texts by the regular expression. 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:
|
slot |
It will route the request to the right handler.
To route the request, it'll percent decode the path component from the url.
The args
object is prepend in the list of captured texts by the regular expression and then passed to the handler. My advice is to don't overuse this feature or you will get a code difficult to understand.
HttpServerRequestRouter& Tufao::HttpServerRequestRouter::map | ( | const QRegExp & | path, |
AbstractHttpServerRequestHandler * | handler | ||
) |
This method maps requests which path components in the url matches the regular expression path
to the given handler
.
HttpServerRequestRouter& Tufao::HttpServerRequestRouter::map | ( | const QRegExp & | path, |
const QByteArray & | method, | ||
AbstractHttpServerRequestHandler * | handler | ||
) |
This method maps requests which path components in the url matches the regular expression path
and uses the HTTP verb method
to the given handler
.