このページでは

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 に定義されている値のいずれかに対応します。ルールの構築時にメソッドが指定されていない場合、既知のすべてのメソッドによるリクエストに一致します。

ハンドラのシグネチャ

ハンドラは、以下のシグネチャを持つコールバック関数です:

  • 最初の引数には、パスから一致したキャプチャグループが渡されます。
  • 2番目の引数には、リクエストの詳細情報が含まれます。
  • 3番目の引数は、レスポンスを送信するために使用されます。

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 は、関数ポインタ、非可変ラムダ、または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 に渡す、バインド済みの呼び出し可能オブジェクトを返します。

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) {
    // マッチからキャプチャされた文字列と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.