HTTP cache backend: do not load resources that must be revalidated

The header field "Cache-Control: must-revalidate" is a strict
requirement for loading the resource from the server, and not reading it
from the cache without revalidating first. With this patch, PreferCache
will load such from the network instead of loading them from the cache,
and AlwaysCache will throw a ContentNotFound error.

Reviewed-by: Markus Goetz
Task-number: QTBUG-18983
This commit is contained in:
Peter Hartmann 2011-05-04 13:49:51 +02:00
parent 0c9f327102
commit 33ce5392e8

View File

@ -66,6 +66,7 @@ void QNetworkAccessCacheBackend::open()
QString msg = QCoreApplication::translate("QNetworkAccessCacheBackend", "Error opening %1")
.arg(this->url().toString());
error(QNetworkReply::ContentNotFoundError, msg);
} else {
setAttribute(QNetworkRequest::SourceIsFromCacheAttribute, true);
}
finished();
@ -85,14 +86,18 @@ bool QNetworkAccessCacheBackend::sendCacheContents()
QNetworkCacheMetaData::AttributesMap attributes = item.attributes();
setAttribute(QNetworkRequest::HttpStatusCodeAttribute, attributes.value(QNetworkRequest::HttpStatusCodeAttribute));
setAttribute(QNetworkRequest::HttpReasonPhraseAttribute, attributes.value(QNetworkRequest::HttpReasonPhraseAttribute));
setAttribute(QNetworkRequest::SourceIsFromCacheAttribute, true);
// set the raw headers
QNetworkCacheMetaData::RawHeaderList rawHeaders = item.rawHeaders();
QNetworkCacheMetaData::RawHeaderList::ConstIterator it = rawHeaders.constBegin(),
end = rawHeaders.constEnd();
for ( ; it != end; ++it)
for ( ; it != end; ++it) {
if (it->first.toLower() == "cache-control" &&
it->second.toLower().contains("must-revalidate")) {
return false;
}
setRawHeader(it->first, it->second);
}
// handle a possible redirect
QVariant redirectionTarget = attributes.value(QNetworkRequest::RedirectionTargetAttribute);