tufao  0.8.1
An asynchronous web framework for C++ built on top of Qt
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Tufao::HttpServerRequestRouter Class Reference

This class provides a robust and high performance HTTP request router. More...

+ Inheritance diagram for Tufao::HttpServerRequestRouter:
+ Collaboration diagram for Tufao::HttpServerRequestRouter:

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.
 
HttpServerRequestRoutermap (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...
 
HttpServerRequestRoutermap (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...
 
HttpServerRequestRouterunmap (const QRegExp &path, AbstractHttpServerRequestHandler *handler)
 Removes all mappings that correspond to path and handler rules and aren't specific to a particular HTTP method.
 
HttpServerRequestRouterunmap (const QRegExp &path, const QByteArray &method, AbstractHttpServerRequestHandler *handler)
 Removes all mappings that correspond to path, method and handler rules.
 
HttpServerRequestRouterunmap (const QRegExp &path)
 Removes all mappings that correspond to path rule.
 
HttpServerRequestRouterunmap (const QRegExp &path, const QByteArray &method)
 Removes all mappings that correspond to path and method rules.
 
HttpServerRequestRouterunmap (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.
 

Detailed Description

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:

#include <QtCore/QCoreApplication>
#include <Tufao/HttpServer>
#include <Tufao/HttpServerRequestRouter>
#include <Tufao/HttpFileServer>
#include <Tufao/HttpPluginServer>
#include "notfound.h"
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(static);
// First we create the event loop and the server objects
QCoreApplication a(argc, argv);
// Then we create a request router and configure it...
// ...using some handlers
// to allow you change the running code without restart the application
Tufao::HttpPluginServer pluginServer("routes.conf", true);
// to server static files under public folder
Tufao::HttpFileServer fileServer("public");
// to respond the remaining requests with a "404 - not found"
NotFoundHandler handler404;
// you should also create handlers to:
// - "/" (maybe redirecting to some other path)
// - "/favicon.ico" (to serve the page's icon)
router.map(QRegExp(""), &pluginServer)
.map(QRegExp(""), &fileServer)
.map(QRegExp(""), &handler404);
// We set the router as the global request handler
QObject::connect(&server, SIGNAL(requestReady(Tufao::HttpServerRequest*,Tufao::HttpServerResponse*)),
// Last, we run our server...
server.listen(QHostAddress::Any, 8080);
// ...and start the event loop
return a.exec();
}
Since
0.3

Member Function Documentation

bool Tufao::HttpServerRequestRouter::handleRequest ( Tufao::HttpServerRequest request,
Tufao::HttpServerResponse response,
const QStringList &  args = QStringList() 
)
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.

Returns
Returns true if one handler able to respond the request is found.
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.

Returns
Returns itself, so you can create several mappings in the same line of code.
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.

Note
The router gives priority to maps that specified a method over the ones that didn't.
Returns
Returns itself, so you can create several mappings in the same line of code.

The documentation for this class was generated from the following file: