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

#ifndef QHTTPSERVERWEBSOCKETUPGRADERESPONSE_H
#define QHTTPSERVERWEBSOCKETUPGRADERESPONSE_H

#include <QtHttpServer/qthttpserverglobal.h>

#include <QtCore/qbytearray.h>

#include <algorithm>

QT_BEGIN_NAMESPACE

class QHttpServerWebSocketUpgradeResponsePrivate;
class QHttpServerWebSocketUpgradeResponse final
{

public:
    Q_HTTPSERVER_EXPORT
    QHttpServerWebSocketUpgradeResponse(const QHttpServerWebSocketUpgradeResponse &other);
    QHttpServerWebSocketUpgradeResponse(QHttpServerWebSocketUpgradeResponse &&other) noexcept
        : responseType(std::move(other.responseType))
        , errorStatus(std::move(other.errorStatus))
        , errorMessage(std::move(other.errorMessage))
        , reserved(std::exchange(other.reserved, nullptr))
    {
    }

    Q_HTTPSERVER_EXPORT ~QHttpServerWebSocketUpgradeResponse();
    Q_HTTPSERVER_EXPORT QHttpServerWebSocketUpgradeResponse &
    operator=(const QHttpServerWebSocketUpgradeResponse &other);
    QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QHttpServerWebSocketUpgradeResponse)

    void swap(QHttpServerWebSocketUpgradeResponse &other) noexcept
    {
        std::swap(responseType, other.responseType);
        std::swap(errorStatus, other.errorStatus);
        errorMessage.swap(other.errorMessage);
        std::swap(reserved, other.reserved);
    }

    enum class ResponseType {
        Accept,
        Deny,
        PassToNext,
    };

    [[nodiscard]] ResponseType type() const { return responseType; };
    [[nodiscard]] int denyStatus() const { return errorStatus; }
    [[nodiscard]] const QByteArray &denyMessage() const & { return errorMessage; }
    [[nodiscard]] QByteArray denyMessage() && { return std::move(errorMessage); }

    Q_HTTPSERVER_EXPORT static QHttpServerWebSocketUpgradeResponse accept();
    Q_HTTPSERVER_EXPORT static QHttpServerWebSocketUpgradeResponse deny();
    Q_HTTPSERVER_EXPORT static QHttpServerWebSocketUpgradeResponse deny(int status,
                                                                        QByteArray message);
    Q_HTTPSERVER_EXPORT static QHttpServerWebSocketUpgradeResponse passToNext();

private:
    QHttpServerWebSocketUpgradeResponse() = delete;
    Q_IMPLICIT QHttpServerWebSocketUpgradeResponse(ResponseType type);
    QHttpServerWebSocketUpgradeResponse(ResponseType type, int status, QByteArray message);

    ResponseType responseType;
    int errorStatus = 403;
    QByteArray errorMessage;
    void *reserved = nullptr;
};

Q_DECLARE_SHARED(QHttpServerWebSocketUpgradeResponse)

QT_END_NAMESPACE

#endif // QHTTPSERVERWEBSOCKETUPGRADERESPONSE_H