From 992baedb8bab4c64af10d75f43f9aec1eae54517 Mon Sep 17 00:00:00 2001 From: Dong-Heon Jung Date: Mon, 18 Aug 2014 12:58:56 +0900 Subject: QNetworkDiskCache: fix expiration calculation heuristic with Last-Modified time Heuristic with last-modified time in Qt has some problems. 1) Remove redundant expirationDate.isInvalid() check expirationDate.isInvalid is already checked. So I removed. 2) Add dateHeader.isInvalid() check The dateHeader is used in expiration calculation. I add invalid check for the dateHeader. *. The dateHeader is the origin server's Date 3) Change diff time calculation. The expirationDate is calculated with time diff. Previous calculation is // The lastModified is earlier than the currentDateTime. // The diff has negative value. int diff = currentDateTime.secsTo(lastModified); // The expirationDate is earlier than lastModified // , currentDateTime and dateHeader. expirationDate = lastModified.addSecs(diff / 10); *. currentDateTime: current time *. lastModified: last modified date in server It means that files are not cached with the heuristic. I changed diff calculation. int diff = lastModified.secsTo(dateHeader); freshness_lifetime = diff / 10; // RFC 2616 13.2.4 4) httpRequest.headerField setting If current_age is larger than 1 day, the cache MUST attach Warning 113. *. The current_age is value of age in header or elapsed time from dateHeader in Qt source code. Previous code does not check current_age is larger than 1 day correctly. // dt = 1970-01-01T00:00:00 + current_age dt.setTime_t(current_age); // currentDateTime is much bigger than 1970-01-01T00:00:00 if (dt.daysTo(currentDateTime) > 1) Task-number: QTBUG-40836 Change-Id: I4b00c3b287e6fafeea6b02681533fe75a198247e Reviewed-by: Jung Dong-Heon Reviewed-by: Peter Hartmann --- src/network/access/qnetworkreplyhttpimpl.cpp | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/network/access/qnetworkreplyhttpimpl.cpp b/src/network/access/qnetworkreplyhttpimpl.cpp index 920dfdbb38..367c92a460 100644 --- a/src/network/access/qnetworkreplyhttpimpl.cpp +++ b/src/network/access/qnetworkreplyhttpimpl.cpp @@ -560,23 +560,21 @@ bool QNetworkReplyHttpImplPrivate::loadFromCacheIfAllowed(QHttpNetworkRequest &h int resident_time = now - response_time; int current_age = corrected_initial_age + resident_time; + int freshness_lifetime = 0; + // RFC 2616 13.2.4 Expiration Calculations - if (!expirationDate.isValid()) { - if (lastModified.isValid()) { - int diff = currentDateTime.secsTo(lastModified); - expirationDate = lastModified.addSecs(diff / 10); - if (httpRequest.headerField("Warning").isEmpty()) { - QDateTime dt; - dt.setTime_t(current_age); - if (dt.daysTo(currentDateTime) > 1) - httpRequest.setHeaderField("Warning", "113"); - } + if (lastModified.isValid() && dateHeader.isValid()) { + int diff = lastModified.secsTo(dateHeader); + freshness_lifetime = diff / 10; + if (httpRequest.headerField("Warning").isEmpty()) { + QDateTime dt = currentDateTime.addSecs(current_age); + if (currentDateTime.daysTo(dt) > 1) + httpRequest.setHeaderField("Warning", "113"); } } - // the cache-saving code below sets the expirationDate with date+max_age - // if "max-age" is present, or to Expires otherwise - int freshness_lifetime = dateHeader.secsTo(expirationDate); + // the cache-saving code below sets the freshness_lifetime with (dateHeader - last_modified) / 10 + // if "last-modified" is present, or to Expires otherwise response_is_fresh = (freshness_lifetime > current_age); } else { // expiration date was calculated earlier (e.g. when storing object to the cache) -- cgit v1.2.3