Merge remote branch 'origin/4.8' into 4.8-from-4.7
[qt:qt.git] / src / network / access / qnetworkreplyimpl.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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 "QtNetwork/qnetworksession.h"
50 #include "qnetworkaccesshttpbackend_p.h"
51 #include "qnetworkaccessmanager_p.h"
52
53 #include <QtCore/QCoreApplication>
54
55 Q_DECLARE_METATYPE(QSharedPointer<char>)
56
57 QT_BEGIN_NAMESPACE
58
59 inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate()
60     : backend(0), outgoingData(0),
61       copyDevice(0),
62       cacheEnabled(false), cacheSaveDevice(0),
63       notificationHandlingPaused(false),
64       bytesDownloaded(0), lastBytesDownloaded(-1), bytesUploaded(-1), preMigrationDownloaded(-1),
65       httpStatusCode(0),
66       state(Idle)
67       , downloadBufferReadPosition(0)
68       , downloadBufferCurrentSize(0)
69       , downloadBufferMaximumSize(0)
70       , downloadBuffer(0)
71 {
72 }
73
74 void QNetworkReplyImplPrivate::_q_startOperation()
75 {
76     // ensure this function is only being called once
77     if (state == Working || state == Finished) {
78         qDebug("QNetworkReplyImpl::_q_startOperation was called more than once");
79         return;
80     }
81     state = Working;
82
83     // note: if that method is called directly, it cannot happen that the backend is 0,
84     // because we just checked via a qobject_cast that we got a http backend (see
85     // QNetworkReplyImplPrivate::setup())
86     if (!backend) {
87         error(QNetworkReplyImpl::ProtocolUnknownError,
88               QCoreApplication::translate("QNetworkReply", "Protocol \"%1\" is unknown").arg(url.scheme())); // not really true!;
89         finished();
90         return;
91     }
92
93     if (!backend->start()) {
94 #ifndef QT_NO_BEARERMANAGEMENT
95         // backend failed to start because the session state is not Connected.
96         // QNetworkAccessManager will call _q_startOperation again for us when the session
97         // state changes.
98         state = WaitingForSession;
99
100         QNetworkSession *session = manager->d_func()->networkSession.data();
101
102         if (session) {
103             Q_Q(QNetworkReplyImpl);
104
105             QObject::connect(session, SIGNAL(error(QNetworkSession::SessionError)),
106                              q, SLOT(_q_networkSessionFailed()));
107
108             if (!session->isOpen())
109                 session->open();
110         } else {
111             qWarning("Backend is waiting for QNetworkSession to connect, but there is none!");
112             state = Working;
113             error(QNetworkReplyImpl::UnknownNetworkError,
114                   QCoreApplication::translate("QNetworkReply", "Network session error."));
115             finished();
116         }
117 #else
118         qWarning("Backend start failed");
119         state = Working;
120         error(QNetworkReplyImpl::UnknownNetworkError,
121               QCoreApplication::translate("QNetworkReply", "backend start error."));
122         finished();
123 #endif
124         return;
125     }
126
127     if (backend && backend->isSynchronous()) {
128         state = Finished;
129         q_func()->setFinished(true);
130     } else {
131         if (state != Finished) {
132             if (operation == QNetworkAccessManager::GetOperation)
133                 pendingNotifications.append(NotifyDownstreamReadyWrite);
134
135             handleNotifications();
136         }
137     }
138 }
139
140 void QNetworkReplyImplPrivate::_q_copyReadyRead()
141 {
142     Q_Q(QNetworkReplyImpl);
143     if (state != Working)
144         return;
145     if (!copyDevice || !q->isOpen())
146         return;
147
148     // FIXME Optimize to use download buffer if it is a QBuffer.
149     // Needs to be done where sendCacheContents() (?) of HTTP is emitting
150     // metaDataChanged ?
151
152     forever {
153         qint64 bytesToRead = nextDownstreamBlockSize();
154         if (bytesToRead == 0)
155             // we'll be called again, eventually
156             break;
157
158         bytesToRead = qBound<qint64>(1, bytesToRead, copyDevice->bytesAvailable());
159         QByteArray byteData;
160         byteData.resize(bytesToRead);
161         qint64 bytesActuallyRead = copyDevice->read(byteData.data(), byteData.size());
162         if (bytesActuallyRead == -1) {
163             byteData.clear();
164             backendNotify(NotifyCopyFinished);
165             break;
166         }
167
168         byteData.resize(bytesActuallyRead);
169         readBuffer.append(byteData);
170
171         if (!copyDevice->isSequential() && copyDevice->atEnd()) {
172             backendNotify(NotifyCopyFinished);
173             bytesDownloaded += bytesActuallyRead;
174             break;
175         }
176
177         bytesDownloaded += bytesActuallyRead;
178     }
179
180     if (bytesDownloaded == lastBytesDownloaded) {
181         // we didn't read anything
182         return;
183     }
184
185     lastBytesDownloaded = bytesDownloaded;
186     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
187     if (preMigrationDownloaded != Q_INT64_C(-1))
188         totalSize = totalSize.toLongLong() + preMigrationDownloaded;
189     pauseNotificationHandling();
190     // emit readyRead before downloadProgress incase this will cause events to be
191     // processed and we get into a recursive call (as in QProgressDialog).
192     emit q->readyRead();
193     emit q->downloadProgress(bytesDownloaded,
194                              totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong());
195     resumeNotificationHandling();
196 }
197
198 void QNetworkReplyImplPrivate::_q_copyReadChannelFinished()
199 {
200     _q_copyReadyRead();
201 }
202
203 void QNetworkReplyImplPrivate::_q_bufferOutgoingDataFinished()
204 {
205     Q_Q(QNetworkReplyImpl);
206
207     // make sure this is only called once, ever.
208     //_q_bufferOutgoingData may call it or the readChannelFinished emission
209     if (state != Buffering)
210         return;
211
212     // disconnect signals
213     QObject::disconnect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData()));
214     QObject::disconnect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished()));
215
216     // finally, start the request
217     QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
218 }
219
220 void QNetworkReplyImplPrivate::_q_bufferOutgoingData()
221 {
222     Q_Q(QNetworkReplyImpl);
223
224     if (!outgoingDataBuffer) {
225         // first call, create our buffer
226         outgoingDataBuffer = QSharedPointer<QRingBuffer>(new QRingBuffer());
227
228         QObject::connect(outgoingData, SIGNAL(readyRead()), q, SLOT(_q_bufferOutgoingData()));
229         QObject::connect(outgoingData, SIGNAL(readChannelFinished()), q, SLOT(_q_bufferOutgoingDataFinished()));
230     }
231
232     qint64 bytesBuffered = 0;
233     qint64 bytesToBuffer = 0;
234
235     // read data into our buffer
236     forever {
237         bytesToBuffer = outgoingData->bytesAvailable();
238         // unknown? just try 2 kB, this also ensures we always try to read the EOF
239         if (bytesToBuffer <= 0)
240             bytesToBuffer = 2*1024;
241
242         char *dst = outgoingDataBuffer->reserve(bytesToBuffer);
243         bytesBuffered = outgoingData->read(dst, bytesToBuffer);
244
245         if (bytesBuffered == -1) {
246             // EOF has been reached.
247             outgoingDataBuffer->chop(bytesToBuffer);
248
249             _q_bufferOutgoingDataFinished();
250             break;
251         } else if (bytesBuffered == 0) {
252             // nothing read right now, just wait until we get called again
253             outgoingDataBuffer->chop(bytesToBuffer);
254
255             break;
256         } else {
257             // don't break, try to read() again
258             outgoingDataBuffer->chop(bytesToBuffer - bytesBuffered);
259         }
260     }
261 }
262
263 #ifndef QT_NO_BEARERMANAGEMENT
264 void QNetworkReplyImplPrivate::_q_networkSessionConnected()
265 {
266     Q_Q(QNetworkReplyImpl);
267
268     if (manager.isNull())
269         return;
270
271     QNetworkSession *session = manager->d_func()->networkSession.data();
272     if (!session)
273         return;
274
275     if (session->state() != QNetworkSession::Connected)
276         return;
277
278     #ifndef QT_NO_NETWORKPROXY
279         // Re-set proxies here as new session might have changed them
280         proxyList = manager->d_func()->queryProxy(QNetworkProxyQuery(request.url()));
281     #endif
282
283     switch (state) {
284     case QNetworkReplyImplPrivate::Buffering:
285     case QNetworkReplyImplPrivate::Working:
286     case QNetworkReplyImplPrivate::Reconnecting:
287         // Migrate existing downloads to new network connection.
288         migrateBackend();
289         break;
290     case QNetworkReplyImplPrivate::WaitingForSession:
291         // Start waiting requests.
292         QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
293         break;
294     default:
295         ;
296     }
297 }
298
299 void QNetworkReplyImplPrivate::_q_networkSessionFailed()
300 {
301     // Abort waiting and working replies.
302     if (state == WaitingForSession || state == Working) {
303         state = Working;
304         error(QNetworkReplyImpl::UnknownNetworkError,
305               QCoreApplication::translate("QNetworkReply", "Network session error."));
306         finished();
307     }
308 }
309 #endif
310
311 void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const QNetworkRequest &req,
312                                      QIODevice *data)
313 {
314     Q_Q(QNetworkReplyImpl);
315
316     outgoingData = data;
317     request = req;
318     url = request.url();
319     operation = op;
320
321     q->QIODevice::open(QIODevice::ReadOnly);
322     // Internal code that does a HTTP reply for the synchronous Ajax
323     // in QtWebKit.
324     QVariant synchronousHttpAttribute = req.attribute(
325             static_cast<QNetworkRequest::Attribute>(QNetworkRequest::SynchronousRequestAttribute));
326     // The synchronous HTTP is a corner case, we will put all upload data in one big QByteArray in the outgoingDataBuffer.
327     // Yes, this is not the most efficient thing to do, but on the other hand synchronous XHR needs to die anyway.
328     if (synchronousHttpAttribute.toBool() && outgoingData) {
329         outgoingDataBuffer = QSharedPointer<QRingBuffer>(new QRingBuffer());
330         qint64 previousDataSize = 0;
331         do {
332             previousDataSize = outgoingDataBuffer->size();
333             outgoingDataBuffer->append(outgoingData->readAll());
334         } while (outgoingDataBuffer->size() != previousDataSize);
335     }
336
337     if (backend)
338         backend->setSynchronous(synchronousHttpAttribute.toBool());
339
340
341     if (outgoingData && backend && !backend->isSynchronous()) {
342         // there is data to be uploaded, e.g. HTTP POST.
343
344         if (!backend->needsResetableUploadData() || !outgoingData->isSequential()) {
345             // backend does not need upload buffering or
346             // fixed size non-sequential
347             // just start the operation
348             QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
349         } else {
350             bool bufferingDisallowed =
351                     req.attribute(QNetworkRequest::DoNotBufferUploadDataAttribute,
352                                   false).toBool();
353
354             if (bufferingDisallowed) {
355                 // if a valid content-length header for the request was supplied, we can disable buffering
356                 // if not, we will buffer anyway
357                 if (req.header(QNetworkRequest::ContentLengthHeader).isValid()) {
358                     QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
359                 } else {
360                     state = Buffering;
361                     QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection);
362                 }
363             } else {
364                 // _q_startOperation will be called when the buffering has finished.
365                 state = Buffering;
366                 QMetaObject::invokeMethod(q, "_q_bufferOutgoingData", Qt::QueuedConnection);
367             }
368         }
369     } else {
370         // for HTTP, we want to send out the request as fast as possible to the network, without
371         // invoking methods in a QueuedConnection
372 #ifndef QT_NO_HTTP
373         if (qobject_cast<QNetworkAccessHttpBackend *>(backend) || (backend && backend->isSynchronous())) {
374             _q_startOperation();
375         } else {
376             QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
377         }
378 #else
379         if (backend && backend->isSynchronous())
380             _q_startOperation();
381         else
382             QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
383 #endif // QT_NO_HTTP
384         }
385 }
386
387 void QNetworkReplyImplPrivate::backendNotify(InternalNotifications notification)
388 {
389     Q_Q(QNetworkReplyImpl);
390     if (!pendingNotifications.contains(notification))
391         pendingNotifications.enqueue(notification);
392
393     if (pendingNotifications.size() == 1)
394         QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated));
395 }
396
397 void QNetworkReplyImplPrivate::handleNotifications()
398 {
399     if (notificationHandlingPaused)
400         return;
401
402     NotificationQueue current = pendingNotifications;
403     pendingNotifications.clear();
404
405     if (state != Working)
406         return;
407
408     while (state == Working && !current.isEmpty()) {
409         InternalNotifications notification = current.dequeue();
410         switch (notification) {
411         case NotifyDownstreamReadyWrite:
412             if (copyDevice)
413                 _q_copyReadyRead();
414             else
415                 backend->downstreamReadyWrite();
416             break;
417
418         case NotifyCloseDownstreamChannel:
419             backend->closeDownstreamChannel();
420             break;
421
422         case NotifyCopyFinished: {
423             QIODevice *dev = copyDevice;
424             copyDevice = 0;
425             backend->copyFinished(dev);
426             break;
427         }
428         }
429     }
430 }
431
432 // Do not handle the notifications while we are emitting downloadProgress
433 // or readyRead
434 void QNetworkReplyImplPrivate::pauseNotificationHandling()
435 {
436     notificationHandlingPaused = true;
437 }
438
439 // Resume notification handling
440 void QNetworkReplyImplPrivate::resumeNotificationHandling()
441 {
442     Q_Q(QNetworkReplyImpl);
443     notificationHandlingPaused = false;
444     if (pendingNotifications.size() >= 1)
445         QCoreApplication::postEvent(q, new QEvent(QEvent::NetworkReplyUpdated));
446 }
447
448 QAbstractNetworkCache *QNetworkReplyImplPrivate::networkCache() const
449 {
450     if (!backend)
451         return 0;
452     return backend->networkCache();
453 }
454
455 void QNetworkReplyImplPrivate::createCache()
456 {
457     // check if we can save and if we're allowed to
458     if (!networkCache()
459         || !request.attribute(QNetworkRequest::CacheSaveControlAttribute, true).toBool()
460         || request.attribute(QNetworkRequest::CacheLoadControlAttribute,
461                              QNetworkRequest::PreferNetwork).toInt()
462             == QNetworkRequest::AlwaysNetwork)
463         return;
464     cacheEnabled = true;
465 }
466
467 bool QNetworkReplyImplPrivate::isCachingEnabled() const
468 {
469     return (cacheEnabled && networkCache() != 0);
470 }
471
472 void QNetworkReplyImplPrivate::setCachingEnabled(bool enable)
473 {
474     if (!enable && !cacheEnabled)
475         return;                 // nothing to do
476     if (enable && cacheEnabled)
477         return;                 // nothing to do either!
478
479     if (enable) {
480         if (bytesDownloaded) {
481             // refuse to enable in this case
482             qCritical("QNetworkReplyImpl: backend error: caching was enabled after some bytes had been written");
483             return;
484         }
485
486         createCache();
487     } else {
488         // someone told us to turn on, then back off?
489         // ok... but you should make up your mind
490         qDebug("QNetworkReplyImpl: setCachingEnabled(true) called after setCachingEnabled(false) -- "
491                "backend %s probably needs to be fixed",
492                backend->metaObject()->className());
493         networkCache()->remove(url);
494         cacheSaveDevice = 0;
495         cacheEnabled = false;
496     }
497 }
498
499 void QNetworkReplyImplPrivate::completeCacheSave()
500 {
501     if (cacheEnabled && errorCode != QNetworkReplyImpl::NoError) {
502         networkCache()->remove(url);
503     } else if (cacheEnabled && cacheSaveDevice) {
504         networkCache()->insert(cacheSaveDevice);
505     }
506     cacheSaveDevice = 0;
507     cacheEnabled = false;
508 }
509
510 void QNetworkReplyImplPrivate::emitUploadProgress(qint64 bytesSent, qint64 bytesTotal)
511 {
512     Q_Q(QNetworkReplyImpl);
513     bytesUploaded = bytesSent;
514     pauseNotificationHandling();
515     emit q->uploadProgress(bytesSent, bytesTotal);
516     resumeNotificationHandling();
517 }
518
519
520 qint64 QNetworkReplyImplPrivate::nextDownstreamBlockSize() const
521 {
522     enum { DesiredBufferSize = 32 * 1024 };
523     if (readBufferMaxSize == 0)
524         return DesiredBufferSize;
525
526     return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount());
527 }
528
529 void QNetworkReplyImplPrivate::initCacheSaveDevice()
530 {
531     Q_Q(QNetworkReplyImpl);
532
533     // The disk cache does not support partial content, so don't even try to
534     // save any such content into the cache.
535     if (q->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 206) {
536         cacheEnabled = false;
537         return;
538     }
539
540     // save the meta data
541     QNetworkCacheMetaData metaData;
542     metaData.setUrl(url);
543     metaData = backend->fetchCacheMetaData(metaData);
544
545     // save the redirect request also in the cache
546     QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute);
547     if (redirectionTarget.isValid()) {
548         QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes();
549         attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
550         metaData.setAttributes(attributes);
551     }
552
553     cacheSaveDevice = networkCache()->prepare(metaData);
554
555     if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) {
556         if (cacheSaveDevice && !cacheSaveDevice->isOpen())
557             qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- "
558                   "class %s probably needs to be fixed",
559                   networkCache()->metaObject()->className());
560
561         networkCache()->remove(url);
562         cacheSaveDevice = 0;
563         cacheEnabled = false;
564     }
565 }
566
567 // we received downstream data and send this to the cache
568 // and to our readBuffer (which in turn gets read by the user of QNetworkReply)
569 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data)
570 {
571     Q_Q(QNetworkReplyImpl);
572     if (!q->isOpen())
573         return;
574
575     if (cacheEnabled && !cacheSaveDevice) {
576         initCacheSaveDevice();
577     }
578
579     qint64 bytesWritten = 0;
580     for (int i = 0; i < data.bufferCount(); i++) {
581         QByteArray const &item = data[i];
582
583         if (cacheSaveDevice)
584             cacheSaveDevice->write(item.constData(), item.size());
585         readBuffer.append(item);
586
587         bytesWritten += item.size();
588     }
589     data.clear();
590
591     bytesDownloaded += bytesWritten;
592     lastBytesDownloaded = bytesDownloaded;
593
594     appendDownstreamDataSignalEmissions();
595 }
596
597 void QNetworkReplyImplPrivate::appendDownstreamDataSignalEmissions()
598 {
599     Q_Q(QNetworkReplyImpl);
600
601     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
602     if (preMigrationDownloaded != Q_INT64_C(-1))
603         totalSize = totalSize.toLongLong() + preMigrationDownloaded;
604     pauseNotificationHandling();
605     // important: At the point of this readyRead(), the data parameter list must be empty,
606     // else implicit sharing will trigger memcpy when the user is reading data!
607     emit q->readyRead();
608     // emit readyRead before downloadProgress incase this will cause events to be
609     // processed and we get into a recursive call (as in QProgressDialog).
610     emit q->downloadProgress(bytesDownloaded,
611                              totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong());
612
613     resumeNotificationHandling();
614     // do we still have room in the buffer?
615     if (nextDownstreamBlockSize() > 0)
616         backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
617 }
618
619 // this is used when it was fetched from the cache, right?
620 void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data)
621 {
622     Q_Q(QNetworkReplyImpl);
623     if (!q->isOpen())
624         return;
625
626     // read until EOF from data
627     if (copyDevice) {
628         qCritical("QNetworkReplyImpl: copy from QIODevice already in progress -- "
629                   "backend probly needs to be fixed");
630         return;
631     }
632
633     copyDevice = data;
634     q->connect(copyDevice, SIGNAL(readyRead()), SLOT(_q_copyReadyRead()));
635     q->connect(copyDevice, SIGNAL(readChannelFinished()), SLOT(_q_copyReadChannelFinished()));
636
637     // start the copy:
638     _q_copyReadyRead();
639 }
640
641 void QNetworkReplyImplPrivate::appendDownstreamData(const QByteArray &data)
642 {
643     Q_UNUSED(data)
644     // TODO implement
645
646     // TODO call
647
648     qFatal("QNetworkReplyImplPrivate::appendDownstreamData not implemented");
649 }
650
651 static void downloadBufferDeleter(char *ptr)
652 {
653     delete[] ptr;
654 }
655
656 char* QNetworkReplyImplPrivate::getDownloadBuffer(qint64 size)
657 {
658     Q_Q(QNetworkReplyImpl);
659
660     if (!downloadBuffer) {
661         // We are requested to create it
662         // Check attribute() if allocating a buffer of that size can be allowed
663         QVariant bufferAllocationPolicy = request.attribute(QNetworkRequest::MaximumDownloadBufferSizeAttribute);
664         if (bufferAllocationPolicy.isValid() && bufferAllocationPolicy.toLongLong() >= size) {
665             downloadBufferCurrentSize = 0;
666             downloadBufferMaximumSize = size;
667             downloadBuffer = new char[downloadBufferMaximumSize]; // throws if allocation fails
668             downloadBufferPointer = QSharedPointer<char>(downloadBuffer, downloadBufferDeleter);
669
670             q->setAttribute(QNetworkRequest::DownloadBufferAttribute, qVariantFromValue<QSharedPointer<char> > (downloadBufferPointer));
671         }
672     }
673
674     return downloadBuffer;
675 }
676
677 void QNetworkReplyImplPrivate::setDownloadBuffer(QSharedPointer<char> sp, qint64 size)
678 {
679     Q_Q(QNetworkReplyImpl);
680
681     downloadBufferPointer = sp;
682     downloadBuffer = downloadBufferPointer.data();
683     downloadBufferCurrentSize = 0;
684     downloadBufferMaximumSize = size;
685     q->setAttribute(QNetworkRequest::DownloadBufferAttribute, qVariantFromValue<QSharedPointer<char> > (downloadBufferPointer));
686 }
687
688
689 void QNetworkReplyImplPrivate::appendDownstreamDataDownloadBuffer(qint64 bytesReceived, qint64 bytesTotal)
690 {
691     Q_Q(QNetworkReplyImpl);
692     if (!q->isOpen())
693         return;
694
695     if (cacheEnabled && !cacheSaveDevice)
696         initCacheSaveDevice();
697
698     if (cacheSaveDevice && bytesReceived == bytesTotal) {
699 //        if (lastBytesDownloaded == -1)
700 //            lastBytesDownloaded = 0;
701 //        cacheSaveDevice->write(downloadBuffer + lastBytesDownloaded, bytesReceived - lastBytesDownloaded);
702
703         // Write everything in one go if we use a download buffer. might be more performant.
704         cacheSaveDevice->write(downloadBuffer, bytesTotal);
705     }
706
707     bytesDownloaded = bytesReceived;
708     lastBytesDownloaded = bytesReceived;
709
710     downloadBufferCurrentSize = bytesReceived;
711
712     // Only emit readyRead when actual data is there
713     // emit readyRead before downloadProgress incase this will cause events to be
714     // processed and we get into a recursive call (as in QProgressDialog).
715     if (bytesDownloaded > 0)
716         emit q->readyRead();
717     emit q->downloadProgress(bytesDownloaded, bytesTotal);
718 }
719
720 void QNetworkReplyImplPrivate::finished()
721 {
722     Q_Q(QNetworkReplyImpl);
723
724     if (state == Finished || state == Aborted || state == WaitingForSession)
725         return;
726
727     pauseNotificationHandling();
728     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
729     if (preMigrationDownloaded != Q_INT64_C(-1))
730         totalSize = totalSize.toLongLong() + preMigrationDownloaded;
731
732     if (!manager.isNull()) {
733 #ifndef QT_NO_BEARERMANAGEMENT
734         QNetworkSession *session = manager->d_func()->networkSession.data();
735         if (session && session->state() == QNetworkSession::Roaming &&
736             state == Working && errorCode != QNetworkReply::OperationCanceledError) {
737             // only content with a known size will fail with a temporary network failure error
738             if (!totalSize.isNull()) {
739                 if (bytesDownloaded != totalSize) {
740                     if (migrateBackend()) {
741                         // either we are migrating or the request is finished/aborted
742                         if (state == Reconnecting || state == WaitingForSession) {
743                             resumeNotificationHandling();
744                             return; // exit early if we are migrating.
745                         }
746                     } else {
747                         error(QNetworkReply::TemporaryNetworkFailureError,
748                               QNetworkReply::tr("Temporary network failure."));
749                     }
750                 }
751             }
752         }
753 #endif
754     }
755     resumeNotificationHandling();
756
757     state = Finished;
758     q->setFinished(true);
759
760     pendingNotifications.clear();
761
762     pauseNotificationHandling();
763     if (totalSize.isNull() || totalSize == -1) {
764         emit q->downloadProgress(bytesDownloaded, bytesDownloaded);
765     }
766
767     if (bytesUploaded == -1 && (outgoingData || outgoingDataBuffer))
768         emit q->uploadProgress(0, 0);
769     resumeNotificationHandling();
770
771     // if we don't know the total size of or we received everything save the cache
772     if (totalSize.isNull() || totalSize == -1 || bytesDownloaded == totalSize)
773         completeCacheSave();
774
775     // note: might not be a good idea, since users could decide to delete us
776     // which would delete the backend too...
777     // maybe we should protect the backend
778     pauseNotificationHandling();
779     emit q->readChannelFinished();
780     emit q->finished();
781     resumeNotificationHandling();
782 }
783
784 void QNetworkReplyImplPrivate::error(QNetworkReplyImpl::NetworkError code, const QString &errorMessage)
785 {
786     Q_Q(QNetworkReplyImpl);
787     // Can't set and emit multiple errors.
788     if (errorCode != QNetworkReply::NoError) {
789         qWarning() << "QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.";
790         return;
791     }
792
793     errorCode = code;
794     q->setErrorString(errorMessage);
795
796     // note: might not be a good idea, since users could decide to delete us
797     // which would delete the backend too...
798     // maybe we should protect the backend
799     emit q->error(code);
800 }
801
802 void QNetworkReplyImplPrivate::metaDataChanged()
803 {
804     Q_Q(QNetworkReplyImpl);
805     // 1. do we have cookies?
806     // 2. are we allowed to set them?
807     if (cookedHeaders.contains(QNetworkRequest::SetCookieHeader) && !manager.isNull()
808         && (static_cast<QNetworkRequest::LoadControl>
809             (request.attribute(QNetworkRequest::CookieSaveControlAttribute,
810                                QNetworkRequest::Automatic).toInt()) == QNetworkRequest::Automatic)) {
811         QList<QNetworkCookie> cookies =
812             qvariant_cast<QList<QNetworkCookie> >(cookedHeaders.value(QNetworkRequest::SetCookieHeader));
813         QNetworkCookieJar *jar = manager->cookieJar();
814         if (jar)
815             jar->setCookiesFromUrl(cookies, url);
816     }
817     emit q->metaDataChanged();
818 }
819
820 void QNetworkReplyImplPrivate::redirectionRequested(const QUrl &target)
821 {
822     attributes.insert(QNetworkRequest::RedirectionTargetAttribute, target);
823 }
824
825 void QNetworkReplyImplPrivate::sslErrors(const QList<QSslError> &errors)
826 {
827 #ifndef QT_NO_OPENSSL
828     Q_Q(QNetworkReplyImpl);
829     emit q->sslErrors(errors);
830 #else
831     Q_UNUSED(errors);
832 #endif
833 }
834
835 QNetworkReplyImpl::QNetworkReplyImpl(QObject *parent)
836     : QNetworkReply(*new QNetworkReplyImplPrivate, parent)
837 {
838 }
839
840 QNetworkReplyImpl::~QNetworkReplyImpl()
841 {
842     Q_D(QNetworkReplyImpl);
843
844     // This code removes the data from the cache if it was prematurely aborted.
845     // See QNetworkReplyImplPrivate::completeCacheSave(), we disable caching there after the cache
846     // save had been properly finished. So if it is still enabled it means we got deleted/aborted.
847     if (d->isCachingEnabled())
848         d->networkCache()->remove(url());
849 }
850
851 void QNetworkReplyImpl::abort()
852 {
853     Q_D(QNetworkReplyImpl);
854     if (d->state == QNetworkReplyImplPrivate::Finished || d->state == QNetworkReplyImplPrivate::Aborted)
855         return;
856
857     // stop both upload and download
858     if (d->outgoingData)
859         disconnect(d->outgoingData, 0, this, 0);
860     if (d->copyDevice)
861         disconnect(d->copyDevice, 0, this, 0);
862
863     QNetworkReply::close();
864
865     if (d->state != QNetworkReplyImplPrivate::Finished) {
866         // call finished which will emit signals
867         d->error(OperationCanceledError, tr("Operation canceled"));
868         if (d->state == QNetworkReplyImplPrivate::WaitingForSession)
869             d->state = QNetworkReplyImplPrivate::Working;
870         d->finished();
871     }
872     d->state = QNetworkReplyImplPrivate::Aborted;
873
874     // finished may access the backend
875     if (d->backend) {
876         d->backend->deleteLater();
877         d->backend = 0;
878     }
879 }
880
881 void QNetworkReplyImpl::close()
882 {
883     Q_D(QNetworkReplyImpl);
884     if (d->state == QNetworkReplyImplPrivate::Aborted ||
885         d->state == QNetworkReplyImplPrivate::Finished)
886         return;
887
888     // stop the download
889     if (d->backend)
890         d->backend->closeDownstreamChannel();
891     if (d->copyDevice)
892         disconnect(d->copyDevice, 0, this, 0);
893
894     QNetworkReply::close();
895
896     // call finished which will emit signals
897     d->error(OperationCanceledError, tr("Operation canceled"));
898     d->finished();
899 }
900
901 bool QNetworkReplyImpl::canReadLine () const
902 {
903     Q_D(const QNetworkReplyImpl);
904     return QNetworkReply::canReadLine() || d->readBuffer.canReadLine();
905 }
906
907
908 /*!
909     Returns the number of bytes available for reading with
910     QIODevice::read(). The number of bytes available may grow until
911     the finished() signal is emitted.
912 */
913 qint64 QNetworkReplyImpl::bytesAvailable() const
914 {
915     // Special case for the "zero copy" download buffer
916     Q_D(const QNetworkReplyImpl);
917     if (d->downloadBuffer) {
918         qint64 maxAvail = d->downloadBufferCurrentSize - d->downloadBufferReadPosition;
919         return QNetworkReply::bytesAvailable() + maxAvail;
920     }
921
922     return QNetworkReply::bytesAvailable() + d_func()->readBuffer.byteAmount();
923 }
924
925 void QNetworkReplyImpl::setReadBufferSize(qint64 size)
926 {
927     Q_D(QNetworkReplyImpl);
928     if (size > d->readBufferMaxSize &&
929         size > d->readBuffer.byteAmount())
930         d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
931
932     QNetworkReply::setReadBufferSize(size);
933
934     if (d->backend)
935         d->backend->setDownstreamLimited(d->readBufferMaxSize > 0);
936 }
937
938 #ifndef QT_NO_OPENSSL
939 QSslConfiguration QNetworkReplyImpl::sslConfigurationImplementation() const
940 {
941     Q_D(const QNetworkReplyImpl);
942     QSslConfiguration config;
943     if (d->backend)
944         d->backend->fetchSslConfiguration(config);
945     return config;
946 }
947
948 void QNetworkReplyImpl::setSslConfigurationImplementation(const QSslConfiguration &config)
949 {
950     Q_D(QNetworkReplyImpl);
951     if (d->backend && !config.isNull())
952         d->backend->setSslConfiguration(config);
953 }
954
955 void QNetworkReplyImpl::ignoreSslErrors()
956 {
957     Q_D(QNetworkReplyImpl);
958     if (d->backend)
959         d->backend->ignoreSslErrors();
960 }
961
962 void QNetworkReplyImpl::ignoreSslErrorsImplementation(const QList<QSslError> &errors)
963 {
964     Q_D(QNetworkReplyImpl);
965     if (d->backend)
966         d->backend->ignoreSslErrors(errors);
967 }
968 #endif  // QT_NO_OPENSSL
969
970 /*!
971     \internal
972 */
973 qint64 QNetworkReplyImpl::readData(char *data, qint64 maxlen)
974 {
975     Q_D(QNetworkReplyImpl);
976
977     // Special case code if we have the "zero copy" download buffer
978     if (d->downloadBuffer) {
979         qint64 maxAvail = qMin<qint64>(d->downloadBufferCurrentSize - d->downloadBufferReadPosition, maxlen);
980         if (maxAvail == 0)
981             return d->state == QNetworkReplyImplPrivate::Finished ? -1 : 0;
982         // FIXME what about "Aborted" state?
983         qMemCopy(data, d->downloadBuffer + d->downloadBufferReadPosition, maxAvail);
984         d->downloadBufferReadPosition += maxAvail;
985         return maxAvail;
986     }
987
988
989     if (d->readBuffer.isEmpty())
990         return d->state == QNetworkReplyImplPrivate::Finished ? -1 : 0;
991     // FIXME what about "Aborted" state?
992
993     d->backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite);
994     if (maxlen == 1) {
995         // optimization for getChar()
996         *data = d->readBuffer.getChar();
997         return 1;
998     }
999
1000     maxlen = qMin<qint64>(maxlen, d->readBuffer.byteAmount());
1001     return d->readBuffer.read(data, maxlen);
1002 }
1003
1004 /*!
1005    \internal Reimplemented for internal purposes
1006 */
1007 bool QNetworkReplyImpl::event(QEvent *e)
1008 {
1009     if (e->type() == QEvent::NetworkReplyUpdated) {
1010         d_func()->handleNotifications();
1011         return true;
1012     }
1013
1014     return QObject::event(e);
1015 }
1016
1017 /*
1018     Migrates the backend of the QNetworkReply to a new network connection if required.  Returns
1019     true if the reply is migrated or it is not required; otherwise returns false.
1020 */
1021 bool QNetworkReplyImplPrivate::migrateBackend()
1022 {
1023     Q_Q(QNetworkReplyImpl);
1024
1025     // Network reply is already finished or aborted, don't need to migrate.
1026     if (state == Finished || state == Aborted)
1027         return true;
1028
1029     // Request has outgoing data, not migrating.
1030     if (outgoingData)
1031         return false;
1032
1033     // Request is serviced from the cache, don't need to migrate.
1034     if (copyDevice)
1035         return true;
1036
1037     // Backend does not support resuming download.
1038     if (backend && !backend->canResume())
1039         return false;
1040
1041     state = QNetworkReplyImplPrivate::Reconnecting;
1042
1043     if (backend) {
1044         delete backend;
1045         backend = 0;
1046     }
1047
1048     cookedHeaders.clear();
1049     rawHeaders.clear();
1050
1051     preMigrationDownloaded = bytesDownloaded;
1052
1053     backend = manager->d_func()->findBackend(operation, request);
1054
1055     if (backend) {
1056         backend->setParent(q);
1057         backend->reply = this;
1058         backend->setResumeOffset(bytesDownloaded);
1059     }
1060
1061 #ifndef QT_NO_HTTP
1062     if (qobject_cast<QNetworkAccessHttpBackend *>(backend)) {
1063         _q_startOperation();
1064     } else {
1065         QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
1066     }
1067 #else
1068     QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection);
1069 #endif // QT_NO_HTTP
1070
1071     return true;
1072 }
1073
1074 #ifndef QT_NO_BEARERMANAGEMENT
1075 QDisabledNetworkReply::QDisabledNetworkReply(QObject *parent,
1076                                              const QNetworkRequest &req,
1077                                              QNetworkAccessManager::Operation op)
1078 :   QNetworkReply(parent)
1079 {
1080     setRequest(req);
1081     setUrl(req.url());
1082     setOperation(op);
1083
1084     qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError");
1085
1086     QString msg = QCoreApplication::translate("QNetworkAccessManager",
1087                                               "Network access is disabled.");
1088     setError(UnknownNetworkError, msg);
1089
1090     QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection,
1091         Q_ARG(QNetworkReply::NetworkError, UnknownNetworkError));
1092     QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
1093 }
1094
1095 QDisabledNetworkReply::~QDisabledNetworkReply()
1096 {
1097 }
1098 #endif
1099
1100 QT_END_NAMESPACE
1101
1102 #include "moc_qnetworkreplyimpl_p.cpp"
1103