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; |
914 TimeDelta lifetime = | 924 bool finite_lifetime; |
915 GetFreshnessLifetime(response_time); | 925 GetFreshnessLifetime(response_time, &lifetime, &finite_lifetime); |
916 if (lifetime == TimeDelta()) | 926 if (!finite_lifetime) |
917 return true; | 927 return Time::Max(); |
918 | 928 Time origin = GetFreshnessOrigin(request_time, response_time); |
919 return lifetime <= GetCurrentAge(request_time, response_time, current_time); | 929 return origin + lifetime; |
920 } | 930 } |
921 | 931 |
922 // From RFC 2616 section 13.2.4: | 932 // From RFC 2616 section 13.2.4: |
923 // | 933 // |
924 // The max-age directive takes priority over Expires, so if max-age is present | 934 // The max-age directive takes priority over Expires, so if max-age is present |
925 // in a response, the calculation is simply: | 935 // in a response, the calculation is simply: |
926 // | 936 // |
927 // freshness_lifetime = max_age_value | 937 // freshness_lifetime = max_age_value |
928 // | 938 // |
929 // Otherwise, if Expires is present in the response, the calculation is: | 939 // Otherwise, if Expires is present in the response, the calculation is: |
930 // | 940 // |
931 // freshness_lifetime = expires_value - date_value | 941 // freshness_lifetime = expires_value - date_value |
932 // | 942 // |
933 // Note that neither of these calculations is vulnerable to clock skew, since | 943 // Note that neither of these calculations is vulnerable to clock skew, since |
934 // all of the information comes from the origin server. | 944 // all of the information comes from the origin server. |
935 // | 945 // |
936 // Also, if the response does have a Last-Modified time, the heuristic | 946 // 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 | 947 // 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%: | 948 // that time. A typical setting of this fraction might be 10%: |
939 // | 949 // |
940 // freshness_lifetime = (date_value - last_modified_value) * 0.10 | 950 // freshness_lifetime = (date_value - last_modified_value) * 0.10 |
941 // | 951 // |
942 TimeDelta HttpResponseHeaders::GetFreshnessLifetime( | 952 void HttpResponseHeaders::GetFreshnessLifetime( |
943 const Time& response_time) const { | 953 Time response_time, |
| 954 TimeDelta* finite_freshness_lifetime, |
| 955 bool* was_finite) const { |
| 956 *finite_freshness_lifetime = TimeDelta(); |
| 957 *was_finite = true; |
| 958 |
944 // Check for headers that force a response to never be fresh. For backwards | 959 // 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: | 960 // compat, we treat "Pragma: no-cache" as a synonym for "Cache-Control: |
946 // no-cache" even though RFC 2616 does not specify it. | 961 // no-cache" even though RFC 2616 does not specify it. |
947 if (HasHeaderValue("cache-control", "no-cache") || | 962 if (HasHeaderValue("cache-control", "no-cache") || |
948 HasHeaderValue("cache-control", "no-store") || | 963 HasHeaderValue("cache-control", "no-store") || |
949 HasHeaderValue("pragma", "no-cache") || | 964 HasHeaderValue("pragma", "no-cache") || |
950 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 | 965 HasHeaderValue("vary", "*")) // see RFC 2616 section 13.6 |
951 return TimeDelta(); // not fresh | 966 return; // not fresh |
952 | 967 |
953 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the | 968 // NOTE: "Cache-Control: max-age" overrides Expires, so we only check the |
954 // Expires header after checking for max-age in GetFreshnessLifetime. This | 969 // Expires header after checking for max-age in GetFreshnessLifetime. This |
955 // is important since "Expires: <date in the past>" means not fresh, but | 970 // is important since "Expires: <date in the past>" means not fresh, but |
956 // it should not trump a max-age value. | 971 // it should not trump a max-age value. |
957 | 972 |
958 TimeDelta max_age_value; | 973 if (GetMaxAgeValue(finite_freshness_lifetime)) |
959 if (GetMaxAgeValue(&max_age_value)) | 974 return; |
960 return max_age_value; | |
961 | 975 |
962 // If there is no Date header, then assume that the server response was | 976 // If there is no Date header, then assume that the server response was |
963 // generated at the time when we received the response. | 977 // generated at the time when we received the response. |
964 Time date_value; | 978 Time date_value; |
965 if (!GetDateValue(&date_value)) | 979 if (!GetDateValue(&date_value)) |
966 date_value = response_time; | 980 date_value = response_time; |
967 | 981 |
968 Time expires_value; | 982 Time expires_value; |
969 if (GetExpiresValue(&expires_value)) { | 983 if (GetExpiresValue(&expires_value)) { |
970 // The expires value can be a date in the past! | 984 // The expires value can be a date in the past! |
971 if (expires_value > date_value) | 985 if (expires_value > date_value) |
972 return expires_value - date_value; | 986 *finite_freshness_lifetime = expires_value - date_value; |
973 | 987 return; |
974 return TimeDelta(); // not fresh | |
975 } | 988 } |
976 | 989 |
977 // From RFC 2616 section 13.4: | 990 // From RFC 2616 section 13.4: |
978 // | 991 // |
979 // A response received with a status code of 200, 203, 206, 300, 301 or 410 | 992 // A response received with a status code of 200, 203, 206, 300, 301 or 410 |
980 // MAY be stored by a cache and used in reply to a subsequent request, | 993 // MAY be stored by a cache and used in reply to a subsequent request, |
981 // subject to the expiration mechanism, unless a cache-control directive | 994 // subject to the expiration mechanism, unless a cache-control directive |
982 // prohibits caching. | 995 // prohibits caching. |
983 // ... | 996 // ... |
984 // A response received with any other status code (e.g. status codes 302 | 997 // A response received with any other status code (e.g. status codes 302 |
(...skipping 11 matching lines...) Expand all Loading... |
996 // the cached response is stale.) | 1009 // the cached response is stale.) |
997 // | 1010 // |
998 if ((response_code_ == 200 || response_code_ == 203 || | 1011 if ((response_code_ == 200 || response_code_ == 203 || |
999 response_code_ == 206) && | 1012 response_code_ == 206) && |
1000 !HasHeaderValue("cache-control", "must-revalidate")) { | 1013 !HasHeaderValue("cache-control", "must-revalidate")) { |
1001 // TODO(darin): Implement a smarter heuristic. | 1014 // TODO(darin): Implement a smarter heuristic. |
1002 Time last_modified_value; | 1015 Time last_modified_value; |
1003 if (GetLastModifiedValue(&last_modified_value)) { | 1016 if (GetLastModifiedValue(&last_modified_value)) { |
1004 // The last-modified value can be a date in the past! | 1017 // The last-modified value can be a date in the past! |
1005 if (last_modified_value <= date_value) | 1018 if (last_modified_value <= date_value) |
1006 return (date_value - last_modified_value) / 10; | 1019 *finite_freshness_lifetime = (date_value - last_modified_value) / 10; |
| 1020 return; |
1007 } | 1021 } |
1008 } | 1022 } |
1009 | 1023 |
1010 // These responses are implicitly fresh (unless otherwise overruled): | 1024 // These responses are implicitly fresh (unless otherwise overruled): |
1011 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410) | 1025 if (response_code_ == 300 || response_code_ == 301 || response_code_ == 410) { |
1012 return TimeDelta::FromMicroseconds(kint64max); | 1026 *was_finite = false; |
| 1027 return; |
| 1028 } |
1013 | 1029 |
1014 return TimeDelta(); // not fresh | 1030 return; // not fresh. |
1015 } | 1031 } |
1016 | 1032 |
1017 // From RFC 2616 section 13.2.3: | 1033 // From RFC 2616 section 13.2.3: |
1018 // | 1034 // |
1019 // Summary of age calculation algorithm, when a cache receives a response: | 1035 // Summary of age calculation algorithm, when a cache receives a response: |
1020 // | 1036 // |
1021 // /* | 1037 // /* |
1022 // * age_value | 1038 // * age_value |
1023 // * is the value of Age: header received by the cache with | 1039 // * is the value of Age: header received by the cache with |
1024 // * this response. | 1040 // * this response. |
1025 // * date_value | 1041 // * date_value |
1026 // * is the value of the origin server's Date: header | 1042 // * is the value of the origin server's Date: header |
1027 // * request_time | 1043 // * request_time |
1028 // * is the (local) time when the cache made the request | 1044 // * is the (local) time when the cache made the request |
1029 // * that resulted in this cached response | 1045 // * that resulted in this cached response |
1030 // * response_time | 1046 // * response_time |
1031 // * is the (local) time when the cache received the | 1047 // * is the (local) time when the cache received the |
1032 // * response | 1048 // * response |
1033 // * now | 1049 // * now |
1034 // * is the current (local) time | 1050 // * is the current (local) time |
1035 // */ | 1051 // */ |
1036 // apparent_age = max(0, response_time - date_value); | 1052 // apparent_age = max(0, response_time - date_value); |
1037 // corrected_received_age = max(apparent_age, age_value); | 1053 // corrected_received_age = max(apparent_age, age_value); |
1038 // response_delay = response_time - request_time; | 1054 // response_delay = response_time - request_time; |
1039 // corrected_initial_age = corrected_received_age + response_delay; | 1055 // corrected_initial_age = corrected_received_age + response_delay; |
1040 // resident_time = now - response_time; | 1056 // resident_time = now - response_time; |
1041 // current_age = corrected_initial_age + resident_time; | 1057 // current_age = corrected_initial_age + resident_time; |
1042 // | 1058 // |
1043 TimeDelta HttpResponseHeaders::GetCurrentAge(const Time& request_time, | 1059 // From this, we can derive the freshness origin, the time at which the |
1044 const Time& response_time, | 1060 // current_age = 0 and solve for now (aka freshness_origin): |
1045 const Time& current_time) const { | 1061 // |
| 1062 // corrected_initial_age + resident_time = 0 |
| 1063 // corrected_initial_age + freshness_origin - response_time = 0 |
| 1064 // freshness_origin = response_time - corrected_initial_age |
| 1065 // |
| 1066 Time HttpResponseHeaders::GetFreshnessOrigin(Time request_time, |
| 1067 Time response_time) const { |
1046 // If there is no Date header, then assume that the server response was | 1068 // If there is no Date header, then assume that the server response was |
1047 // generated at the time when we received the response. | 1069 // generated at the time when we received the response. |
1048 Time date_value; | 1070 Time date_value; |
1049 if (!GetDateValue(&date_value)) | 1071 if (!GetDateValue(&date_value)) |
1050 date_value = response_time; | 1072 date_value = response_time; |
1051 | 1073 |
1052 // If there is no Age header, then assume age is zero. GetAgeValue does not | 1074 // 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. | 1075 // modify its out param if the value does not exist. |
1054 TimeDelta age_value; | 1076 TimeDelta age_value; |
1055 GetAgeValue(&age_value); | 1077 GetAgeValue(&age_value); |
1056 | 1078 |
1057 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); | 1079 TimeDelta apparent_age = std::max(TimeDelta(), response_time - date_value); |
1058 TimeDelta corrected_received_age = std::max(apparent_age, age_value); | 1080 TimeDelta corrected_received_age = std::max(apparent_age, age_value); |
1059 TimeDelta response_delay = response_time - request_time; | 1081 TimeDelta response_delay = response_time - request_time; |
1060 TimeDelta corrected_initial_age = corrected_received_age + response_delay; | 1082 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 | 1083 |
1064 return current_age; | 1084 return response_time - corrected_initial_age; |
1065 } | 1085 } |
1066 | 1086 |
1067 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { | 1087 bool HttpResponseHeaders::GetMaxAgeValue(TimeDelta* result) const { |
1068 std::string name = "cache-control"; | 1088 std::string name = "cache-control"; |
1069 std::string value; | 1089 std::string value; |
1070 | 1090 |
1071 const char kMaxAgePrefix[] = "max-age="; | 1091 const char kMaxAgePrefix[] = "max-age="; |
1072 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; | 1092 const size_t kMaxAgePrefixLen = arraysize(kMaxAgePrefix) - 1; |
1073 | 1093 |
1074 void* iter = NULL; | 1094 void* iter = NULL; |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1415 | 1435 |
1416 // Next, look for 'bypass'. | 1436 // Next, look for 'bypass'. |
1417 if (GetChromeProxyBypassDuration("bypass=", &proxy_info->bypass_duration)) | 1437 if (GetChromeProxyBypassDuration("bypass=", &proxy_info->bypass_duration)) |
1418 return true; | 1438 return true; |
1419 | 1439 |
1420 return false; | 1440 return false; |
1421 } | 1441 } |
1422 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) | 1442 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) |
1423 | 1443 |
1424 } // namespace net | 1444 } // namespace net |
OLD | NEW |