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|, | |
darin (slow to review)
2014/03/26 16:16:27
what is "freshness_origin"?
gavinp
2016/04/29 15:04:51
Removed this.
| |
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. | |
darin (slow to review)
2014/03/26 16:16:27
nit: this is no longer called "freshness_expiry".
gavinp
2016/04/29 15:04:51
That's right. Thanks for catching this.
| |
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::GetExpirationTime(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()) | |
917 return true; | |
918 | 926 |
919 return lifetime <= GetCurrentAge(request_time, response_time, current_time); | 927 // From RFC 2616 section 13.2.3: |
928 // | |
929 // Summary of age calculation algorithm, when a cache receives a response: | |
930 // | |
931 // /* | |
932 // * age_value | |
933 // * is the value of Age: header received by the cache with | |
934 // * this response. | |
935 // * date_value | |
936 // * is the value of the origin server's Date: header | |
937 // * request_time | |
938 // * is the (local) time when the cache made the request | |
939 // * that resulted in this cached response | |
940 // * response_time | |
941 // * is the (local) time when the cache received the | |
942 // * response | |
943 // * now | |
944 // * is the current (local) time | |
945 // */ | |
946 // apparent_age = max(0, response_time - date_value); | |
947 // corrected_received_age = max(apparent_age, age_value); | |
948 // response_delay = response_time - request_time; | |
949 // corrected_initial_age = corrected_received_age + response_delay; | |
950 // resident_time = now - response_time; | |
951 // current_age = corrected_initial_age + resident_time; | |
darin (slow to review)
2014/03/26 16:16:27
it would help to add a comment below here (a "NOTE
gavinp
2016/04/29 15:04:51
Hmmm, except it doesn't, because this function lac
| |
952 // | |
953 | |
954 // If there is no Date header, then assume that the server response was | |
955 // generated at the time when we received the response. | |
956 Time date_value; | |
957 if (!GetDateValue(&date_value)) | |
958 date_value = response_time; | |
959 | |
960 // If there is no Age header, then assume age is zero. GetAgeValue does not | |
961 // modify its out param if the value does not exist. | |
962 TimeDelta age_value; | |
963 GetAgeValue(&age_value); | |
964 | |
965 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); | |
966 TimeDelta corrected_received_age = std::max(apparent_age, age_value); | |
967 TimeDelta response_delay = response_time - request_time; | |
968 TimeDelta corrected_initial_age = corrected_received_age + response_delay; | |
969 | |
970 // From the age calculation algorithm above, we can derive the corrected | |
971 // response time, the |now| for which |current_age = 0|. Substituting into the | |
972 // RFC 2616 13.2.3 equations and simplifying: | |
973 // | |
974 // 0 = corrected_initial_age + resident_time | |
975 // | |
976 // corrected_initial_age + corrected_response_time - response_time = 0 | |
977 // | |
978 // corrected_response_time = response_time - corrected_initial_age | |
979 | |
980 Time corrected_response_time = response_time - corrected_initial_age; | |
981 return corrected_response_time + lifetime; | |
920 } | 982 } |
921 | 983 |
922 // From RFC 2616 section 13.2.4: | 984 // From RFC 2616 section 13.2.4: |
923 // | 985 // |
924 // 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 |
925 // in a response, the calculation is simply: | 987 // in a response, the calculation is simply: |
926 // | 988 // |
927 // freshness_lifetime = max_age_value | 989 // freshness_lifetime = max_age_value |
928 // | 990 // |
929 // Otherwise, if Expires is present in the response, the calculation is: | 991 // Otherwise, if Expires is present in the response, the calculation is: |
930 // | 992 // |
931 // freshness_lifetime = expires_value - date_value | 993 // freshness_lifetime = expires_value - date_value |
932 // | 994 // |
933 // 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 |
934 // all of the information comes from the origin server. | 996 // all of the information comes from the origin server. |
935 // | 997 // |
936 // 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 |
937 // 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 |
938 // that time. A typical setting of this fraction might be 10%: | 1000 // that time. A typical setting of this fraction might be 10%: |
939 // | 1001 // |
940 // freshness_lifetime = (date_value - last_modified_value) * 0.10 | 1002 // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
941 // | 1003 // |
942 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( | 1004 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 | 1005 // 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: | 1006 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: |
946 // no-cache" even though RFC 2616 does not specify it. | 1007 // no-cache" even though RFC 2616 does not specify it. |
947 if (HasHeaderValue("cache-control", "no-cache") || | 1008 if (HasHeaderValue("cache-control", "no-cache") || |
948 HasHeaderValue("cache-control", "no-store") || | 1009 HasHeaderValue("cache-control", "no-store") || |
949 HasHeaderValue("pragma", "no-cache") || | 1010 HasHeaderValue("pragma", "no-cache") || |
950 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 | 1011 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 |
951 return TimeDelta(); // not fresh | 1012 return TimeDelta(); // not fresh |
952 | 1013 |
953 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 1014 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1007 } | 1068 } |
1008 } | 1069 } |
1009 | 1070 |
1010 // These responses are implicitly fresh (unless otherwise overruled): | 1071 // These responses are implicitly fresh (unless otherwise overruled): |
1011 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410) | 1072 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410) |
1012 return TimeDelta::Max(); | 1073 return TimeDelta::Max(); |
1013 | 1074 |
1014 return TimeDelta(); // not fresh | 1075 return TimeDelta(); // not fresh |
1015 } | 1076 } |
1016 | 1077 |
1017 // From RFC 2616 section 13.2.3: | |
1018 // | |
1019 // Summary of age calculation algorithm, when a cache receives a response: | |
1020 // | |
1021 // /* | |
1022 // * age_value | |
1023 // * is the value of Age: header received by the cache with | |
1024 // * this response. | |
1025 // * date_value | |
1026 // * is the value of the origin server's Date: header | |
1027 // * request_time | |
1028 // * is the (local) time when the cache made the request | |
1029 // * that resulted in this cached response | |
1030 // * response_time | |
1031 // * is the (local) time when the cache received the | |
1032 // * response | |
1033 // * now | |
1034 // * is the current (local) time | |
1035 // */ | |
1036 // apparent_age = max(0, response_time - date_value); | |
1037 // corrected_received_age = max(apparent_age, age_value); | |
1038 // response_delay = response_time - request_time; | |
1039 // corrected_initial_age = corrected_received_age + response_delay; | |
1040 // resident_time = now - response_time; | |
1041 // current_age = corrected_initial_age + resident_time; | |
1042 // | |
1043 TimeDelta HttpResponseHeaders::GetCurrentAge(const Time& request_time, | |
1044 const Time& response_time, | |
1045 const Time& current_time) const { | |
1046 // If there is no Date header, then assume that the server response was | |
1047 // generated at the time when we received the response. | |
1048 Time date_value; | |
1049 if (!GetDateValue(&date_value)) | |
1050 date_value = response_time; | |
1051 | |
1052 // 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. | |
1054 TimeDelta age_value; | |
1055 GetAgeValue(&age_value); | |
1056 | |
1057 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); | |
1058 TimeDelta corrected_received_age = std::max(apparent_age, age_value); | |
1059 TimeDelta response_delay = response_time - request_time; | |
1060 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 | |
1064 return current_age; | |
1065 } | |
1066 | |
1067 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { | 1078 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { |
1068 std::string name = "cache-control"; | 1079 std::string name = "cache-control"; |
1069 std::string value; | 1080 std::string value; |
1070 | 1081 |
1071 const char kMaxAgePrefix[] = "max-age="; | 1082 const char kMaxAgePrefix[] = "max-age="; |
1072 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; | 1083 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
1073 | 1084 |
1074 void* iter = NULL; | 1085 void* iter = NULL; |
1075 while (EnumerateHeader(&iter, name, &value)) { | 1086 while (EnumerateHeader(&iter, name, &value)) { |
1076 if (value.size() > kMaxAgePrefixLen) { | 1087 if (value.size() > kMaxAgePrefixLen) { |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1441 iter = NULL; | 1452 iter = NULL; |
1442 while (EnumerateHeader(&iter, "via", &value)) | 1453 while (EnumerateHeader(&iter, "via", &value)) |
1443 if (value == kDeprecatedChromeProxyViaValue) | 1454 if (value == kDeprecatedChromeProxyViaValue) |
1444 return true; | 1455 return true; |
1445 | 1456 |
1446 return false; | 1457 return false; |
1447 } | 1458 } |
1448 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) | 1459 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) |
1449 | 1460 |
1450 } // namespace net | 1461 } // namespace net |
OLD | NEW |