1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QHTTPSERVERROUTERRULE_H
5#define QHTTPSERVERROUTERRULE_H
6
7#include <QtHttpServer/qhttpserverrequest.h>
8
9#include <QtCore/qcontainerfwd.h>
10
11#include <functional> // for std::function
12#include <initializer_list>
13#include <memory>
14
15QT_BEGIN_NAMESPACE
16
17class QString;
18class QHttpServerRequest;
19class QHttpServerResponder;
20class QRegularExpressionMatch;
21class QHttpServerRouter;
22
23class QHttpServerRouterRulePrivate;
24class Q_HTTPSERVER_EXPORT QHttpServerRouterRule
25{
26 Q_DECLARE_PRIVATE(QHttpServerRouterRule)
27 Q_DISABLE_COPY_MOVE(QHttpServerRouterRule)
28
29public:
30 using RouterHandler = std::function<void(const QRegularExpressionMatch &,
31 const QHttpServerRequest &, QHttpServerResponder &&)>;
32
33 explicit QHttpServerRouterRule(const QString &pathPattern, RouterHandler routerHandler);
34 explicit QHttpServerRouterRule(const QString &pathPattern,
35 const QHttpServerRequest::Methods methods,
36 RouterHandler routerHandler);
37 virtual ~QHttpServerRouterRule();
38
39protected:
40 bool exec(const QHttpServerRequest &request, QHttpServerResponder &responder) const;
41
42 bool hasValidMethods() const;
43
44 bool createPathRegexp(std::initializer_list<QMetaType> metaTypes,
45 const QHash<QMetaType, QString> &converters);
46
47 virtual bool matches(const QHttpServerRequest &request,
48 QRegularExpressionMatch *match) const;
49
50 QHttpServerRouterRule(QHttpServerRouterRulePrivate *d);
51
52private:
53 std::unique_ptr<QHttpServerRouterRulePrivate> d_ptr;
54
55 friend class QHttpServerRouter;
56};
57
58QT_END_NAMESPACE
59
60#endif // QHTTPSERVERROUTERRULE_H
61

source code of qthttpserver/src/httpserver/qhttpserverrouterrule.h