Qt 6.x
The Qt SDK
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
5#include "private/qnoncontiguousbytedevice_p.h"
6
8
10
12 QHttpNetworkRequest::Priority pri, const QUrl &newUrl)
13 : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(nullptr),
14 autoDecompress(false), pipeliningAllowed(false), http2Allowed(true),
15 http2Direct(false), withCredentials(true), preConnect(false), redirectCount(0),
16 redirectPolicy(QNetworkRequest::ManualRedirectPolicy)
17{
18}
19
22 operation(other.operation),
23 customVerb(other.customVerb),
24 priority(other.priority),
25 uploadByteDevice(other.uploadByteDevice),
26 autoDecompress(other.autoDecompress),
27 pipeliningAllowed(other.pipeliningAllowed),
28 http2Allowed(other.http2Allowed),
29 http2Direct(other.http2Direct),
30 h2cAllowed(other.h2cAllowed),
31 withCredentials(other.withCredentials),
32 ssl(other.ssl),
33 preConnect(other.preConnect),
34 needResendWithCredentials(other.needResendWithCredentials),
35 redirectCount(other.redirectCount),
36 redirectPolicy(other.redirectPolicy),
37 peerVerifyName(other.peerVerifyName)
38{
39}
40
42{
43}
44
46{
48 && (operation == other.operation)
49 && (priority == other.priority)
50 && (uploadByteDevice == other.uploadByteDevice)
51 && (autoDecompress == other.autoDecompress)
52 && (pipeliningAllowed == other.pipeliningAllowed)
53 && (http2Allowed == other.http2Allowed)
54 && (http2Direct == other.http2Direct)
55 && (h2cAllowed == other.h2cAllowed)
56 // we do not clear the customVerb in setOperation
57 && (operation != QHttpNetworkRequest::Custom || (customVerb == other.customVerb))
58 && (withCredentials == other.withCredentials)
59 && (ssl == other.ssl)
60 && (preConnect == other.preConnect)
61 && (redirectPolicy == other.redirectPolicy)
62 && (peerVerifyName == other.peerVerifyName)
63 && (needResendWithCredentials == other.needResendWithCredentials)
64 ;
65}
66
68{
69 switch (d->operation) {
71 return "GET";
73 return "HEAD";
75 return "POST";
77 return "OPTIONS";
79 return "PUT";
81 return "DELETE";
83 return "TRACE";
85 return "CONNECT";
87 return d->customVerb;
88 default:
89 break;
90 }
91 return QByteArray();
92}
93
94QByteArray QHttpNetworkRequest::uri(bool throughProxy) const
95{
97
98 // for POST, query data is sent as content
101 // for requests through proxy, the Request-URI contains full url
102 if (!throughProxy)
104 QUrl copy = d->url;
105 if (copy.path().isEmpty())
106 copy.setPath(QStringLiteral("/"));
107 else
109 QByteArray uri = copy.toEncoded(format);
110 return uri;
111}
112
114{
117 ba.reserve(40 + fields.size()*25); // very rough lower bound estimation
118
119 ba += request.methodName();
120 ba += ' ';
121 ba += request.uri(throughProxy);
122
123 ba += " HTTP/";
124 ba += QByteArray::number(request.majorVersion());
125 ba += '.';
126 ba += QByteArray::number(request.minorVersion());
127 ba += "\r\n";
128
129 QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();
130 QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();
131 for (; it != endIt; ++it) {
132 ba += it->first;
133 ba += ": ";
134 ba += it->second;
135 ba += "\r\n";
136 }
137 if (request.d->operation == QHttpNetworkRequest::Post) {
138 // add content type, if not set in the request
139 if (request.headerField("content-type").isEmpty() && ((request.d->uploadByteDevice && request.d->uploadByteDevice->size() > 0) || request.d->url.hasQuery())) {
140 //Content-Type is mandatory. We can't say anything about the encoding, but x-www-form-urlencoded is the most likely to work.
141 //This warning indicates a bug in application code not setting a required header.
142 //Note that if using QHttpMultipart, the content-type is set in QNetworkAccessManagerPrivate::prepareMultipart already
143 qWarning("content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.");
144 ba += "Content-Type: application/x-www-form-urlencoded\r\n";
145 }
146 if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
148 ba += "Content-Length: ";
149 ba += QByteArray::number(query.size());
150 ba += "\r\n\r\n";
151 ba += query;
152 } else {
153 ba += "\r\n";
154 }
155 } else {
156 ba += "\r\n";
157 }
158 return ba;
159}
160
161
162// QHttpNetworkRequest
163
165 : d(new QHttpNetworkRequestPrivate(operation, priority, url))
166{
167}
168
171{
172}
173
175{
176}
177
179{
180 return d->url;
181}
183{
184 d->url = url;
185}
186
188{
189 return d->ssl;
190}
192{
193 d->ssl = s;
194}
195
197{
198 return d->preConnect;
199}
201{
202 d->preConnect = preConnect;
203}
204
206{
208}
209
211{
213}
214
216{
217 return d->redirectPolicy;
218}
219
221{
222 return d->redirectCount;
223}
224
226{
227 d->redirectCount = count;
228}
229
231{
232 return d->contentLength();
233}
234
236{
238}
239
241{
242 return d->parser.headers();
243}
244
246{
247 return d->headerField(name, defaultValue);
248}
249
251{
253}
254
256{
258}
259
261{
262 d->clearHeaders();
263}
264
266{
267 d = other.d;
268 return *this;
269}
270
272{
273 return d->operator==(*other.d);
274}
275
277{
278 return d->operation;
279}
280
282{
283 d->operation = operation;
284}
285
287{
288 return d->customVerb;
289}
290
292{
294}
295
297{
298 return d->priority;
299}
300
302{
303 d->priority = priority;
304}
305
307{
308 return d->pipeliningAllowed;
309}
310
312{
313 d->pipeliningAllowed = b;
314}
315
317{
318 return d->http2Allowed;
319}
320
322{
323 d->http2Allowed = b;
324}
325
327{
328 return d->http2Direct;
329}
330
332{
333 d->http2Direct = b;
334}
335
337{
338 return d->h2cAllowed;
339}
340
342{
343 d->h2cAllowed = b;
344}
345
347{
348 return d->withCredentials;
349}
350
352{
353 d->withCredentials = b;
354}
355
357{
358 d->uploadByteDevice = bd;
359}
360
362{
363 return d->uploadByteDevice;
364}
365
367{
368 return 1;
369}
370
372{
373 return 1;
374}
375
377{
378 return d->peerVerifyName;
379}
380
382{
383 d->peerVerifyName = peerName;
384}
385
387
\inmodule QtCore
Definition qbytearray.h:57
void reserve(qsizetype size)
Attempts to allocate memory for at least size bytes.
Definition qbytearray.h:557
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
const QList< QPair< QByteArray, QByteArray > > & headers() const
bool operator==(const QHttpNetworkHeaderPrivate &other) const
void setHeaderField(const QByteArray &name, const QByteArray &data)
void setContentLength(qint64 length)
void prependHeaderField(const QByteArray &name, const QByteArray &data)
QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue=QByteArray()) const
bool operator==(const QHttpNetworkRequestPrivate &other) const
QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op, QHttpNetworkRequest::Priority pri, const QUrl &newUrl=QUrl())
QHttpNetworkRequest::Priority priority
QNonContiguousByteDevice * uploadByteDevice
QNetworkRequest::RedirectPolicy redirectPolicy
QHttpNetworkRequest::Operation operation
static QByteArray header(const QHttpNetworkRequest &request, bool throughProxy)
QList< QPair< QByteArray, QByteArray > > header() const override
void setCustomVerb(const QByteArray &customOperation)
QHttpNetworkRequest(const QUrl &url=QUrl(), Operation operation=Get, Priority priority=NormalPriority)
int majorVersion() const override
bool operator==(const QHttpNetworkRequest &other) const
void setPriority(Priority priority)
QByteArray methodName() const
int minorVersion() const override
void setPreConnect(bool preConnect)
void setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
void setPeerVerifyName(const QString &peerName)
void setHeaderField(const QByteArray &name, const QByteArray &data) override
QHttpNetworkRequest & operator=(const QHttpNetworkRequest &other)
void setUrl(const QUrl &url) override
QByteArray customVerb() const
QUrl url() const override
void setContentLength(qint64 length) override
QByteArray uri(bool throughProxy) const
QNonContiguousByteDevice * uploadByteDevice() const
void setOperation(Operation operation)
Operation operation() const
QByteArray headerField(const QByteArray &name, const QByteArray &defaultValue=QByteArray()) const override
void prependHeaderField(const QByteArray &name, const QByteArray &data)
qint64 contentLength() const override
void setRedirectCount(int count)
QNetworkRequest::RedirectPolicy redirectPolicy() const
void setUploadByteDevice(QNonContiguousByteDevice *bd)
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
const_iterator constBegin() const noexcept
Definition qlist.h:615
const_iterator constEnd() const noexcept
Definition qlist.h:616
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QVariant header(KnownHeaders header) const
Returns the value of the known network header header if it is present in this request.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QByteArray toLatin1() const &
Definition qstring.h:559
\inmodule QtCore
Definition qurl.h:94
bool hasQuery() const
Definition qurl.cpp:2510
QString query(ComponentFormattingOptions=PrettyDecoded) const
Returns the query string of the URL if there's a query string, or an empty result if not.
Definition qurl.cpp:2606
@ RemoveScheme
Definition qurl.h:105
@ RemoveFragment
Definition qurl.h:112
@ RemoveQuery
Definition qurl.h:111
@ NormalizePathSegments
Definition qurl.h:117
@ RemoveUserInfo
Definition qurl.h:107
@ RemoveAuthority
Definition qurl.h:109
@ FullyEncoded
Definition qurl.h:129
QSet< QString >::iterator it
Combined button and popup list for selecting options.
static jboolean copy(JNIEnv *, jobject)
#define qWarning
Definition qlogging.h:162
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1369
GLboolean GLboolean GLboolean b
GLenum GLuint GLenum GLsizei length
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLint GLsizei GLsizei GLenum format
GLenum query
GLdouble s
[6]
Definition qopenglext.h:235
#define QStringLiteral(str)
long long qint64
Definition qtypes.h:55
QByteArray ba
[0]
QUrl url("example.com")
[constructor-url-reference]
QSharedPointer< T > other(t)
[5]
QSizePolicy policy
QNetworkRequest request(url)