summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverrouterrule.cpp
blob: 1200e0a01e5a9f49e69eb5df18786af21fdbcb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtHttpServer/qhttpserverrouterrule.h>
#include <QtHttpServer/qhttpserverresponder.h>

#include <private/qhttpserverrouterrule_p.h>
#include <private/qhttpserverrequest_p.h>

#include <QtCore/qmetaobject.h>
#include <QtCore/qloggingcategory.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qstringbuilder.h>
#include <QtCore/qdebug.h>

QT_BEGIN_NAMESPACE

Q_STATIC_LOGGING_CATEGORY(lcRouterRule, "qt.httpserver.router.rule")

/*!
    \class QHttpServerRouterRule
    \since 6.4
    \brief The QHttpServerRouterRule is the base class for QHttpServerRouter rules.
    \inmodule QtHttpServer

    QHttpServerRouterRule defines the relationship between a request path, an
    HTTP method, and the corresponding handler callback. A QHttpServerRouter
    is a collection of these rules, executing the appropriate handler when a
    request matches both the path and method. The handler is responsible for
    generating the response.

    \section1 Paths and Patterns

    Each QHttpServerRouterRule includes a path or pattern that determines
    which requests it can handle. Paths may contain placeholders that
    are passed to the handler. The examples below illustrate path patterns
    using the QHttpServer::route() convenience method, though they can also
    be set using the QHttpServerRouterRule constructor.

    In the simplest case the path is a string with a leading \c "/":
    \code
    QHttpServer server;
    server.route("/user", [] () { return "hello user"; } );
    \endcode

    This path pattern defines a rule that directs all requests to \c "/user"
    to the specified handler, which in this case is a simple lambda function.
    (Note that when using QHttpServerRouterRule directly, the handler syntax
    differs—see below.)

    A trailing \c "/" in the path pattern allows the rule to match additional
    paths with arguments after the \c "/". When using the QHttpServer::route()
    convenience method, the argument is automatically passed to the lambda
    function:
    \code
    server.route("/user/", [] ( qint64 id ) { return "hello user"; } );
    \endcode
    This would match request paths such as \c "/user/1", \c "/user/2", and so
    on.

    \section2 Capturing Arguments in the Path

    You can place arguments anywhere in the path pattern using the \c "<arg>"
    placeholders, and multiple of them are supported in the path:
    \code
    server.route("/user/<arg>/history", [] (qint64 id){ return "hello user"; } );
    server.route("/user/<arg>/history/", [] (qint64 id, qint64 page){ return "hello user"; } );
    \endcode
    For example, this would match a request like \c "/user/1/history/2". Any
    data type registered in QHttpServerRouter::converters() can be used in
    both the callback function and the corresponding placeholders in
    the path.

    \section1 Request Method

    The request method corresponds to one of the values in
    QHttpServerRequest::Method. If no method is specified when constructing
    a rule, it will match requests of any known method.

    \section1 Handler Signature

    A handler is a callback function with the following signature:
    \code
    void (*)(const QRegularExpressionMatch &, const QHttpServerRequest &, QHttpServerResponder &);
    \endcode
    \list
        \li The first argument receives any matched capture groups from the
        path.
        \li The second argument contains request details.
        \li The third argument is used to send the response.
    \endlist

    \section1 Adding Rules to QHttpServerRouter

    The example below demonstrates how to create and register a new rule with
    a handler in \l QHttpServerRouter:
    \code
    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"
    \endcode

    \note This is a low-level API. For higher-level alternatives, see
    \l QHttpServer.

    \note Regular expressions are not supported in path patterns, but you can
    use \l QHttpServerRouter::addConverter() to match \c "<arg>" to a
    specific type.
*/

/*! \fn template <typename Functor, typename ViewTraits = QHttpServerRouterViewTraits<Functor>> static typename ViewTraits::BindableType QHttpServerRouterRule::bindCaptured(QObject *receiver, Functor &&slot, const QRegularExpressionMatch &match) const

    Binds the given \a receiver and \a slot with arguments extracted from the
    URL. The function returns a bound callable that takes any remaining
    arguments required by the handler, supplying them to \a slot after the
    URL-derived values.

    Each captured value from the URL (as a string) is converted to the
    corresponding parameter type in the handler based on its position,
    ensuring it can be passed as \a match.

    \code
    QHttpServerRouter router;

    auto pageView = [] (const QString &page, const quint32 num) {
        qDebug("page: %s, num: %d", qPrintable(page), num);
    };
    using ViewHandler = decltype(pageView);

    auto rule = std::make_unique<QHttpServerRouterRule>(
        "/<arg>/<arg>/log",
        [&router, &pageView] (QRegularExpressionMatch &match,
                              const QHttpServerRequest &request,
                              QHttpServerResponder &&responder) {
        // Bind and call viewHandler with match's captured string and quint32:
        QHttpServerRouterRule::bindCaptured(pageView, match)();
    });

    router.addRule<ViewHandler>(std::move(rule));
    \endcode
*/

/*! \fn template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QHttpServerRequest::Methods methods, const QObject *receiver, Functor &&slot)

    Creates a routing rule for \a pathPattern and \a methods, connecting it
    to the specified \a receiver and \a slot.
    \list
        \li The \a slot can be a function pointer, non-mutable lambda, or any
        other copyable callable with \c const call operator.
        \li If \a slot is callable, \a receiver acts as its context object.
        \li The handler remains valid until the \a receiver is destroyed.
    \endlist
    The rule can handle any combination of available HTTP methods.

    \sa QHttpServerRequest::Methods
*/

