|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qnetworkreplyimpl_p.h" |
|
43 #include "qnetworkaccessbackend_p.h" |
|
44 #include "qnetworkcookie.h" |
|
45 #include "qabstractnetworkcache.h" |
|
46 #include "QtCore/qcoreapplication.h" |
|
47 #include "QtCore/qdatetime.h" |
|
48 #include "QtNetwork/qsslconfiguration.h" |
|
49 #include "qnetworkaccesshttpbackend_p.h" |
|
50 |
|
51 #include <QtCore/QCoreApplication> |
|
52 |
|
53 QT_BEGIN_NAMESPACE |
|
54 |
|
55 inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate() |
|
56 : backend(0), outgoingData(0), outgoingDataBuffer(0), |
|
57 copyDevice(0), |
|
58 cacheEnabled(false), cacheSaveDevice(0), |
|
59 notificationHandlingPaused(false), |
|
60 bytesDownloaded(0), lastBytesDownloaded(-1), bytesUploaded(-1), |
|
61 httpStatusCode(0), |
|
62 state(Idle) |
|
63 { |
|
64 } |
|
65 |
|
66 void QNetworkReplyImplPrivate::_q_startOperation() |
|
67 { |
|
68 // ensure this function is only being called once |
|
69 if (state == Working) { |
|
70 qDebug("QNetworkReplyImpl::_q_startOperation was called more than once"); |
|
71 return; |
|
72 } |
|
73 state = Working; |
|
74 |
|
75 // note: if that method is called directly, it cannot happen that the backend is 0, |
|
76 // because we just checked via a qobject_cast that we got a http backend (see |
|
77 // QNetworkReplyImplPrivate::setup()) |
|
78 if (!backend) { |
|
79 error(QNetworkReplyImpl::ProtocolUnknownError, |
|
80 QCoreApplication::translate("QNetworkReply", "Protocol \"%1\" is unknown").arg(url.scheme())); // not really true!; |
|
81 finished(); |
|
82 return; |
|
83 } |
|
84 |
|
85 backend->open(); |
|
86 if (state != Finished) { |
|
87 if (operation == QNetworkAccessManager::GetOperation) |
|
88 pendingNotifications.append(NotifyDownstreamReadyWrite); |
|
89 |
|
90 handleNotifications(); |
|
91 } |
|
92 } |
|
93 |
|
94 void QNetworkReplyImplPrivate::_q_copyReadyRead() |
|
95 { |
|
96 Q_Q(QNetworkReplyImpl); |
|
97 if (state != Working) |
|
98 return; |
|
99 if (!copyDevice || !q->isOpen()) |
|
100 return; |
|
101 |
|
102 forever { |
|
103 qint64 bytesToRead = nextDownstreamBlockSize(); |
|
104 if (bytesToRead == 0) |
|
105 // we'll be called again, eventually |
|
106 break; |
|
107 |
|
108 bytesToRead = qBound<qint64>(1, bytesToRead, copyDevice->bytesAvailable()); |
|
109 QByteArray byteData; |
|
110 byteData.resize(bytesToRead); |
|
111 qint64 bytesActuallyRead = copyDevice->read(byteData.data(), byteData.size()); |
|
112 if (bytesActuallyRead == -1) { |
|
113 byteData.clear(); |
|
114 backendNotify(NotifyCopyFinished); |
|
115 break; |
|
116 } |
|
117 |
|
118 byteData.resize(bytesActuallyRead); |
|
119 readBuffer.append(byteData); |
|
120 |
|
121 if (!copyDevice->isSequential() && copyDevice->atEnd()) { |
|
122 backendNotify(NotifyCopyFinished); |
|
123 bytesDownloaded += bytesActuallyRead; |
|
124 break; |
|
125 } |
|
126 |
|
127 bytesDownloaded += bytesActuallyRead; |
|
128 } |
|
129 |
|
130 if (bytesDownloaded == lastBytesDownloaded) { |
|
131 // we didn't read anything |
|
132 return; |
|
133 } |
|
134 |
|
135 lastBytesDownloaded = bytesDownloaded; |
|
136 QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); |
|
137 pauseNotificationHandling(); |
|
138 emit q->downloadProgress(bytesDownloaded, |
|
139 totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); |
|
140 emit q->readyRead(); |
|
141 resumeNotificationHandling(); |
|
142 } |
|
143 |
|
144 void QNetworkReplyImplPrivate::_q_copyReadChannelFinished() |
|
145 { |
|
146 _q_copyReadyRead(); |
|
147 } |
|
148 |
|
149 void QNetworkReplyImplPrivate::_q_bufferOutgoingDataFinished() |
|
150 { |
|
151 Q_Q(QNetworkReplyImpl); |
|
152 |
|
153 // make sure this is only called once, ever. |
|
154 //_q_bufferOutgoingData may call it or the readChannelFinished emission |
|
155 if (state != Buffering) |
|
156 return; |
|
157 |
|
158 // disconnect signals |
|
159 QObject::disconnect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData())); |
|
160 QObject::disconnect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished())); |
|
161 |
|
162 // finally, start the request |
|
163 QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); |
|
164 } |
|
165 |
|
166 void QNetworkReplyImplPrivate::_q_bufferOutgoingData() |
|
167 { |
|
168 Q_Q(QNetworkReplyImpl); |
|
169 |
|
170 if (!outgoingDataBuffer) { |
|
171 // first call, create our buffer |
|
172 outgoingDataBuffer = new QRingBuffer(); |
|
173 |
|
174 QObject::connect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData())); |
|
175 QObject::connect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished())); |
|
176 } |
|
177 |
|
178 qint64 bytesBuffered = 0; |
|
179 qint64 bytesToBuffer = 0; |
|
180 |
|
181 // read data into our buffer |
|
182 forever { |
|
183 bytesToBuffer = outgoingData->bytesAvailable(); |
|
184 // unknown? just try 2 kB, this also ensures we always try to read the EOF |
|
185 if (bytesToBuffer <= 0) |
|
186 bytesToBuffer = 2*1024; |
|
187 |
|
188 char *dst = outgoingDataBuffer->reserve(bytesToBuffer); |
|
189 bytesBuffered = outgoingData->read(dst, bytesToBuffer); |
|
190 |
|
191 if (bytesBuffered == -1) { |
|
192 // EOF has been reached. |
|
193 outgoingDataBuffer->chop(bytesToBuffer); |
|
194 |
|
195 _q_bufferOutgoingDataFinished(); |
|
196 break; |
|
197 } else if (bytesBuffered == 0) { |
|
198 // nothing read right now, just wait until we get called again |
|
199 outgoingDataBuffer->chop(bytesToBuffer); |
|
200 |
|
201 break; |
|
202 } else { |
|
203 // don't break, try to read() again |
|
204 outgoingDataBuffer->chop(bytesToBuffer - bytesBuffered); |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const QNetworkRequest &req, |
|
210 QIODevice *data) |
|
211 { |
|
212 Q_Q(QNetworkReplyImpl); |
|
213 |
|
214 outgoingData = data; |
|
215 request = req; |
|
216 url = request.url(); |
|
217 operation = op; |
|
218 |
|
219 if (outgoingData && backend) { |
|
220 // there is data to be uploaded, e.g. HTTP POST. |
|
221 |
|
222 if (!backend->needsResetableUploadData() || !outgoingData->isSequential()) { |
|
223 // backend does not need upload buffering or |
|
224 // fixed size non-sequential |
|
225 // just start the operation |
|
226 QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); |
|
227 } else { |
|
228 bool bufferingDisallowed = |
|
229 req.attribute(QNetworkRequest::DoNotBufferUploadDataAttribute, |
|
230 false).toBool(); |
|
231 |
|
232 if (bufferingDisallowed) { |
|
233 // if a valid content-length header for the request was supplied, we can disable buffering |
|
234 // if not, we will buffer anyway |
|
235 if (req.header(QNetworkRequest::ContentLengthHeader).isValid()) { |
|
236 QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); |
|
237 } else { |
|
238 state = Buffering; |
|
239 QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection); |
|
240 } |
|
241 } else { |
|
242 // _q_startOperation will be called when the buffering has finished. |
|
243 state = Buffering; |
|
244 QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection); |
|
245 } |
|
246 } |
|
247 } else { |
|
248 // No outgoing data (e.g. HTTP GET request) |
|
249 // or no backend |
|
250 // if no backend, _q_startOperation will handle the error of this |
|
251 |
|
252 // for HTTP, we want to send out the request as fast as possible to the network, without |
|
253 // invoking methods in a QueuedConnection |
|
254 if (qobject_cast<QNetworkAccessHttpBackend *>(backend)) { |
|
255 _q_startOperation(); |
|
256 } else { |
|
257 QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); |
|
258 } |
|
259 } |
|
260 |
|
261 q->QIODevice::open(QIODevice::ReadOnly); |
|
262 } |
|
263 |
|
264 void QNetworkReplyImplPrivate::backendNotify(InternalNotifications notification) |
|
265 { |
|
266 Q_Q(QNetworkReplyImpl); |
|
267 if (!pendingNotifications.contains(notification)) |
|
268 pendingNotifications.enqueue(notification); |
|
269 |
|
270 if (pendingNotifications.size() == 1) |
|
271 QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated)); |
|
272 } |
|
273 |
|
274 void QNetworkReplyImplPrivate::handleNotifications() |
|
275 { |
|
276 if (notificationHandlingPaused) |
|
277 return; |
|
278 |
|
279 NotificationQueue current = pendingNotifications; |
|
280 pendingNotifications.clear(); |
|
281 |
|
282 if (state != Working) |
|
283 return; |
|
284 |
|
285 while (state == Working && !current.isEmpty()) { |
|
286 InternalNotifications notification = current.dequeue(); |
|
287 switch (notification) { |
|
288 case NotifyDownstreamReadyWrite: |
|
289 if (copyDevice) |
|
290 _q_copyReadyRead(); |
|
291 else |
|
292 backend->downstreamReadyWrite(); |
|
293 break; |
|
294 |
|
295 case NotifyCloseDownstreamChannel: |
|
296 backend->closeDownstreamChannel(); |
|
297 break; |
|
298 |
|
299 case NotifyCopyFinished: { |
|
300 QIODevice *dev = copyDevice; |
|
301 copyDevice = 0; |
|
302 backend->copyFinished(dev); |
|
303 break; |
|
304 } |
|
305 } |
|
306 } |
|
307 } |
|
308 |
|
309 // Do not handle the notifications while we are emitting downloadProgress |
|
310 // or readyRead |
|
311 void QNetworkReplyImplPrivate::pauseNotificationHandling() |
|
312 { |
|
313 notificationHandlingPaused = true; |
|
314 } |
|
315 |
|
316 // Resume notification handling |
|
317 void QNetworkReplyImplPrivate::resumeNotificationHandling() |
|
318 { |
|
319 Q_Q(QNetworkReplyImpl); |
|
320 notificationHandlingPaused = false; |
|
321 if (pendingNotifications.size() >= 1) |
|
322 QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated)); |
|
323 } |
|
324 |
|
325 QAbstractNetworkCache *QNetworkReplyImplPrivate::networkCache() const |
|
326 { |
|
327 if (!backend) |
|
328 return 0; |
|
329 return backend->networkCache(); |
|
330 } |
|
331 |
|
332 void QNetworkReplyImplPrivate::createCache() |
|
333 { |
|
334 // check if we can save and if we're allowed to |
|
335 if (!networkCache() |
|
336 || !request.attribute(QNetworkRequest::CacheSaveControlAttribute, true).toBool() |
|
337 || request.attribute(QNetworkRequest::CacheLoadControlAttribute, |
|
338 QNetworkRequest::PreferNetwork).toInt() |
|
339 == QNetworkRequest::AlwaysNetwork) |
|
340 return; |
|
341 cacheEnabled = true; |
|
342 } |
|
343 |
|
344 bool QNetworkReplyImplPrivate::isCachingEnabled() const |
|
345 { |
|
346 return (cacheEnabled && networkCache() != 0); |
|
347 } |
|
348 |
|
349 void QNetworkReplyImplPrivate::setCachingEnabled(bool enable) |
|
350 { |
|
351 if (!enable && !cacheEnabled) |
|
352 return; // nothing to do |
|
353 if (enable && cacheEnabled) |
|
354 return; // nothing to do either! |
|
355 |
|
356 if (enable) { |
|
357 if (bytesDownloaded) { |
|
358 // refuse to enable in this case |
|
359 qCritical("QNetworkReplyImpl: backend error: caching was enabled after some bytes had been written"); |
|
360 return; |
|
361 } |
|
362 |
|
363 createCache(); |
|
364 } else { |
|
365 // someone told us to turn on, then back off? |
|
366 // ok... but you should make up your mind |
|
367 qDebug("QNetworkReplyImpl: setCachingEnabled(true) called after setCachingEnabled(false) -- " |
|
368 "backend %s probably needs to be fixed", |
|
369 backend->metaObject()->className()); |
|
370 networkCache()->remove(url); |
|
371 cacheSaveDevice = 0; |
|
372 cacheEnabled = false; |
|
373 } |
|
374 } |
|
375 |
|
376 void QNetworkReplyImplPrivate::completeCacheSave() |
|
377 { |
|
378 if (cacheEnabled && errorCode != QNetworkReplyImpl::NoError) { |
|
379 networkCache()->remove(url); |
|
380 } else if (cacheEnabled && cacheSaveDevice) { |
|
381 networkCache()->insert(cacheSaveDevice); |
|
382 } |
|
383 cacheSaveDevice = 0; |
|
384 cacheEnabled = false; |
|
385 } |
|
386 |
|
387 void QNetworkReplyImplPrivate::emitUploadProgress(qint64 bytesSent, qint64 bytesTotal) |
|
388 { |
|
389 Q_Q(QNetworkReplyImpl); |
|
390 bytesUploaded = bytesSent; |
|
391 pauseNotificationHandling(); |
|
392 emit q->uploadProgress(bytesSent, bytesTotal); |
|
393 resumeNotificationHandling(); |
|
394 } |
|
395 |
|
396 |
|
397 qint64 QNetworkReplyImplPrivate::nextDownstreamBlockSize() const |
|
398 { |
|
399 enum { DesiredBufferSize = 32 * 1024 }; |
|
400 if (readBufferMaxSize == 0) |
|
401 return DesiredBufferSize; |
|
402 |
|
403 return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount()); |
|
404 } |
|
405 |
|
406 // we received downstream data and send this to the cache |
|
407 // and to our readBuffer (which in turn gets read by the user of QNetworkReply) |
|
408 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data) |
|
409 { |
|
410 Q_Q(QNetworkReplyImpl); |
|
411 if (!q->isOpen()) |
|
412 return; |
|
413 |
|
414 if (cacheEnabled && !cacheSaveDevice) { |
|
415 // save the meta data |
|
416 QNetworkCacheMetaData metaData; |
|
417 metaData.setUrl(url); |
|
418 metaData = backend->fetchCacheMetaData(metaData); |
|
419 |
|
420 // save the redirect request also in the cache |
|
421 QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute); |
|
422 if (redirectionTarget.isValid()) { |
|
423 QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes(); |
|
424 attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget); |
|
425 metaData.setAttributes(attributes); |
|
426 } |
|
427 |
|
428 cacheSaveDevice = networkCache()->prepare(metaData); |
|
429 |
|
430 if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) { |
|
431 if (cacheSaveDevice && !cacheSaveDevice->isOpen()) |
|
432 qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- " |
|
433 "class %s probably needs to be fixed", |
|
434 networkCache()->metaObject()->className()); |
|
435 |
|
436 networkCache()->remove(url); |
|
437 cacheSaveDevice = 0; |
|
438 cacheEnabled = false; |
|
439 } |
|
440 } |
|
441 |
|
442 qint64 bytesWritten = 0; |
|
443 for (int i = 0; i < data.bufferCount(); i++) { |
|
444 QByteArray item = data[i]; |
|
445 |
|
446 if (cacheSaveDevice) |
|
447 cacheSaveDevice->write(item.constData(), item.size()); |
|
448 readBuffer.append(item); |
|
449 |
|
450 bytesWritten += item.size(); |
|
451 } |
|
452 data.clear(); |
|
453 |
|
454 bytesDownloaded += bytesWritten; |
|
455 lastBytesDownloaded = bytesDownloaded; |
|
456 |
|
457 QPointer<QNetworkReplyImpl> qq = q; |
|
458 |
|
459 QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); |
|
460 pauseNotificationHandling(); |
|
461 emit q->downloadProgress(bytesDownloaded, |
|
462 totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); |
|
463 // important: At the point of this readyRead(), the data parameter list must be empty, |
|
464 // else implicit sharing will trigger memcpy when the user is reading data! |
|
465 emit q->readyRead(); |
|
466 |
|
467 // hopefully we haven't been deleted here |
|
468 if (!qq.isNull()) { |
|
469 resumeNotificationHandling(); |
|
470 // do we still have room in the buffer? |
|
471 if (nextDownstreamBlockSize() > 0) |
|
472 backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite); |
|
473 } |
|
474 } |
|
475 |
|
476 // this is used when it was fetched from the cache, right? |
|
477 void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data) |
|
478 { |
|
479 Q_Q(QNetworkReplyImpl); |
|
480 if (!q->isOpen()) |
|
481 return; |
|
482 |
|
483 // read until EOF from data |
|
484 if (copyDevice) { |
|
485 qCritical("QNetworkReplyImpl: copy from QIODevice already in progress -- " |
|
486 "backend probly needs to be fixed"); |
|
487 return; |
|
488 } |
|
489 |
|
490 copyDevice = data; |
|
491 q->connect(copyDevice, SIGNAL(readyRead()), SLOT(_q_copyReadyRead())); |
|
492 q->connect(copyDevice, SIGNAL(readChannelFinished()), SLOT(_q_copyReadChannelFinished())); |
|
493 |
|
494 // start the copy: |
|
495 _q_copyReadyRead(); |
|
496 } |
|
497 |
|
498 void QNetworkReplyImplPrivate::finished() |
|
499 { |
|
500 Q_Q(QNetworkReplyImpl); |
|
501 if (state == Finished || state == Aborted) |
|
502 return; |
|
503 |
|
504 state = Finished; |
|
505 pendingNotifications.clear(); |
|
506 |
|
507 pauseNotificationHandling(); |
|
508 QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); |
|
509 if (totalSize.isNull() || totalSize == -1) { |
|
510 emit q->downloadProgress(bytesDownloaded, bytesDownloaded); |
|
511 } |
|
512 |
|
513 if (bytesUploaded == -1 && (outgoingData || outgoingDataBuffer)) |
|
514 emit q->uploadProgress(0, 0); |
|
515 resumeNotificationHandling(); |
|
516 |
|
517 completeCacheSave(); |
|
518 |
|
519 // note: might not be a good idea, since users could decide to delete us |
|
520 // which would delete the backend too... |
|
521 // maybe we should protect the backend |
|
522 pauseNotificationHandling(); |
|
523 emit q->readChannelFinished(); |
|
524 emit q->finished(); |
|
525 resumeNotificationHandling(); |
|
526 } |
|
527 |
|
528 void QNetworkReplyImplPrivate::error(QNetworkReplyImpl::NetworkError code, const QString &errorMessage) |
|
529 { |
|
530 Q_Q(QNetworkReplyImpl); |
|
531 |
|
532 errorCode = code; |
|
533 q->setErrorString(errorMessage); |
|
534 |
|
535 // note: might not be a good idea, since users could decide to delete us |
|
536 // which would delete the backend too... |
|
537 // maybe we should protect the backend |
|
538 emit q->error(code); |
|
539 } |
|
540 |
|
541 void QNetworkReplyImplPrivate::metaDataChanged() |
|
542 { |
|
543 Q_Q(QNetworkReplyImpl); |
|
544 // do we have cookies? |
|
545 if (cookedHeaders.contains(QNetworkRequest::SetCookieHeader) && !manager.isNull()) { |
|
546 QList<QNetworkCookie> cookies = |
|
547 qvariant_cast<QList<QNetworkCookie> >(cookedHeaders.value(QNetworkRequest::SetCookieHeader)); |
|
548 QNetworkCookieJar *jar = manager->cookieJar(); |
|
549 if (jar) |
|
550 jar->setCookiesFromUrl(cookies, url); |
|
551 } |
|
552 emit q->metaDataChanged(); |
|
553 } |
|
554 |
|
555 void QNetworkReplyImplPrivate::redirectionRequested(const QUrl &target) |
|
556 { |
|
557 attributes.insert(QNetworkRequest::RedirectionTargetAttribute, target); |
|
558 } |
|
559 |
|
560 void QNetworkReplyImplPrivate::sslErrors(const QList<QSslError> &errors) |
|
561 { |
|
562 #ifndef QT_NO_OPENSSL |
|
563 Q_Q(QNetworkReplyImpl); |
|
564 emit q->sslErrors(errors); |
|
565 #else |
|
566 Q_UNUSED(errors); |
|
567 #endif |
|
568 } |
|
569 |
|
570 bool QNetworkReplyImplPrivate::isFinished() const |
|
571 { |
|
572 return (state == Finished || state == Aborted); |
|
573 } |
|
574 |
|
575 QNetworkReplyImpl::QNetworkReplyImpl(QObject *parent) |
|
576 : QNetworkReply(*new QNetworkReplyImplPrivate, parent) |
|
577 { |
|
578 } |
|
579 |
|
580 QNetworkReplyImpl::~QNetworkReplyImpl() |
|
581 { |
|
582 Q_D(QNetworkReplyImpl); |
|
583 if (d->isCachingEnabled()) |
|
584 d->networkCache()->remove(url()); |
|
585 if (d->outgoingDataBuffer) |
|
586 delete d->outgoingDataBuffer; |
|
587 } |
|
588 |
|
589 void QNetworkReplyImpl::abort() |
|
590 { |
|
591 Q_D(QNetworkReplyImpl); |
|
592 if (d->state == QNetworkReplyImplPrivate::Finished || d->state == QNetworkReplyImplPrivate::Aborted) |
|
593 return; |
|
594 |
|
595 // stop both upload and download |
|
596 if (d->outgoingData) |
|
597 disconnect(d->outgoingData, 0, this, 0); |
|
598 if (d->copyDevice) |
|
599 disconnect(d->copyDevice, 0, this, 0); |
|
600 |
|
601 QNetworkReply::close(); |
|
602 |
|
603 if (d->state != QNetworkReplyImplPrivate::Finished) { |
|
604 // emit signals |
|
605 d->error(OperationCanceledError, tr("Operation canceled")); |
|
606 d->finished(); |
|
607 } |
|
608 d->state = QNetworkReplyImplPrivate::Aborted; |
|
609 |
|
610 // finished may access the backend |
|
611 if (d->backend) { |
|
612 d->backend->deleteLater(); |
|
613 d->backend = 0; |
|
614 } |
|
615 } |
|
616 |
|
617 void QNetworkReplyImpl::close() |
|
618 { |
|
619 Q_D(QNetworkReplyImpl); |
|
620 if (d->state == QNetworkReplyImplPrivate::Aborted || |
|
621 d->state == QNetworkReplyImplPrivate::Finished) |
|
622 return; |
|
623 |
|
624 // stop the download |
|
625 if (d->backend) |
|
626 d->backend->closeDownstreamChannel(); |
|
627 if (d->copyDevice) |
|
628 disconnect(d->copyDevice, 0, this, 0); |
|
629 |
|
630 QNetworkReply::close(); |
|
631 |
|
632 // emit signals |
|
633 d->error(OperationCanceledError, tr("Operation canceled")); |
|
634 d->finished(); |
|
635 } |
|
636 |
|
637 /*! |
|
638 Returns the number of bytes available for reading with |
|
639 QIODevice::read(). The number of bytes available may grow until |
|
640 the finished() signal is emitted. |
|
641 */ |
|
642 qint64 QNetworkReplyImpl::bytesAvailable() const |
|
643 { |
|
644 return QNetworkReply::bytesAvailable() + d_func()->readBuffer.byteAmount(); |
|
645 } |
|
646 |
|
647 void QNetworkReplyImpl::setReadBufferSize(qint64 size) |
|
648 { |
|
649 Q_D(QNetworkReplyImpl); |
|
650 if (size > d->readBufferMaxSize && |
|
651 size > d->readBuffer.byteAmount()) |
|
652 d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite); |
|
653 |
|
654 QNetworkReply::setReadBufferSize(size); |
|
655 |
|
656 if (d->backend) |
|
657 d->backend->setDownstreamLimited(d->readBufferMaxSize > 0); |
|
658 } |
|
659 |
|
660 #ifndef QT_NO_OPENSSL |
|
661 QSslConfiguration QNetworkReplyImpl::sslConfigurationImplementation() const |
|
662 { |
|
663 Q_D(const QNetworkReplyImpl); |
|
664 QSslConfiguration config; |
|
665 if (d->backend) |
|
666 d->backend->fetchSslConfiguration(config); |
|
667 return config; |
|
668 } |
|
669 |
|
670 void QNetworkReplyImpl::setSslConfigurationImplementation(const QSslConfiguration &config) |
|
671 { |
|
672 Q_D(QNetworkReplyImpl); |
|
673 if (d->backend && !config.isNull()) |
|
674 d->backend->setSslConfiguration(config); |
|
675 } |
|
676 |
|
677 void QNetworkReplyImpl::ignoreSslErrors() |
|
678 { |
|
679 Q_D(QNetworkReplyImpl); |
|
680 if (d->backend) |
|
681 d->backend->ignoreSslErrors(); |
|
682 } |
|
683 |
|
684 void QNetworkReplyImpl::ignoreSslErrorsImplementation(const QList<QSslError> &errors) |
|
685 { |
|
686 Q_D(QNetworkReplyImpl); |
|
687 if (d->backend) |
|
688 d->backend->ignoreSslErrors(errors); |
|
689 } |
|
690 #endif // QT_NO_OPENSSL |
|
691 |
|
692 /*! |
|
693 \internal |
|
694 */ |
|
695 qint64 QNetworkReplyImpl::readData(char *data, qint64 maxlen) |
|
696 { |
|
697 Q_D(QNetworkReplyImpl); |
|
698 if (d->readBuffer.isEmpty()) |
|
699 return d->state == QNetworkReplyImplPrivate::Finished ? -1 : 0; |
|
700 |
|
701 d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite); |
|
702 if (maxlen == 1) { |
|
703 // optimization for getChar() |
|
704 *data = d->readBuffer.getChar(); |
|
705 return 1; |
|
706 } |
|
707 |
|
708 maxlen = qMin<qint64>(maxlen, d->readBuffer.byteAmount()); |
|
709 return d->readBuffer.read(data, maxlen); |
|
710 } |
|
711 |
|
712 /*! |
|
713 \internal Reimplemented for internal purposes |
|
714 */ |
|
715 bool QNetworkReplyImpl::event(QEvent *e) |
|
716 { |
|
717 if (e->type() == QEvent::NetworkReplyUpdated) { |
|
718 d_func()->handleNotifications(); |
|
719 return true; |
|
720 } |
|
721 |
|
722 return QObject::event(e); |
|
723 } |
|
724 |
|
725 QT_END_NAMESPACE |
|
726 |
|
727 #include "moc_qnetworkreplyimpl_p.cpp" |
|
728 |