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