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" を持つすべてのリクエストを指定された hanlder に転送するルールを作成します。
パスパターンにはさらに末尾の "/" を含めることができ、末尾の "/" の後に引数を持つパスの集合を扱うルールを作成します。引数はQRegularExpressionMatch としてRuleに転送されます。QHttpServer::route 便利メソッドを使うと、引数は直接ラムダに転送されます:
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"; } );
これは例えば、リクエストURL「/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 は、関数ポインタ、ミュータブルでないラムダ、または const call 演算子を持つ他のコピー可能な呼び出し可能オブジェクトにすることもできます。その場合、receiver はコンテキストオブジェクトとなる。ハンドラは、受信オブジェクトが破棄されるまで有効である。
このルールは、利用可能な HTTP メソッドの任意の組み合わせを受け入れます。
QHttpServerRequest::Methodsも参照のこと 。
template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QObject *receiver, Functor &&slot)
これはオーバーロードされた関数である。
pathPattern,QHttpServerRequest::Method::AnyKnown のルールを構築し、それをreceiver とslot に接続する。slot は、関数ポインタ、ミュータブルでないラムダ、または const call 演算子を持つ他のコピー可能な呼び出し可能な関数にすることもできます。その場合、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由来の値の後にスロットに供給する、バインドされた関数を返します。URLに(文字列として)適用された正規表現の各マッチは、match として渡せるように、その位置でハンドラのパラメータの型に変換される。
QHttpServerRouterルーター;autopageView= [](constQStringページ, constquint32num) 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) {// マッチがキャプチャした文字列と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 がこのルールにマッチするかどうかを判定する。
この仮想関数は、request がこのルールにマッチするかどうかを調べるために、exec() から呼び出される。マッチした場合、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.