tufao  1.3.0
An asynchronous web framework for C++ built on top of Qt
Tufao::HttpUpgradeRouter Class Reference

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

+ Inheritance diagram for Tufao::HttpUpgradeRouter:
+ Collaboration diagram for Tufao::HttpUpgradeRouter:

Classes

struct  Mapping
 This class describes a request handler and a filter. More...
 

Public Types

typedef std::function< bool(HttpServerRequest &, const QByteArray &head)> Handler
 It's a simple typedef for the type of handler accepted by the HttpUpgradeRouter. More...
 

Public Slots

bool handleUpgrade (Tufao::HttpServerRequest &request, const QByteArray &head) override
 It will route the request to the right handler. More...
 

Public Member Functions

 HttpUpgradeRouter (QObject *parent=0)
 Constructs a HttpServerRequestRouter object.
 
 HttpUpgradeRouter (std::initializer_list< Mapping > mappings, QObject *parent=0)
 Constructs a HttpServerRequestRouter object initialized with mappings.
 
 ~HttpUpgradeRouter ()
 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.
 
void clear ()
 Removes all mappings.
 
- Public Member Functions inherited from Tufao::AbstractHttpUpgradeHandler
 operator std::function< bool (HttpServerRequest &, const QByteArray &)>()
 Implicit conversion operator to std::function functor object. More...
 
 operator std::function< void (HttpServerRequest &, const QByteArray &)>()
 Implicit conversion operator to std::function functor object. More...
 
virtual bool handleUpgrade (Tufao::HttpServerRequest &request, const QByteArray &head)=0
 Handles the HTTP request. More...
 

Detailed Description

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

It allows to register a chain of upgrade handlers. This router uses mapping rules based on the url's path component 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. 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.

The code below provides an example usage:

int main(int argc, char *argv[])
{
using namespace Tufao;
QCoreApplication a(argc, argv);
// ...
HttpUpgradeRouter upgradeRouter{
{QRegularExpression{"^/chat$"}, [](HttpServerRequest &request,
const QByteArray &head) -> bool {
WebSocket *ws = new WebSocket;
ws->startServerHandshake(request, head);
// ...
}
}
};
HttpServer server;
server.setUpgradeHandler(upgradeRouter);
QObject::connect(&server, &HttpServer::requestReady,
server.listen(QHostAddress::Any, 8080);
return a.exec();
}
See also
HttpServer::setUpgradeHandler
Since
1.0

Member Typedef Documentation

typedef std::function<bool(HttpServerRequest&, const QByteArray &head)> Tufao::HttpUpgradeRouter::Handler

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

Since
1.0

Member Function Documentation

bool Tufao::HttpUpgradeRouter::handleUpgrade ( Tufao::HttpServerRequest request,
const QByteArray &  head 
)
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::HttpUpgradeRouter::map ( Mapping  map)

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

Returns
The index of the new mapping.
int Tufao::HttpUpgradeRouter::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.

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