src/network/access/qnetworkreplyimpl.cpp
changeset 23 89e065397ea6
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
22:79de32ba3296 23:89e065397ea6
   401         return DesiredBufferSize;
   401         return DesiredBufferSize;
   402 
   402 
   403     return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount());
   403     return qMax<qint64>(0, readBufferMaxSize - readBuffer.byteAmount());
   404 }
   404 }
   405 
   405 
       
   406 void QNetworkReplyImplPrivate::initCacheSaveDevice()
       
   407 {
       
   408     Q_Q(QNetworkReplyImpl);
       
   409 
       
   410     // save the meta data
       
   411     QNetworkCacheMetaData metaData;
       
   412     metaData.setUrl(url);
       
   413     metaData = backend->fetchCacheMetaData(metaData);
       
   414 
       
   415     // save the redirect request also in the cache
       
   416     QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute);
       
   417     if (redirectionTarget.isValid()) {
       
   418         QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes();
       
   419         attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget);
       
   420         metaData.setAttributes(attributes);
       
   421     }
       
   422 
       
   423     cacheSaveDevice = networkCache()->prepare(metaData);
       
   424 
       
   425     if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) {
       
   426         if (cacheSaveDevice && !cacheSaveDevice->isOpen())
       
   427             qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- "
       
   428                   "class %s probably needs to be fixed",
       
   429                   networkCache()->metaObject()->className());
       
   430 
       
   431         networkCache()->remove(url);
       
   432         cacheSaveDevice = 0;
       
   433         cacheEnabled = false;
       
   434     }
       
   435 }
       
   436 
   406 // we received downstream data and send this to the cache
   437 // 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)
   438 // and to our readBuffer (which in turn gets read by the user of QNetworkReply)
   408 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data)
   439 void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data)
   409 {
   440 {
   410     Q_Q(QNetworkReplyImpl);
   441     Q_Q(QNetworkReplyImpl);
   411     if (!q->isOpen())
   442     if (!q->isOpen())
   412         return;
   443         return;
   413 
   444 
   414     if (cacheEnabled && !cacheSaveDevice) {
   445     if (cacheEnabled && !cacheSaveDevice) {
   415         // save the meta data
   446         initCacheSaveDevice();
   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     }
   447     }
   441 
   448 
   442     qint64 bytesWritten = 0;
   449     qint64 bytesWritten = 0;
   443     for (int i = 0; i < data.bufferCount(); i++) {
   450     for (int i = 0; i < data.bufferCount(); i++) {
   444         QByteArray item = data[i];
   451         QByteArray const &item = data[i];
   445 
   452 
   446         if (cacheSaveDevice)
   453         if (cacheSaveDevice)
   447             cacheSaveDevice->write(item.constData(), item.size());
   454             cacheSaveDevice->write(item.constData(), item.size());
   448         readBuffer.append(item);
   455         readBuffer.append(item);
   449 
   456 
   451     }
   458     }
   452     data.clear();
   459     data.clear();
   453 
   460 
   454     bytesDownloaded += bytesWritten;
   461     bytesDownloaded += bytesWritten;
   455     lastBytesDownloaded = bytesDownloaded;
   462     lastBytesDownloaded = bytesDownloaded;
       
   463 
       
   464     appendDownstreamDataSignalEmissions();
       
   465 }
       
   466 
       
   467 void QNetworkReplyImplPrivate::appendDownstreamDataSignalEmissions()
       
   468 {
       
   469     Q_Q(QNetworkReplyImpl);
   456 
   470 
   457     QPointer<QNetworkReplyImpl> qq = q;
   471     QPointer<QNetworkReplyImpl> qq = q;
   458 
   472 
   459     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
   473     QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader);
   460     pauseNotificationHandling();
   474     pauseNotificationHandling();
   493 
   507 
   494     // start the copy:
   508     // start the copy:
   495     _q_copyReadyRead();
   509     _q_copyReadyRead();
   496 }
   510 }
   497 
   511 
       
   512 void QNetworkReplyImplPrivate::appendDownstreamData(const QByteArray &data)
       
   513 {
       
   514     // TODO implement
       
   515 
       
   516     // TODO call
       
   517 
       
   518     qFatal("QNetworkReplyImplPrivate::appendDownstreamData not implemented");
       
   519 }
       
   520 
   498 void QNetworkReplyImplPrivate::finished()
   521 void QNetworkReplyImplPrivate::finished()
   499 {
   522 {
   500     Q_Q(QNetworkReplyImpl);
   523     Q_Q(QNetworkReplyImpl);
   501     if (state == Finished || state == Aborted)
   524     if (state == Finished || state == Aborted)
   502         return;
   525         return;
   578 }
   601 }
   579 
   602 
   580 QNetworkReplyImpl::~QNetworkReplyImpl()
   603 QNetworkReplyImpl::~QNetworkReplyImpl()
   581 {
   604 {
   582     Q_D(QNetworkReplyImpl);
   605     Q_D(QNetworkReplyImpl);
       
   606 
       
   607     // This code removes the data from the cache if it was prematurely aborted.
       
   608     // See QNetworkReplyImplPrivate::completeCacheSave(), we disable caching there after the cache
       
   609     // save had been properly finished. So if it is still enabled it means we got deleted/aborted.
   583     if (d->isCachingEnabled())
   610     if (d->isCachingEnabled())
   584         d->networkCache()->remove(url());
   611         d->networkCache()->remove(url());
       
   612 
   585     if (d->outgoingDataBuffer)
   613     if (d->outgoingDataBuffer)
   586         delete d->outgoingDataBuffer;
   614         delete d->outgoingDataBuffer;
   587 }
   615 }
   588 
   616 
   589 void QNetworkReplyImpl::abort()
   617 void QNetworkReplyImpl::abort()