Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/access/qhttpmultipart.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qhttpmultipart.h" | - | ||||||||||||||||||
35 | #include "qhttpmultipart_p.h" | - | ||||||||||||||||||
36 | #include "QtCore/qdatetime.h" // for initializing the random number generator with QTime | - | ||||||||||||||||||
37 | #include "QtCore/qmutex.h" | - | ||||||||||||||||||
38 | #include "QtCore/qthreadstorage.h" | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | /*! | - | ||||||||||||||||||
43 | \class QHttpPart | - | ||||||||||||||||||
44 | \brief The QHttpPart class holds a body part to be used inside a | - | ||||||||||||||||||
45 | HTTP multipart MIME message. | - | ||||||||||||||||||
46 | \since 4.8 | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | \ingroup network | - | ||||||||||||||||||
49 | \ingroup shared | - | ||||||||||||||||||
50 | \inmodule QtNetwork | - | ||||||||||||||||||
51 | - | |||||||||||||||||||
52 | The QHttpPart class holds a body part to be used inside a HTTP | - | ||||||||||||||||||
53 | multipart MIME message (which is represented by the QHttpMultiPart class). | - | ||||||||||||||||||
54 | A QHttpPart consists of a header block | - | ||||||||||||||||||
55 | and a data block, which are separated by each other by two | - | ||||||||||||||||||
56 | consecutive new lines. An example for one part would be: | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | \snippet code/src_network_access_qhttppart.cpp 0 | - | ||||||||||||||||||
59 | - | |||||||||||||||||||
60 | For setting headers, use setHeader() and setRawHeader(), which behave | - | ||||||||||||||||||
61 | exactly like QNetworkRequest::setHeader() and QNetworkRequest::setRawHeader(). | - | ||||||||||||||||||
62 | - | |||||||||||||||||||
63 | For reading small pieces of data, use setBody(); for larger data blocks | - | ||||||||||||||||||
64 | like e.g. images, use setBodyDevice(). The latter method saves memory by | - | ||||||||||||||||||
65 | not copying the data internally, but reading directly from the device. | - | ||||||||||||||||||
66 | This means that the device must be opened and readable at the moment when | - | ||||||||||||||||||
67 | the multipart message containing the body part is sent on the network via | - | ||||||||||||||||||
68 | QNetworkAccessManager::post(). | - | ||||||||||||||||||
69 | - | |||||||||||||||||||
70 | To construct a QHttpPart with a small body, consider the following snippet | - | ||||||||||||||||||
71 | (this produces the data shown in the example above): | - | ||||||||||||||||||
72 | - | |||||||||||||||||||
73 | \snippet code/src_network_access_qhttppart.cpp 1 | - | ||||||||||||||||||
74 | - | |||||||||||||||||||
75 | To construct a QHttpPart reading from a device (e.g. a file), the following | - | ||||||||||||||||||
76 | can be applied: | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | \snippet code/src_network_access_qhttppart.cpp 2 | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | Be aware that QHttpPart does not take ownership of the device when set, so | - | ||||||||||||||||||
81 | it is the developer's responsibility to destroy it when it is not needed anymore. | - | ||||||||||||||||||
82 | A good idea might be to set the multipart message as parent object for the device, | - | ||||||||||||||||||
83 | as documented at the documentation for QHttpMultiPart. | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | \sa QHttpMultiPart, QNetworkAccessManager | - | ||||||||||||||||||
86 | */ | - | ||||||||||||||||||
87 | - | |||||||||||||||||||
88 | - | |||||||||||||||||||
89 | /*! | - | ||||||||||||||||||
90 | Constructs an empty QHttpPart object. | - | ||||||||||||||||||
91 | */ | - | ||||||||||||||||||
92 | QHttpPart::QHttpPart() : d(new QHttpPartPrivate) | - | ||||||||||||||||||
93 | { | - | ||||||||||||||||||
94 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||||||||
95 | - | |||||||||||||||||||
96 | /*! | - | ||||||||||||||||||
97 | Creates a copy of \a other. | - | ||||||||||||||||||
98 | */ | - | ||||||||||||||||||
99 | QHttpPart::QHttpPart(const QHttpPart &other) : d(other.d) | - | ||||||||||||||||||
100 | { | - | ||||||||||||||||||
101 | } executed 51 times by 2 tests: end of block Executed by:
| 51 | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | /*! | - | ||||||||||||||||||
104 | Destroys this QHttpPart. | - | ||||||||||||||||||
105 | */ | - | ||||||||||||||||||
106 | QHttpPart::~QHttpPart() | - | ||||||||||||||||||
107 | { | - | ||||||||||||||||||
108 | d = 0; | - | ||||||||||||||||||
109 | } executed 72 times by 2 tests: end of block Executed by:
| 72 | ||||||||||||||||||
110 | - | |||||||||||||||||||
111 | /*! | - | ||||||||||||||||||
112 | Creates a copy of \a other. | - | ||||||||||||||||||
113 | */ | - | ||||||||||||||||||
114 | QHttpPart &QHttpPart::operator=(const QHttpPart &other) | - | ||||||||||||||||||
115 | { | - | ||||||||||||||||||
116 | d = other.d; | - | ||||||||||||||||||
117 | return *this; never executed: return *this; | 0 | ||||||||||||||||||
118 | } | - | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | /*! | - | ||||||||||||||||||
121 | \fn void QHttpPart::swap(QHttpPart &other) | - | ||||||||||||||||||
122 | \since 5.0 | - | ||||||||||||||||||
123 | - | |||||||||||||||||||
124 | Swaps this HTTP part with \a other. This function is very fast and | - | ||||||||||||||||||
125 | never fails. | - | ||||||||||||||||||
126 | */ | - | ||||||||||||||||||
127 | - | |||||||||||||||||||
128 | /*! | - | ||||||||||||||||||
129 | Returns \c true if this object is the same as \a other (i.e., if they | - | ||||||||||||||||||
130 | have the same headers and body). | - | ||||||||||||||||||
131 | - | |||||||||||||||||||
132 | \sa operator!=() | - | ||||||||||||||||||
133 | */ | - | ||||||||||||||||||
134 | bool QHttpPart::operator==(const QHttpPart &other) const | - | ||||||||||||||||||
135 | { | - | ||||||||||||||||||
136 | return d == other.d || *d == *other.d; never executed: return d == other.d || *d == *other.d;
| 0 | ||||||||||||||||||
137 | } | - | ||||||||||||||||||
138 | - | |||||||||||||||||||
139 | /*! | - | ||||||||||||||||||
140 | \fn bool QHttpPart::operator!=(const QHttpPart &other) const | - | ||||||||||||||||||
141 | - | |||||||||||||||||||
142 | Returns \c true if this object is not the same as \a other. | - | ||||||||||||||||||
143 | - | |||||||||||||||||||
144 | \sa operator==() | - | ||||||||||||||||||
145 | */ | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | /*! | - | ||||||||||||||||||
148 | Sets the value of the known header \a header to be \a value, | - | ||||||||||||||||||
149 | overriding any previously set headers. | - | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | \sa QNetworkRequest::KnownHeaders, setRawHeader(), QNetworkRequest::setHeader() | - | ||||||||||||||||||
152 | */ | - | ||||||||||||||||||
153 | void QHttpPart::setHeader(QNetworkRequest::KnownHeaders header, const QVariant &value) | - | ||||||||||||||||||
154 | { | - | ||||||||||||||||||
155 | d->setCookedHeader(header, value); | - | ||||||||||||||||||
156 | } executed 72 times by 2 tests: end of block Executed by:
| 72 | ||||||||||||||||||
157 | - | |||||||||||||||||||
158 | /*! | - | ||||||||||||||||||
159 | Sets the header \a headerName to be of value \a headerValue. If \a | - | ||||||||||||||||||
160 | headerName corresponds to a known header (see | - | ||||||||||||||||||
161 | QNetworkRequest::KnownHeaders), the raw format will be parsed and | - | ||||||||||||||||||
162 | the corresponding "cooked" header will be set as well. | - | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | \note Setting the same header twice overrides the previous | - | ||||||||||||||||||
165 | setting. To accomplish the behaviour of multiple HTTP headers of | - | ||||||||||||||||||
166 | the same name, you should concatenate the two values, separating | - | ||||||||||||||||||
167 | them with a comma (",") and set one single raw header. | - | ||||||||||||||||||
168 | - | |||||||||||||||||||
169 | \sa QNetworkRequest::KnownHeaders, setHeader(), QNetworkRequest::setRawHeader() | - | ||||||||||||||||||
170 | */ | - | ||||||||||||||||||
171 | void QHttpPart::setRawHeader(const QByteArray &headerName, const QByteArray &headerValue) | - | ||||||||||||||||||
172 | { | - | ||||||||||||||||||
173 | d->setRawHeader(headerName, headerValue); | - | ||||||||||||||||||
174 | } executed 26 times by 2 tests: end of block Executed by:
| 26 | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | /*! | - | ||||||||||||||||||
177 | Sets the body of this MIME part to \a body. The body set with this method | - | ||||||||||||||||||
178 | will be used unless the device is set via setBodyDevice(). For a large | - | ||||||||||||||||||
179 | amount of data (e.g. an image), use setBodyDevice(), which will not copy | - | ||||||||||||||||||
180 | the data internally. | - | ||||||||||||||||||
181 | - | |||||||||||||||||||
182 | \sa setBodyDevice() | - | ||||||||||||||||||
183 | */ | - | ||||||||||||||||||
184 | void QHttpPart::setBody(const QByteArray &body) | - | ||||||||||||||||||
185 | { | - | ||||||||||||||||||
186 | d->setBody(body); | - | ||||||||||||||||||
187 | } executed 18 times by 1 test: end of block Executed by:
| 18 | ||||||||||||||||||
188 | - | |||||||||||||||||||
189 | /*! | - | ||||||||||||||||||
190 | Sets the device to read the content from to \a device. For large amounts of data | - | ||||||||||||||||||
191 | this method should be preferred over setBody(), | - | ||||||||||||||||||
192 | because the content is not copied when using this method, but read | - | ||||||||||||||||||
193 | directly from the device. | - | ||||||||||||||||||
194 | \a device must be open and readable. QHttpPart does not take ownership | - | ||||||||||||||||||
195 | of \a device, i.e. the device must be closed and destroyed if necessary. | - | ||||||||||||||||||
196 | if \a device is sequential (e.g. sockets, but not files), | - | ||||||||||||||||||
197 | QNetworkAccessManager::post() should be called after \a device has | - | ||||||||||||||||||
198 | emitted finished(). | - | ||||||||||||||||||
199 | For unsetting the device and using data set via setBody(), use | - | ||||||||||||||||||
200 | "setBodyDevice(0)". | - | ||||||||||||||||||
201 | - | |||||||||||||||||||
202 | \sa setBody(), QNetworkAccessManager::post() | - | ||||||||||||||||||
203 | */ | - | ||||||||||||||||||
204 | void QHttpPart::setBodyDevice(QIODevice *device) | - | ||||||||||||||||||
205 | { | - | ||||||||||||||||||
206 | d->setBodyDevice(device); | - | ||||||||||||||||||
207 | } executed 31 times by 2 tests: end of block Executed by:
| 31 | ||||||||||||||||||
208 | - | |||||||||||||||||||
209 | - | |||||||||||||||||||
210 | - | |||||||||||||||||||
211 | /*! | - | ||||||||||||||||||
212 | \class QHttpMultiPart | - | ||||||||||||||||||
213 | \brief The QHttpMultiPart class resembles a MIME multipart message to be sent over HTTP. | - | ||||||||||||||||||
214 | \since 4.8 | - | ||||||||||||||||||
215 | - | |||||||||||||||||||
216 | \ingroup network | - | ||||||||||||||||||
217 | \inmodule QtNetwork | - | ||||||||||||||||||
218 | - | |||||||||||||||||||
219 | The QHttpMultiPart resembles a MIME multipart message, as described in RFC 2046, | - | ||||||||||||||||||
220 | which is to be sent over HTTP. | - | ||||||||||||||||||
221 | A multipart message consists of an arbitrary number of body parts (see QHttpPart), | - | ||||||||||||||||||
222 | which are separated by a unique boundary. The boundary of the QHttpMultiPart is | - | ||||||||||||||||||
223 | constructed with the string "boundary_.oOo._" followed by random characters, | - | ||||||||||||||||||
224 | and provides enough uniqueness to make sure it does not occur inside the parts itself. | - | ||||||||||||||||||
225 | If desired, the boundary can still be set via setBoundary(). | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | As an example, consider the following code snippet, which constructs a multipart | - | ||||||||||||||||||
228 | message containing a text part followed by an image part: | - | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | \snippet code/src_network_access_qhttpmultipart.cpp 0 | - | ||||||||||||||||||
231 | - | |||||||||||||||||||
232 | \sa QHttpPart, QNetworkAccessManager::post() | - | ||||||||||||||||||
233 | */ | - | ||||||||||||||||||
234 | - | |||||||||||||||||||
235 | /*! | - | ||||||||||||||||||
236 | \enum QHttpMultiPart::ContentType | - | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | List of known content types for a multipart subtype as described | - | ||||||||||||||||||
239 | in RFC 2046 and others. | - | ||||||||||||||||||
240 | - | |||||||||||||||||||
241 | \value MixedType corresponds to the "multipart/mixed" subtype, | - | ||||||||||||||||||
242 | meaning the body parts are independent of each other, as described | - | ||||||||||||||||||
243 | in RFC 2046. | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | \value RelatedType corresponds to the "multipart/related" subtype, | - | ||||||||||||||||||
246 | meaning the body parts are related to each other, as described in RFC 2387. | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | \value FormDataType corresponds to the "multipart/form-data" | - | ||||||||||||||||||
249 | subtype, meaning the body parts contain form elements, as described in RFC 2388. | - | ||||||||||||||||||
250 | - | |||||||||||||||||||
251 | \value AlternativeType corresponds to the "multipart/alternative" | - | ||||||||||||||||||
252 | subtype, meaning the body parts are alternative representations of | - | ||||||||||||||||||
253 | the same information, as described in RFC 2046. | - | ||||||||||||||||||
254 | - | |||||||||||||||||||
255 | \sa setContentType() | - | ||||||||||||||||||
256 | */ | - | ||||||||||||||||||
257 | - | |||||||||||||||||||
258 | /*! | - | ||||||||||||||||||
259 | Constructs a QHttpMultiPart with content type MixedType and sets | - | ||||||||||||||||||
260 | \a parent as the parent object. | - | ||||||||||||||||||
261 | - | |||||||||||||||||||
262 | \sa QHttpMultiPart::ContentType | - | ||||||||||||||||||
263 | */ | - | ||||||||||||||||||
264 | QHttpMultiPart::QHttpMultiPart(QObject *parent) : QObject(*new QHttpMultiPartPrivate, parent) | - | ||||||||||||||||||
265 | { | - | ||||||||||||||||||
266 | Q_D(QHttpMultiPart); | - | ||||||||||||||||||
267 | d->contentType = MixedType; | - | ||||||||||||||||||
268 | } executed 27 times by 1 test: end of block Executed by:
| 27 | ||||||||||||||||||
269 | - | |||||||||||||||||||
270 | /*! | - | ||||||||||||||||||
271 | Constructs a QHttpMultiPart with content type \a contentType and | - | ||||||||||||||||||
272 | sets parent as the parent object. | - | ||||||||||||||||||
273 | - | |||||||||||||||||||
274 | \sa QHttpMultiPart::ContentType | - | ||||||||||||||||||
275 | */ | - | ||||||||||||||||||
276 | QHttpMultiPart::QHttpMultiPart(QHttpMultiPart::ContentType contentType, QObject *parent) : QObject(*new QHttpMultiPartPrivate, parent) | - | ||||||||||||||||||
277 | { | - | ||||||||||||||||||
278 | Q_D(QHttpMultiPart); | - | ||||||||||||||||||
279 | d->contentType = contentType; | - | ||||||||||||||||||
280 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||||||||
281 | - | |||||||||||||||||||
282 | /*! | - | ||||||||||||||||||
283 | Destroys the multipart. | - | ||||||||||||||||||
284 | */ | - | ||||||||||||||||||
285 | QHttpMultiPart::~QHttpMultiPart() | - | ||||||||||||||||||
286 | { | - | ||||||||||||||||||
287 | } | - | ||||||||||||||||||
288 | - | |||||||||||||||||||
289 | /*! | - | ||||||||||||||||||
290 | Appends \a httpPart to this multipart. | - | ||||||||||||||||||
291 | */ | - | ||||||||||||||||||
292 | void QHttpMultiPart::append(const QHttpPart &httpPart) | - | ||||||||||||||||||
293 | { | - | ||||||||||||||||||
294 | d_func()->parts.append(httpPart); | - | ||||||||||||||||||
295 | } executed 51 times by 2 tests: end of block Executed by:
| 51 | ||||||||||||||||||
296 | - | |||||||||||||||||||
297 | /*! | - | ||||||||||||||||||
298 | Sets the content type to \a contentType. The content type will be used | - | ||||||||||||||||||
299 | in the HTTP header section when sending the multipart message via | - | ||||||||||||||||||
300 | QNetworkAccessManager::post(). | - | ||||||||||||||||||
301 | In case you want to use a multipart subtype not contained in | - | ||||||||||||||||||
302 | QHttpMultiPart::ContentType, | - | ||||||||||||||||||
303 | you can add the "Content-Type" header field to the QNetworkRequest | - | ||||||||||||||||||
304 | by hand, and then use this request together with the multipart | - | ||||||||||||||||||
305 | message for posting. | - | ||||||||||||||||||
306 | - | |||||||||||||||||||
307 | \sa QHttpMultiPart::ContentType, QNetworkAccessManager::post() | - | ||||||||||||||||||
308 | */ | - | ||||||||||||||||||
309 | void QHttpMultiPart::setContentType(QHttpMultiPart::ContentType contentType) | - | ||||||||||||||||||
310 | { | - | ||||||||||||||||||
311 | d_func()->contentType = contentType; | - | ||||||||||||||||||
312 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||
313 | - | |||||||||||||||||||
314 | /*! | - | ||||||||||||||||||
315 | returns the boundary. | - | ||||||||||||||||||
316 | - | |||||||||||||||||||
317 | \sa setBoundary() | - | ||||||||||||||||||
318 | */ | - | ||||||||||||||||||
319 | QByteArray QHttpMultiPart::boundary() const | - | ||||||||||||||||||
320 | { | - | ||||||||||||||||||
321 | return d_func()->boundary; executed 135 times by 2 tests: return d_func()->boundary; Executed by:
| 135 | ||||||||||||||||||
322 | } | - | ||||||||||||||||||
323 | - | |||||||||||||||||||
324 | /*! | - | ||||||||||||||||||
325 | Sets the boundary to \a boundary. | - | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | Usually, you do not need to generate a boundary yourself; upon construction | - | ||||||||||||||||||
328 | the boundary is initiated with the string "boundary_.oOo._" followed by random | - | ||||||||||||||||||
329 | characters, and provides enough uniqueness to make sure it does not occur | - | ||||||||||||||||||
330 | inside the parts itself. | - | ||||||||||||||||||
331 | - | |||||||||||||||||||
332 | \sa boundary() | - | ||||||||||||||||||
333 | */ | - | ||||||||||||||||||
334 | void QHttpMultiPart::setBoundary(const QByteArray &boundary) | - | ||||||||||||||||||
335 | { | - | ||||||||||||||||||
336 | d_func()->boundary = boundary; | - | ||||||||||||||||||
337 | } never executed: end of block | 0 | ||||||||||||||||||
338 | - | |||||||||||||||||||
339 | - | |||||||||||||||||||
340 | - | |||||||||||||||||||
341 | // ------------------------------------------------------------------ | - | ||||||||||||||||||
342 | // ----------- implementations of private classes: ------------------ | - | ||||||||||||||||||
343 | // ------------------------------------------------------------------ | - | ||||||||||||||||||
344 | - | |||||||||||||||||||
345 | - | |||||||||||||||||||
346 | - | |||||||||||||||||||
347 | qint64 QHttpPartPrivate::bytesAvailable() const | - | ||||||||||||||||||
348 | { | - | ||||||||||||||||||
349 | checkHeaderCreated(); | - | ||||||||||||||||||
350 | qint64 bytesAvailable = header.count(); | - | ||||||||||||||||||
351 | if (bodyDevice) {
| 0 | ||||||||||||||||||
352 | bytesAvailable += bodyDevice->bytesAvailable() - readPointer; | - | ||||||||||||||||||
353 | } else { never executed: end of block | 0 | ||||||||||||||||||
354 | bytesAvailable += body.count() - readPointer; | - | ||||||||||||||||||
355 | } never executed: end of block | 0 | ||||||||||||||||||
356 | // the device might have closed etc., so make sure we do not return a negative value | - | ||||||||||||||||||
357 | return qMax(bytesAvailable, (qint64) 0); never executed: return qMax(bytesAvailable, (qint64) 0); | 0 | ||||||||||||||||||
358 | } | - | ||||||||||||||||||
359 | - | |||||||||||||||||||
360 | qint64 QHttpPartPrivate::readData(char *data, qint64 maxSize) | - | ||||||||||||||||||
361 | { | - | ||||||||||||||||||
362 | checkHeaderCreated(); | - | ||||||||||||||||||
363 | qint64 bytesRead = 0; | - | ||||||||||||||||||
364 | qint64 headerDataCount = header.count(); | - | ||||||||||||||||||
365 | - | |||||||||||||||||||
366 | // read header if it has not been read yet | - | ||||||||||||||||||
367 | if (readPointer < headerDataCount) {
| 36-1004 | ||||||||||||||||||
368 | bytesRead = qMin(headerDataCount - readPointer, maxSize); | - | ||||||||||||||||||
369 | const char *headerData = header.constData(); | - | ||||||||||||||||||
370 | memcpy(data, headerData + readPointer, bytesRead); | - | ||||||||||||||||||
371 | readPointer += bytesRead; | - | ||||||||||||||||||
372 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||||||||
373 | // read content if there is still space | - | ||||||||||||||||||
374 | if (bytesRead < maxSize) {
| 0-1040 | ||||||||||||||||||
375 | if (bodyDevice) {
| 152-888 | ||||||||||||||||||
376 | qint64 dataBytesRead = bodyDevice->read(data + bytesRead, maxSize - bytesRead); | - | ||||||||||||||||||
377 | if (dataBytesRead == -1)
| 0-888 | ||||||||||||||||||
378 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
379 | bytesRead += dataBytesRead; | - | ||||||||||||||||||
380 | readPointer += dataBytesRead; | - | ||||||||||||||||||
381 | } else { executed 888 times by 2 tests: end of block Executed by:
| 888 | ||||||||||||||||||
382 | qint64 contentBytesRead = qMin(body.count() - readPointer + headerDataCount, maxSize - bytesRead); | - | ||||||||||||||||||
383 | const char *contentData = body.constData(); | - | ||||||||||||||||||
384 | // if this method is called several times, we need to find the | - | ||||||||||||||||||
385 | // right offset in the content ourselves: | - | ||||||||||||||||||
386 | memcpy(data + bytesRead, contentData + readPointer - headerDataCount, contentBytesRead); | - | ||||||||||||||||||
387 | bytesRead += contentBytesRead; | - | ||||||||||||||||||
388 | readPointer += contentBytesRead; | - | ||||||||||||||||||
389 | } executed 152 times by 1 test: end of block Executed by:
| 152 | ||||||||||||||||||
390 | } | - | ||||||||||||||||||
391 | return bytesRead; executed 1040 times by 2 tests: return bytesRead; Executed by:
| 1040 | ||||||||||||||||||
392 | } | - | ||||||||||||||||||
393 | - | |||||||||||||||||||
394 | qint64 QHttpPartPrivate::size() const | - | ||||||||||||||||||
395 | { | - | ||||||||||||||||||
396 | checkHeaderCreated(); | - | ||||||||||||||||||
397 | qint64 size = header.count(); | - | ||||||||||||||||||
398 | if (bodyDevice) {
| 576-2396 | ||||||||||||||||||
399 | size += bodyDevice->size(); | - | ||||||||||||||||||
400 | } else { executed 2396 times by 2 tests: end of block Executed by:
| 2396 | ||||||||||||||||||
401 | size += body.count(); | - | ||||||||||||||||||
402 | } executed 576 times by 1 test: end of block Executed by:
| 576 | ||||||||||||||||||
403 | return size; executed 2972 times by 2 tests: return size; Executed by:
| 2972 | ||||||||||||||||||
404 | } | - | ||||||||||||||||||
405 | - | |||||||||||||||||||
406 | bool QHttpPartPrivate::reset() | - | ||||||||||||||||||
407 | { | - | ||||||||||||||||||
408 | bool ret = true; | - | ||||||||||||||||||
409 | if (bodyDevice)
| 0 | ||||||||||||||||||
410 | if (!bodyDevice->reset())
| 0 | ||||||||||||||||||
411 | ret = false; never executed: ret = false; | 0 | ||||||||||||||||||
412 | readPointer = 0; | - | ||||||||||||||||||
413 | return ret; never executed: return ret; | 0 | ||||||||||||||||||
414 | } | - | ||||||||||||||||||
415 | void QHttpPartPrivate::checkHeaderCreated() const | - | ||||||||||||||||||
416 | { | - | ||||||||||||||||||
417 | if (!headerCreated) {
| 26-3986 | ||||||||||||||||||
418 | // copied from QHttpNetworkRequestPrivate::header() and adapted | - | ||||||||||||||||||
419 | QList<QPair<QByteArray, QByteArray> > fields = allRawHeaders(); | - | ||||||||||||||||||
420 | QList<QPair<QByteArray, QByteArray> >::const_iterator it = fields.constBegin(); | - | ||||||||||||||||||
421 | for (; it != fields.constEnd(); ++it)
| 26-70 | ||||||||||||||||||
422 | header += it->first + ": " + it->second + "\r\n"; executed 70 times by 2 tests: header += it->first + ": " + it->second + "\r\n"; Executed by:
| 70 | ||||||||||||||||||
423 | header += "\r\n"; | - | ||||||||||||||||||
424 | headerCreated = true; | - | ||||||||||||||||||
425 | } executed 26 times by 2 tests: end of block Executed by:
| 26 | ||||||||||||||||||
426 | } executed 4012 times by 2 tests: end of block Executed by:
| 4012 | ||||||||||||||||||
427 | - | |||||||||||||||||||
428 | Q_GLOBAL_STATIC(QThreadStorage<bool *>, seedCreatedStorage); executed 1 time by 1 test: end of block Executed by:
executed 1 time by 1 test: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 37 times by 2 tests: return &holder.value; Executed by:
| 0-37 | ||||||||||||||||||
429 | - | |||||||||||||||||||
430 | QHttpMultiPartPrivate::QHttpMultiPartPrivate() : contentType(QHttpMultiPart::MixedType), device(new QHttpMultiPartIODevice(this)) | - | ||||||||||||||||||
431 | { | - | ||||||||||||||||||
432 | if (!seedCreatedStorage()->hasLocalData()) {
| 2-33 | ||||||||||||||||||
433 | qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(this)); | - | ||||||||||||||||||
434 | seedCreatedStorage()->setLocalData(new bool(true)); | - | ||||||||||||||||||
435 | } executed 2 times by 2 tests: end of block Executed by:
| 2 | ||||||||||||||||||
436 | - | |||||||||||||||||||
437 | boundary = QByteArray("boundary_.oOo._") | - | ||||||||||||||||||
438 | + QByteArray::number(qrand()).toBase64() | - | ||||||||||||||||||
439 | + QByteArray::number(qrand()).toBase64() | - | ||||||||||||||||||
440 | + QByteArray::number(qrand()).toBase64(); | - | ||||||||||||||||||
441 | - | |||||||||||||||||||
442 | // boundary must not be longer than 70 characters, see RFC 2046, section 5.1.1 | - | ||||||||||||||||||
443 | if (boundary.count() > 70)
| 0-35 | ||||||||||||||||||
444 | boundary = boundary.left(70); never executed: boundary = boundary.left(70); | 0 | ||||||||||||||||||
445 | } executed 35 times by 2 tests: end of block Executed by:
| 35 | ||||||||||||||||||
446 | - | |||||||||||||||||||
447 | qint64 QHttpMultiPartIODevice::size() const | - | ||||||||||||||||||
448 | { | - | ||||||||||||||||||
449 | // if not done yet, we calculate the size and the offsets of each part, | - | ||||||||||||||||||
450 | // including boundary (needed later in readData) | - | ||||||||||||||||||
451 | if (deviceSize == -1) {
| 24-3638 | ||||||||||||||||||
452 | qint64 currentSize = 0; | - | ||||||||||||||||||
453 | qint64 boundaryCount = multiPart->boundary.count(); | - | ||||||||||||||||||
454 | for (int a = 0; a < multiPart->parts.count(); a++) {
| 24-36 | ||||||||||||||||||
455 | partOffsets.append(currentSize); | - | ||||||||||||||||||
456 | // 4 additional bytes for the "--" before and the "\r\n" after the boundary, | - | ||||||||||||||||||
457 | // and 2 bytes for the "\r\n" after the content | - | ||||||||||||||||||
458 | currentSize += boundaryCount + 4 + multiPart->parts.at(a).d->size() + 2; | - | ||||||||||||||||||
459 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||||||||
460 | currentSize += boundaryCount + 6; // size for ending boundary, 2 beginning and ending dashes and "\r\n" | - | ||||||||||||||||||
461 | deviceSize = currentSize; | - | ||||||||||||||||||
462 | } executed 24 times by 2 tests: end of block Executed by:
| 24 | ||||||||||||||||||
463 | return deviceSize; executed 3662 times by 2 tests: return deviceSize; Executed by:
| 3662 | ||||||||||||||||||
464 | } | - | ||||||||||||||||||
465 | - | |||||||||||||||||||
466 | bool QHttpMultiPartIODevice::isSequential() const | - | ||||||||||||||||||
467 | { | - | ||||||||||||||||||
468 | for (int a = 0; a < multiPart->parts.count(); a++) {
| 3684-9288 | ||||||||||||||||||
469 | QIODevice *device = multiPart->parts.at(a).d->bodyDevice; | - | ||||||||||||||||||
470 | // we are sequential if any of the bodyDevices of our parts are sequential; | - | ||||||||||||||||||
471 | // when reading from a byte array, we are not sequential | - | ||||||||||||||||||
472 | if (device && device->isSequential())
| 0-8016 | ||||||||||||||||||
473 | return true; never executed: return true; | 0 | ||||||||||||||||||
474 | } executed 9288 times by 2 tests: end of block Executed by:
| 9288 | ||||||||||||||||||
475 | return false; executed 3684 times by 2 tests: return false; Executed by:
| 3684 | ||||||||||||||||||
476 | } | - | ||||||||||||||||||
477 | - | |||||||||||||||||||
478 | bool QHttpMultiPartIODevice::reset() | - | ||||||||||||||||||
479 | { | - | ||||||||||||||||||
480 | for (int a = 0; a < multiPart->parts.count(); a++)
| 0 | ||||||||||||||||||
481 | if (!multiPart->parts[a].d->reset())
| 0 | ||||||||||||||||||
482 | return false; never executed: return false; | 0 | ||||||||||||||||||
483 | readPointer = 0; | - | ||||||||||||||||||
484 | return true; never executed: return true; | 0 | ||||||||||||||||||
485 | } | - | ||||||||||||||||||
486 | qint64 QHttpMultiPartIODevice::readData(char *data, qint64 maxSize) | - | ||||||||||||||||||
487 | { | - | ||||||||||||||||||
488 | qint64 bytesRead = 0, index = 0; | - | ||||||||||||||||||
489 | - | |||||||||||||||||||
490 | // skip the parts we have already read | - | ||||||||||||||||||
491 | while (index < multiPart->parts.count() &&
| 8-1860 | ||||||||||||||||||
492 | readPointer >= partOffsets.at(index) + multiPart->parts.at(index).d->size()
| 838-1022 | ||||||||||||||||||
493 | + multiPart->boundary.count() + 6) // 6 == 2 boundary dashes, \r\n after boundary, \r\n after multipart
| 838-1022 | ||||||||||||||||||
494 | index++; executed 838 times by 2 tests: index++; Executed by:
| 838 | ||||||||||||||||||
495 | - | |||||||||||||||||||
496 | // read the data | - | ||||||||||||||||||
497 | while (bytesRead < maxSize && index < multiPart->parts.count()) {
| 26-1066 | ||||||||||||||||||
498 | - | |||||||||||||||||||
499 | // check whether we need to read the boundary of the current part | - | ||||||||||||||||||
500 | QByteArray boundaryData = "--" + multiPart->boundary + "\r\n"; | - | ||||||||||||||||||
501 | qint64 boundaryCount = boundaryData.count(); | - | ||||||||||||||||||
502 | qint64 partIndex = readPointer - partOffsets.at(index); | - | ||||||||||||||||||
503 | if (partIndex < boundaryCount) {
| 36-1004 | ||||||||||||||||||
504 | qint64 boundaryBytesRead = qMin(boundaryCount - partIndex, maxSize - bytesRead); | - | ||||||||||||||||||
505 | memcpy(data + bytesRead, boundaryData.constData() + partIndex, boundaryBytesRead); | - | ||||||||||||||||||
506 | bytesRead += boundaryBytesRead; | - | ||||||||||||||||||
507 | readPointer += boundaryBytesRead; | - | ||||||||||||||||||
508 | partIndex += boundaryBytesRead; | - | ||||||||||||||||||
509 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||||||||
510 | - | |||||||||||||||||||
511 | // check whether we need to read the data of the current part | - | ||||||||||||||||||
512 | if (bytesRead < maxSize && partIndex >= boundaryCount && partIndex < boundaryCount + multiPart->parts.at(index).d->size()) {
| 0-1040 | ||||||||||||||||||
513 | qint64 dataBytesRead = multiPart->parts[index].d->readData(data + bytesRead, maxSize - bytesRead); | - | ||||||||||||||||||
514 | if (dataBytesRead == -1)
| 0-1040 | ||||||||||||||||||
515 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
516 | bytesRead += dataBytesRead; | - | ||||||||||||||||||
517 | readPointer += dataBytesRead; | - | ||||||||||||||||||
518 | partIndex += dataBytesRead; | - | ||||||||||||||||||
519 | } executed 1040 times by 2 tests: end of block Executed by:
| 1040 | ||||||||||||||||||
520 | - | |||||||||||||||||||
521 | // check whether we need to read the ending CRLF of the current part | - | ||||||||||||||||||
522 | if (bytesRead < maxSize && partIndex >= boundaryCount + multiPart->parts.at(index).d->size()) {
| 0-1004 | ||||||||||||||||||
523 | if (bytesRead == maxSize - 1)
| 0-36 | ||||||||||||||||||
524 | return bytesRead; never executed: return bytesRead; | 0 | ||||||||||||||||||
525 | memcpy(data + bytesRead, "\r\n", 2); | - | ||||||||||||||||||
526 | bytesRead += 2; | - | ||||||||||||||||||
527 | readPointer += 2; | - | ||||||||||||||||||
528 | index++; | - | ||||||||||||||||||
529 | } executed 36 times by 2 tests: end of block Executed by:
| 36 | ||||||||||||||||||
530 | } executed 1040 times by 2 tests: end of block Executed by:
| 1040 | ||||||||||||||||||
531 | // check whether we need to return the final boundary | - | ||||||||||||||||||
532 | if (bytesRead < maxSize && index == multiPart->parts.count()) {
| 0-1004 | ||||||||||||||||||
533 | QByteArray finalBoundary = "--" + multiPart->boundary + "--\r\n"; | - | ||||||||||||||||||
534 | qint64 boundaryIndex = readPointer + finalBoundary.count() - size(); | - | ||||||||||||||||||
535 | qint64 lastBoundaryBytesRead = qMin(finalBoundary.count() - boundaryIndex, maxSize - bytesRead); | - | ||||||||||||||||||
536 | memcpy(data + bytesRead, finalBoundary.constData() + boundaryIndex, lastBoundaryBytesRead); | - | ||||||||||||||||||
537 | bytesRead += lastBoundaryBytesRead; | - | ||||||||||||||||||
538 | readPointer += lastBoundaryBytesRead; | - | ||||||||||||||||||
539 | } executed 26 times by 2 tests: end of block Executed by:
| 26 | ||||||||||||||||||
540 | return bytesRead; executed 1030 times by 2 tests: return bytesRead; Executed by:
| 1030 | ||||||||||||||||||
541 | } | - | ||||||||||||||||||
542 | - | |||||||||||||||||||
543 | qint64 QHttpMultiPartIODevice::writeData(const char *data, qint64 maxSize) | - | ||||||||||||||||||
544 | { | - | ||||||||||||||||||
545 | Q_UNUSED(data); | - | ||||||||||||||||||
546 | Q_UNUSED(maxSize); | - | ||||||||||||||||||
547 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
548 | } | - | ||||||||||||||||||
549 | - | |||||||||||||||||||
550 | - | |||||||||||||||||||
551 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |