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= [](constquint64ページ)    qDebug() << "page" << page;
};usingViewHandler=decltype(pageView);QHttpServerRouterrouter;// リクエスト"/page/<number>"にコールバックpageViewを登録する// 例えば:"/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()

QHttpServerRouter を破壊する。

template <typename Type> bool QHttpServerRouter::addConverter(QAnyStringView regexp)

regexp でパース可能な型の新しいコンバータを追加し、成功した場合はtrue を返し、そうでない場合はfalse を返す。成功した場合、登録された型はQHttpServerRouterRule のハンドラの引数として使用できる。正規表現は、ルールのパスパターンを解析するために使用される。

Type型のコンバータがすでに存在する場合、そのコンバータの正規表現は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)

regexp でパースできるmetaType の新しいコンバータを追加します。metaType のコンバータを持つことで、この型をQHttpServerRouterRule のパスパターンで使用することができます。 正規表現は、パスパターンからmetaType 型のパラメータをパースするために使用されます。

すでにmetaType 型のコンバーターがある場合、そのコンバーターの正規表現は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 のリストを生成します。次に、パスを解析し、各<arg> を、リストからその型に対応する正規表現で置き換えます。ruleQHttpServerRouter インスタンスを変更してはならない。

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

HTTPサーバーの各新規request を、responder を使用して処理する。

ルールのリストを繰り返し、最初にマッチするルールを見つけ、そのルールを 実行して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.