Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qhttpnetworkrequest.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include "private/qnoncontiguousbytedevice_p.h"
7
8#include <QtCore/private/qtools_p.h>
9
11
13
14QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op,
15 QHttpNetworkRequest::Priority pri, const QUrl &newUrl)
16 : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(nullptr),
17 autoDecompress(false), pipeliningAllowed(false), http2Allowed(true),
18 http2Direct(false), withCredentials(true), preConnect(false), redirectCount(0),
19 redirectPolicy(QNetworkRequest::ManualRedirectPolicy)
20{
21}
22
44
48
50{
51 return QHttpNetworkHeaderPrivate::operator==(other)
52 && (operation == other.operation)
53 && (fullLocalServerName == other.fullLocalServerName)
54 && (priority == other.priority)
55 && (uploadByteDevice == other.uploadByteDevice)
56 && (autoDecompress == other.autoDecompress)
57 && (pipeliningAllowed == other.pipeliningAllowed)
58 && (http2Allowed == other.http2Allowed)
59 && (http2Direct == other.http2Direct)
60 && (h2cAllowed == other.h2cAllowed)
61 // we do not clear the customVerb in setOperation
62 && (operation != QHttpNetworkRequest::Custom || (customVerb == other.customVerb))
63 && (withCredentials == other.withCredentials)
64 && (ssl == other.ssl)
65 && (preConnect == other.preConnect)
66 && (redirectPolicy == other.redirectPolicy)
67 && (peerVerifyName == other.peerVerifyName)
68 && (needResendWithCredentials == other.needResendWithCredentials)
69 ;
70}
71
72QByteArray QHttpNetworkRequest::methodName() const
73{
74 switch (d->operation) {
75 case QHttpNetworkRequest::Get:
76 return "GET";
77 case QHttpNetworkRequest::Head:
78 return "HEAD";
79 case QHttpNetworkRequest::Post:
80 return "POST";
81 case QHttpNetworkRequest::Options:
82 return "OPTIONS";
83 case QHttpNetworkRequest::Put:
84 return "PUT";
85 case QHttpNetworkRequest::Delete:
86 return "DELETE";
87 case QHttpNetworkRequest::Trace:
88 return "TRACE";
89 case QHttpNetworkRequest::Connect:
90 return "CONNECT";
91 case QHttpNetworkRequest::Custom:
92 return d->customVerb;
93 default:
94 break;
95 }
96 return QByteArray();
97}
98
99QByteArray QHttpNetworkRequest::uri(bool throughProxy) const
100{
101 QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded);
102
103 // for POST, query data is sent as content
104 if (d->operation == QHttpNetworkRequest::Post && !d->uploadByteDevice)
105 format |= QUrl::RemoveQuery;
106 // for requests through proxy, the Request-URI contains full url
107 if (!throughProxy)
108 format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;
109 QUrl copy = d->url;
110 if (copy.path().isEmpty())
111 copy.setPath(QStringLiteral("/"));
112 else
113 format |= QUrl::NormalizePathSegments;
114 QByteArray uri = copy.toEncoded(format);
115 return uri;
116}
117
118QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)
119{
120 const QHttpHeaders headers = request.header();
121 QByteArray ba;
122 ba.reserve(40 + headers.size() * 25); // very rough lower bound estimation
123
124 ba += request.methodName();
125 ba += ' ';
126 ba += request.uri(throughProxy);
127
128 ba += " HTTP/";
129 ba += QByteArray::number(request.majorVersion());
130 ba += '.';
131 ba += QByteArray::number(request.minorVersion());
132 ba += "\r\n";
133
134 for (qsizetype i = 0; i < headers.size(); ++i) {
135 ba += headers.nameAt(i);
136 ba += ": ";
137 ba += headers.valueAt(i);
138 ba += "\r\n";
139 }
140 if (request.d->operation == QHttpNetworkRequest::Post) {
141 // add content type, if not set in the request
142 if (request.headerField("content-type").isEmpty() && ((request.d->uploadByteDevice && request.d->uploadByteDevice->size() > 0) || request.d->url.hasQuery())) {
143 //Content-Type is mandatory. We can't say anything about the encoding, but x-www-form-urlencoded is the most likely to work.
144 //This warning indicates a bug in application code not setting a required header.
145 //Note that if using QHttpMultipart, the content-type is set in QNetworkAccessManagerPrivate::prepareMultipart already
146 qWarning("content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.");
147 ba += "Content-Type: application/x-www-form-urlencoded\r\n";
148 }
149 if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
150 QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1();
151 ba += "Content-Length: ";
152 ba += QByteArray::number(query.size());
153 ba += "\r\n\r\n";
154 ba += query;
155 } else {
156 ba += "\r\n";
157 }
158 } else {
159 ba += "\r\n";
160 }
161 return ba;
162}
163
164
165// QHttpNetworkRequest
166
167QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)
168 : d(new QHttpNetworkRequestPrivate(operation, priority, url))
169{
170}
171
172QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)
173 : QHttpNetworkHeader(other), d(other.d)
174{
175}
176
177QHttpNetworkRequest::~QHttpNetworkRequest()
178{
179}
180
181QUrl QHttpNetworkRequest::url() const
182{
183 return d->url;
184}
185void QHttpNetworkRequest::setUrl(const QUrl &url)
186{
187 d->url = url;
188}
189
190bool QHttpNetworkRequest::isSsl() const
191{
192 return d->ssl;
193}
194void QHttpNetworkRequest::setSsl(bool s)
195{
196 d->ssl = s;
197}
198
199bool QHttpNetworkRequest::isPreConnect() const
200{
201 return d->preConnect;
202}
203void QHttpNetworkRequest::setPreConnect(bool preConnect)
204{
205 d->preConnect = preConnect;
206}
207
208bool QHttpNetworkRequest::isFollowRedirects() const
209{
210 return d->redirectPolicy != QNetworkRequest::ManualRedirectPolicy;
211}
212
213void QHttpNetworkRequest::setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
214{
215 d->redirectPolicy = policy;
216}
217
218QNetworkRequest::RedirectPolicy QHttpNetworkRequest::redirectPolicy() const
219{
220 return d->redirectPolicy;
221}
222
223int QHttpNetworkRequest::redirectCount() const
224{
225 return d->redirectCount;
226}
227
228void QHttpNetworkRequest::setRedirectCount(int count)
229{
230 d->redirectCount = count;
231}
232
233qint64 QHttpNetworkRequest::contentLength() const
234{
235 return d->contentLength();
236}
237
238void QHttpNetworkRequest::setContentLength(qint64 length)
239{
240 d->setContentLength(length);
241}
242
243QHttpHeaders QHttpNetworkRequest::header() const
244{
245 return d->parser.headers();
246}
247
248QByteArray QHttpNetworkRequest::headerField(QByteArrayView name, const QByteArray &defaultValue) const
249{
250 return d->headerField(name, defaultValue);
251}
252
253void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)
254{
255 d->setHeaderField(name, data);
256}
257
258void QHttpNetworkRequest::prependHeaderField(const QByteArray &name, const QByteArray &data)
259{
260 d->prependHeaderField(name, data);
261}
262
263void QHttpNetworkRequest::clearHeaders()
264{
265 d->clearHeaders();
266}
267
268QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)
269{
270 QHttpNetworkHeader::operator=(other);
271 d = other.d;
272 return *this;
273}
274
275bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const
276{
277 return d->operator==(*other.d);
278}
279
280QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const
281{
282 return d->operation;
283}
284
285void QHttpNetworkRequest::setOperation(Operation operation)
286{
287 d->operation = operation;
288}
289
290QByteArray QHttpNetworkRequest::customVerb() const
291{
292 return d->customVerb;
293}
294
295void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)
296{
297 d->customVerb = customVerb;
298}
299
300QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const
301{
302 return d->priority;
303}
304
305void QHttpNetworkRequest::setPriority(Priority priority)
306{
307 d->priority = priority;
308}
309
310bool QHttpNetworkRequest::isPipeliningAllowed() const
311{
312 return d->pipeliningAllowed;
313}
314
315void QHttpNetworkRequest::setPipeliningAllowed(bool b)
316{
317 d->pipeliningAllowed = b;
318}
319
320bool QHttpNetworkRequest::isHTTP2Allowed() const
321{
322 return d->http2Allowed;
323}
324
325void QHttpNetworkRequest::setHTTP2Allowed(bool b)
326{
327 d->http2Allowed = b;
328}
329
330bool QHttpNetworkRequest::isHTTP2Direct() const
331{
332 return d->http2Direct;
333}
334
335void QHttpNetworkRequest::setHTTP2Direct(bool b)
336{
337 d->http2Direct = b;
338}
339
340bool QHttpNetworkRequest::isH2cAllowed() const
341{
342 return d->h2cAllowed;
343}
344
345void QHttpNetworkRequest::setH2cAllowed(bool b)
346{
347 d->h2cAllowed = b;
348}
349
350bool QHttpNetworkRequest::withCredentials() const
351{
352 return d->withCredentials;
353}
354
355void QHttpNetworkRequest::setWithCredentials(bool b)
356{
357 d->withCredentials = b;
358}
359
360void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)
361{
362 d->uploadByteDevice = bd;
363}
364
365QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const
366{
367 return d->uploadByteDevice;
368}
369
370int QHttpNetworkRequest::majorVersion() const
371{
372 return 1;
373}
374
375int QHttpNetworkRequest::minorVersion() const
376{
377 return 1;
378}
379
380QString QHttpNetworkRequest::peerVerifyName() const
381{
382 return d->peerVerifyName;
383}
384
385void QHttpNetworkRequest::setPeerVerifyName(const QString &peerName)
386{
387 d->peerVerifyName = peerName;
388}
389
390QString QHttpNetworkRequest::fullLocalServerName() const
391{
392 return d->fullLocalServerName;
393}
394
395void QHttpNetworkRequest::setFullLocalServerName(const QString &fullServerName)
396{
397 d->fullLocalServerName = fullServerName;
398}
399
400bool QHttpNetworkRequest::methodIsIdempotent() const
401{
402 using Op = Operation;
403 constexpr auto knownSafe = std::array{ Op::Get, Op::Head, Op::Put, Op::Trace, Op::Options };
404 return std::any_of(knownSafe.begin(), knownSafe.end(),
405 [currentOp = d->operation](auto op) { return op == currentOp; });
406}
407
408QT_END_NAMESPACE
bool operator==(const QHttpNetworkRequestPrivate &other) const
QNonContiguousByteDevice * uploadByteDevice
QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other)
Combined button and popup list for selecting options.
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1398