| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | // Qt-Security score:significant reason:default |
| 4 | |
| 5 | #ifndef QHTTPSERVERROUTER_H |
| 6 | #define QHTTPSERVERROUTER_H |
| 7 | |
| 8 | #include <QtHttpServer/qthttpserverglobal.h> |
| 9 | #include <QtHttpServer/qhttpserverrouterviewtraits.h> |
| 10 | |
| 11 | #include <QtCore/qscopedpointer.h> |
| 12 | #include <QtCore/qmetatype.h> |
| 13 | |
| 14 | #include <functional> |
| 15 | #include <initializer_list> |
| 16 | #include <memory> |
| 17 | |
| 18 | QT_BEGIN_NAMESPACE |
| 19 | |
| 20 | class QAbstractHttpServer; |
| 21 | class QHttpServerResponder; |
| 22 | class QHttpServerRequest; |
| 23 | class QHttpServerRouterRule; |
| 24 | |
| 25 | class QHttpServerRouterPrivate; |
| 26 | class QHttpServerRouter |
| 27 | { |
| 28 | Q_DECLARE_PRIVATE(QHttpServerRouter) |
| 29 | Q_DISABLE_COPY_MOVE(QHttpServerRouter) |
| 30 | |
| 31 | public: |
| 32 | Q_HTTPSERVER_EXPORT QHttpServerRouter(QAbstractHttpServer *server); |
| 33 | Q_HTTPSERVER_EXPORT ~QHttpServerRouter(); |
| 34 | |
| 35 | template<typename Type> |
| 36 | bool addConverter(QAnyStringView regexp) { |
| 37 | // The QMetaType converter registry is shared by all parts of Qt which uses it. |
| 38 | // Only register a converter if it is not already registered. If registering fails, |
| 39 | // check that it has been registered by a different thread between the two calls |
| 40 | // or return false. The registerConverter function will output an warning if a |
| 41 | // converter is already registered. |
| 42 | if (!QMetaType::hasRegisteredConverterFunction<QString, Type>() |
| 43 | && !QMetaType::registerConverter<QString, Type>() |
| 44 | && !QMetaType::hasRegisteredConverterFunction<QString, Type>()) |
| 45 | return false; |
| 46 | |
| 47 | addConverter(QMetaType::fromType<Type>(), regexp); |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | Q_HTTPSERVER_EXPORT void addConverter(QMetaType metaType, QAnyStringView regexp); |
| 52 | Q_HTTPSERVER_EXPORT void removeConverter(QMetaType metaType); |
| 53 | Q_HTTPSERVER_EXPORT void clearConverters(); |
| 54 | Q_HTTPSERVER_EXPORT const QHash<QMetaType, QString> &converters() const &; |
| 55 | Q_HTTPSERVER_EXPORT QHash<QMetaType, QString> converters() &&; |
| 56 | |
| 57 | template<typename ViewHandler, typename ViewTraits = QHttpServerRouterViewTraits<ViewHandler>> |
| 58 | QHttpServerRouterRule *addRule(std::unique_ptr<QHttpServerRouterRule> rule) |
| 59 | { |
| 60 | return addRuleHelper<ViewTraits>( |
| 61 | std::move(rule), |
| 62 | typename ViewTraits::Arguments::Indexes{}); |
| 63 | } |
| 64 | |
| 65 | Q_HTTPSERVER_EXPORT bool handleRequest(const QHttpServerRequest &request, |
| 66 | QHttpServerResponder &responder) const; |
| 67 | |
| 68 | private: |
| 69 | template<typename ViewTraits, size_t ... Idx> |
| 70 | QHttpServerRouterRule *addRuleHelper(std::unique_ptr<QHttpServerRouterRule> rule, |
| 71 | std::index_sequence<Idx...>) |
| 72 | { |
| 73 | return addRuleImpl(rule: std::move(rule), metaTypes: {ViewTraits::Arguments::template metaType<Idx>()...}); |
| 74 | } |
| 75 | |
| 76 | Q_HTTPSERVER_EXPORT QHttpServerRouterRule *addRuleImpl(std::unique_ptr<QHttpServerRouterRule> rule, |
| 77 | std::initializer_list<QMetaType> metaTypes); |
| 78 | |
| 79 | std::unique_ptr<QHttpServerRouterPrivate> d_ptr; |
| 80 | }; |
| 81 | |
| 82 | QT_END_NAMESPACE |
| 83 | |
| 84 | #endif // QHTTPSERVERROUTER_H |
| 85 | |