/*! \fn template <typename Functor> QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern, const QObject *receiver, Functor &&slot)

    \overload

    This overload constructs a routing rule for \a pathPattern and associates
    it with \a receiver and \a slot.
    \list
        \li It defaults to \l QHttpServerRequest::Method::AnyKnown, meaning
        it will match any recognized HTTP method.
        \li The \a slot can be a function pointer, non-mutable lambda, or any
        other copyable callable with \c const call operator.
        \li If \a slot is callable, \a receiver acts as its context object.
        \li The handler remains valid until the \a receiver is destroyed.
    \endlist
*/
QHttpServerRouterRule::QHttpServerRouterRule(const QString &pathPattern,
                                             const QHttpServerRequest::Methods methods,
                                             const QObject *context,
                                             QtPrivate::QSlotObjectBase *slotObjRaw)
    : QHttpServerRouterRule(new QHttpServerRouterRulePrivate{
              pathPattern, methods, QtPrivate::SlotObjUniquePtr(slotObjRaw), QPointer(context), {}})
{
    Q_ASSERT(slotObjRaw);
}

/*!
    \internal
*/
QHttpServerRouterRule::QHttpServerRouterRule(QHttpServerRouterRulePrivate *d)
    : d_ptr(d)
{
}

/*!
    Destroys a QHttpServerRouterRule.
*/
QHttpServerRouterRule::~QHttpServerRouterRule()
{
}

/*!
    Retrieves the context object associated with this rule. This object
    serves as the receiver responsible for handling the request.
*/
const QObject *QHttpServerRouterRule::contextObject() const
{
    Q_D(const QHttpServerRouterRule);
    return d->context;
}

/*!
    Validates the Request Method. Returns \c true if the specified HTTP
    method is valid.
*/
bool QHttpServerRouterRule::hasValidMethods() const
{
    Q_D(const QHttpServerRouterRule);
    return d->methods & QHttpServerRequest::Method::AnyKnown;
}

/*!
    Executes the rule. Processes the given \a request by checking if it
    matches this rule.
    \list
        \li This function is called by QHttpServerRouter when a new
        \a request is received.
        \li If the \a request matches the rule, it handles the request by
        sending a response through the provided \a responder and returns
        \c true.
        \li If there is no match, it returns \c false.
    \endlist
*/
bool QHttpServerRouterRule::exec(const QHttpServerRequest &request,
                                 QHttpServerResponder &responder) const
{
    Q_D(const QHttpServerRouterRule);
    if (!d->routerHandler)
        return false;

    QRegularExpressionMatch match;
    if (!matches(request, &match))
        return false;

    void *args[] = { nullptr, &match, const_cast<QHttpServerRequest *>(&request), &responder };
    Q_ASSERT(d->routerHandler);
    d->routerHandler->call(nullptr, args);
    return true;
}

/*!
    Determines whether the provided \a request meets the conditions of this
    rule.
    \list
        \li This virtual function is called by \l exec() to evaluate the
        \a request.
        \li If the request matches, the details are stored in \a match
        (which \e{must not} be \c nullptr), and the function returns \c true.
        \li Otherwise, it returns \c false.
    \endlist
*/
bool QHttpServerRouterRule::matches(const QHttpServerRequest &request,
                                    QRegularExpressionMatch *match) const
{
    Q_D(const QHttpServerRouterRule);

    if (d->methods && !(d->methods & request.method()))
        return false;

    *match = d->pathRegexp.match(request.url().path());
    return (match->hasMatch() && d->pathRegexp.captureCount() == match->lastCapturedIndex());
}

/*!
    \internal
*/
bool QHttpServerRouterRule::createPathRegexp(std::initializer_list<QMetaType> metaTypes,
                                             const QHash<QMetaType, QString> &converters)
{
    Q_D(QHttpServerRouterRule);

    QString pathRegexp = d->pathPattern;
    const QLatin1StringView arg("<arg>");
    for (auto metaType : metaTypes) {
        if (metaType.id() >= QMetaType::User
            && !QMetaType::hasRegisteredConverterFunction(QMetaType::fromType<QString>(), metaType)) {
            qCWarning(lcRouterRule,
                      "%s has not registered a converter to QString. "
                      "Use QHttpServerRouter::addConveter<Type>(converter).",
                      metaType.name());
            return false;
        }

        auto it = converters.constFind(metaType);
        if (it == converters.end()) {
            qCWarning(lcRouterRule, "Can not find converter for type: %s", metaType.name());
            return false;
        }

        if (it->isEmpty())
            continue;

        const auto index = pathRegexp.indexOf(arg);
        const QString &regexp = QLatin1Char('(') % *it % QLatin1Char(')');
        if (index == -1)
            pathRegexp.append(regexp);
        else
            pathRegexp.replace(index, arg.size(), regexp);
    }

    if (pathRegexp.indexOf(arg) != -1) {
        qCWarning(lcRouterRule) << "not enough types or one of the types is not supported, regexp:"
                                << pathRegexp
                                << ", pattern:" << d->pathPattern
                                << ", types:" << metaTypes;
        return false;
    }

    if (!pathRegexp.startsWith(QLatin1Char('^')))
        pathRegexp = QLatin1Char('^') % pathRegexp;
    if (!pathRegexp.endsWith(QLatin1Char('$')))
        pathRegexp += u'$';

    qCDebug(lcRouterRule) << "url pathRegexp:" << pathRegexp;

    d->pathRegexp.setPattern(pathRegexp);
    d->pathRegexp.optimize();
    return true;
}

QT_END_NAMESPACE