QHttpServerRouter Class
提供将路径绑定到ViewHandler
的函数 ... 更多...
头文件: | #include <QHttpServerRouter> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS HttpServer) target_link_libraries(mytarget PRIVATE Qt6::HttpServer) |
qmake: | QT += httpserver |
自 | Qt 6.4 |
公共函数
QHttpServerRouter(QAbstractHttpServer *server) | |
~QHttpServerRouter() | |
bool | addConverter(QAnyStringView regexp) |
void | addConverter(QMetaType metaType, QAnyStringView regexp) |
QHttpServerRouterRule * | addRule(std::unique_ptr<QHttpServerRouterRule> rule) |
void | clearConverters() |
QHash<QMetaType, QString> | converters() && |
const QHash<QMetaType, QString> & | converters() const & |
bool | handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) const |
void | removeConverter(QMetaType metaType) |
详细说明
QHttpServerRouter 是一个通过基于规则的系统将 HTTP 请求分发到相应处理程序的类。
你可以注册新的QHttpServerRouterRules ,它代表了请求路径和相应的处理程序。路由中的变量可以用请求路径中的占位符指定。处理程序以QRegularExpressionMatch 的形式获取占位符的值。参数可以是converter 可用的任何类型。处理程序的创建可通过QHttpServerRouterRule::bindCaptured() 简化。QHttpServerRouter 实例不得修改其规则。
注: 这是 HTTP 服务器的低级路由 API。
最小示例
autopageView= [](constquint64page) { qDebug() << "page" << page; };usingViewHandler=decltype(pageView);QHttpServerRouterrouter;//在请求"/page/<number>"时注册回调页面视图//例如:"/page/10", "/page/15"router.addRule<ViewHandler>( newQHttpServerRouterRule("/page/", [=](QRegularExpressionMatch&match, constQHttpServerRequest&, QHttpServerResponder&&){autoboundView=QHttpServerRouterRule::bindCaptured(pageView,match);// 它调用 pageViewboundView(); }));
成员函数文档
QHash<QMetaType, QString> QHttpServerRouter::converters() &&
const QHash<QMetaType, QString> &QHttpServerRouter::converters() const &
返回已在QHttpServerRouter 注册的转换器类型和正则表达式的映射。这些类型可用于QHttpServerRouterRules 的路径模式。
默认情况下可使用以下转换器:
常量 | 常量 |
---|---|
QMetaType::Int | |
QMetaType::Long | |
QMetaType::LongLong | |
QMetaType::Short | |
QMetaType::UInt | |
QMetaType::ULong | |
QMetaType::ULongLong | |
QMetaType::UShort | |
QMetaType::Double | |
QMetaType::Float | |
QMetaType::QString | |
QMetaType::QByteArray | |
QMetaType::QUrl | |
QMetaType::Void | 空转换器。 |
另请参阅 addConverter 和clearConverters 。
QHttpServerRouter::QHttpServerRouter(QAbstractHttpServer *server)
创建带有默认转换器的 QHttpServerRouter 对象。
另请参阅 converters 。
[noexcept]
QHttpServerRouter::~QHttpServerRouter()
template <typename Type> bool QHttpServerRouter::addConverter(QAnyStringView regexp)
为可使用regexp 解析的Type添加新转换器,如果成功则返回true
,否则返回false
。如果成功,注册的类型可用作QHttpServerRouterRule 的处理程序的参数。正则表达式将用于解析规则的路径模式。
如果已经存在Type 类型的转换器,该转换器的 regexp 将被替换为regexp 。
自定义转换器可以通过QMetaType 系统扩展可用的类型转换。
定义一个带有QString 构造函数的类:
struct CustomArg { int data = 10; CustomArg() {} ; CustomArg(const QString &urlArg) : data(urlArg.toInt()) {} };
要在HTTP server 中使用自定义类型,请使用此函数对其进行注册,并使用新类型定义一个route 处理程序:
server.router()->addConverter<CustomArg>(u"[+-]?\\d+"); server.route("/customTest/<arg>", [] (const CustomArg custom) { return QString::number(custom.data); });
另请参阅 converters 和clearConverters 。
void QHttpServerRouter::addConverter(QMetaType metaType, QAnyStringView regexp)
为metaType 添加新的转换器,该转换器可与regexp 一起解析。有了metaType 的转换器,就可以在QHttpServerRouterRule 的路径模式中使用该类型。正则表达式用于解析路径模式中metaType 类型的参数。
如果已有metaType 类型的转换器,则转换器的 regexp 将被替换为regexp 。
另请参见 converters 和clearConverters 。
template <typename ViewHandler, typename ViewTraits = QHttpServerRouterViewTraits<ViewHandler>> QHttpServerRouterRule *QHttpServerRouter::addRule(std::unique_ptr<QHttpServerRouterRule> rule)
向路由器添加新的rule 。
如果成功,则返回指向新规则的指针,否则返回nullptr
。
在 addRule 中,我们确定 ViewHandler 参数并生成一个QMetaType::Type id 列表。然后解析路径,用列表中的 regexp 类型替换每个<arg>
。rule 不得修改QHttpServerRouter 实例。
QHttpServerRouter router; using ViewHandler = decltype([] (const QString &page, const quint32 num) { }); auto rule = std::make_unique<QHttpServerRouterRule>( "/<arg>/<arg>/log", [] (QRegularExpressionMatch &match, const QHttpServerRequest &request, QHttpServerResponder &&responder) { }); router.addRule<ViewHandler>(std::move(rule));
void QHttpServerRouter::clearConverters()
删除所有转换器。
注意: clearConverters() 不会设置默认转换器。
另请参阅 converters 和addConverter 。
bool QHttpServerRouter::handleRequest(const QHttpServerRequest &request, QHttpServerResponder &responder) const
使用responder 处理 HTTP 服务器的每个新request 。
遍历规则列表,找到第一个匹配的规则,然后执行该规则,返回true
。如果没有规则匹配请求,则返回false
。
void QHttpServerRouter::removeConverter(QMetaType metaType)
移除metaType 类型的转换器。
另请参阅 addConverter 。
© 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.