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

Side by Side 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: Stop setting Cache-Control: max-age=0 on async revalidations. 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // The rules for header parsing were borrowed from Firefox: 5 // The rules for header parsing were borrowed from Firefox:
6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo nseHead.cpp 6 // http://lxr.mozilla.org/seamonkey/source/netwerk/protocol/http/src/nsHttpRespo nseHead.cpp
7 // The rules for parsing content-types were also borrowed from Firefox: 7 // The rules for parsing content-types were also borrowed from Firefox:
8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 8 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834
9 9
10 #include "net/http/http_response_headers.h" 10 #include "net/http/http_response_headers.h"
(...skipping 938 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 949
950 // From RFC 2616 section 13.2.4: 950 // From RFC 2616 section 13.2.4:
951 // 951 //
952 // The calculation to determine if a response has expired is quite simple: 952 // The calculation to determine if a response has expired is quite simple:
953 // 953 //
954 // response_is_fresh = (freshness_lifetime > current_age) 954 // response_is_fresh = (freshness_lifetime > current_age)
955 // 955 //
956 // Of course, there are other factors that can force a response to always be 956 // Of course, there are other factors that can force a response to always be
957 // validated or re-fetched. 957 // validated or re-fetched.
958 // 958 //
959 bool HttpResponseHeaders::RequiresValidation(const Time& request_time, 959 // From RFC 5861 section 3, a stale response may be used while revalidation is
960 const Time& response_time, 960 // performed in the background if
961 const Time& current_time) const { 961 //
962 TimeDelta lifetime = 962 // freshness_lifetime + stale_while_revalidate > current_age
963 GetFreshnessLifetime(response_time); 963 //
964 if (lifetime == TimeDelta()) 964 HttpResponseHeaders::ValidationType HttpResponseHeaders::RequiresValidation(
rvargas (doing something else) 2014/08/29 23:36:07 We don't need the class name on the return value,
Adam Rice 2014/09/01 14:43:00 Yes, we do.
965 return true; 965 const Time& request_time,
966 const Time& response_time,
967 const Time& current_time) const {
968 TimeDelta lifetime;
969 if (GetFreshnessLifetime(response_time, &lifetime) == NEVER_FRESH)
970 return VALIDATION_SYNCHRONOUS;
971 TimeDelta age = GetCurrentAge(request_time, response_time, current_time);
966 972
967 return lifetime <= GetCurrentAge(request_time, response_time, current_time); 973 if (lifetime > age)
974 return VALIDATION_NONE;
975
976 TimeDelta stale_while_revalidate;
977 if (GetStaleWhileRevalidateValue(&stale_while_revalidate) &&
978 lifetime + stale_while_revalidate > age)
979 return VALIDATION_ASYNCHRONOUS;
980
981 return VALIDATION_SYNCHRONOUS;
968 } 982 }
969 983
970 // From RFC 2616 section 13.2.4: 984 // From RFC 2616 section 13.2.4:
971 // 985 //
972 // The max-age directive takes priority over Expires, so if max-age is present 986 // The max-age directive takes priority over Expires, so if max-age is present
973 // in a response, the calculation is simply: 987 // in a response, the calculation is simply:
974 // 988 //
975 // freshness_lifetime = max_age_value 989 // freshness_lifetime = max_age_value
976 // 990 //
977 // Otherwise, if Expires is present in the response, the calculation is: 991 // Otherwise, if Expires is present in the response, the calculation is:
978 // 992 //
979 // freshness_lifetime = expires_value - date_value 993 // freshness_lifetime = expires_value - date_value
980 // 994 //
981 // Note that neither of these calculations is vulnerable to clock skew, since 995 // Note that neither of these calculations is vulnerable to clock skew, since
982 // all of the information comes from the origin server. 996 // all of the information comes from the origin server.
983 // 997 //
984 // Also, if the response does have a Last-Modified time, the heuristic 998 // Also, if the response does have a Last-Modified time, the heuristic
985 // expiration value SHOULD be no more than some fraction of the interval since 999 // expiration value SHOULD be no more than some fraction of the interval since
986 // that time. A typical setting of this fraction might be 10%: 1000 // that time. A typical setting of this fraction might be 10%:
987 // 1001 //
988 // freshness_lifetime = (date_value - last_modified_value) * 0.10 1002 // freshness_lifetime = (date_value - last_modified_value) * 0.10
989 // 1003 //
990 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( 1004 HttpResponseHeaders::FreshnessType HttpResponseHeaders::GetFreshnessLifetime(
rvargas (doing something else) 2014/08/29 23:36:07 :( Can we just say that in order to have async re
Adam Rice 2014/09/01 14:43:00 The problem is that max-age=0,stale-while-revalida
rvargas (doing something else) 2014/09/03 01:25:53 The ugly code (two, maybe contradictory outputs) i
Adam Rice 2014/09/05 15:17:11 If this is true, then it should apply also for max
rvargas (doing something else) 2014/09/05 22:52:39 I don't follow what you mean.
991 const Time& response_time) const { 1005 const Time& response_time,
1006 TimeDelta* lifetime) const {
1007 *lifetime = TimeDelta();
1008
992 // Check for headers that force a response to never be fresh. For backwards 1009 // Check for headers that force a response to never be fresh. For backwards
993 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: 1010 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control:
994 // no-cache" even though RFC 2616 does not specify it. 1011 // no-cache" even though RFC 2616 does not specify it.
995 if (HasHeaderValue("cache-control", "no-cache") || 1012 if (HasHeaderValue("cache-control", "no-cache") ||
996 HasHeaderValue("cache-control", "no-store") || 1013 HasHeaderValue("cache-control", "no-store") ||
997 HasHeaderValue("pragma", "no-cache") || 1014 HasHeaderValue("pragma", "no-cache") ||
998 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 1015 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6
999 return TimeDelta(); // not fresh 1016 return NEVER_FRESH;
1000 1017
1001 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the 1018 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the
1002 // Expires header after checking for max-age in GetFreshnessLifetime. This 1019 // Expires header after checking for max-age in GetFreshnessLifetime. This
1003 // is important since "Expires: <date in the past>" means not fresh, but 1020 // is important since "Expires: <date in the past>" means not fresh, but
1004 // it should not trump a max-age value. 1021 // it should not trump a max-age value.
1005 1022
1006 TimeDelta max_age_value; 1023 if (GetMaxAgeValue(lifetime))
1007 if (GetMaxAgeValue(&max_age_value)) 1024 return MAYBE_FRESH;
1008 return max_age_value;
1009 1025
1010 // If there is no Date header, then assume that the server response was 1026 // If there is no Date header, then assume that the server response was
1011 // generated at the time when we received the response. 1027 // generated at the time when we received the response.
1012 Time date_value; 1028 Time date_value;
1013 if (!GetDateValue(&date_value)) 1029 if (!GetDateValue(&date_value))
1014 date_value = response_time; 1030 date_value = response_time;
1015 1031
1016 Time expires_value; 1032 Time expires_value;
1017 if (GetExpiresValue(&expires_value)) { 1033 if (GetExpiresValue(&expires_value)) {
1018 // The expires value can be a date in the past! 1034 // The expires value can be a date in the past!
1019 if (expires_value > date_value) 1035 if (expires_value > date_value) {
1020 return expires_value - date_value; 1036 *lifetime = expires_value - date_value;
1037 return MAYBE_FRESH;
1038 }
1021 1039
1022 return TimeDelta(); // not fresh 1040 return NEVER_FRESH;
1023 } 1041 }
1024 1042
1025 // From RFC 2616 section 13.4: 1043 // From RFC 2616 section 13.4:
1026 // 1044 //
1027 // A response received with a status code of 200, 203, 206, 300, 301 or 410 1045 // A response received with a status code of 200, 203, 206, 300, 301 or 410
1028 // MAY be stored by a cache and used in reply to a subsequent request, 1046 // MAY be stored by a cache and used in reply to a subsequent request,
1029 // subject to the expiration mechanism, unless a cache-control directive 1047 // subject to the expiration mechanism, unless a cache-control directive
1030 // prohibits caching. 1048 // prohibits caching.
1031 // ... 1049 // ...
1032 // A response received with any other status code (e.g. status codes 302 1050 // A response received with any other status code (e.g. status codes 302
(...skipping 12 matching lines...) Expand all
1045 // 1063 //
1046 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an 1064 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an
1047 // experimental RFC that adds 308 permanent redirect as well, for which "any 1065 // experimental RFC that adds 308 permanent redirect as well, for which "any
1048 // future references ... SHOULD use one of the returned URIs." 1066 // future references ... SHOULD use one of the returned URIs."
1049 if ((response_code_ == 200 || response_code_ == 203 || 1067 if ((response_code_ == 200 || response_code_ == 203 ||
1050 response_code_ == 206) && 1068 response_code_ == 206) &&
1051 !HasHeaderValue("cache-control", "must-revalidate")) { 1069 !HasHeaderValue("cache-control", "must-revalidate")) {
1052 // TODO(darin): Implement a smarter heuristic. 1070 // TODO(darin): Implement a smarter heuristic.
1053 Time last_modified_value; 1071 Time last_modified_value;
1054 if (GetLastModifiedValue(&last_modified_value)) { 1072 if (GetLastModifiedValue(&last_modified_value)) {
1055 // The last-modified value can be a date in the past! 1073 // The last-modified value can be a date in the future!
1056 if (last_modified_value <= date_value) 1074 if (last_modified_value <= date_value) {
1057 return (date_value - last_modified_value) / 10; 1075 *lifetime = (date_value - last_modified_value) / 10;
1076 return MAYBE_FRESH;
1077 }
1058 } 1078 }
1059 } 1079 }
1060 1080
1061 // These responses are implicitly fresh (unless otherwise overruled): 1081 // These responses are implicitly fresh (unless otherwise overruled):
1062 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || 1082 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 ||
1063 response_code_ == 410) { 1083 response_code_ == 410) {
1064 return TimeDelta::Max(); 1084 *lifetime = TimeDelta::Max();
1085 return MAYBE_FRESH;
1065 } 1086 }
1066 1087
1067 return TimeDelta(); // not fresh 1088 return NEVER_FRESH;
1068 } 1089 }
1069 1090
1070 // From RFC 2616 section 13.2.3: 1091 // From RFC 2616 section 13.2.3:
1071 // 1092 //
1072 // Summary of age calculation algorithm, when a cache receives a response: 1093 // Summary of age calculation algorithm, when a cache receives a response:
1073 // 1094 //
1074 // /* 1095 // /*
1075 // * age_value 1096 // * age_value
1076 // * is the value of Age: header received by the cache with 1097 // * is the value of Age: header received by the cache with
1077 // * this response. 1098 // * this response.
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1397 return true; 1418 return true;
1398 } 1419 }
1399 1420
1400 bool HttpResponseHeaders::IsChunkEncoded() const { 1421 bool HttpResponseHeaders::IsChunkEncoded() const {
1401 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. 1422 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies.
1402 return GetHttpVersion() >= HttpVersion(1, 1) && 1423 return GetHttpVersion() >= HttpVersion(1, 1) &&
1403 HasHeaderValue("Transfer-Encoding", "chunked"); 1424 HasHeaderValue("Transfer-Encoding", "chunked");
1404 } 1425 }
1405 1426
1406 } // namespace net 1427 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698