| 1 | // Copyright (C) 2019 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/qhttpserverresponse.h> |
| 6 | |
| 7 | #include <private/qhttpserverliterals_p.h> |
| 8 | #include <private/qhttpserverresponse_p.h> |
| 9 | #include <private/qhttpserverresponder_p.h> |
| 10 | |
| 11 | #include <QtCore/qfile.h> |
| 12 | #include <QtCore/qjsondocument.h> |
| 13 | #include <QtCore/qjsonobject.h> |
| 14 | #include <QtCore/qmimedatabase.h> |
| 15 | #include <QtNetwork/qtcpsocket.h> |
| 16 | |
| 17 | QT_BEGIN_NAMESPACE |
| 18 | |
| 19 | /*! |
| 20 | \class QHttpServerResponse |
| 21 | \since 6.4 |
| 22 | \inmodule QtHttpServer |
| 23 | \brief Encapsulates an HTTP response. |
| 24 | |
| 25 | API for creating, reading and modifying a response from an HTTP server, |
| 26 | and for writing its contents to a QHttpServerResponder. |
| 27 | It has numerous constructors, and \c static function \c fromFile for |
| 28 | constructing it from the contents of a file. There are functions for |
| 29 | setting, getting, and removing headers, and for getting the data, status |
| 30 | code and mime type. |
| 31 | */ |
| 32 | |
| 33 | /*! |
| 34 | \internal |
| 35 | */ |
| 36 | QHttpServerResponsePrivate::QHttpServerResponsePrivate( |
| 37 | QByteArray &&d, const QHttpServerResponse::StatusCode sc) |
| 38 | : data(std::move(d)), |
| 39 | statusCode(sc) |
| 40 | { } |
| 41 | |
| 42 | /*! |
| 43 | \internal |
| 44 | */ |
| 45 | QHttpServerResponsePrivate::QHttpServerResponsePrivate(const QHttpServerResponse::StatusCode sc) |
| 46 | : statusCode(sc) |
| 47 | { } |
| 48 | |
| 49 | /*! |
| 50 | \typealias QHttpServerResponse::StatusCode |
| 51 | |
| 52 | Type alias for QHttpServerResponder::StatusCode |
| 53 | */ |
| 54 | |
| 55 | /*! |
| 56 | \fn QHttpServerResponse::QHttpServerResponse(QHttpServerResponse &&other) noexcept |
| 57 | |
| 58 | Move-constructs a new QHttpServerResponse from \a other. |
| 59 | */ |
| 60 | |
| 61 | /*! |
| 62 | \fn QHttpServerResponse &QHttpServerResponse::operator=( |
| 63 | QHttpServerResponse &&other) noexcept |
| 64 | |
| 65 | Move-assigns \a other to this QHttpServerResponse instance. |
| 66 | */ |
| 67 | |
| 68 | /*! |
| 69 | \fn void QHttpServerResponse::swap(QHttpServerResponse &other) |
| 70 | |
| 71 | Swaps this QHttpServerResponse with \a other. This operation is very |
| 72 | fast and never fails. |
| 73 | */ |
| 74 | |
| 75 | /*! |
| 76 | Creates a QHttpServerResponse object with the status code \a statusCode. |
| 77 | */ |
| 78 | QHttpServerResponse::QHttpServerResponse(QHttpServerResponse::StatusCode statusCode) |
| 79 | : QHttpServerResponse(QHttpServerLiterals::contentTypeXEmpty(), QByteArray(), statusCode) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | /*! |
| 84 | Creates a QHttpServerResponse object from \a data with the status code \a status. |
| 85 | */ |
| 86 | QHttpServerResponse::QHttpServerResponse(const char *data, StatusCode status) |
| 87 | : QHttpServerResponse(QByteArray::fromRawData(data, size: qstrlen(str: data)), status) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | /*! |
| 92 | Creates a QHttpServerResponse object from \a data with the status code \a status. |
| 93 | */ |
| 94 | QHttpServerResponse::QHttpServerResponse(const QString &data, StatusCode status) |
| 95 | : QHttpServerResponse(data.toUtf8(), status) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | /*! |
| 100 | Creates a QHttpServerResponse object from \a data with the status code \a status. |
| 101 | */ |
| 102 | QHttpServerResponse::QHttpServerResponse(const QByteArray &data, StatusCode status) |
| 103 | : QHttpServerResponse(QMimeDatabase().mimeTypeForData(data).name().toLocal8Bit(), data, status) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | /*! |
| 108 | Move-constructs a QHttpServerResponse whose body will contain the given |
| 109 | \a data with the status code \a status. |
| 110 | */ |
| 111 | QHttpServerResponse::QHttpServerResponse(QByteArray &&data, StatusCode status) |
| 112 | : QHttpServerResponse(QMimeDatabase().mimeTypeForData(data).name().toLocal8Bit(), |
| 113 | std::move(data), status) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | /*! |
| 118 | Creates a QHttpServerResponse object from \a data with the status code \a status. |
| 119 | */ |
| 120 | QHttpServerResponse::QHttpServerResponse(const QJsonObject &data, StatusCode status) |
| 121 | : QHttpServerResponse(QHttpServerLiterals::contentTypeJson(), |
| 122 | QJsonDocument(data).toJson(format: QJsonDocument::Compact), status) |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | /*! |
| 127 | Creates a QHttpServerResponse object from \a data with the status code \a status. |
| 128 | */ |
| 129 | QHttpServerResponse::QHttpServerResponse(const QJsonArray &data, StatusCode status) |
| 130 | : QHttpServerResponse(QHttpServerLiterals::contentTypeJson(), |
| 131 | QJsonDocument(data).toJson(format: QJsonDocument::Compact), status) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | /*! |
| 136 | \fn QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, |
| 137 | const QByteArray &data, |
| 138 | StatusCode status) |
| 139 | \fn QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, |
| 140 | QByteArray &&data, |
| 141 | StatusCode status) |
| 142 | |
| 143 | Creates a QHttpServer response. |
| 144 | |
| 145 | The response will use the given \a status code and deliver the \a data as |
| 146 | its body, with a \c ContentType header describing it as being of MIME type |
| 147 | \a mimeType. |
| 148 | */ |
| 149 | QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, |
| 150 | const QByteArray &data, |
| 151 | StatusCode status) |
| 152 | : d_ptr(new QHttpServerResponsePrivate(QByteArray(data), status)) |
| 153 | { |
| 154 | if (!mimeType.isEmpty()) { |
| 155 | d_ptr->headers.append(name: QHttpHeaders::WellKnownHeader::ContentType, value: mimeType); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | QHttpServerResponse::QHttpServerResponse(const QByteArray &mimeType, |
| 160 | QByteArray &&data, |
| 161 | StatusCode status) |
| 162 | : d_ptr(new QHttpServerResponsePrivate(std::move(data), status)) |
| 163 | { |
| 164 | if (!mimeType.isEmpty()) { |
| 165 | d_ptr->headers.append(name: QHttpHeaders::WellKnownHeader::ContentType, value: mimeType); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /*! |
| 170 | Destroys a QHttpServerResponse object. |
| 171 | */ |
| 172 | QHttpServerResponse::~QHttpServerResponse() |
| 173 | { |
| 174 | delete d_ptr; |
| 175 | }; |
| 176 | |
| 177 | /*! |
| 178 | Returns a QHttpServerResponse from the content of the file \a fileName. |
| 179 | |
| 180 | It is the caller's responsibility to sanity-check the filename, and to have |
| 181 | a well-defined policy for which files the server will request. |
| 182 | */ |
| 183 | QHttpServerResponse QHttpServerResponse::fromFile(const QString &fileName) |
| 184 | { |
| 185 | QFile file(fileName); |
| 186 | if (!file.open(flags: QFile::ReadOnly)) |
| 187 | return QHttpServerResponse(StatusCode::NotFound); |
| 188 | const QByteArray data = file.readAll(); |
| 189 | file.close(); |
| 190 | const QByteArray mimeType = QMimeDatabase().mimeTypeForFileNameAndData(fileName, data).name().toLocal8Bit(); |
| 191 | return QHttpServerResponse(mimeType, data); |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | Returns the response body. |
| 196 | */ |
| 197 | QByteArray QHttpServerResponse::data() const |
| 198 | { |
| 199 | Q_D(const QHttpServerResponse); |
| 200 | return d->data; |
| 201 | } |
| 202 | |
| 203 | /*! |
| 204 | Returns the status code. |
| 205 | */ |
| 206 | QHttpServerResponse::StatusCode QHttpServerResponse::statusCode() const |
| 207 | { |
| 208 | Q_D(const QHttpServerResponse); |
| 209 | return d->statusCode; |
| 210 | } |
| 211 | |
| 212 | /*! |
| 213 | Returns the value of the HTTP \c "Content-Type" header. |
| 214 | |
| 215 | \note Default value is \c{"text/html"}. |
| 216 | */ |
| 217 | QByteArray QHttpServerResponse::mimeType() const |
| 218 | { |
| 219 | Q_D(const QHttpServerResponse); |
| 220 | return d->headers.value(name: QHttpHeaders::WellKnownHeader::ContentType, |
| 221 | defaultValue: QHttpServerLiterals::contentTypeTextHtml()).toByteArray(); |
| 222 | } |
| 223 | |
| 224 | /*! |
| 225 | Returns the currently set HTTP headers. |
| 226 | |
| 227 | \since 6.8 |
| 228 | */ |
| 229 | QHttpHeaders QHttpServerResponse::headers() const |
| 230 | { |
| 231 | Q_D(const QHttpServerResponse); |
| 232 | return d->headers; |
| 233 | } |
| 234 | |
| 235 | /*! |
| 236 | Sets \a newHeaders as the HTTP headers, overriding any previously set headers. |
| 237 | |
| 238 | \since 6.8 |
| 239 | */ |
| 240 | void QHttpServerResponse::setHeaders(QHttpHeaders &&newHeaders) |
| 241 | { |
| 242 | Q_D(QHttpServerResponse); |
| 243 | d->headers = std::move(newHeaders); |
| 244 | } |
| 245 | |
| 246 | /*! |
| 247 | \overload |
| 248 | \since 6.8 |
| 249 | */ |
| 250 | void QHttpServerResponse::setHeaders(const QHttpHeaders &newHeaders) |
| 251 | { |
| 252 | Q_D(QHttpServerResponse); |
| 253 | d->headers = newHeaders; |
| 254 | } |
| 255 | |
| 256 | QT_END_NAMESPACE |
| 257 |
Definitions
Learn Advanced QML with KDAB
Find out more
