tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
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:

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...
 

Detailed Description

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:

#include <QCoreApplication>
#include <Tufao/HttpPluginServer>
#include <Tufao/HttpFileServer>
#include <Tufao/NotFoundHandler>
#include <Tufao/HttpServerRequestRouter>
#include <Tufao/HttpServer>
using namespace Tufao;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
HttpPluginServer plugins{"routes.json"};
{QRegularExpression{""}, plugins},
{QRegularExpression{""}, HttpFileServer::handler("public")},
{QRegularExpression{""}, NotFoundHandler::handler()}
};
HttpServer server;
QObject::connect(&server, &HttpServer::requestReady,
server.listen(QHostAddress::Any, 8080);
return a.exec();
}
Since
0.3

Member Typedef Documentation

It's a simple typedef for the type of handler accepted by the HttpServerRequestRouter.

Since
1.0

Constructor & Destructor Documentation

Tufao::HttpServerRequestRouter::HttpServerRequestRouter ( std::initializer_list< Mapping mappings,
QObject *  parent = 0 
)
explicit

Constructs a HttpServerRequestRouter object initialized with mappings.

Since
1.0

Member Function Documentation

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

Note
The router won't touch the HttpServerRequest::customData if the regex don't capture any text.

See example below:

bool Handler::handleRequest(HttpServerRequest &request,
HttpServerResponse &response)
{
// ...
QStringList args = request.customData().toMap()["args"].toStringList();
// ...
}

If there is already an object set for this request, the router will do the following steps:

  1. If the object is not a QVariantMap, override it
  2. If the object already has a item with the key "args", but the value is not a QStringList, override the item
  3. Append the list of captured texts in the object["args"]
Note
The request's custom data is copied at the beginning and is used to restore the custom data state before call every handler. The state will also be restored if no handlers are capable of handle the request.
Returns
Returns true if one handler able to respond the request is found.
Since
1.0
int Tufao::HttpServerRequestRouter::map ( Mapping  map)

Chain map to the list of handlers available to handle requests.

Returns
The index of the new mapping.
Since
1.0
int Tufao::HttpServerRequestRouter::map ( std::initializer_list< Mapping map)

Chain map to the list of handlers available to handle requests.

Returns
The index of the first element in the new mapping range.
Since
1.0
void Tufao::HttpServerRequestRouter::unmap ( int  index)

Removes the mapping at index.

Since
1.0

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