Chromium Code Reviews| 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..69eef0c4c38744ab338c62bea00b2b4068d1b5a9 100644 |
| --- a/net/http/http_response_headers.cc |
| +++ b/net/http/http_response_headers.cc |
| @@ -956,15 +956,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 { |
| + TimeDelta lifetime; |
| + if (GetFreshnessLifetime(response_time, &lifetime) == NEVER_FRESH) |
| + return VALIDATION_SYNCHRONOUS; |
| + TimeDelta age = GetCurrentAge(request_time, response_time, current_time); |
| + |
| + if (lifetime > age) |
| + return VALIDATION_NONE; |
| + |
| + TimeDelta stale_while_revalidate; |
| + if (GetStaleWhileRevalidateValue(&stale_while_revalidate) && |
| + lifetime + stale_while_revalidate > age) |
| + return VALIDATION_ASYNCHRONOUS; |
| + |
| + return VALIDATION_SYNCHRONOUS; |
| } |
| // From RFC 2616 section 13.2.4: |
| @@ -987,8 +1001,9 @@ bool HttpResponseHeaders::RequiresValidation(const Time& request_time, |
| // |
| // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
| // |
| -TimeDelta HttpResponseHeaders::GetFreshnessLifetime( |
| - const Time& response_time) const { |
| +HttpResponseHeaders::FreshnessType HttpResponseHeaders::GetFreshnessLifetime( |
| + const Time& response_time, |
| + TimeDelta* lifetime) 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: |
| // no-cache" even though RFC 2616 does not specify it. |
| @@ -996,17 +1011,17 @@ 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 NEVER_FRESH; |
|
yhirano
2014/08/27 09:24:29
You should clear *lifetime here. Reverting L1007 c
Adam Rice
2014/08/28 03:14:35
Done.
|
| // 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; |
| + if (GetMaxAgeValue(lifetime)) |
| + return MAYBE_FRESH; |
| + *lifetime = TimeDelta(); |
| // If there is no Date header, then assume that the server response was |
| // generated at the time when we received the response. |
| Time date_value; |
| @@ -1016,10 +1031,12 @@ 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) { |
| + *lifetime = expires_value - date_value; |
| + return MAYBE_FRESH; |
| + } |
| - return TimeDelta(); // not fresh |
| + return NEVER_FRESH; |
| } |
| // From RFC 2616 section 13.4: |
| @@ -1052,19 +1069,22 @@ 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) { |
| + *lifetime = (date_value - last_modified_value) / 10; |
| + return MAYBE_FRESH; |
| + } |
| } |
| } |
| // These responses are implicitly fresh (unless otherwise overruled): |
| if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || |
| response_code_ == 410) { |
| - return TimeDelta::Max(); |
| + *lifetime = TimeDelta::Max(); |
| + return MAYBE_FRESH; |
| } |
| - return TimeDelta(); // not fresh |
| + return NEVER_FRESH; |
| } |
| // From RFC 2616 section 13.2.3: |