summaryrefslogtreecommitdiffstats
path: root/src/httpserver/qhttpserverrequest.cpp
blob: 7313ac8e0410f9413e9e36dbc3956a3c4132d541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qhttpserverrequest_p.h"

#include <QtHttpServer/private/qhttpserverparser_p.h>
#include <QtHttpServer/qhttpserverrequest.h>
#include <QtNetwork/qhttpheaders.h>

#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>
#include <QtNetwork/qtcpsocket.h>
#if QT_CONFIG(ssl)
#include <QtNetwork/qsslsocket.h>
#endif
#if QT_CONFIG(http)
#include <QtNetwork/private/qhttp2connection_p.h>
#endif

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QHttpServerRequestPrivate)

#if !defined(QT_NO_DEBUG_STREAM)

/*!
    \fn QDebug QHttpServerRequest::operator<<(QDebug debug, const QHttpServerRequest &request)

    Writes information about \a request to the \a debug stream.

    \sa QDebug
 */
Q_HTTPSERVER_EXPORT QDebug operator<<(QDebug debug, const QHttpServerRequest &request)
{
    QDebugStateSaver saver(debug);
    debug.nospace() << "QHttpServerRequest(";
    debug << "(Url: " << request.url() << ")";
    debug << "(Headers: " << request.headers() << ")";
    debug << "(RemoteHost: " << request.remoteAddress() << ")";
    debug << "(BodySize: " << request.body().size() << ")";
    debug << ')';
    return debug;
}

#endif

/*!
    \internal
*/
QHttpServerRequestPrivate::QHttpServerRequestPrivate(const QHostAddress &remoteAddress,
                                                     quint16 remotePort,
                                                     const QHostAddress &localAddress,
                                                     quint16 localPort)
    : remoteAddress(remoteAddress),
      remotePort(remotePort),
      localAddress(localAddress),
      localPort(localPort)
{
}

#if QT_CONFIG(ssl)
/*!
    \internal
*/
QHttpServerRequestPrivate::QHttpServerRequestPrivate(const QHostAddress &remoteAddress,
                                                     quint16 remotePort,
                                                     const QHostAddress &localAddress,
                                                     quint16 localPort,
                                                     const QSslConfiguration &sslConfiguration)
    : remoteAddress(remoteAddress),
      remotePort(remotePort),
      localAddress(localAddress),
      localPort(localPort),
      sslConfiguration(sslConfiguration)
{
}
#endif

/*!
    \class QHttpServerRequest
    \since 6.4
    \inmodule QtHttpServer
    \brief Encapsulates an HTTP request.

    API for accessing the different parameters of an incoming request.
*/

/*!
    \enum QHttpServerRequest::Method

    This enum type specifies an HTTP request method:

    \value Unknown
        An unknown method.
    \value Get
        HTTP GET method.
    \value Put
        HTTP PUT method.
    \value Delete
        HTTP DELETE method.
    \value Post
        HTTP POST method.
    \value Head
        HTTP HEAD method.
    \value Options
        HTTP OPTIONS method.
    \value Patch
        HTTP PATCH method (\l {https://www.rfc-editor.org/rfc/rfc5789}{RFC 5789}).
    \value Connect
        HTTP CONNECT method.
    \value Trace
        HTTP TRACE method.
    \value AnyKnown
        Combination of all known methods.
*/

/*!
    \internal
*/
QHttpServerRequest::QHttpServerRequest(const QHostAddress &remoteAddress, quint16 remotePort,
                                       const QHostAddress &localAddress, quint16 localPort)
    : d(new QHttpServerRequestPrivate(remoteAddress, remotePort, localAddress, localPort))
{}

#if QT_CONFIG(ssl)
/*!
    \internal
*/
QHttpServerRequest::QHttpServerRequest(const QHostAddress &remoteAddress, quint16 remotePort,
                                       const QHostAddress &localAddress, quint16 localPort,
                                       const QSslConfiguration &sslConfiguration)
    : d(new QHttpServerRequestPrivate(remoteAddress, remotePort, localAddress, localPort,
                                      sslConfiguration))
{}
#endif

