QHttpServerRouterRule Class
QHttpServerRouterRule 是QHttpServerRouter 规则的基类。更多
头文件: | #include <QHttpServerRouterRule> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS HttpServer) target_link_libraries(mytarget PRIVATE Qt6::HttpServer) |
qmake: | QT += httpserver |
自 | Qt 6.4 |
公共函数
QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, const QObject *receiver, Functor &&slot) | |
QHttpServerRouterRule(const QString &pathPattern, const QObject *receiver, Functor &&slot) | |
virtual | ~QHttpServerRouterRule() |
const QObject * | contextObject() const |
静态公共成员
typename ViewTraits::BindableType | bindCaptured(QObject *receiver, Functor &&slot, const QRegularExpressionMatch &match) |
保护函数
bool | exec(const QHttpServerRequest &request, QHttpServerResponder &responder) const |
bool | hasValidMethods() const |
virtual bool | matches(const QHttpServerRequest &request, QRegularExpressionMatch *match) const |
详细说明
QHttpServerRouterRule 表达了请求路径、HTTP 请求方法和相应处理程序回调之间的联系。QHttpServerRouter 是此类规则的集合,如果路径和请求方法与请求匹配,就会从中调用处理程序。处理程序回调必须提供对请求的响应。
路径和模式
每个 QHttpServerRouterRule 都包含一个路径或路径模式,它定义了可以通过处理程序提供响应的路径。路径可以包含转发给规则处理程序的占位符。下面的路径模式示例使用QHttpServer::route() 方便方法显示,但也可以提供给 QHttpServerRouterRule 构造函数。
在最简单的情况下,路径是一个以"/"
为前导的字符串:
QHttpServer server; server.route("/user", [] () { return "hello user"; } );
这种路径模式会创建一个规则,将所有带有"/user"
的请求转发给所提供的处理程序,在这种情况下,处理程序就是一个简单的 lambda(注意,直接使用 QHttpServerRouterRule 时,处理程序的语法会有所不同,见下文)。
路径模式还可以包含一个尾部"/"
,以创建一个规则,处理尾部"/"
之后的参数路径集合。使用QHttpServer::route 方便方法,参数会直接转发到 lambda:
server.route("/user/", [] ( qint64 id ) { return "hello user"; } );
这将匹配请求路径"/user/1"
、"/user/2"
等。
通过使用"<arg>"
占位符,参数可以在路径模式中自由定位。该关键字还允许使用多个占位符。
server.route("/user/<arg>/history", [] (qint64 id){ return "hello user"; } ); server.route("/user/<arg>/history/", [] (qint64 id, qint64 page){ return "hello user"; } );
例如,这将匹配请求路径"/user/1/history/2"
。在QHttpServerRouter::converters() 中注册的所有类型都可以在回调和相应的占位符中使用。
请求方法
请求方法是QHttpServerRequest::Method 中的一个。如果没有为规则构造的任何重载提供方法,规则将匹配任何请求方法。
处理程序签名
处理程序是一个回调,其签名为
void (*)(const QRegularExpressionMatch &, const QHttpServerRequest &, QHttpServerResponder &);
处理程序回调接收任何匹配的占位符作为第一个参数。第二个参数包含请求的详细信息,处理程序必须将响应写入最后一个参数。
下面的代码示例展示了如何创建带有相应处理程序的新规则并将其添加到QHttpServerRouter :
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 = QHttpServerRouterRule::bindCaptured<ViewHandler>( this, 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"
注: 这是一个低级 API,更高级别的 API 请参见QHttpServer 。
注: 不支持路径模式中的正则表达式,但可以使用QHttpServerRouter::addConverter() 注册(将"<arg>"
的使用与特定类型相匹配)。
成员函数文档
template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, const QObject *receiver, Functor &&slot)
为pathPattern,methods 构建一个规则,并将其连接到receiver 和slot 。slot 也可以是函数指针、不可变 lambda 或其他任何带有 const 调用操作符的可复制可调用对象。在这种情况下,receiver 将是一个上下文对象。处理程序将一直有效,直到接收者对象被销毁。
该规则接受任何可用 HTTP 方法的组合。
另请参见 QHttpServerRequest::Methods 。
template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QObject *receiver, Functor &&slot)
这是一个重载函数。
为pathPattern,QHttpServerRequest::Method::AnyKnown 构造一个规则,并将其连接到receiver 和slot 。slot 也可以是函数指针、不可变 lambda 或其他任何带有 const 调用操作符的可复制可调用对象。在这种情况下,receiver 将是一个上下文对象。处理程序将一直有效,直到接收者对象被销毁。
[virtual noexcept]
QHttpServerRouterRule::~QHttpServerRouterRule()
[static]
template <typename Functor, typename ViewTraits = QHttpServerRouterViewTraits<Functor>> typename ViewTraits::BindableType QHttpServerRouterRule::bindCaptured(QObject *receiver, Functor &&slot, const QRegularExpressionMatch &match)
为receiver 和slot 提供从 URL 派生的参数。返回绑定函数,该函数接受处理程序可能接受的任何其余参数,并将这些参数提供给 URL 派生值之后的插槽。应用于 URL 的 regex 的每个匹配(作为字符串)都会转换为处理程序参数在其位置处的类型,因此可以作为match 传递。
QHttpServerRouterrouter;autopageView= [](constQStringpage, constquint32num) { qDebug("page: %s, num: %d", qPrintable(page), num); };usingViewHandler=decltype(pageView);autorule=std::make_unique<QHttpServerRouterRule>("/<arg>/<arg>/log", [&router, &pageView](QRegularExpressionMatch匹配, 常量QHttpServerRequest&request、 QHttpServerResponder&&responder) {// 使用匹配捕获的字符串和 quint32 绑定并调用 viewHandler: QHttpServerRouterRule::bindCaptured(pageView,match)(); }); router.addRule<ViewHandler>(std::move(rule));
const QObject *QHttpServerRouterRule::contextObject() const
返回此规则的上下文对象。这是必须处理请求的接收器。
[protected]
bool QHttpServerRouterRule::exec(const QHttpServerRequest &request, QHttpServerResponder &responder) const
为给定的request 执行该规则。
QHttpServerRouter 收到新请求时会调用此函数。如果给定的request 与此规则相匹配,则此函数通过向给定的responder 发送响应来处理请求,然后返回true
。否则,返回false
。
[protected]
bool QHttpServerRouterRule::hasValidMethods() const
如果方法有效,则返回true
[virtual protected]
bool QHttpServerRouterRule::matches(const QHttpServerRequest &request, QRegularExpressionMatch *match) const
确定给定的request 是否与此规则匹配。
exec() 调用此虚函数来检查request 是否与此规则匹配。如果发现匹配,则将其存储在match 指向的对象中(该对象不得是nullptr
),此函数返回true
。否则,返回false
。
© 2025 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.