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 887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
898 response_code == 303 || | 898 response_code == 303 || |
899 response_code == 307); | 899 response_code == 307); |
900 } | 900 } |
901 | 901 |
902 // From RFC 2616 section 13.2.4: | 902 // From RFC 2616 section 13.2.4: |
903 // | 903 // |
904 // The calculation to determine if a response has expired is quite simple: | 904 // The calculation to determine if a response has expired is quite simple: |
905 // | 905 // |
906 // response_is_fresh = (freshness_lifetime > current_age) | 906 // response_is_fresh = (freshness_lifetime > current_age) |
907 // | 907 // |
908 // Into this equation, we substitute |current_age = now - freshness_origin|, | |
909 // giving: | |
910 // | |
911 // response_is_fresh = (freshness_lifetime > now - freshness_origin) | |
912 // | |
913 // response_is_fresh = (freshness_lifetime + freshness_origin > now) | |
914 // | |
915 // And we call this quantity |freshness_lifetime + freshness_origin| the | |
916 // freshness_expiry. | |
917 // | |
908 // Of course, there are other factors that can force a response to always be | 918 // Of course, there are other factors that can force a response to always be |
909 // validated or re-fetched. | 919 // validated or re-fetched. |
910 // | 920 // |
911 bool HttpResponseHeaders::RequiresValidation(const Time& request_time, | 921 Time HttpResponseHeaders::GetFreshnessExpiry(Time request_time, |
912 const Time& response_time, | 922 Time response_time) const { |
913 const Time& current_time) const { | 923 TimeDelta lifetime = GetFreshnessLifetime(response_time); |
914 TimeDelta lifetime = | 924 if (lifetime.is_max()) |
915 GetFreshnessLifetime(response_time); | 925 return Time::Max(); |
916 if (lifetime == TimeDelta()) | 926 Time origin = GetFreshnessOrigin(request_time, response_time); |
917 return true; | 927 return origin + lifetime; |
928 } | |
918 | 929 |
919 return lifetime <= GetCurrentAge(request_time, response_time, current_time); | 930 bool HttpResponseHeaders::RequiresValidation(base::Time request_time, |
gavinp
2014/02/13 18:50:26
s/base::Time/Time/
| |
931 base::Time response_time, | |
932 base::Time current_time) const { | |
933 return GetFreshnessExpiry(request_time, response_time) <= current_time; | |
920 } | 934 } |
921 | 935 |
922 // From RFC 2616 section 13.2.4: | 936 // From RFC 2616 section 13.2.4: |
923 // | 937 // |
924 // The max-age directive takes priority over Expires, so if max-age is present | 938 // The max-age directive takes priority over Expires, so if max-age is present |
925 // in a response, the calculation is simply: | 939 // in a response, the calculation is simply: |
926 // | 940 // |
927 // freshness_lifetime = max_age_value | 941 // freshness_lifetime = max_age_value |
928 // | 942 // |
929 // Otherwise, if Expires is present in the response, the calculation is: | 943 // Otherwise, if Expires is present in the response, the calculation is: |
930 // | 944 // |
931 // freshness_lifetime = expires_value - date_value | 945 // freshness_lifetime = expires_value - date_value |
932 // | 946 // |
933 // Note that neither of these calculations is vulnerable to clock skew, since | 947 // Note that neither of these calculations is vulnerable to clock skew, since |
934 // all of the information comes from the origin server. | 948 // all of the information comes from the origin server. |
935 // | 949 // |
936 // Also, if the response does have a Last-Modified time, the heuristic | 950 // Also, if the response does have a Last-Modified time, the heuristic |
937 // expiration value SHOULD be no more than some fraction of the interval since | 951 // expiration value SHOULD be no more than some fraction of the interval since |
938 // that time. A typical setting of this fraction might be 10%: | 952 // that time. A typical setting of this fraction might be 10%: |
939 // | 953 // |
940 // freshness_lifetime = (date_value - last_modified_value) * 0.10 | 954 // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
941 // | 955 // |
942 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( | 956 TimeDelta HttpResponseHeaders::GetFreshnessLifetime(Time response_time) const { |
943 const Time& response_time) const { | |
944 // Check for headers that force a response to never be fresh. For backwards | 957 // Check for headers that force a response to never be fresh. For backwards |
945 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: | 958 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: |
946 // no-cache" even though RFC 2616 does not specify it. | 959 // no-cache" even though RFC 2616 does not specify it. |
947 if (HasHeaderValue("cache-control", "no-cache") || | 960 if (HasHeaderValue("cache-control", "no-cache") || |
948 HasHeaderValue("cache-control", "no-store") || | 961 HasHeaderValue("cache-control", "no-store") || |
949 HasHeaderValue("pragma", "no-cache") || | 962 HasHeaderValue("pragma", "no-cache") || |
950 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 | 963 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 |
951 return TimeDelta(); // not fresh | 964 return TimeDelta(); // not fresh |
952 | 965 |
953 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 966 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1033 // * now | 1046 // * now |
1034 // * is the current (local) time | 1047 // * is the current (local) time |
1035 // */ | 1048 // */ |
1036 // apparent_age = max(0, response_time - date_value); | 1049 // apparent_age = max(0, response_time - date_value); |
1037 // corrected_received_age = max(apparent_age, age_value); | 1050 // corrected_received_age = max(apparent_age, age_value); |
1038 // response_delay = response_time - request_time; | 1051 // response_delay = response_time - request_time; |
1039 // corrected_initial_age = corrected_received_age + response_delay; | 1052 // corrected_initial_age = corrected_received_age + response_delay; |
1040 // resident_time = now - response_time; | 1053 // resident_time = now - response_time; |
1041 // current_age = corrected_initial_age + resident_time; | 1054 // current_age = corrected_initial_age + resident_time; |
1042 // | 1055 // |
1043 TimeDelta HttpResponseHeaders::GetCurrentAge(const Time& request_time, | 1056 // From this, we can derive the freshness origin, the time at which the |
1044 const Time& response_time, | 1057 // current_age = 0 and solve for now (aka freshness_origin): |
1045 const Time& current_time) const { | 1058 // |
1059 // corrected_initial_age + resident_time = 0 | |
1060 // corrected_initial_age + freshness_origin - response_time = 0 | |
1061 // freshness_origin = response_time - corrected_initial_age | |
1062 // | |
1063 Time HttpResponseHeaders::GetFreshnessOrigin(Time request_time, | |
1064 Time response_time) const { | |
1046 // If there is no Date header, then assume that the server response was | 1065 // If there is no Date header, then assume that the server response was |
1047 // generated at the time when we received the response. | 1066 // generated at the time when we received the response. |
1048 Time date_value; | 1067 Time date_value; |
1049 if (!GetDateValue(&date_value)) | 1068 if (!GetDateValue(&date_value)) |
1050 date_value = response_time; | 1069 date_value = response_time; |
1051 | 1070 |
1052 // If there is no Age header, then assume age is zero. GetAgeValue does not | 1071 // If there is no Age header, then assume age is zero. GetAgeValue does not |
1053 // modify its out param if the value does not exist. | 1072 // modify its out param if the value does not exist. |
1054 TimeDelta age_value; | 1073 TimeDelta age_value; |
1055 GetAgeValue(&age_value); | 1074 GetAgeValue(&age_value); |
1056 | 1075 |
1057 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); | 1076 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); |
1058 TimeDelta corrected_received_age = std::max(apparent_age, age_value); | 1077 TimeDelta corrected_received_age = std::max(apparent_age, age_value); |
1059 TimeDelta response_delay = response_time - request_time; | 1078 TimeDelta response_delay = response_time - request_time; |
1060 TimeDelta corrected_initial_age = corrected_received_age + response_delay; | 1079 TimeDelta corrected_initial_age = corrected_received_age + response_delay; |
1061 TimeDelta resident_time = current_time - response_time; | |
1062 TimeDelta current_age = corrected_initial_age + resident_time; | |
1063 | 1080 |
1064 return current_age; | 1081 return response_time - corrected_initial_age; |
1065 } | 1082 } |
1066 | 1083 |
1067 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { | 1084 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { |
1068 std::string name = "cache-control"; | 1085 std::string name = "cache-control"; |
1069 std::string value; | 1086 std::string value; |
1070 | 1087 |
1071 const char kMaxAgePrefix[] = "max-age="; | 1088 const char kMaxAgePrefix[] = "max-age="; |
1072 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; | 1089 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
1073 | 1090 |
1074 void* iter = NULL; | 1091 void* iter = NULL; |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1415 | 1432 |
1416 // Next, look for 'bypass'. | 1433 // Next, look for 'bypass'. |
1417 if (GetChromeProxyBypassDuration("bypass=", &proxy_info->bypass_duration)) | 1434 if (GetChromeProxyBypassDuration("bypass=", &proxy_info->bypass_duration)) |
1418 return true; | 1435 return true; |
1419 | 1436 |
1420 return false; | 1437 return false; |
1421 } | 1438 } |
1422 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) | 1439 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) |
1423 | 1440 |
1424 } // namespace net | 1441 } // namespace net |
OLD | NEW |