Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Unified Diff: net/http/http_response_headers.cc

Issue 455623003: stale-while-revalidate experimental implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add load flag LOAD_ASYNC_REVALIDATION. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/http/http_response_headers.cc
diff --git a/net/http/http_response_headers.cc b/net/http/http_response_headers.cc
index 3a9f7e04fe0c4bad6a232ae52270e55427532d03..8c4102cb1dfc9388fb4821339899751c1b0f9cee 100644
--- a/net/http/http_response_headers.cc
+++ b/net/http/http_response_headers.cc
@@ -133,6 +133,13 @@ struct HttpResponseHeaders::ParsedHeader {
//-----------------------------------------------------------------------------
+HttpResponseHeaders::Freshness::Freshness() : usable(false) {}
+HttpResponseHeaders::Freshness::Freshness(const TimeDelta& lifetime)
+ : usable(true), lifetime(lifetime) {}
+HttpResponseHeaders::Freshness::~Freshness() {}
+
+//-----------------------------------------------------------------------------
+
HttpResponseHeaders::HttpResponseHeaders(const std::string& raw_input)
: response_code_(-1) {
Parse(raw_input);
@@ -956,15 +963,29 @@ bool HttpResponseHeaders::IsRedirectResponseCode(int response_code) {
// Of course, there are other factors that can force a response to always be
// validated or re-fetched.
//
-bool HttpResponseHeaders::RequiresValidation(const Time& request_time,
- const Time& response_time,
- const Time& current_time) const {
- TimeDelta lifetime =
- GetFreshnessLifetime(response_time);
- if (lifetime == TimeDelta())
- return true;
-
- return lifetime <= GetCurrentAge(request_time, response_time, current_time);
+// From RFC 5861 section 3, a stale response may be used while revalidation is
+// performed in the background if
+//
+// freshness_lifetime + stale_while_revalidate > current_age
+//
+HttpResponseHeaders::ValidationType HttpResponseHeaders::RequiresValidation(
+ const Time& request_time,
+ const Time& response_time,
+ const Time& current_time) const {
+ Freshness freshness = GetFreshnessLifetime(response_time);
+ if (!freshness.usable)
+ return VALIDATION_SYNCHRONOUS;
+ TimeDelta age = GetCurrentAge(request_time, response_time, current_time);
+
+ if (freshness.lifetime > age)
+ return VALIDATION_NONE;
+
+ TimeDelta stale_while_revalidate;
+ if (GetStaleWhileRevalidateValue(&stale_while_revalidate) &&
+ freshness.lifetime + stale_while_revalidate > age)
+ return VALIDATION_ASYNCHRONOUS;
+
+ return VALIDATION_SYNCHRONOUS;
}
// From RFC 2616 section 13.2.4:
@@ -987,7 +1008,7 @@ bool HttpResponseHeaders::RequiresValidation(const Time& request_time,
//
// freshness_lifetime = (date_value - last_modified_value) * 0.10
//
-TimeDelta HttpResponseHeaders::GetFreshnessLifetime(
+HttpResponseHeaders::Freshness HttpResponseHeaders::GetFreshnessLifetime(
const Time& response_time) const {
// Check for headers that force a response to never be fresh. For backwards
// compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control:
@@ -996,16 +1017,15 @@ TimeDelta HttpResponseHeaders::GetFreshnessLifetime(
HasHeaderValue("cache-control", "no-store") ||
HasHeaderValue("pragma", "no-cache") ||
HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6
- return TimeDelta(); // not fresh
+ return Freshness();
// NOTE: "Cache-Control: max-age" overrides Expires, so we only check the
// Expires header after checking for max-age in GetFreshnessLifetime. This
// is important since "Expires: <date in the past>" means not fresh, but
// it should not trump a max-age value.
-
- TimeDelta max_age_value;
- if (GetMaxAgeValue(&max_age_value))
- return max_age_value;
+ TimeDelta lifetime;
+ if (GetMaxAgeValue(&lifetime))
+ return Freshness(lifetime);
rvargas (doing something else) 2014/09/05 22:52:40 We have to look at must-revalidate before deciding
rvargas (doing something else) 2014/09/11 02:33:06 ?
Adam Rice 2014/09/12 01:46:48 The existing code doesn't look at must-revalidate
rvargas (doing something else) 2014/09/12 18:33:12 must-revalidate doesn't matter for the current cod
Adam Rice 2014/09/16 11:53:00 Done.
// If there is no Date header, then assume that the server response was
// generated at the time when we received the response.
@@ -1016,10 +1036,11 @@ TimeDelta HttpResponseHeaders::GetFreshnessLifetime(
Time expires_value;
if (GetExpiresValue(&expires_value)) {
// The expires value can be a date in the past!
- if (expires_value > date_value)
- return expires_value - date_value;
+ if (expires_value > date_value) {
+ return Freshness(expires_value - date_value);
+ }
- return TimeDelta(); // not fresh
+ return Freshness();
rvargas (doing something else) 2014/09/05 22:52:40 This may be usable
Adam Rice 2014/09/09 12:36:45 Acknowledged.
}
// From RFC 2616 section 13.4:
@@ -1052,19 +1073,20 @@ TimeDelta HttpResponseHeaders::GetFreshnessLifetime(
// TODO(darin): Implement a smarter heuristic.
Time last_modified_value;
if (GetLastModifiedValue(&last_modified_value)) {
- // The last-modified value can be a date in the past!
- if (last_modified_value <= date_value)
- return (date_value - last_modified_value) / 10;
+ // The last-modified value can be a date in the future!
+ if (last_modified_value <= date_value) {
+ return Freshness((date_value - last_modified_value) / 10);
+ }
}
}
// These responses are implicitly fresh (unless otherwise overruled):
if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 ||
response_code_ == 410) {
- return TimeDelta::Max();
+ return Freshness(TimeDelta::Max());
rvargas (doing something else) 2014/09/05 22:52:40 must-revalidate again
Adam Rice 2014/09/09 12:36:46 This never becomes stale, so must-revalidate is a
rvargas (doing something else) 2014/09/11 02:33:05 doh!
}
- return TimeDelta(); // not fresh
+ return Freshness();
rvargas (doing something else) 2014/09/05 22:52:40 may be usable. I think things are simpler if this
Adam Rice 2014/09/09 12:36:46 Done.
}
// From RFC 2616 section 13.2.3:

Powered by Google App Engine
This is Rietveld 408576698