Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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 + headers.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 for (qsizetype i = 0; i < headers.size(); ++i) {
130 ba += headers.nameAt(i);
131 ba += ": ";
132 ba += headers.valueAt(i);
133 ba += "\r\n";
134 }
135 if (request.d->operation == QHttpNetworkRequest::Post) {
136 // add content type, if not set in the request
137 if (request.headerField("content-type").isEmpty() && ((request.d->uploadByteDevice && request.d->uploadByteDevice->size() > 0) || request.d->url.hasQuery())) {
138 //Content-Type is mandatory. We can't say anything about the encoding, but x-www-form-urlencoded is the most likely to work.
139 //This warning indicates a bug in application code not setting a required header.
140 //Note that if using QHttpMultipart, the content-type is set in QNetworkAccessManagerPrivate::prepareMultipart already
141 qWarning("content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.");
142 ba += "Content-Type: application/x-www-form-urlencoded\r\n";
143 }
144 if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {
146 ba += "Content-Length: ";
147 ba += QByteArray::number(query.size());
148 ba += "\r\n\r\n";
149 ba += query;
150 } else {
151 ba += "\r\n";
152 }
153 } else {
154 ba += "\r\n";
155 }
156 return ba;
157}
158
159
160// QHttpNetworkRequest
161
163 : d(new QHttpNetworkRequestPrivate(operation, priority, url))
164{
165}
166
171
175
177{
178 return d->url;
179}
181{
182 d->url = url;
183}
184
186{
187 return d->ssl;
188}
190{
191 d->ssl = s;
192}
193
195{
196 return d->preConnect;
197}
199{
200 d->preConnect = preConnect;
201}
202
207
212
217
219{
220 return d->redirectCount;
221}
222
227
229{
230 return d->contentLength();
231}
232
237
239{
240 return d->parser.headers();
241}
242
244{
245 return d->headerField(name, defaultValue);
246}
247
252
257
262
268
270{
271 return d->operator==(*other.d);
272}
273
278
283
288
290{
292}
293
298
303
308
313
315{
316 return d->http2Allowed;
317}
318
323
325{
326 return d->http2Direct;
327}
328
330{
331 d->http2Direct = b;
332}
333
335{
336 return d->h2cAllowed;
337}
338
340{
341 d->h2cAllowed = b;
342}
343
345{
346 return d->withCredentials;
347}
348
353
358
363
365{
366 return 1;
367}
368
370{
371 return 1;
372}
373
378
380{
381 d->peerVerifyName = peerName;
382}
383
385
\inmodule QtCore
Definition qbytearray.h:57
void reserve(qsizetype size)
Attempts to allocate memory for at least size bytes.
Definition qbytearray.h:634
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
const QHttpHeaders & headers() const
Q_NETWORK_EXPORT qsizetype size() const noexcept
Returns the number of header entries.
Q_NETWORK_EXPORT QLatin1StringView nameAt(qsizetype i) const noexcept
Returns the header name at index i.
Q_NETWORK_EXPORT QByteArrayView valueAt(qsizetype i) const noexcept
Returns the header value at index i.
QByteArray headerField(QByteArrayView name, const QByteArray &defaultValue=QByteArray()) 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)
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)
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
QByteArray headerField(QByteArrayView name, const QByteArray &defaultValue=QByteArray()) const override
QUrl url() const override
void setContentLength(qint64 length) override
QByteArray uri(bool throughProxy) const
QNonContiguousByteDevice * uploadByteDevice() const
void setOperation(Operation operation)
Operation operation() const
void prependHeaderField(const QByteArray &name, const QByteArray &data)
qint64 contentLength() const override
void setRedirectCount(int count)
QHttpHeaders header() const override
QNetworkRequest::RedirectPolicy redirectPolicy() const
void setUploadByteDevice(QNonContiguousByteDevice *bd)
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:129
QByteArray toLatin1() const &
Definition qstring.h:630
\inmodule QtCore
Definition qurl.h:94
bool hasQuery() const
Definition qurl.cpp:2513
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:2609
@ 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
Combined button and popup list for selecting options.
static jboolean copy(JNIEnv *, jobject)
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define qWarning
Definition qlogging.h:166
#define QT_IMPL_METATYPE_EXTERN(TYPE)
Definition qmetatype.h:1390
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
GLdouble s
[6]
Definition qopenglext.h:235
GLenum query
#define QStringLiteral(str)
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
QByteArray ba
[0]
QUrl url("example.com")
[constructor-url-reference]
QSharedPointer< T > other(t)
[5]
QSizePolicy policy
QNetworkRequest request(url)