QHttpServerRouterRule Class

The QHttpServerRouterRule is the base class for QHttpServerRouter 规则。 更多...

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

公共类型

RouterHandler

公共函数

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

保护函数

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

详细描述

Use QHttpServerRouterRule to specify expected request parameters:

常量 描述
path QUrl::path ()
HTTP methods QHttpServerRequest::Methods
callback User-defined response callback

注意: 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
        boundViewHandler();
    });
    // QHttpServerRouter
    router.addRule<ViewHandler>(std::move(rule));
}
// 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
					

注意: 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 ().

成员类型文档编制

[alias] QHttpServerRouterRule:: RouterHandler

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

成员函数文档编制

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

另请参阅 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.

另请参阅 QHttpServerRequest::Methods .

[virtual noexcept] QHttpServerRouterRule:: ~QHttpServerRouterRule ()

销毁 QHttpServerRouterRule .

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

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

此函数被调用通过 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

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

此虚函数被调用由 exec () 以校验是否 request matches this rule. If a match is found, it is stored in the object pointed to by match (which 不必 be nullptr ) and this function returns true . Otherwise, it returns false .