QHttpServerRouterRule Class▲
- 
					
Header: QHttpServerRouterRule
 - 
					
Since: Qt 6.4
 - 
					
CMake:
find_package(Qt6 REQUIRED COMPONENTS HttpServer)
target_link_libraries(mytarget PRIVATE Qt6::HttpServer)
 - 
					
qmake: QT += httpserver
 
Detailed Description▲
Use QHttpServerRouterRule to specify expected request parameters:
| 
							 Constant  | 
						
							 Description  | 
					
|---|---|
| 
							 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,
                                                QTcpSocket *const socket) 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 regexpRegular 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 &, QTcpSocket *)>
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▲
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▲
See also QHttpServerRequest::Methods
[virtual] QHttpServerRouterRule::~QHttpServerRouterRule()▲
Destroys a QHttpServerRouterRule.
[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.


