Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 899 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 910 | 910 |
| 911 // From RFC 2616 section 13.2.4: | 911 // From RFC 2616 section 13.2.4: |
| 912 // | 912 // |
| 913 // The calculation to determine if a response has expired is quite simple: | 913 // The calculation to determine if a response has expired is quite simple: |
| 914 // | 914 // |
| 915 // response_is_fresh = (freshness_lifetime > current_age) | 915 // response_is_fresh = (freshness_lifetime > current_age) |
| 916 // | 916 // |
| 917 // Of course, there are other factors that can force a response to always be | 917 // Of course, there are other factors that can force a response to always be |
| 918 // validated or re-fetched. | 918 // validated or re-fetched. |
| 919 // | 919 // |
| 920 // From RFC 5861 section 3, a stale response may be used while revalidation is | 920 bool HttpResponseHeaders::RequiresValidation(const Time& request_time, |
| 921 // performed in the background if | 921 const Time& response_time, |
| 922 // | 922 const Time& current_time) const { |
| 923 // freshness_lifetime + stale_while_revalidate > current_age | |
| 924 // | |
| 925 ValidationType HttpResponseHeaders::RequiresValidation( | |
| 926 const Time& request_time, | |
| 927 const Time& response_time, | |
| 928 const Time& current_time) const { | |
| 929 FreshnessLifetimes lifetimes = GetFreshnessLifetimes(response_time); | 923 FreshnessLifetimes lifetimes = GetFreshnessLifetimes(response_time); |
| 930 if (lifetimes.freshness.is_zero() && lifetimes.staleness.is_zero()) | 924 if (lifetimes.freshness.is_zero()) |
| 931 return VALIDATION_SYNCHRONOUS; | 925 return true; |
| 932 | 926 |
| 933 TimeDelta age = GetCurrentAge(request_time, response_time, current_time); | 927 TimeDelta age = GetCurrentAge(request_time, response_time, current_time); |
| 934 | 928 |
| 935 if (lifetimes.freshness > age) | 929 if (lifetimes.freshness > age) |
| 936 return VALIDATION_NONE; | 930 return false; |
| 937 | 931 |
| 938 if (lifetimes.freshness + lifetimes.staleness > age) | 932 return true; |
|
mmenke
2017/03/23 21:39:35
optional: Maybe just replace these last 4 lines w
scottmg
2017/03/24 22:46:00
Done. (I was a bit nervous about messing up on a b
| |
| 939 return VALIDATION_ASYNCHRONOUS; | |
| 940 | |
| 941 return VALIDATION_SYNCHRONOUS; | |
| 942 } | 933 } |
| 943 | 934 |
| 944 // From RFC 2616 section 13.2.4: | 935 // From RFC 2616 section 13.2.4: |
| 945 // | 936 // |
| 946 // The max-age directive takes priority over Expires, so if max-age is present | 937 // The max-age directive takes priority over Expires, so if max-age is present |
| 947 // in a response, the calculation is simply: | 938 // in a response, the calculation is simply: |
| 948 // | 939 // |
| 949 // freshness_lifetime = max_age_value | 940 // freshness_lifetime = max_age_value |
| 950 // | 941 // |
| 951 // Otherwise, if Expires is present in the response, the calculation is: | 942 // Otherwise, if Expires is present in the response, the calculation is: |
| 952 // | 943 // |
| 953 // freshness_lifetime = expires_value - date_value | 944 // freshness_lifetime = expires_value - date_value |
| 954 // | 945 // |
| 955 // Note that neither of these calculations is vulnerable to clock skew, since | 946 // Note that neither of these calculations is vulnerable to clock skew, since |
| 956 // all of the information comes from the origin server. | 947 // all of the information comes from the origin server. |
| 957 // | 948 // |
| 958 // Also, if the response does have a Last-Modified time, the heuristic | 949 // Also, if the response does have a Last-Modified time, the heuristic |
| 959 // expiration value SHOULD be no more than some fraction of the interval since | 950 // expiration value SHOULD be no more than some fraction of the interval since |
| 960 // that time. A typical setting of this fraction might be 10%: | 951 // that time. A typical setting of this fraction might be 10%: |
| 961 // | 952 // |
| 962 // freshness_lifetime = (date_value - last_modified_value) * 0.10 | 953 // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
| 963 // | 954 // |
| 964 // If the stale-while-revalidate directive is present, then it is used to set | |
| 965 // the |staleness| time, unless it overridden by another directive. | |
| 966 // | |
| 967 HttpResponseHeaders::FreshnessLifetimes | 955 HttpResponseHeaders::FreshnessLifetimes |
| 968 HttpResponseHeaders::GetFreshnessLifetimes(const Time& response_time) const { | 956 HttpResponseHeaders::GetFreshnessLifetimes(const Time& response_time) const { |
| 969 FreshnessLifetimes lifetimes; | 957 FreshnessLifetimes lifetimes; |
| 970 // Check for headers that force a response to never be fresh. For backwards | 958 // Check for headers that force a response to never be fresh. For backwards |
| 971 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: | 959 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: |
| 972 // no-cache" even though RFC 2616 does not specify it. | 960 // no-cache" even though RFC 2616 does not specify it. |
| 973 if (HasHeaderValue("cache-control", "no-cache") || | 961 if (HasHeaderValue("cache-control", "no-cache") || |
| 974 HasHeaderValue("cache-control", "no-store") || | 962 HasHeaderValue("cache-control", "no-store") || |
| 975 HasHeaderValue("pragma", "no-cache") || | 963 HasHeaderValue("pragma", "no-cache") || |
| 976 // Vary: * is never usable: see RFC 2616 section 13.6. | 964 // Vary: * is never usable: see RFC 2616 section 13.6. |
| 977 HasHeaderValue("vary", "*")) { | 965 HasHeaderValue("vary", "*")) { |
| 978 return lifetimes; | 966 return lifetimes; |
| 979 } | 967 } |
| 980 | 968 |
| 981 // Cache-Control directive must_revalidate overrides stale-while-revalidate. | |
| 982 bool must_revalidate = HasHeaderValue("cache-control", "must-revalidate"); | 969 bool must_revalidate = HasHeaderValue("cache-control", "must-revalidate"); |
|
mmenke
2017/03/23 21:39:35
Can get rid of the must_revalidate local, and inli
scottmg
2017/03/24 22:46:00
Done.
| |
| 983 | 970 |
| 984 if (must_revalidate || !GetStaleWhileRevalidateValue(&lifetimes.staleness)) { | |
| 985 DCHECK_EQ(TimeDelta(), lifetimes.staleness); | |
| 986 } | |
| 987 | |
| 988 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 971 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
| 989 // Expires header after checking for max-age in GetFreshnessLifetimes. This | 972 // Expires header after checking for max-age in GetFreshnessLifetimes. This |
| 990 // is important since "Expires: <date in the past>" means not fresh, but | 973 // is important since "Expires: <date in the past>" means not fresh, but |
| 991 // it should not trump a max-age value. | 974 // it should not trump a max-age value. |
| 992 if (GetMaxAgeValue(&lifetimes.freshness)) | 975 if (GetMaxAgeValue(&lifetimes.freshness)) |
| 993 return lifetimes; | 976 return lifetimes; |
| 994 | 977 |
| 995 // If there is no Date header, then assume that the server response was | 978 // If there is no Date header, then assume that the server response was |
| 996 // generated at the time when we received the response. | 979 // generated at the time when we received the response. |
| 997 Time date_value; | 980 Time date_value; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1044 lifetimes.freshness = (date_value - last_modified_value) / 10; | 1027 lifetimes.freshness = (date_value - last_modified_value) / 10; |
| 1045 return lifetimes; | 1028 return lifetimes; |
| 1046 } | 1029 } |
| 1047 } | 1030 } |
| 1048 } | 1031 } |
| 1049 | 1032 |
| 1050 // These responses are implicitly fresh (unless otherwise overruled): | 1033 // These responses are implicitly fresh (unless otherwise overruled): |
| 1051 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || | 1034 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || |
| 1052 response_code_ == 410) { | 1035 response_code_ == 410) { |
| 1053 lifetimes.freshness = TimeDelta::Max(); | 1036 lifetimes.freshness = TimeDelta::Max(); |
| 1054 lifetimes.staleness = TimeDelta(); // It should never be stale. | |
| 1055 return lifetimes; | 1037 return lifetimes; |
| 1056 } | 1038 } |
| 1057 | 1039 |
| 1058 // Our heuristic freshness estimate for this resource is 0 seconds, in | 1040 // Our heuristic freshness estimate for this resource is 0 seconds, in |
| 1059 // accordance with common browser behaviour. However, stale-while-revalidate | 1041 // accordance with common browser behaviour. |
| 1060 // may still apply. | |
| 1061 DCHECK_EQ(TimeDelta(), lifetimes.freshness); | 1042 DCHECK_EQ(TimeDelta(), lifetimes.freshness); |
| 1062 return lifetimes; | 1043 return lifetimes; |
| 1063 } | 1044 } |
| 1064 | 1045 |
| 1065 // From RFC 7234 section 4.2.3: | 1046 // From RFC 7234 section 4.2.3: |
| 1066 // | 1047 // |
| 1067 // The following data is used for the age calculation: | 1048 // The following data is used for the age calculation: |
| 1068 // | 1049 // |
| 1069 // age_value | 1050 // age_value |
| 1070 // | 1051 // |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1161 } | 1142 } |
| 1162 | 1143 |
| 1163 bool HttpResponseHeaders::GetLastModifiedValue(Time* result) const { | 1144 bool HttpResponseHeaders::GetLastModifiedValue(Time* result) const { |
| 1164 return GetTimeValuedHeader("Last-Modified", result); | 1145 return GetTimeValuedHeader("Last-Modified", result); |
| 1165 } | 1146 } |
| 1166 | 1147 |
| 1167 bool HttpResponseHeaders::GetExpiresValue(Time* result) const { | 1148 bool HttpResponseHeaders::GetExpiresValue(Time* result) const { |
| 1168 return GetTimeValuedHeader("Expires", result); | 1149 return GetTimeValuedHeader("Expires", result); |
| 1169 } | 1150 } |
| 1170 | 1151 |
| 1171 bool HttpResponseHeaders::GetStaleWhileRevalidateValue( | |
| 1172 TimeDelta* result) const { | |
| 1173 return GetCacheControlDirective("stale-while-revalidate", result); | |
| 1174 } | |
| 1175 | |
| 1176 bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name, | 1152 bool HttpResponseHeaders::GetTimeValuedHeader(const std::string& name, |
| 1177 Time* result) const { | 1153 Time* result) const { |
| 1178 std::string value; | 1154 std::string value; |
| 1179 if (!EnumerateHeader(nullptr, name, &value)) | 1155 if (!EnumerateHeader(nullptr, name, &value)) |
| 1180 return false; | 1156 return false; |
| 1181 | 1157 |
| 1182 // When parsing HTTP dates it's beneficial to default to GMT because: | 1158 // When parsing HTTP dates it's beneficial to default to GMT because: |
| 1183 // 1. RFC2616 3.3.1 says times should always be specified in GMT | 1159 // 1. RFC2616 3.3.1 says times should always be specified in GMT |
| 1184 // 2. Only counter-example incorrectly appended "UTC" (crbug.com/153759) | 1160 // 2. Only counter-example incorrectly appended "UTC" (crbug.com/153759) |
| 1185 // 3. When adjusting cookie expiration times for clock skew | 1161 // 3. When adjusting cookie expiration times for clock skew |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1346 return true; | 1322 return true; |
| 1347 } | 1323 } |
| 1348 | 1324 |
| 1349 bool HttpResponseHeaders::IsChunkEncoded() const { | 1325 bool HttpResponseHeaders::IsChunkEncoded() const { |
| 1350 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1326 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
| 1351 return GetHttpVersion() >= HttpVersion(1, 1) && | 1327 return GetHttpVersion() >= HttpVersion(1, 1) && |
| 1352 HasHeaderValue("Transfer-Encoding", "chunked"); | 1328 HasHeaderValue("Transfer-Encoding", "chunked"); |
| 1353 } | 1329 } |
| 1354 | 1330 |
| 1355 } // namespace net | 1331 } // namespace net |
| OLD | NEW |