QHttpServerRouter Class

URL を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 に置き換えられる。

最小限の例:

structCustomArg {intdata= 10; CustomArg() {} ; CustomArg(constQString&urlArg) : data(urlArg.toInt()) {} }; Q_DECLARE_METATYPE(CustomArg);QHttpServerRouterrouter; router.addConverter<CustomArg>(u"[+-]?    qDebug("data: %d", customArg.data);
};usingViewHandler=decltype(pageView);autorule=std::make_unique<QHttpServerRouterRule>("/<arg>/log", [&router, &pageView](QRegularExpressionMatch&match, constQHttpServerRequest&requestQHttpServerResponder&&responder) {// マッチがキャプチャした文字列とquint32でviewHandlerをバインドして呼び出すQHttpServerRouterRule::bindCaptured(pageView,match)(); }); router.addRule<ViewHandler>(std::move(rule));

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 のリストを生成します。次に、URLを解析し、<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.