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 938 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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( |
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( |
991 const Time& response_time) const { | 1005 const Time& response_time, |
1006 TimeDelta* lifetime) const { | |
992 // Check for headers that force a response to never be fresh. For backwards | 1007 // 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: | 1008 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: |
994 // no-cache" even though RFC 2616 does not specify it. | 1009 // no-cache" even though RFC 2616 does not specify it. |
995 if (HasHeaderValue("cache-control", "no-cache") || | 1010 if (HasHeaderValue("cache-control", "no-cache") || |
996 HasHeaderValue("cache-control", "no-store") || | 1011 HasHeaderValue("cache-control", "no-store") || |
997 HasHeaderValue("pragma", "no-cache") || | 1012 HasHeaderValue("pragma", "no-cache") || |
998 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 | 1013 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 |
999 return TimeDelta(); // not fresh | 1014 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.
| |
1000 | 1015 |
1001 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 1016 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
1002 // Expires header after checking for max-age in GetFreshnessLifetime. This | 1017 // Expires header after checking for max-age in GetFreshnessLifetime. This |
1003 // is important since "Expires: <date in the past>" means not fresh, but | 1018 // is important since "Expires: <date in the past>" means not fresh, but |
1004 // it should not trump a max-age value. | 1019 // it should not trump a max-age value. |
1005 | 1020 |
1006 TimeDelta max_age_value; | 1021 if (GetMaxAgeValue(lifetime)) |
1007 if (GetMaxAgeValue(&max_age_value)) | 1022 return MAYBE_FRESH; |
1008 return max_age_value; | |
1009 | 1023 |
1024 *lifetime = TimeDelta(); | |
1010 // If there is no Date header, then assume that the server response was | 1025 // If there is no Date header, then assume that the server response was |
1011 // generated at the time when we received the response. | 1026 // generated at the time when we received the response. |
1012 Time date_value; | 1027 Time date_value; |
1013 if (!GetDateValue(&date_value)) | 1028 if (!GetDateValue(&date_value)) |
1014 date_value = response_time; | 1029 date_value = response_time; |
1015 | 1030 |
1016 Time expires_value; | 1031 Time expires_value; |
1017 if (GetExpiresValue(&expires_value)) { | 1032 if (GetExpiresValue(&expires_value)) { |
1018 // The expires value can be a date in the past! | 1033 // The expires value can be a date in the past! |
1019 if (expires_value > date_value) | 1034 if (expires_value > date_value) { |
1020 return expires_value - date_value; | 1035 *lifetime = expires_value - date_value; |
1036 return MAYBE_FRESH; | |
1037 } | |
1021 | 1038 |
1022 return TimeDelta(); // not fresh | 1039 return NEVER_FRESH; |
1023 } | 1040 } |
1024 | 1041 |
1025 // From RFC 2616 section 13.4: | 1042 // From RFC 2616 section 13.4: |
1026 // | 1043 // |
1027 // A response received with a status code of 200, 203, 206, 300, 301 or 410 | 1044 // 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, | 1045 // 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 | 1046 // subject to the expiration mechanism, unless a cache-control directive |
1030 // prohibits caching. | 1047 // prohibits caching. |
1031 // ... | 1048 // ... |
1032 // A response received with any other status code (e.g. status codes 302 | 1049 // A response received with any other status code (e.g. status codes 302 |
(...skipping 12 matching lines...) Expand all Loading... | |
1045 // | 1062 // |
1046 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an | 1063 // 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 | 1064 // experimental RFC that adds 308 permanent redirect as well, for which "any |
1048 // future references ... SHOULD use one of the returned URIs." | 1065 // future references ... SHOULD use one of the returned URIs." |
1049 if ((response_code_ == 200 || response_code_ == 203 || | 1066 if ((response_code_ == 200 || response_code_ == 203 || |
1050 response_code_ == 206) && | 1067 response_code_ == 206) && |
1051 !HasHeaderValue("cache-control", "must-revalidate")) { | 1068 !HasHeaderValue("cache-control", "must-revalidate")) { |
1052 // TODO(darin): Implement a smarter heuristic. | 1069 // TODO(darin): Implement a smarter heuristic. |
1053 Time last_modified_value; | 1070 Time last_modified_value; |
1054 if (GetLastModifiedValue(&last_modified_value)) { | 1071 if (GetLastModifiedValue(&last_modified_value)) { |
1055 // The last-modified value can be a date in the past! | 1072 // The last-modified value can be a date in the future! |
1056 if (last_modified_value <= date_value) | 1073 if (last_modified_value <= date_value) { |
1057 return (date_value - last_modified_value) / 10; | 1074 *lifetime = (date_value - last_modified_value) / 10; |
1075 return MAYBE_FRESH; | |
1076 } | |
1058 } | 1077 } |
1059 } | 1078 } |
1060 | 1079 |
1061 // These responses are implicitly fresh (unless otherwise overruled): | 1080 // These responses are implicitly fresh (unless otherwise overruled): |
1062 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || | 1081 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || |
1063 response_code_ == 410) { | 1082 response_code_ == 410) { |
1064 return TimeDelta::Max(); | 1083 *lifetime = TimeDelta::Max(); |
1084 return MAYBE_FRESH; | |
1065 } | 1085 } |
1066 | 1086 |
1067 return TimeDelta(); // not fresh | 1087 return NEVER_FRESH; |
1068 } | 1088 } |
1069 | 1089 |
1070 // From RFC 2616 section 13.2.3: | 1090 // From RFC 2616 section 13.2.3: |
1071 // | 1091 // |
1072 // Summary of age calculation algorithm, when a cache receives a response: | 1092 // Summary of age calculation algorithm, when a cache receives a response: |
1073 // | 1093 // |
1074 // /* | 1094 // /* |
1075 // * age_value | 1095 // * age_value |
1076 // * is the value of Age: header received by the cache with | 1096 // * is the value of Age: header received by the cache with |
1077 // * this response. | 1097 // * this response. |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1397 return true; | 1417 return true; |
1398 } | 1418 } |
1399 | 1419 |
1400 bool HttpResponseHeaders::IsChunkEncoded() const { | 1420 bool HttpResponseHeaders::IsChunkEncoded() const { |
1401 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1421 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
1402 return GetHttpVersion() >= HttpVersion(1, 1) && | 1422 return GetHttpVersion() >= HttpVersion(1, 1) && |
1403 HasHeaderValue("Transfer-Encoding", "chunked"); | 1423 HasHeaderValue("Transfer-Encoding", "chunked"); |
1404 } | 1424 } |
1405 | 1425 |
1406 } // namespace net | 1426 } // namespace net |
OLD | NEW |