本页内容

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 中的某个值。如果在构建规则时未指定方法,则该规则将匹配任何已知方法的请求。

处理程序签名

处理程序是一个具有以下签名的回调函数:

  • 第一个参数接收来自路径的任何匹配到的捕获组。
  • 第二个参数包含请求的详细信息。
  • 第三个参数用于发送响应。

向 QHttpServerRouter 添加规则

下面的示例演示了如何在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);
        boundViewHandler(); // Execute the handler
    });

    // Add rule to the router
    router.addRule<ViewHandler>(std::move(rule));
}

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

注意:这是一个 低级 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 调用运算符的可复制可调用对象。
  • 如果slot 是可调用的,则receiver 将作为其上下文对象。
  • 该处理程序在receiver 被销毁之前一直有效。

该规则可处理任何可用的 HTTP 方法组合。

另请参阅 QHttpServerRequest::Methods 。

template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QObject *receiver, Functor &&slot)

此重载会为pathPattern 构建一条路由规则,并将其与receiver 和slot 关联起来。

  • 其默认值为QHttpServerRequest::Method::AnyKnown ,这意味着它将匹配任何被识别的 HTTP 方法。
  • slot 可以是函数指针、不可变的 lambda 表达式,或任何其他具有const 调用运算符的可复制可调用对象。
  • 如果slot 是可调用的,则receiver 将作为其上下文对象。
  • 该处理程序在receiver 被销毁之前始终有效。

这是一个重载函数。

[virtual noexcept] QHttpServerRouterRule::~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派生的值传递给slot 之后,将这些参数一并提供给 。

从 URL 中捕获的每个值(作为字符串)都会根据其位置转换为处理程序中对应的参数类型,以确保其能够作为match 传递。

QHttpServerRouter router;

autopageView= [](constQString&page, constquint32 num) {
    qDebug("page: %s, num: %d", qPrintable(page), num);
};
usingViewHandler=decltype(pageView);

autorule=std::make_unique<QHttpServerRouterRule>(
    "/<arg>/<arg>/log",
    [&router, &pageView](QRegularExpressionMatch&match,
                          constQHttpServerRequest&request,
                          QHttpServerResponder&&responder) {
    // 使用 match 捕获的字符串和 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 ,检查其是否符合该规则。

  • 当接收到新的request 时,QHttpServerRouter 会调用此函数。
  • 如果request 与规则匹配,则通过提供的responder 发送响应来处理该请求,并返回true 。
  • 如果不匹配,则返回false 。

[protected] bool QHttpServerRouterRule::hasValidMethods() const

验证请求方法。如果指定的 HTTP 方法有效,则返回true 。

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

确定所提供的request 是否符合本规则的条件。

  • 该虚拟函数由exec()调用,用于评估request 。
  • 如果请求匹配,则将详细信息存储在match 中(该对象不得是nullptr ),并且该函数返回true 。
  • 否则,该函数返回false 。

© 2026 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.