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