PySide6.QtHttpServer.QHttpServerRouter

class QHttpServerRouter

Provides functions to bind a URL to a ViewHandler. More

Synopsis

Methods

Note

This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE

Detailed Description

QHttpServerRouter is a class to distribute http requests to their respective handlers with a rule based system.

You can register new QHttpServerRouterRules , that represent a request path and the respective handler. Variable parts in the route can be specified with placeholder in the request path. The handler gets the placeholders value as a QRegularExpressionMatch. The arguments can be of any type for which a converter is available. The handler creation can be simplified with bindCaptured . A QHttpServerRouter instance must not be modifed by its rules.

Note

This is a low-level routing API for an HTTP server.

Minimal example:

auto pageView = [] (const quint64 page) {
    qDebug() << "page" << page;
};
using ViewHandler = decltype(pageView);

QHttpServerRouter router;

// register callback pageView on request "/page/<number>"
// for example: "/page/10", "/page/15"
router.addRule<ViewHandler>(
    new QHttpServerRouterRule("/page/", [=] (QRegularExpressionMatch &match,
                                             const QHttpServerRequest &,
                                             QHttpServerResponder &&) {
    auto boundView = QHttpServerRouterRule::bindCaptured(pageView, match);

    // it calls pageView
    boundView();
}));
__init__(server)
Parameters:

serverQAbstractHttpServer

Creates a QHttpServerRouter object with default converters.

See also

converters

addConverter(metaType, regexp)
Parameters:

Adds a new converter for metaType that can be parsed with regexp. Having a converter for a metaType enables to use this type in a path pattern of a QHttpServerRouterRule . The regular expression is used to parse parameters of type metaType from the path pattern.

If there is already a converter of type metaType, that converter’s regexp is replaced with regexp.

clearConverters()

Removes all converters.

Note

clearConverters() does not set up default converters.

converters()
Return type:

Dictionary with keys of type .QMetaType and values of type QString.

handleRequest(request, responder)
Parameters:
Return type:

bool

Handles each new request for the HTTP server using responder.

Iterates through the list of rules to find the first that matches, then executes this rule, returning true. Returns false if no rule matches the request.

removeConverter(metaType)
Parameters:

metaTypeQMetaType

Removes the converter for type metaType.

See also

addConverter