summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverrouter.h
blob: 1c2f6f2c27e6e6ecb1c98782c6b3cb5138cd5234 (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QHTTPSERVERROUTER_H
#define QHTTPSERVERROUTER_H

#include <QtHttpServer/qthttpserverglobal.h>
#include <QtHttpServer/qhttpserverrouterviewtraits.h>

#include <QtCore/qscopedpointer.h>
#include <QtCore/qmetatype.h>

#include <functional>
#include <initializer_list>
#include <memory>

QT_BEGIN_NAMESPACE

class QAbstractHttpServer;
class QHttpServerResponder;
class QHttpServerRequest;
class QHttpServerRouterRule;

class QHttpServerRouterPrivate;
class QHttpServerRouter
{
    Q_DECLARE_PRIVATE(QHttpServerRouter)
    Q_DISABLE_COPY_MOVE(QHttpServerRouter)

public:
    Q_HTTPSERVER_EXPORT QHttpServerRouter(QAbstractHttpServer *server);
    Q_HTTPSERVER_EXPORT ~QHttpServerRouter();

    template<typename Type>
    bool addConverter(QAnyStringView regexp) {
        // The QMetaType converter registry is shared by all parts of Qt which uses it.
        // Only register a converter if it is not already registered. If registering fails,
        // check that it has been registered by a different thread between the two calls
        // or return false. The registerConverter function will output an warning if a
        // converter is already registered.
        if (!QMetaType::hasRegisteredConverterFunction<QString, Type>()
            && !QMetaType::registerConverter<QString, Type>()
            && !QMetaType::hasRegisteredConverterFunction<QString, Type>())
            return false;

        addConverter(QMetaType::fromType<Type>(), regexp);
        return true;
    }

    Q_HTTPSERVER_EXPORT void addConverter(QMetaType metaType, QAnyStringView regexp);
    Q_HTTPSERVER_EXPORT void removeConverter(QMetaType metaType);
    Q_HTTPSERVER_EXPORT void clearConverters();
    Q_HTTPSERVER_EXPORT const QHash<QMetaType, QString> &converters() const &;
    Q_HTTPSERVER_EXPORT QHash<QMetaType, QString> converters() &&;

    template<typename ViewHandler, typename ViewTraits = QHttpServerRouterViewTraits<ViewHandler>>
    QHttpServerRouterRule *addRule(std::unique_ptr<QHttpServerRouterRule> rule)
    {
        return addRuleHelper<ViewTraits>(
                std::move(rule),
                typename ViewTraits::Arguments::Indexes{});
    }

    Q_HTTPSERVER_EXPORT bool handleRequest(const QHttpServerRequest &request,
                                           QHttpServerResponder &responder) const;

private:
    template<typename ViewTraits, size_t ... Idx>
    QHttpServerRouterRule *addRuleHelper(std::unique_ptr<QHttpServerRouterRule> rule,
                       std::index_sequence<Idx...>)
    {
        return addRuleImpl(std::move(rule), {ViewTraits::Arguments::template metaType<Idx>()...});
    }

    Q_HTTPSERVER_EXPORT QHttpServerRouterRule *addRuleImpl(std::unique_ptr<QHttpServerRouterRule> rule,
                                         std::initializer_list<QMetaType> metaTypes);

    std::unique_ptr<QHttpServerRouterPrivate> d_ptr;
};

QT_END_NAMESPACE

#endif // QHTTPSERVERROUTER_H