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"
へのすべてのリクエストを指定されたハンドラ (この場合は単純なラムダ関数) に送るルールを定義します。(QHttpServerRouterRule を直接使う場合はハンドラの構文が異なることに注意してください。)
パスパターンの末尾に"/"
を指定することで、ルールは"/"
の後に引数を持つ追加のパスにマッチするようになります。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"; } );
例えば、これは"/user/1/history/2"
のようなリクエストにマッチします。QHttpServerRouter::converters() に登録されているデータ型は、コールバック関数とパス内の対応するプレースホルダの両方で使用できます。
リクエストメソッド
リクエストメソッドは、QHttpServerRequest::Method の値のいずれかに対応する。 ルール構築時にメソッドが指定されない場合、既知のメソッドのリクエストに マッチする。
ハンドラのシグネチャ
ハンドラは、以下のシグネチャを持つコールバック関数である:
void (*)(const QRegularExpressionMatch &, const QHttpServerRequest &, QHttpServerResponder &);
- 最初の引数は、パスからマッチしたキャプチャグループを受け取る。
- 第二引数はリクエストの詳細を含む。
- 第三引数はレスポンスを送信するために使用される。
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 です。より高レベルの API についてはQHttpServer を参照してください。
注意: 正規表現はパスパターンではサポートされていませんが、"<arg>"
を特定の型にマッチさせるためにQHttpServerRouter::addConverter() を使用することができます。
メンバー関数ドキュメント
template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, const QObject *receiver, Functor &&slot)
pathPattern とmethods のルーティングルールを作成し、指定されたreceiver とslot に接続します。
- slot は、関数ポインタ、ミュータブルでないラムダ、または
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 は、関数ポインタ、ミュータブルでないラムダ、または
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 に供給する、バインドされた callable を返します。
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 がこのルールにマッチするかどうかをチェックして処理する。
- この関数は、新しい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 がこのルールの条件を満たすかどうかを判定する。
- この仮想関数は、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.