void tst_QNetworkCacheMetaData::stream() { QNetworkCacheMetaData data; data.setUrl(QUrl(EXAMPLE_URL)); QNetworkCacheMetaData::RawHeaderList headers; headers.append(QNetworkCacheMetaData::RawHeader("foo", "Bar")); data.setRawHeaders(headers); data.setLastModified(QDateTime::currentDateTime()); data.setExpirationDate(QDateTime::currentDateTime()); data.setSaveToDisk(false); QBuffer buffer; buffer.open(QIODevice::ReadWrite); QDataStream stream(&buffer); stream << data; buffer.seek(0); QNetworkCacheMetaData data2; stream >> data2; QCOMPARE(data2, data); }
void QNetworkReplyImplPrivate::initCacheSaveDevice() { Q_Q(QNetworkReplyImpl); // The disk cache does not support partial content, so don't even try to // save any such content into the cache. if (q->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 206) { cacheEnabled = false; return; } // save the meta data QNetworkCacheMetaData metaData; metaData.setUrl(url); metaData = backend->fetchCacheMetaData(metaData); // save the redirect request also in the cache QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute); if (redirectionTarget.isValid()) { QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes(); attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget); metaData.setAttributes(attributes); } cacheSaveDevice = networkCache()->prepare(metaData); if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) { if (cacheSaveDevice && !cacheSaveDevice->isOpen()) qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- " "class %s probably needs to be fixed", networkCache()->metaObject()->className()); networkCache()->remove(url); cacheSaveDevice = 0; cacheEnabled = false; } else { q->connect(networkCache(), SIGNAL(destroyed()), SLOT(_q_cacheDestroyed())); } }
bool saveToCache(const QUrl& url, const QByteArray& file) { if (auto cache = NetworkAccessManager::getInstance().cache()) { if (!cache->metaData(url).isValid()) { QNetworkCacheMetaData metaData; metaData.setUrl(url); metaData.setSaveToDisk(true); metaData.setLastModified(QDateTime::currentDateTime()); metaData.setExpirationDate(QDateTime()); // Never expires // ioDevice is managed by the cache and should either be passed back to insert or remove! if (auto ioDevice = cache->prepare(metaData)) { ioDevice->write(file); cache->insert(ioDevice); qCDebug(asset_client) << url.toDisplayString() << "saved to disk cache"; return true; } qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache."; } } else { qCWarning(asset_client) << "No disk cache to save assets to."; } return false; }
// we received downstream data and send this to the cache // and to our readBuffer (which in turn gets read by the user of QNetworkReply) void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data) { Q_Q(QNetworkReplyImpl); if (!q->isOpen()) return; if (cacheEnabled && !cacheSaveDevice) { // save the meta data QNetworkCacheMetaData metaData; metaData.setUrl(url); metaData = backend->fetchCacheMetaData(metaData); // save the redirect request also in the cache QVariant redirectionTarget = q->attribute(QNetworkRequest::RedirectionTargetAttribute); if (redirectionTarget.isValid()) { QNetworkCacheMetaData::AttributesMap attributes = metaData.attributes(); attributes.insert(QNetworkRequest::RedirectionTargetAttribute, redirectionTarget); metaData.setAttributes(attributes); } cacheSaveDevice = networkCache()->prepare(metaData); if (!cacheSaveDevice || (cacheSaveDevice && !cacheSaveDevice->isOpen())) { if (cacheSaveDevice && !cacheSaveDevice->isOpen()) qCritical("QNetworkReplyImpl: network cache returned a device that is not open -- " "class %s probably needs to be fixed", networkCache()->metaObject()->className()); networkCache()->remove(url); cacheSaveDevice = 0; cacheEnabled = false; } } qint64 bytesWritten = 0; for (int i = 0; i < data.bufferCount(); i++) { QByteArray item = data[i]; if (cacheSaveDevice) cacheSaveDevice->write(item.constData(), item.size()); readBuffer.append(item); bytesWritten += item.size(); } data.clear(); bytesDownloaded += bytesWritten; lastBytesDownloaded = bytesDownloaded; QPointer<QNetworkReplyImpl> qq = q; QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); if (preMigrationDownloaded != Q_INT64_C(-1)) totalSize = totalSize.toLongLong() + preMigrationDownloaded; pauseNotificationHandling(); emit q->downloadProgress(bytesDownloaded, totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); // important: At the point of this readyRead(), the data parameter list must be empty, // else implicit sharing will trigger memcpy when the user is reading data! emit q->readyRead(); // hopefully we haven't been deleted here if (!qq.isNull()) { resumeNotificationHandling(); // do we still have room in the buffer? if (nextDownstreamBlockSize() > 0) backendNotify(QNetworkReplyImplPrivate::NotifyDownstreamReadyWrite); } }