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 | |
4 | #include "qhttpnetworkrequest_p.h" |
5 | #include "private/qnoncontiguousbytedevice_p.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | QT_IMPL_METATYPE_EXTERN(QHttpNetworkRequest) |
10 | |
11 | QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op, |
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 | |
20 | QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other) // = default |
21 | : QHttpNetworkHeaderPrivate(other), |
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 | |
41 | QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate() |
42 | { |
43 | } |
44 | |
45 | bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const |
46 | { |
47 | return QHttpNetworkHeaderPrivate::operator==(other) |
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 | |
67 | QByteArray QHttpNetworkRequest::methodName() const |
68 | { |
69 | switch (d->operation) { |
70 | case QHttpNetworkRequest::Get: |
71 | return "GET" ; |
72 | case QHttpNetworkRequest::Head: |
73 | return "HEAD" ; |
74 | case QHttpNetworkRequest::Post: |
75 | return "POST" ; |
76 | case QHttpNetworkRequest::Options: |
77 | return "OPTIONS" ; |
78 | case QHttpNetworkRequest::Put: |
79 | return "PUT" ; |
80 | case QHttpNetworkRequest::Delete: |
81 | return "DELETE" ; |
82 | case QHttpNetworkRequest::Trace: |
83 | return "TRACE" ; |
84 | case QHttpNetworkRequest::Connect: |
85 | return "CONNECT" ; |
86 | case QHttpNetworkRequest::Custom: |
87 | return d->customVerb; |
88 | default: |
89 | break; |
90 | } |
91 | return QByteArray(); |
92 | } |
93 | |
94 | QByteArray QHttpNetworkRequest::uri(bool throughProxy) const |
95 | { |
96 | QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded); |
97 | |
98 | // for POST, query data is sent as content |
99 | if (d->operation == QHttpNetworkRequest::Post && !d->uploadByteDevice) |
100 | format |= QUrl::RemoveQuery; |
101 | // for requests through proxy, the Request-URI contains full url |
102 | if (!throughProxy) |
103 | format |= QUrl::RemoveScheme | QUrl::RemoveAuthority; |
104 | QUrl copy = d->url; |
105 | if (copy.path().isEmpty()) |
106 | copy.setPath(QStringLiteral("/" )); |
107 | else |
108 | format |= QUrl::NormalizePathSegments; |
109 | QByteArray uri = copy.toEncoded(options: format); |
110 | return uri; |
111 | } |
112 | |
113 | QByteArray QHttpNetworkRequestPrivate::(const QHttpNetworkRequest &request, bool throughProxy) |
114 | { |
115 | QList<QPair<QByteArray, QByteArray> > fields = request.header(); |
116 | QByteArray ba; |
117 | ba.reserve(asize: 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(name: "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(msg: "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()) { |
147 | QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1(); |
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 | |
164 | QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority) |
165 | : d(new QHttpNetworkRequestPrivate(operation, priority, url)) |
166 | { |
167 | } |
168 | |
169 | QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other) |
170 | : QHttpNetworkHeader(other), d(other.d) |
171 | { |
172 | } |
173 | |
174 | QHttpNetworkRequest::~QHttpNetworkRequest() |
175 | { |
176 | } |
177 | |
178 | QUrl QHttpNetworkRequest::url() const |
179 | { |
180 | return d->url; |
181 | } |
182 | void QHttpNetworkRequest::setUrl(const QUrl &url) |
183 | { |
184 | d->url = url; |
185 | } |
186 | |
187 | bool QHttpNetworkRequest::isSsl() const |
188 | { |
189 | return d->ssl; |
190 | } |
191 | void QHttpNetworkRequest::setSsl(bool s) |
192 | { |
193 | d->ssl = s; |
194 | } |
195 | |
196 | bool QHttpNetworkRequest::isPreConnect() const |
197 | { |
198 | return d->preConnect; |
199 | } |
200 | void QHttpNetworkRequest::setPreConnect(bool preConnect) |
201 | { |
202 | d->preConnect = preConnect; |
203 | } |
204 | |
205 | bool QHttpNetworkRequest::isFollowRedirects() const |
206 | { |
207 | return d->redirectPolicy != QNetworkRequest::ManualRedirectPolicy; |
208 | } |
209 | |
210 | void QHttpNetworkRequest::setRedirectPolicy(QNetworkRequest::RedirectPolicy policy) |
211 | { |
212 | d->redirectPolicy = policy; |
213 | } |
214 | |
215 | QNetworkRequest::RedirectPolicy QHttpNetworkRequest::redirectPolicy() const |
216 | { |
217 | return d->redirectPolicy; |
218 | } |
219 | |
220 | int QHttpNetworkRequest::redirectCount() const |
221 | { |
222 | return d->redirectCount; |
223 | } |
224 | |
225 | void QHttpNetworkRequest::setRedirectCount(int count) |
226 | { |
227 | d->redirectCount = count; |
228 | } |
229 | |
230 | qint64 QHttpNetworkRequest::contentLength() const |
231 | { |
232 | return d->contentLength(); |
233 | } |
234 | |
235 | void QHttpNetworkRequest::setContentLength(qint64 length) |
236 | { |
237 | d->setContentLength(length); |
238 | } |
239 | |
240 | QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::() const |
241 | { |
242 | return d->parser.headers(); |
243 | } |
244 | |
245 | QByteArray QHttpNetworkRequest::(const QByteArray &name, const QByteArray &defaultValue) const |
246 | { |
247 | return d->headerField(name, defaultValue); |
248 | } |
249 | |
250 | void QHttpNetworkRequest::(const QByteArray &name, const QByteArray &data) |
251 | { |
252 | d->setHeaderField(name, data); |
253 | } |
254 | |
255 | void QHttpNetworkRequest::(const QByteArray &name, const QByteArray &data) |
256 | { |
257 | d->prependHeaderField(name, data); |
258 | } |
259 | |
260 | void QHttpNetworkRequest::() |
261 | { |
262 | d->clearHeaders(); |
263 | } |
264 | |
265 | QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other) |
266 | { |
267 | d = other.d; |
268 | return *this; |
269 | } |
270 | |
271 | bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const |
272 | { |
273 | return d->operator==(other: *other.d); |
274 | } |
275 | |
276 | QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const |
277 | { |
278 | return d->operation; |
279 | } |
280 | |
281 | void QHttpNetworkRequest::setOperation(Operation operation) |
282 | { |
283 | d->operation = operation; |
284 | } |
285 | |
286 | QByteArray QHttpNetworkRequest::customVerb() const |
287 | { |
288 | return d->customVerb; |
289 | } |
290 | |
291 | void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb) |
292 | { |
293 | d->customVerb = customVerb; |
294 | } |
295 | |
296 | QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const |
297 | { |
298 | return d->priority; |
299 | } |
300 | |
301 | void QHttpNetworkRequest::setPriority(Priority priority) |
302 | { |
303 | d->priority = priority; |
304 | } |
305 | |
306 | bool QHttpNetworkRequest::isPipeliningAllowed() const |
307 | { |
308 | return d->pipeliningAllowed; |
309 | } |
310 | |
311 | void QHttpNetworkRequest::setPipeliningAllowed(bool b) |
312 | { |
313 | d->pipeliningAllowed = b; |
314 | } |
315 | |
316 | bool QHttpNetworkRequest::isHTTP2Allowed() const |
317 | { |
318 | return d->http2Allowed; |
319 | } |
320 | |
321 | void QHttpNetworkRequest::setHTTP2Allowed(bool b) |
322 | { |
323 | d->http2Allowed = b; |
324 | } |
325 | |
326 | bool QHttpNetworkRequest::isHTTP2Direct() const |
327 | { |
328 | return d->http2Direct; |
329 | } |
330 | |
331 | void QHttpNetworkRequest::setHTTP2Direct(bool b) |
332 | { |
333 | d->http2Direct = b; |
334 | } |
335 | |
336 | bool QHttpNetworkRequest::isH2cAllowed() const |
337 | { |
338 | return d->h2cAllowed; |
339 | } |
340 | |
341 | void QHttpNetworkRequest::setH2cAllowed(bool b) |
342 | { |
343 | d->h2cAllowed = b; |
344 | } |
345 | |
346 | bool QHttpNetworkRequest::withCredentials() const |
347 | { |
348 | return d->withCredentials; |
349 | } |
350 | |
351 | void QHttpNetworkRequest::setWithCredentials(bool b) |
352 | { |
353 | d->withCredentials = b; |
354 | } |
355 | |
356 | void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd) |
357 | { |
358 | d->uploadByteDevice = bd; |
359 | } |
360 | |
361 | QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const |
362 | { |
363 | return d->uploadByteDevice; |
364 | } |
365 | |
366 | int QHttpNetworkRequest::majorVersion() const |
367 | { |
368 | return 1; |
369 | } |
370 | |
371 | int QHttpNetworkRequest::minorVersion() const |
372 | { |
373 | return 1; |
374 | } |
375 | |
376 | QString QHttpNetworkRequest::peerVerifyName() const |
377 | { |
378 | return d->peerVerifyName; |
379 | } |
380 | |
381 | void QHttpNetworkRequest::setPeerVerifyName(const QString &peerName) |
382 | { |
383 | d->peerVerifyName = peerName; |
384 | } |
385 | |
386 | QT_END_NAMESPACE |
387 | |
388 | |