1// Copyright (C) 2024 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#include <QtHttpServer/qhttpserverwebsocketupgraderesponse.h>
6
7#include <QtCore/QString>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt::Literals::StringLiterals;
12
13/*!
14 \class QHttpServerWebSocketUpgradeResponse
15 \since 6.8
16 \inmodule QtHttpServer
17 \brief Response to return when verifying WebSocket upgrades on HTTP server.
18
19 Use this class to return when determining whether a socket upgrade should
20 succeed. If type() is \l Accept upgrade the socket, if type() is \l Deny
21 send an error with the given denyStatus() and denyMessage(), and if type()
22 is \l PassToNext proceed to the next registered handler.
23 If all handlers return \l PassToNext or none exist,
24 QAbstractHttpServer::missingHandler() is executed.
25
26 \sa QAbstractHttpServer::addWebSocketUpgradeVerifier(),
27 QAbstractHttpServer::missingHandler()
28*/
29
30/*!
31 \enum QHttpServerWebSocketUpgradeResponse::ResponseType
32
33 Response types
34
35 \value Accept Accept the WebSocket upgrade request.
36 \value Deny Deny the WebSocket upgrade request.
37 \value PassToNext Pass the Websocket upgrade decision to the next verifier if any.
38 \sa QAbstractHttpServer::addWebSocketUpgradeVerifier(), type()
39*/
40
41/*!
42 \internal
43*/
44QHttpServerWebSocketUpgradeResponse::QHttpServerWebSocketUpgradeResponse(ResponseType type)
45 : responseType(type), errorMessage("Forbidden"_ba), reserved(nullptr)
46{
47}
48
49/*!
50 \internal
51*/
52QHttpServerWebSocketUpgradeResponse::QHttpServerWebSocketUpgradeResponse(ResponseType type,
53 int status,
54 QByteArray message)
55 : responseType(type), errorStatus(status), errorMessage(message), reserved(nullptr)
56{
57}
58
59/*\fn QHttpServerWebSocketUpgradeResponse::QHttpServerWebSocketUpgradeResponse(QHttpServerWebSocketUpgradeResponse &&other) noexcept
60
61 Move-constructs an instance of a QHttpServerWebSocketUpgradeResponse object from \a other.
62*/
63
64/*!
65 Copy-constructs an instance of a QHttpServerWebSocketUpgradeResponse object from \a other.
66*/
67QHttpServerWebSocketUpgradeResponse::QHttpServerWebSocketUpgradeResponse(
68 const QHttpServerWebSocketUpgradeResponse &other)
69 : responseType(other.responseType),
70 errorStatus(other.errorStatus),
71 errorMessage(other.errorMessage),
72 reserved(nullptr)
73{
74}
75
76/*!
77 Destroys a QHttpServerWebSocketUpgradeResponse object.
78*/
79QHttpServerWebSocketUpgradeResponse::~QHttpServerWebSocketUpgradeResponse() noexcept = default;
80
81/*!
82 \fn QHttpServerWebSocketUpgradeResponse &QHttpServerWebSocketUpgradeResponse::operator=(QHttpServerWebSocketUpgradeResponse &&other) noexcept
83
84 Move-assigns the values of \a other to this object.
85*/
86
87/*!
88 Copy-assigns the values of \a other to this object.
89*/
90QHttpServerWebSocketUpgradeResponse &
91QHttpServerWebSocketUpgradeResponse::operator=(const QHttpServerWebSocketUpgradeResponse &other)
92{
93 responseType = other.responseType;
94 errorStatus = other.errorStatus;
95 errorMessage = other.errorMessage;
96 reserved = nullptr;
97 return *this;
98}
99
100/*!
101 \fn void QHttpServerWebSocketUpgradeResponse::swap(QHttpServerWebSocketUpgradeResponse &other) noexcept
102 Swaps the contents of this with \a other
103*/
104
105/*!
106 Creates an instance of QHttpServerWebSocketUpgradeResponse with type()
107 \l Accept.
108
109 \sa ResponseType, type()
110*/
111QHttpServerWebSocketUpgradeResponse QHttpServerWebSocketUpgradeResponse::accept()
112{
113 return QHttpServerWebSocketUpgradeResponse::ResponseType::Accept;
114}
115
116/*!
117 Creates an instance of QHttpServerWebSocketUpgradeResponse with
118 \l type() \l Deny, \l denyStatus() \c 403 and the \l denyMessage()
119 \c "Forbidden".
120
121 \sa ResponseType, type(), denyStatus(), denyMessage()
122*/
123QHttpServerWebSocketUpgradeResponse QHttpServerWebSocketUpgradeResponse::deny()
124{
125 return QHttpServerWebSocketUpgradeResponse::ResponseType::Deny;
126}
127
128/*!
129 Creates an instance of QHttpServerWebSocketUpgradeResponse with type()
130 \l Deny, denyStatus() \a status and denyMessage() \a message.
131
132 \sa ResponseType, type(), denyStatus(), denyMessage()
133*/
134QHttpServerWebSocketUpgradeResponse QHttpServerWebSocketUpgradeResponse::deny(int status,
135 QByteArray message)
136{
137 return { QHttpServerWebSocketUpgradeResponse::ResponseType::Deny, status, message };
138}
139
140/*!
141 Creates an instance of QHttpServerWebSocketUpgradeResponse with type() \l PassToNext.
142
143 \sa ResponseType, type()
144*/
145QHttpServerWebSocketUpgradeResponse QHttpServerWebSocketUpgradeResponse::passToNext()
146{
147 return QHttpServerWebSocketUpgradeResponse::ResponseType::PassToNext;
148}
149
150/*!
151 \fn QHttpServerWebSocketUpgradeResponse::ResponseType QHttpServerWebSocketUpgradeResponse::type() const
152
153 Returns the type of response.
154
155 \sa ResponseType
156*/
157
158/*!
159 \fn int QHttpServerWebSocketUpgradeResponse::denyStatus() const
160
161 Returns the HTTP status code to return if type() is \l Deny.
162*/
163
164/*!
165 \fn const QByteArray &QHttpServerWebSocketUpgradeResponse::denyMessage() const &
166
167 Returns the error message to return if type() is \l Deny.
168*/
169
170/*!
171 \fn QByteArray QHttpServerWebSocketUpgradeResponse::denyMessage() &&
172
173 Returns the error message to return if type() is \l Deny.
174*/
175
176QT_END_NAMESPACE
177

source code of qthttpserver/src/httpserver/qhttpserverwebsocketupgraderesponse.cpp