| 1 | // Copyright (C) 2022 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 "qhttpserverrequest_p.h" |
| 6 | |
| 7 | #include <QtHttpServer/private/qhttpserverparser_p.h> |
| 8 | #include <QtHttpServer/qhttpserverrequest.h> |
| 9 | #include <QtNetwork/qhttpheaders.h> |
| 10 | |
| 11 | #include <QtCore/qdebug.h> |
| 12 | #include <QtCore/qloggingcategory.h> |
| 13 | #include <QtNetwork/qtcpsocket.h> |
| 14 | #if QT_CONFIG(ssl) |
| 15 | #include <QtNetwork/qsslsocket.h> |
| 16 | #endif |
| 17 | #if QT_CONFIG(http) |
| 18 | #include <QtNetwork/private/qhttp2connection_p.h> |
| 19 | #endif |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | using namespace Qt::StringLiterals; |
| 24 | |
| 25 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QHttpServerRequestPrivate) |
| 26 | |
| 27 | #if !defined(QT_NO_DEBUG_STREAM) |
| 28 | |
| 29 | /*! |
| 30 | \fn QDebug QHttpServerRequest::operator<<(QDebug debug, const QHttpServerRequest &request) |
| 31 | |
| 32 | Writes information about \a request to the \a debug stream. |
| 33 | |
| 34 | \sa QDebug |
| 35 | */ |
| 36 | Q_HTTPSERVER_EXPORT QDebug operator<<(QDebug debug, const QHttpServerRequest &request) |
| 37 | { |
| 38 | QDebugStateSaver saver(debug); |
| 39 | debug.nospace() << "QHttpServerRequest(" ; |
| 40 | debug << "(Url: " << request.url() << ")" ; |
| 41 | debug << "(Headers: " << request.headers() << ")" ; |
| 42 | debug << "(RemoteHost: " << request.remoteAddress() << ")" ; |
| 43 | debug << "(BodySize: " << request.body().size() << ")" ; |
| 44 | debug << ')'; |
| 45 | return debug; |
| 46 | } |
| 47 | |
| 48 | #endif |
| 49 | |
| 50 | /*! |
| 51 | \internal |
| 52 | */ |
| 53 | QHttpServerRequestPrivate::QHttpServerRequestPrivate(const QHostAddress &remoteAddress, |
| 54 | quint16 remotePort, |
| 55 | const QHostAddress &localAddress, |
| 56 | quint16 localPort) |
| 57 | : remoteAddress(remoteAddress), |
| 58 | remotePort(remotePort), |
| 59 | localAddress(localAddress), |
| 60 | localPort(localPort) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | #if QT_CONFIG(ssl) |
| 65 | /*! |
| 66 | \internal |
| 67 | */ |
| 68 | QHttpServerRequestPrivate::QHttpServerRequestPrivate(const QHostAddress &remoteAddress, |
| 69 | quint16 remotePort, |
| 70 | const QHostAddress &localAddress, |
| 71 | quint16 localPort, |
| 72 | const QSslConfiguration &sslConfiguration) |
| 73 | : remoteAddress(remoteAddress), |
| 74 | remotePort(remotePort), |
| 75 | localAddress(localAddress), |
| 76 | localPort(localPort), |
| 77 | sslConfiguration(sslConfiguration) |
| 78 | { |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | /*! |
| 83 | \class QHttpServerRequest |
| 84 | \since 6.4 |
| 85 | \inmodule QtHttpServer |
| 86 | \brief Encapsulates an HTTP request. |
| 87 | |
| 88 | API for accessing the different parameters of an incoming request. |
| 89 | */ |
| 90 | |
| 91 | /*! |
| 92 | \enum QHttpServerRequest::Method |
| 93 | |
| 94 | This enum type specifies an HTTP request method: |
| 95 | |
| 96 | \value Unknown |
| 97 | An unknown method. |
| 98 | \value Get |
| 99 | HTTP GET method. |
| 100 | \value Put |
| 101 | HTTP PUT method. |
| 102 | \value Delete |
| 103 | HTTP DELETE method. |
| 104 | \value Post |
| 105 | HTTP POST method. |
| 106 | \value Head |
| 107 | HTTP HEAD method. |
| 108 | \value Options |
| 109 | HTTP OPTIONS method. |
| 110 | \value Patch |
| 111 | HTTP PATCH method (\l {https://www.rfc-editor.org/rfc/rfc5789}{RFC 5789}). |
| 112 | \value Connect |
| 113 | HTTP CONNECT method. |
| 114 | \value Trace |
| 115 | HTTP TRACE method. |
| 116 | \value AnyKnown |
| 117 | Combination of all known methods. |
| 118 | */ |
| 119 | |
| 120 | /*! |
| 121 | \internal |
| 122 | */ |
| 123 | QHttpServerRequest::QHttpServerRequest(const QHostAddress &remoteAddress, quint16 remotePort, |
| 124 | const QHostAddress &localAddress, quint16 localPort) |
| 125 | : d(new QHttpServerRequestPrivate(remoteAddress, remotePort, localAddress, localPort)) |
| 126 | {} |
| 127 | |
| 128 | #if QT_CONFIG(ssl) |
| 129 | /*! |
| 130 | \internal |
| 131 | */ |
| 132 | QHttpServerRequest::QHttpServerRequest(const QHostAddress &remoteAddress, quint16 remotePort, |
| 133 | const QHostAddress &localAddress, quint16 localPort, |
| 134 | const QSslConfiguration &sslConfiguration) |
| 135 | : d(new QHttpServerRequestPrivate(remoteAddress, remotePort, localAddress, localPort, |
| 136 | sslConfiguration)) |
| 137 | {} |
| 138 | #endif |
| 139 | |
| 140 | /*! |
| 141 | Constructs a QHttpServerRequest. |
| 142 | |
| 143 | \since 6.10 |
| 144 | */ |
| 145 | QHttpServerRequest::QHttpServerRequest() = default; |
| 146 | |
| 147 | /*! |
| 148 | Copy constructs a QHttpServerRequest using \a other. |
| 149 | |
| 150 | \since 6.10 |
| 151 | */ |
| 152 | QHttpServerRequest::QHttpServerRequest(const QHttpServerRequest &other) = default; |
| 153 | |
| 154 | /*! |
| 155 | Assigns a QHttpServerRequest using \a other. |
| 156 | |
| 157 | \since 6.10 |
| 158 | */ |
| 159 | QHttpServerRequest &QHttpServerRequest::operator=(const QHttpServerRequest &other) = default; |
| 160 | |
| 161 | /*! |
| 162 | \fn QHttpServerRequest::QHttpServerRequest(QHttpServerRequest &&other) noexcept |
| 163 | |
| 164 | Move constructs a QHttpServerRequest using \a other. |
| 165 | |
| 166 | \since 6.10 |
| 167 | */ |
| 168 | |
| 169 | /*! |
| 170 | \fn QHttpServerRequest &QHttpServerRequest::operator=(QHttpServerRequest &&other) noexcept |
| 171 | |
| 172 | Move assigns a QHttpServerRequest using \a other. |
| 173 | |
| 174 | \since 6.10 |
| 175 | */ |
| 176 | |
| 177 | /*! |
| 178 | \fn void QHttpServerRequest::swap(QHttpServerRequest &other) noexcept |
| 179 | |
| 180 | Swaps values between this and \a other. |
| 181 | |
| 182 | \since 6.10 |
| 183 | */ |
| 184 | |
| 185 | /*! |
| 186 | Destroys a QHttpServerRequest |
| 187 | */ |
| 188 | QHttpServerRequest::~QHttpServerRequest() { } |
| 189 | |
| 190 | /*! |
| 191 | Returns the combined value of all headers with the named \a key. |
| 192 | */ |
| 193 | QByteArray QHttpServerRequest::value(const QByteArray &key) const |
| 194 | { |
| 195 | return d->headers.combinedValue(name: key); |
| 196 | } |
| 197 | |
| 198 | /*! |
| 199 | Returns the URL the request asked for. |
| 200 | */ |
| 201 | QUrl QHttpServerRequest::url() const |
| 202 | { |
| 203 | return d->url; |
| 204 | } |
| 205 | |
| 206 | /*! |
| 207 | Returns the query in the request. |
| 208 | */ |
| 209 | QUrlQuery QHttpServerRequest::query() const |
| 210 | { |
| 211 | return QUrlQuery(d->url.query()); |
| 212 | } |
| 213 | |
| 214 | /*! |
| 215 | Returns the method of the request. |
| 216 | */ |
| 217 | QHttpServerRequest::Method QHttpServerRequest::method() const |
| 218 | { |
| 219 | return d->method; |
| 220 | } |
| 221 | |
| 222 | /*! |
| 223 | \fn const QHttpHeaders &QHttpServerRequest::headers() const & |
| 224 | \fn QHttpHeaders QHttpServerRequest::headers() && |
| 225 | |
| 226 | Returns all the request headers. |
| 227 | */ |
| 228 | const QHttpHeaders &QHttpServerRequest::() const & |
| 229 | { |
| 230 | return d->headers; |
| 231 | } |
| 232 | |
| 233 | QHttpHeaders QHttpServerRequest::() && |
| 234 | { |
| 235 | return std::move(d->headers); |
| 236 | } |
| 237 | |
| 238 | /*! |
| 239 | Returns the body of the request. |
| 240 | */ |
| 241 | QByteArray QHttpServerRequest::body() const |
| 242 | { |
| 243 | return d->body; |
| 244 | } |
| 245 | |
| 246 | /*! |
| 247 | Returns the address of the origin host of the request. |
| 248 | */ |
| 249 | QHostAddress QHttpServerRequest::remoteAddress() const |
| 250 | { |
| 251 | return d->remoteAddress; |
| 252 | } |
| 253 | |
| 254 | /*! |
| 255 | Returns the port of the origin host of the request. |
| 256 | |
| 257 | \since 6.5 |
| 258 | */ |
| 259 | quint16 QHttpServerRequest::remotePort() const |
| 260 | { |
| 261 | return d->remotePort; |
| 262 | } |
| 263 | |
| 264 | /*! |
| 265 | Returns the host address of the local socket which received the request. |
| 266 | |
| 267 | \since 6.5 |
| 268 | */ |
| 269 | QHostAddress QHttpServerRequest::localAddress() const |
| 270 | { |
| 271 | return d->localAddress; |
| 272 | } |
| 273 | |
| 274 | /*! |
| 275 | Returns the port of the local socket which received the request. |
| 276 | |
| 277 | \since 6.5 |
| 278 | */ |
| 279 | quint16 QHttpServerRequest::localPort() const |
| 280 | { |
| 281 | return d->localPort; |
| 282 | } |
| 283 | |
| 284 | #if QT_CONFIG(ssl) |
| 285 | /*! |
| 286 | Returns the configuration of the established TLS connection. |
| 287 | The configurations will return true for isNull() if the connection |
| 288 | is not using TLS. |
| 289 | |
| 290 | \since 6.7 |
| 291 | */ |
| 292 | QSslConfiguration QHttpServerRequest::sslConfiguration() const |
| 293 | { |
| 294 | return d->sslConfiguration; |
| 295 | } |
| 296 | #endif |
| 297 | |
| 298 | /*! |
| 299 | \internal |
| 300 | */ |
| 301 | QHttpServerRequest QHttpServerRequest::create(const QHttpServerParser &parser) |
| 302 | { |
| 303 | QHttpServerRequest request(parser.remoteAddress, parser.remotePort, parser.localAddress, |
| 304 | parser.localPort); |
| 305 | request.d->url = parser.url; |
| 306 | request.d->method = parser.method; |
| 307 | request.d->headers = parser.headers; |
| 308 | request.d->body = parser.body; |
| 309 | request.d->majorVersion = parser.majorVersion; |
| 310 | request.d->minorVersion = parser.minorVersion; |
| 311 | return request; |
| 312 | } |
| 313 | |
| 314 | #if QT_CONFIG(ssl) |
| 315 | /*! |
| 316 | \internal |
| 317 | */ |
| 318 | QHttpServerRequest QHttpServerRequest::create(const QHttpServerParser &parser, |
| 319 | const QSslConfiguration &configuration) |
| 320 | { |
| 321 | QHttpServerRequest request = create(parser); |
| 322 | request.d->sslConfiguration = configuration; |
| 323 | return request; |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | QT_END_NAMESPACE |
| 328 | |
| 329 | #include "moc_qhttpserverrequest.cpp" |
| 330 | |