QHttpServerRouterRule Class

The QHttpServerRouterRule is the base class for QHttpServerRouter rules. More...

Header: #include <QHttpServerRouterRule>
CMake: find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
qmake: QT += httpserver
Since: Qt 6.4

Public Types

Public Functions

QHttpServerRouterRule(const QString &pathPattern, QHttpServerRouterRule::RouterHandler routerHandler)
QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, QHttpServerRouterRule::RouterHandler routerHandler)
virtual ~QHttpServerRouterRule()

Protected Functions

bool exec(const QHttpServerRequest &request, QHttpServerResponder &responder) const
bool hasValidMethods() const
virtual bool matches(const QHttpServerRequest &request, QRegularExpressionMatch *match) const

Detailed Description

Use QHttpServerRouterRule to specify expected request parameters:

HTTPmethods QHttpServerRequest::Methods
callbackUser-defined response callback

Note: This is a low level API, see QHttpServer for higher level alternatives.

Example of QHttpServerRouterRule and QHttpServerRouter usage:

template<typename ViewHandler>
void route(const char *path, const QHttpServerRequest::Methods methods, ViewHandler &&viewHandler)
    auto rule = std::make_unique<QHttpServerRouterRule>(
            path, methods, [this, viewHandler = std::forward<ViewHandler>(viewHandler)]
                                               (QRegularExpressionMatch &match,
                                                const QHttpServerRequest &request,
                                                QHttpServerResponder &&responder) mutable {
        auto boundViewHandler = router.bindCaptured<ViewHandler>(
                std::move(viewHandler), match);
        // call viewHandler

    // QHttpServerRouter

// Valid:
route("/user/", [] (qint64 id) { } );                            // "/user/1"
                                                                 // "/user/3"
route("/user/<arg>/history", [] (qint64 id) { } );               // "/user/1/history"
                                                                 // "/user/2/history"
route("/user/<arg>/history/", [] (qint64 id, qint64 page) { } ); // "/user/1/history/1"
                                                                 // "/user/2/history/2"

// Invalid:
route("/user/<arg>", [] () { } );  // ERROR: path pattern has <arg>, but ViewHandler does not have any arguments
route("/user/\\d+", [] () { } );   // ERROR: path pattern does not support manual regexp

Note: Regular expressions in the path pattern are not supported, but can be registered (to match a use of "<val>" to a specific type) using QHttpServerRouter::addConverter().

Member Type Documentation

[alias] QHttpServerRouterRule::RouterHandler

Type alias for std::function<void(const QRegularExpressionMatch &,const QHttpServerRequest &, QHttpServerResponder &&)>

Member Function Documentation

[explicit] QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, QHttpServerRouterRule::RouterHandler routerHandler)

Constructs a rule with pathPattern pathPattern, and routerHandler routerHandler.

The rule accepts all HTTP methods by default.

See also QHttpServerRequest::Methods.

[explicit] QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, QHttpServerRouterRule::RouterHandler routerHandler)

Constructs a rule with pathPattern pathPattern, methods methods and routerHandler routerHandler.

The rule accepts any combinations of available HTTP methods.

See also QHttpServerRequest::Methods.

[virtual] QHttpServerRouterRule::~QHttpServerRouterRule()

Destroys a QHttpServerRouterRule.

[protected] bool QHttpServerRouterRule::exec(const QHttpServerRequest &request, QHttpServerResponder &responder) const

Executes this rule for the given request, if it matches.

This function is called by QHttpServerRouter when it receives a new request. If the given request matches this rule, this function handles the request by delivering a response to the given responder, then returns true. Otherwise, it returns false.

[protected] bool QHttpServerRouterRule::hasValidMethods() const

Returns true if the methods is valid

[virtual protected] bool QHttpServerRouterRule::matches(const QHttpServerRequest &request, QRegularExpressionMatch *match) const

Determines whether a given request matches this rule.

This virtual function is called by exec() to check if request matches this rule. If a match is found, it is stored in the object pointed to by match (which must not be nullptr) and this function returns true. Otherwise, it returns false.

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.