| 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 ValidationType HttpResponseHeaders::RequiresValidation( |
| 965 return true; | 965 const Time& request_time, |
| 966 const Time& response_time, |
| 967 const Time& current_time) const { |
| 968 FreshnessLifetimes lifetimes = GetFreshnessLifetimes(response_time); |
| 969 if (lifetimes.fresh == TimeDelta() && lifetimes.stale == TimeDelta()) |
| 970 return VALIDATION_SYNCHRONOUS; |
| 966 | 971 |
| 967 return lifetime <= GetCurrentAge(request_time, response_time, current_time); | 972 TimeDelta age = GetCurrentAge(request_time, response_time, current_time); |
| 973 |
| 974 if (lifetimes.fresh > age) |
| 975 return VALIDATION_NONE; |
| 976 |
| 977 if (lifetimes.fresh + lifetimes.stale > age) |
| 978 return VALIDATION_ASYNCHRONOUS; |
| 979 |
| 980 return VALIDATION_SYNCHRONOUS; |
| 968 } | 981 } |
| 969 | 982 |
| 970 // From RFC 2616 section 13.2.4: | 983 // From RFC 2616 section 13.2.4: |
| 971 // | 984 // |
| 972 // The max-age directive takes priority over Expires, so if max-age is present | 985 // The max-age directive takes priority over Expires, so if max-age is present |
| 973 // in a response, the calculation is simply: | 986 // in a response, the calculation is simply: |
| 974 // | 987 // |
| 975 // freshness_lifetime = max_age_value | 988 // freshness_lifetime = max_age_value |
| 976 // | 989 // |
| 977 // Otherwise, if Expires is present in the response, the calculation is: | 990 // Otherwise, if Expires is present in the response, the calculation is: |
| 978 // | 991 // |
| 979 // freshness_lifetime = expires_value - date_value | 992 // freshness_lifetime = expires_value - date_value |
| 980 // | 993 // |
| 981 // Note that neither of these calculations is vulnerable to clock skew, since | 994 // Note that neither of these calculations is vulnerable to clock skew, since |
| 982 // all of the information comes from the origin server. | 995 // all of the information comes from the origin server. |
| 983 // | 996 // |
| 984 // Also, if the response does have a Last-Modified time, the heuristic | 997 // 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 | 998 // 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%: | 999 // that time. A typical setting of this fraction might be 10%: |
| 987 // | 1000 // |
| 988 // freshness_lifetime = (date_value - last_modified_value) * 0.10 | 1001 // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
| 989 // | 1002 // |
| 990 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( | 1003 // If the stale-while-revalidate directive is present, then it is used to set |
| 991 const Time& response_time) const { | 1004 // the |stale| time, unless it overridden by another directive. |
| 1005 // |
| 1006 HttpResponseHeaders::FreshnessLifetimes |
| 1007 HttpResponseHeaders::GetFreshnessLifetimes(const Time& response_time) const { |
| 1008 FreshnessLifetimes lifetimes; |
| 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 // Vary: * is never usable: see RFC 2616 section 13.6. |
| 999 return TimeDelta(); // not fresh | 1016 HasHeaderValue("vary", "*")) { |
| 1017 return lifetimes; |
| 1018 } |
| 1019 |
| 1020 // Cache-Control directive must_revalidate overrides stale-while-revalidate. |
| 1021 bool must_revalidate = HasHeaderValue("cache-control", "must-revalidate"); |
| 1022 |
| 1023 if (must_revalidate || !GetStaleWhileRevalidateValue(&lifetimes.stale)) |
| 1024 DCHECK(lifetimes.stale == TimeDelta()); |
| 1000 | 1025 |
| 1001 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 1026 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
| 1002 // Expires header after checking for max-age in GetFreshnessLifetime. This | 1027 // Expires header after checking for max-age in GetFreshnessLifetimes. This |
| 1003 // is important since "Expires: <date in the past>" means not fresh, but | 1028 // is important since "Expires: <date in the past>" means not fresh, but |
| 1004 // it should not trump a max-age value. | 1029 // it should not trump a max-age value. |
| 1005 | 1030 if (GetMaxAgeValue(&lifetimes.fresh)) |
| 1006 TimeDelta max_age_value; | 1031 return lifetimes; |
| 1007 if (GetMaxAgeValue(&max_age_value)) | |
| 1008 return max_age_value; | |
| 1009 | 1032 |
| 1010 // If there is no Date header, then assume that the server response was | 1033 // If there is no Date header, then assume that the server response was |
| 1011 // generated at the time when we received the response. | 1034 // generated at the time when we received the response. |
| 1012 Time date_value; | 1035 Time date_value; |
| 1013 if (!GetDateValue(&date_value)) | 1036 if (!GetDateValue(&date_value)) |
| 1014 date_value = response_time; | 1037 date_value = response_time; |
| 1015 | 1038 |
| 1016 Time expires_value; | 1039 Time expires_value; |
| 1017 if (GetExpiresValue(&expires_value)) { | 1040 if (GetExpiresValue(&expires_value)) { |
| 1018 // The expires value can be a date in the past! | 1041 // The expires value can be a date in the past! |
| 1019 if (expires_value > date_value) | 1042 if (expires_value > date_value) { |
| 1020 return expires_value - date_value; | 1043 lifetimes.fresh = expires_value - date_value; |
| 1044 return lifetimes; |
| 1045 } |
| 1021 | 1046 |
| 1022 return TimeDelta(); // not fresh | 1047 DCHECK(lifetimes.fresh == TimeDelta()); |
| 1048 return lifetimes; |
| 1023 } | 1049 } |
| 1024 | 1050 |
| 1025 // From RFC 2616 section 13.4: | 1051 // From RFC 2616 section 13.4: |
| 1026 // | 1052 // |
| 1027 // A response received with a status code of 200, 203, 206, 300, 301 or 410 | 1053 // 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, | 1054 // 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 | 1055 // subject to the expiration mechanism, unless a cache-control directive |
| 1030 // prohibits caching. | 1056 // prohibits caching. |
| 1031 // ... | 1057 // ... |
| 1032 // A response received with any other status code (e.g. status codes 302 | 1058 // A response received with any other status code (e.g. status codes 302 |
| 1033 // and 307) MUST NOT be returned in a reply to a subsequent request unless | 1059 // and 307) MUST NOT be returned in a reply to a subsequent request unless |
| 1034 // there are cache-control directives or another header(s) that explicitly | 1060 // there are cache-control directives or another header(s) that explicitly |
| 1035 // allow it. | 1061 // allow it. |
| 1036 // | 1062 // |
| 1037 // From RFC 2616 section 14.9.4: | 1063 // From RFC 2616 section 14.9.4: |
| 1038 // | 1064 // |
| 1039 // When the must-revalidate directive is present in a response received by | 1065 // When the must-revalidate directive is present in a response received by |
| 1040 // a cache, that cache MUST NOT use the entry after it becomes stale to | 1066 // a cache, that cache MUST NOT use the entry after it becomes stale to |
| 1041 // respond to a subsequent request without first revalidating it with the | 1067 // respond to a subsequent request without first revalidating it with the |
| 1042 // origin server. (I.e., the cache MUST do an end-to-end revalidation every | 1068 // origin server. (I.e., the cache MUST do an end-to-end revalidation every |
| 1043 // time, if, based solely on the origin server's Expires or max-age value, | 1069 // time, if, based solely on the origin server's Expires or max-age value, |
| 1044 // the cached response is stale.) | 1070 // the cached response is stale.) |
| 1045 // | 1071 // |
| 1046 // https://datatracker.ietf.org/doc/draft-reschke-http-status-308/ is an | 1072 // 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 | 1073 // experimental RFC that adds 308 permanent redirect as well, for which "any |
| 1048 // future references ... SHOULD use one of the returned URIs." | 1074 // future references ... SHOULD use one of the returned URIs." |
| 1049 if ((response_code_ == 200 || response_code_ == 203 || | 1075 if ((response_code_ == 200 || response_code_ == 203 || |
| 1050 response_code_ == 206) && | 1076 response_code_ == 206) && !must_revalidate) { |
| 1051 !HasHeaderValue("cache-control", "must-revalidate")) { | |
| 1052 // TODO(darin): Implement a smarter heuristic. | 1077 // TODO(darin): Implement a smarter heuristic. |
| 1053 Time last_modified_value; | 1078 Time last_modified_value; |
| 1054 if (GetLastModifiedValue(&last_modified_value)) { | 1079 if (GetLastModifiedValue(&last_modified_value)) { |
| 1055 // The last-modified value can be a date in the past! | 1080 // The last-modified value can be a date in the future! |
| 1056 if (last_modified_value <= date_value) | 1081 if (last_modified_value <= date_value) { |
| 1057 return (date_value - last_modified_value) / 10; | 1082 lifetimes.fresh = (date_value - last_modified_value) / 10; |
| 1083 return lifetimes; |
| 1084 } |
| 1058 } | 1085 } |
| 1059 } | 1086 } |
| 1060 | 1087 |
| 1061 // These responses are implicitly fresh (unless otherwise overruled): | 1088 // These responses are implicitly fresh (unless otherwise overruled): |
| 1062 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || | 1089 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 308 || |
| 1063 response_code_ == 410) { | 1090 response_code_ == 410) { |
| 1064 return TimeDelta::Max(); | 1091 lifetimes.fresh = TimeDelta::Max(); |
| 1092 lifetimes.stale = TimeDelta(); // It should never be stale. |
| 1093 return lifetimes; |
| 1065 } | 1094 } |
| 1066 | 1095 |
| 1067 return TimeDelta(); // not fresh | 1096 // Our heuristic freshness estimate for this resource is 0 seconds, in |
| 1097 // accordance with common browser behaviour. However, stale-while-revalidate |
| 1098 // may still apply. |
| 1099 DCHECK(lifetimes.fresh == TimeDelta()); |
| 1100 return lifetimes; |
| 1068 } | 1101 } |
| 1069 | 1102 |
| 1070 // From RFC 2616 section 13.2.3: | 1103 // From RFC 2616 section 13.2.3: |
| 1071 // | 1104 // |
| 1072 // Summary of age calculation algorithm, when a cache receives a response: | 1105 // Summary of age calculation algorithm, when a cache receives a response: |
| 1073 // | 1106 // |
| 1074 // /* | 1107 // /* |
| 1075 // * age_value | 1108 // * age_value |
| 1076 // * is the value of Age: header received by the cache with | 1109 // * is the value of Age: header received by the cache with |
| 1077 // * this response. | 1110 // * this response. |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1397 return true; | 1430 return true; |
| 1398 } | 1431 } |
| 1399 | 1432 |
| 1400 bool HttpResponseHeaders::IsChunkEncoded() const { | 1433 bool HttpResponseHeaders::IsChunkEncoded() const { |
| 1401 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. | 1434 // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
| 1402 return GetHttpVersion() >= HttpVersion(1, 1) && | 1435 return GetHttpVersion() >= HttpVersion(1, 1) && |
| 1403 HasHeaderValue("Transfer-Encoding", "chunked"); | 1436 HasHeaderValue("Transfer-Encoding", "chunked"); |
| 1404 } | 1437 } |
| 1405 | 1438 |
| 1406 } // namespace net | 1439 } // namespace net |
| OLD | NEW |