HTTP cache backend: do not load resources that must be revalidated
authorPeter Hartmann <peter.hartmann@nokia.com>
Wed, 4 May 2011 11:49:51 +0000 (13:49 +0200)
committerPeter Hartmann <peter.hartmann@nokia.com>
Wed, 4 May 2011 12:35:09 +0000 (14:35 +0200)
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

src/network/access/qnetworkaccesscachebackend.cpp

index 13f4cd9..c585848 100644 (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);