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