qhttpnetworkrequest.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/network/access/qhttpnetworkrequest.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Contact: https://www.qt.io/licensing/-
5**-
6** This file is part of the QtNetwork module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see https://www.qt.io/terms-conditions. For further-
15** information use the contact form at https://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 3 as published by the Free Software-
20** Foundation and appearing in the file LICENSE.LGPL3 included in the-
21** packaging of this file. Please review the following information to-
22** ensure the GNU Lesser General Public License version 3 requirements-
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
24**-
25** GNU General Public License Usage-
26** Alternatively, this file may be used under the terms of the GNU-
27** General Public License version 2.0 or (at your option) the GNU General-
28** Public license version 3 or any later version approved by the KDE Free-
29** Qt Foundation. The licenses are as published by the Free Software-
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
31** included in the packaging of this file. Please review the following-
32** information to ensure the GNU General Public License requirements will-
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
34** https://www.gnu.org/licenses/gpl-3.0.html.-
35**-
36** $QT_END_LICENSE$-
37**-
38****************************************************************************/-
39-
40#include "qhttpnetworkrequest_p.h"-
41#include "private/qnoncontiguousbytedevice_p.h"-
42-
43#ifndef QT_NO_HTTP-
44-
45QT_BEGIN_NAMESPACE-
46-
47QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(QHttpNetworkRequest::Operation op,-
48 QHttpNetworkRequest::Priority pri, const QUrl &newUrl)-
49 : QHttpNetworkHeaderPrivate(newUrl), operation(op), priority(pri), uploadByteDevice(0),-
50 autoDecompress(false), pipeliningAllowed(false), spdyAllowed(false),-
51 withCredentials(true), preConnect(false), followRedirect(false), redirectCount(0)-
52{-
53}-
54-
55QHttpNetworkRequestPrivate::QHttpNetworkRequestPrivate(const QHttpNetworkRequestPrivate &other) // = default-
56 : QHttpNetworkHeaderPrivate(other)-
{),
57 operation=(other.operation;),-
58 customVerb(other.customVerb),-
59 priority=(other.priority;),-
60 uploadByteDevice=(other.uploadByteDevice;),-
61 autoDecompress=(other.autoDecompress;),-
62 pipeliningAllowed=(other.pipeliningAllowed;),-
63 spdyAllowed=(other.spdyAllowed;-
customVerb = other.customVerb;),
64 withCredentials=(other.withCredentials;),-
65 ssl=(other.ssl;),-
66 preConnect=(other.preConnect;),-
67 followRedirect=(other.followRedirect;),-
68 redirectCount=(other.redirectCount;)-
69{-
70}
executed 2184 times by 8 tests: end of block
Executed by:
  • tst_QAbstractNetworkCache
  • tst_QHttpNetworkConnection
  • tst_QNetworkAccessManager_And_QProgressDialog
  • tst_QNetworkDiskCache
  • tst_QNetworkProxyFactory
  • tst_QNetworkReply
  • tst_QXmlInputSource
  • tst_Spdy