/*!
    Constructs a QHttpServerRequest.

    \since 6.10
*/
QHttpServerRequest::QHttpServerRequest() = default;

/*!
    Copy constructs a QHttpServerRequest using \a other.

    \since 6.10
*/
QHttpServerRequest::QHttpServerRequest(const QHttpServerRequest &other) = default;

/*!
    Assigns a QHttpServerRequest using \a other.

    \since 6.10
*/
QHttpServerRequest &QHttpServerRequest::operator=(const QHttpServerRequest &other) = default;

/*!
    \fn QHttpServerRequest::QHttpServerRequest(QHttpServerRequest &&other) noexcept

    Move constructs a QHttpServerRequest using \a other.

    \since 6.10
*/

/*!
    \fn QHttpServerRequest &QHttpServerRequest::operator=(QHttpServerRequest &&other) noexcept

    Move assigns a QHttpServerRequest using \a other.

    \since 6.10
*/

/*!
    \fn void QHttpServerRequest::swap(QHttpServerRequest &other) noexcept

    Swaps values between this and \a other.

    \since 6.10
*/

/*!
    Destroys a QHttpServerRequest
*/
QHttpServerRequest::~QHttpServerRequest() { }

/*!
    Returns the combined value of all headers with the named \a key.
*/
QByteArray QHttpServerRequest::value(const QByteArray &key) const
{
    return d->headers.combinedValue(key);
}

/*!
    Returns the URL the request asked for.
*/
QUrl QHttpServerRequest::url() const
{
    return d->url;
}

/*!
    Returns the query in the request.
*/
QUrlQuery QHttpServerRequest::query() const
{
    return QUrlQuery(d->url.query());
}

/*!
    Returns the method of the request.
*/
QHttpServerRequest::Method QHttpServerRequest::method() const
{
    return d->method;
}

/*!
    \fn const QHttpHeaders &QHttpServerRequest::headers() const &
    \fn QHttpHeaders QHttpServerRequest::headers() &&

    Returns all the request headers.
*/
const QHttpHeaders &QHttpServerRequest::headers() const &
{
    return d->headers;
}

QHttpHeaders QHttpServerRequest::headers() &&
{
    return std::move(d->headers);
}

/*!
    Returns the body of the request.
*/
QByteArray QHttpServerRequest::body() const
{
    return d->body;
}

/*!
    Returns the address of the origin host of the request.
*/
QHostAddress QHttpServerRequest::remoteAddress() const
{
    return d->remoteAddress;
}

/*!
    Returns the port of the origin host of the request.

    \since 6.5
*/
quint16 QHttpServerRequest::remotePort() const
{
    return d->remotePort;
}

/*!
    Returns the host address of the local socket which received the request.

    \since 6.5
*/
QHostAddress QHttpServerRequest::localAddress() const
{
    return d->localAddress;
}

/*!
    Returns the port of the local socket which received the request.

    \since 6.5
*/
quint16 QHttpServerRequest::localPort() const
{
    return d->localPort;
}

#if QT_CONFIG(ssl)
/*!
    Returns the configuration of the established TLS connection.
    The configurations will return true for isNull() if the connection
    is not using TLS.

    \since 6.7
*/
QSslConfiguration QHttpServerRequest::sslConfiguration() const
{
    return d->sslConfiguration;
}
#endif

/*!
    \internal
*/
QHttpServerRequest QHttpServerRequest::create(const QHttpServerParser &parser)
{
    QHttpServerRequest request(parser.remoteAddress, parser.remotePort, parser.localAddress,
                               parser.localPort);
    request.d->url = parser.url;
    request.d->method = parser.method;
    request.d->headers = parser.headers;
    request.d->body = parser.body;
    return request;
}

#if QT_CONFIG(ssl)
/*!
    \internal
*/
QHttpServerRequest QHttpServerRequest::create(const QHttpServerParser &parser,
                                              const QSslConfiguration &configuration)
{
    QHttpServerRequest request = create(parser);
    request.d->sslConfiguration = configuration;
    return request;
}
#endif

QT_END_NAMESPACE

#include "moc_qhttpserverrequest.cpp"