2184
71-
72QHttpNetworkRequestPrivate::~QHttpNetworkRequestPrivate()-
73{-
74}-
75-
76bool QHttpNetworkRequestPrivate::operator==(const QHttpNetworkRequestPrivate &other) const-
77{-
78 return QHttpNetworkHeaderPrivate::operator==(other)-
79 && (operation == other.operation)-
80 && (priority == other.priority)-
81 && (uploadByteDevice == other.uploadByteDevice)-
82 && (autoDecompress == other.autoDecompress)-
83 && (pipeliningAllowed == other.pipeliningAllowed)-
84 && (spdyAllowed == other.spdyAllowed)-
85 // we do not clear the customVerb in setOperation-
86 && (operation != QHttpNetworkRequest::Custom || (customVerb == other.customVerb))-
87 && (withCredentials == other.withCredentials)-
88 && (ssl == other.ssl)-
89 && (preConnect == other.preConnect);-
90}-
91-
92QByteArray QHttpNetworkRequest::methodName() const-
93{-
94 switch (d->operation) {-
95 case QHttpNetworkRequest::Get:-
96 return "GET";-
97 case QHttpNetworkRequest::Head:-
98 return "HEAD";-
99 case QHttpNetworkRequest::Post:-
100 return "POST";-
101 case QHttpNetworkRequest::Options:-
102 return "OPTIONS";-
103 case QHttpNetworkRequest::Put:-
104 return "PUT";-
105 case QHttpNetworkRequest::Delete:-
106 return "DELETE";-
107 case QHttpNetworkRequest::Trace:-
108 return "TRACE";-
109 case QHttpNetworkRequest::Connect:-
110 return "CONNECT";-
111 case QHttpNetworkRequest::Custom:-
112 return d->customVerb;-
113 default:-
114 break;-
115 }-
116 return QByteArray();-
117}-
118-
119QByteArray QHttpNetworkRequest::uri(bool throughProxy) const-
120{-
121 QUrl::FormattingOptions format(QUrl::RemoveFragment | QUrl::RemoveUserInfo | QUrl::FullyEncoded);-
122-
123 // for POST, query data is sent as content-
124 if (d->operation == QHttpNetworkRequest::Post && !d->uploadByteDevice)-
125 format |= QUrl::RemoveQuery;-
126 // for requests through proxy, the Request-URI contains full url-
127 if (!throughProxy)-
128 format |= QUrl::RemoveScheme | QUrl::RemoveAuthority;-
129 QUrl copy = d->url;-
130 if (copy.path().isEmpty())-
131 copy.setPath(QStringLiteral("/"));-
132 QByteArray uri = copy.toEncoded(format);-
133 return uri;-
134}-
135-
136QByteArray QHttpNetworkRequestPrivate::header(const QHttpNetworkRequest &request, bool throughProxy)-
137{-
138 QList<QPair<QByteArray, QByteArray> > fields = request.header();-
139 QByteArray ba;-
140 ba.reserve(40 + fields.length()*25); // very rough lower bound estimation-
141-
142 ba += request.methodName();-
143 ba += ' ';-
144 ba += request.uri(throughProxy);-
145-
146 ba += " HTTP/";-
147 ba += QByteArray::number(request.majorVersion());-
148 ba += '.';-
149 ba += QByteArray::number(request.minorVersion());-
150 ba += "\r\n";-
151-
152 QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin();-
153 QList<QPair<QByteArray, QByteArray> >::const_iterator endIt = fields.constEnd();-
154 for (; it != endIt; ++it) {-
155 ba += it->first;-
156 ba += ": ";-
157 ba += it->second;-
158 ba += "\r\n";-
159 }-
160 if (request.d->operation == QHttpNetworkRequest::Post) {-
161 // add content type, if not set in the request-
162 if (request.headerField("content-type").isEmpty() && ((request.d->uploadByteDevice && request.d->uploadByteDevice->size() > 0) || request.d->url.hasQuery())) {-
163 //Content-Type is mandatory. We can't say anything about the encoding, but x-www-form-urlencoded is the most likely to work.-
164 //This warning indicates a bug in application code not setting a required header.-
165 //Note that if using QHttpMultipart, the content-type is set in QNetworkAccessManagerPrivate::prepareMultipart already-
166 qWarning("content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.");-
167 ba += "Content-Type: application/x-www-form-urlencoded\r\n";-
168 }-
169 if (!request.d->uploadByteDevice && request.d->url.hasQuery()) {-
170 QByteArray query = request.d->url.query(QUrl::FullyEncoded).toLatin1();-
171 ba += "Content-Length: ";-
172 ba += QByteArray::number(query.size());-
173 ba += "\r\n\r\n";-
174 ba += query;-
175 } else {-
176 ba += "\r\n";-
177 }-
178 } else {-
179 ba += "\r\n";-
180 }-
181 return ba;-
182}-
183-
184-
185// QHttpNetworkRequest-
186-
187QHttpNetworkRequest::QHttpNetworkRequest(const QUrl &url, Operation operation, Priority priority)-
188 : d(new QHttpNetworkRequestPrivate(operation, priority, url))-
189{-
190}-
191-
192QHttpNetworkRequest::QHttpNetworkRequest(const QHttpNetworkRequest &other)-
193 : QHttpNetworkHeader(other), d(other.d)-
194{-
195}-
196-
197QHttpNetworkRequest::~QHttpNetworkRequest()-
198{-
199}-
200-
201QUrl QHttpNetworkRequest::url() const-
202{-
203 return d->url;-
204}-
205void QHttpNetworkRequest::setUrl(const QUrl &url)-
206{-
207 d->url = url;-
208}-
209-
210bool QHttpNetworkRequest::isSsl() const-
211{-
212 return d->ssl;-
213}-
214void QHttpNetworkRequest::setSsl(bool s)-
215{-
216 d->ssl = s;-
217}-
218-
219bool QHttpNetworkRequest::isPreConnect() const-
220{-
221 return d->preConnect;-
222}-
223void QHttpNetworkRequest::setPreConnect(bool preConnect)-
224{-
225 d->preConnect = preConnect;-
226}-
227-
228bool QHttpNetworkRequest::isFollowRedirects() const-
229{-
230 return d->followRedirect;-
231}-
232-
233void QHttpNetworkRequest::setFollowRedirects(bool followRedirect)-
234{-
235 d->followRedirect = followRedirect;-
236}-
237-
238int QHttpNetworkRequest::redirectCount() const-
239{-
240 return d->redirectCount;-
241}-
242-
243void QHttpNetworkRequest::setRedirectCount(int count)-
244{-
245 d->redirectCount = count;-
246}-
247-
248qint64 QHttpNetworkRequest::contentLength() const-
249{-
250 return d->contentLength();-
251}-
252-
253void QHttpNetworkRequest::setContentLength(qint64 length)-
254{-
255 d->setContentLength(length);-
256}-
257-
258QList<QPair<QByteArray, QByteArray> > QHttpNetworkRequest::header() const-
259{-
260 return d->fields;-
261}-
262-
263QByteArray QHttpNetworkRequest::headerField(const QByteArray &name, const QByteArray &defaultValue) const-
264{-
265 return d->headerField(name, defaultValue);-
266}-
267-
268void QHttpNetworkRequest::setHeaderField(const QByteArray &name, const QByteArray &data)-
269{-
270 d->setHeaderField(name, data);-
271}-
272-
273QHttpNetworkRequest &QHttpNetworkRequest::operator=(const QHttpNetworkRequest &other)-
274{-
275 d = other.d;-
276 return *this;-
277}-
278-
279bool QHttpNetworkRequest::operator==(const QHttpNetworkRequest &other) const-
280{-
281 return d->operator==(*other.d);-
282}-
283-
284QHttpNetworkRequest::Operation QHttpNetworkRequest::operation() const-
285{-
286 return d->operation;-
287}-
288-
289void QHttpNetworkRequest::setOperation(Operation operation)-
290{-
291 d->operation = operation;-
292}-
293-
294QByteArray QHttpNetworkRequest::customVerb() const-
295{-
296 return d->customVerb;-
297}-
298-
299void QHttpNetworkRequest::setCustomVerb(const QByteArray &customVerb)-
300{-
301 d->customVerb = customVerb;-
302}-
303-
304QHttpNetworkRequest::Priority QHttpNetworkRequest::priority() const-
305{-
306 return d->priority;-
307}-
308-
309void QHttpNetworkRequest::setPriority(Priority priority)-
310{-
311 d->priority = priority;-
312}-
313-
314bool QHttpNetworkRequest::isPipeliningAllowed() const-
315{-
316 return d->pipeliningAllowed;-
317}-
318-
319void QHttpNetworkRequest::setPipeliningAllowed(bool b)-
320{-
321 d->pipeliningAllowed = b;-
322}-
323-
324bool QHttpNetworkRequest::isSPDYAllowed() const-
325{-
326 return d->spdyAllowed;-
327}-
328-
329void QHttpNetworkRequest::setSPDYAllowed(bool b)-
330{-
331 d->spdyAllowed = b;-
332}-
333-
334bool QHttpNetworkRequest::withCredentials() const-
335{-
336 return d->withCredentials;-
337}-
338-
339void QHttpNetworkRequest::setWithCredentials(bool b)-
340{-
341 d->withCredentials = b;-
342}-
343-
344void QHttpNetworkRequest::setUploadByteDevice(QNonContiguousByteDevice *bd)-
345{-
346 d->uploadByteDevice = bd;-
347}-
348-
349QNonContiguousByteDevice* QHttpNetworkRequest::uploadByteDevice() const-
350{-
351 return d->uploadByteDevice;-
352}-
353-
354int QHttpNetworkRequest::majorVersion() const-
355{-
356 return 1;-
357}-
358-
359int QHttpNetworkRequest::minorVersion() const-
360{-
361 return 1;-
362}-
363-
364-
365QT_END_NAMESPACE-
366-
367#endif-
368-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9