OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | 5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
16 #include "net/proxy/proxy_service.h" | 16 #include "net/proxy/proxy_service.h" |
17 | 17 |
18 using base::StringPiece; | 18 using base::StringPiece; |
19 using base::TimeDelta; | 19 using base::TimeDelta; |
20 using net::ProxyService; | 20 using net::ProxyService; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kChromeProxyHeader[] = "chrome-proxy"; | |
25 const char kActionValueDelimiter = '='; | |
26 | |
27 const char kChromeProxyActionBlock[] = "block"; | |
28 const char kChromeProxyActionBypass[] = "bypass"; | |
29 | |
30 // Actions for tamper detection fingerprints. | |
31 const char kChromeProxyActionFingerprintChromeProxy[] = "fcp"; | |
32 const char kChromeProxyActionFingerprintVia[] = "fvia"; | |
33 const char kChromeProxyActionFingerprintOtherHeaders[] = "foh"; | |
34 const char kChromeProxyActionFingerprintContentLength[] = "fcl"; | |
35 | |
36 const int kShortBypassMaxSeconds = 59; | 24 const int kShortBypassMaxSeconds = 59; |
37 const int kMediumBypassMaxSeconds = 300; | 25 const int kMediumBypassMaxSeconds = 300; |
38 | 26 |
39 // Returns a random bypass duration between 1 and 5 minutes. | 27 // Returns a random bypass duration between 1 and 5 minutes. |
40 base::TimeDelta GetDefaultBypassDuration() { | 28 base::TimeDelta GetDefaultBypassDuration() { |
41 const int64 delta_ms = | 29 const int64 delta_ms = |
42 base::RandInt(base::TimeDelta::FromMinutes(1).InMilliseconds(), | 30 base::RandInt(base::TimeDelta::FromMinutes(1).InMilliseconds(), |
43 base::TimeDelta::FromMinutes(5).InMilliseconds()); | 31 base::TimeDelta::FromMinutes(5).InMilliseconds()); |
44 return TimeDelta::FromMilliseconds(delta_ms); | 32 return TimeDelta::FromMilliseconds(delta_ms); |
45 } | 33 } |
46 } // namespace | 34 } // namespace |
47 | 35 |
48 namespace data_reduction_proxy { | 36 namespace data_reduction_proxy { |
49 | 37 |
50 bool GetDataReductionProxyActionValue( | |
51 const net::HttpResponseHeaders* headers, | |
52 const std::string& action_prefix, | |
53 std::string* action_value) { | |
54 DCHECK(headers); | |
55 DCHECK(!action_prefix.empty()); | |
56 // A valid action does not include a trailing '='. | |
57 DCHECK(action_prefix[action_prefix.size() - 1] != kActionValueDelimiter); | |
58 void* iter = NULL; | |
59 std::string value; | |
60 std::string prefix = action_prefix + kActionValueDelimiter; | |
61 | |
62 while (headers->EnumerateHeader(&iter, kChromeProxyHeader, &value)) { | |
63 if (value.size() > prefix.size()) { | |
64 if (LowerCaseEqualsASCII(value.begin(), | |
65 value.begin() + prefix.size(), | |
66 prefix.c_str())) { | |
67 if (action_value) | |
68 *action_value = value.substr(prefix.size()); | |
69 return true; | |
70 } | |
71 } | |
72 } | |
73 return false; | |
74 } | |
75 | |
76 bool ParseHeadersAndSetBypassDuration(const net::HttpResponseHeaders* headers, | 38 bool ParseHeadersAndSetBypassDuration(const net::HttpResponseHeaders* headers, |
77 const std::string& action_prefix, | 39 const std::string& action_prefix, |
78 base::TimeDelta* bypass_duration) { | 40 base::TimeDelta* bypass_duration) { |
79 DCHECK(headers); | |
80 DCHECK(!action_prefix.empty()); | |
81 // A valid action does not include a trailing '='. | |
82 DCHECK(action_prefix[action_prefix.size() - 1] != kActionValueDelimiter); | |
83 void* iter = NULL; | 41 void* iter = NULL; |
84 std::string value; | 42 std::string value; |
85 std::string prefix = action_prefix + kActionValueDelimiter; | 43 std::string name = "chrome-proxy"; |
86 | 44 |
87 while (headers->EnumerateHeader(&iter, kChromeProxyHeader, &value)) { | 45 while (headers->EnumerateHeader(&iter, name, &value)) { |
88 if (value.size() > prefix.size()) { | 46 if (value.size() > action_prefix.size()) { |
89 if (LowerCaseEqualsASCII(value.begin(), | 47 if (LowerCaseEqualsASCII(value.begin(), |
90 value.begin() + prefix.size(), | 48 value.begin() + action_prefix.size(), |
91 prefix.c_str())) { | 49 action_prefix.c_str())) { |
92 int64 seconds; | 50 int64 seconds; |
93 if (!base::StringToInt64( | 51 if (!base::StringToInt64( |
94 StringPiece(value.begin() + prefix.size(), value.end()), | 52 StringPiece(value.begin() + action_prefix.size(), value.end()), |
95 &seconds) || seconds < 0) { | 53 &seconds) || seconds < 0) { |
96 continue; // In case there is a well formed instruction. | 54 continue; // In case there is a well formed instruction. |
97 } | 55 } |
98 if (seconds != 0) { | 56 if (seconds != 0) { |
99 *bypass_duration = TimeDelta::FromSeconds(seconds); | 57 *bypass_duration = TimeDelta::FromSeconds(seconds); |
100 } else { | 58 } else { |
101 // Server deferred to us to choose a duration. Default to a random | 59 // Server deferred to us to choose a duration. Default to a random |
102 // duration between one and five minutes. | 60 // duration between one and five minutes. |
103 *bypass_duration = GetDefaultBypassDuration(); | 61 *bypass_duration = GetDefaultBypassDuration(); |
104 } | 62 } |
(...skipping 12 matching lines...) Expand all Loading... |
117 // <duration> is the number of seconds to wait before retrying | 75 // <duration> is the number of seconds to wait before retrying |
118 // the proxy. If the duration is 0, then the default proxy retry delay | 76 // the proxy. If the duration is 0, then the default proxy retry delay |
119 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. | 77 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. |
120 // 'bypass' instructs Chrome to bypass the currently connected data reduction | 78 // 'bypass' instructs Chrome to bypass the currently connected data reduction |
121 // proxy, whereas 'block' instructs Chrome to bypass all available data | 79 // proxy, whereas 'block' instructs Chrome to bypass all available data |
122 // reduction proxies. | 80 // reduction proxies. |
123 | 81 |
124 // 'block' takes precedence over 'bypass', so look for it first. | 82 // 'block' takes precedence over 'bypass', so look for it first. |
125 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. | 83 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. |
126 if (ParseHeadersAndSetBypassDuration( | 84 if (ParseHeadersAndSetBypassDuration( |
127 headers, kChromeProxyActionBlock, &proxy_info->bypass_duration)) { | 85 headers, "block=", &proxy_info->bypass_duration)) { |
128 proxy_info->bypass_all = true; | 86 proxy_info->bypass_all = true; |
129 return true; | 87 return true; |
130 } | 88 } |
131 | 89 |
132 // Next, look for 'bypass'. | 90 // Next, look for 'bypass'. |
133 if (ParseHeadersAndSetBypassDuration( | 91 if (ParseHeadersAndSetBypassDuration( |
134 headers, kChromeProxyActionBypass, &proxy_info->bypass_duration)) { | 92 headers, "bypass=", &proxy_info->bypass_duration)) { |
135 return true; | 93 return true; |
136 } | 94 } |
137 return false; | 95 return false; |
138 } | 96 } |
139 | 97 |
140 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers, | 98 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { |
141 bool* has_intermediary) { | |
142 const size_t kVersionSize = 4; | 99 const size_t kVersionSize = 4; |
143 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy"; | 100 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy"; |
144 size_t value_len = strlen(kDataReductionProxyViaValue); | 101 size_t value_len = strlen(kDataReductionProxyViaValue); |
145 void* iter = NULL; | 102 void* iter = NULL; |
146 std::string value; | 103 std::string value; |
147 | 104 |
148 // Case-sensitive comparison of |value|. Assumes the received protocol and the | 105 // Case-sensitive comparison of |value|. Assumes the received protocol and the |
149 // space following it are always |kVersionSize| characters. E.g., | 106 // space following it are always |kVersionSize| characters. E.g., |
150 // 'Via: 1.1 Chrome-Compression-Proxy' | 107 // 'Via: 1.1 Chrome-Compression-Proxy' |
151 while (headers->EnumerateHeader(&iter, "via", &value)) { | 108 while (headers->EnumerateHeader(&iter, "via", &value)) { |
152 if (value.size() >= kVersionSize + value_len && | 109 if (value.size() >= kVersionSize + value_len && |
153 !value.compare(kVersionSize, value_len, kDataReductionProxyViaValue)) { | 110 !value.compare(kVersionSize, value_len, kDataReductionProxyViaValue)) |
154 if (has_intermediary) | |
155 // We assume intermediary exists if there is another Via header after | |
156 // the data reduction proxy's Via header. | |
157 *has_intermediary = !(headers->EnumerateHeader(&iter, "via", &value)); | |
158 return true; | 111 return true; |
159 } | |
160 } | 112 } |
161 | 113 |
162 // TODO(bengr): Remove deprecated header value. | 114 // TODO(bengr): Remove deprecated header value. |
163 const char kDeprecatedDataReductionProxyViaValue[] = | 115 const char kDeprecatedDataReductionProxyViaValue[] = |
164 "1.1 Chrome Compression Proxy"; | 116 "1.1 Chrome Compression Proxy"; |
165 iter = NULL; | 117 iter = NULL; |
166 while (headers->EnumerateHeader(&iter, "via", &value)) | 118 while (headers->EnumerateHeader(&iter, "via", &value)) |
167 if (value == kDeprecatedDataReductionProxyViaValue) { | 119 if (value == kDeprecatedDataReductionProxyViaValue) |
168 if (has_intermediary) | |
169 *has_intermediary = !(headers->EnumerateHeader(&iter, "via", &value)); | |
170 return true; | 120 return true; |
171 } | |
172 | 121 |
173 return false; | 122 return false; |
174 } | 123 } |
175 | 124 |
176 net::ProxyService::DataReductionProxyBypassType | 125 net::ProxyService::DataReductionProxyBypassType |
177 GetDataReductionProxyBypassType( | 126 GetDataReductionProxyBypassType( |
178 const net::HttpResponseHeaders* headers, | 127 const net::HttpResponseHeaders* headers, |
179 DataReductionProxyInfo* data_reduction_proxy_info) { | 128 DataReductionProxyInfo* data_reduction_proxy_info) { |
180 DCHECK(data_reduction_proxy_info); | 129 DCHECK(data_reduction_proxy_info); |
181 if (ParseHeadersAndSetProxyInfo(headers, data_reduction_proxy_info)) { | 130 if (ParseHeadersAndSetProxyInfo(headers, data_reduction_proxy_info)) { |
(...skipping 13 matching lines...) Expand all Loading... |
195 if (headers->response_code() == net::HTTP_BAD_GATEWAY) | 144 if (headers->response_code() == net::HTTP_BAD_GATEWAY) |
196 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY; | 145 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY; |
197 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) | 146 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) |
198 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE; | 147 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE; |
199 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be | 148 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be |
200 // interpreted by data reduction proxy. | 149 // interpreted by data reduction proxy. |
201 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED && | 150 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED && |
202 !headers->HasHeader("Proxy-Authenticate")) { | 151 !headers->HasHeader("Proxy-Authenticate")) { |
203 return ProxyService::MALFORMED_407; | 152 return ProxyService::MALFORMED_407; |
204 } | 153 } |
205 if (!HasDataReductionProxyViaHeader(headers, NULL) && | 154 if (!HasDataReductionProxyViaHeader(headers) && |
206 (headers->response_code() != net::HTTP_NOT_MODIFIED)) { | 155 (headers->response_code() != net::HTTP_NOT_MODIFIED)) { |
207 // A Via header might not be present in a 304. Since the goal of a 304 | 156 // A Via header might not be present in a 304. Since the goal of a 304 |
208 // response is to minimize information transfer, a sender in general | 157 // response is to minimize information transfer, a sender in general |
209 // should not generate representation metadata other than Cache-Control, | 158 // should not generate representation metadata other than Cache-Control, |
210 // Content-Location, Date, ETag, Expires, and Vary. | 159 // Content-Location, Date, ETag, Expires, and Vary. |
211 | 160 |
212 // The proxy Via header might also not be present in a 4xx response. | 161 // The proxy Via header might also not be present in a 4xx response. |
213 // Separate this case from other responses that are missing the header. | 162 // Separate this case from other responses that are missing the header. |
214 if (headers->response_code() >= net::HTTP_BAD_REQUEST && | 163 if (headers->response_code() >= net::HTTP_BAD_REQUEST && |
215 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { | 164 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { |
216 return ProxyService::MISSING_VIA_HEADER_4XX; | 165 return ProxyService::MISSING_VIA_HEADER_4XX; |
217 } | 166 } |
218 return ProxyService::MISSING_VIA_HEADER_OTHER; | 167 return ProxyService::MISSING_VIA_HEADER_OTHER; |
219 } | 168 } |
220 // There is no bypass event. | 169 // There is no bypass event. |
221 return ProxyService::BYPASS_EVENT_TYPE_MAX; | 170 return ProxyService::BYPASS_EVENT_TYPE_MAX; |
222 } | 171 } |
223 | 172 |
224 bool GetDataReductionProxyActionFingerprintChromeProxy( | |
225 const net::HttpResponseHeaders* headers, | |
226 std::string* chrome_proxy_fingerprint) { | |
227 return GetDataReductionProxyActionValue( | |
228 headers, | |
229 kChromeProxyActionFingerprintChromeProxy, | |
230 chrome_proxy_fingerprint); | |
231 } | |
232 | |
233 bool GetDataReductionProxyActionFingerprintVia( | |
234 const net::HttpResponseHeaders* headers, | |
235 std::string* via_fingerprint) { | |
236 return GetDataReductionProxyActionValue( | |
237 headers, | |
238 kChromeProxyActionFingerprintVia, | |
239 via_fingerprint); | |
240 } | |
241 | |
242 bool GetDataReductionProxyActionFingerprintOtherHeaders( | |
243 const net::HttpResponseHeaders* headers, | |
244 std::string* other_headers_fingerprint) { | |
245 return GetDataReductionProxyActionValue( | |
246 headers, | |
247 kChromeProxyActionFingerprintOtherHeaders, | |
248 other_headers_fingerprint); | |
249 } | |
250 | |
251 bool GetDataReductionProxyActionFingerprintContentLength( | |
252 const net::HttpResponseHeaders* headers, | |
253 std::string* content_length_fingerprint) { | |
254 return GetDataReductionProxyActionValue( | |
255 headers, | |
256 kChromeProxyActionFingerprintContentLength, | |
257 content_length_fingerprint); | |
258 } | |
259 | |
260 void GetDataReductionProxyHeaderWithFingerprintRemoved( | |
261 const net::HttpResponseHeaders* headers, | |
262 std::vector<std::string>* values) { | |
263 DCHECK(values); | |
264 std::string chrome_proxy_fingerprint_prefix = std::string( | |
265 kChromeProxyActionFingerprintChromeProxy) + kActionValueDelimiter; | |
266 | |
267 std::string value; | |
268 void* iter = NULL; | |
269 while (headers->EnumerateHeader(&iter, kChromeProxyHeader, &value)) { | |
270 if (!LowerCaseEqualsASCII( | |
271 value.begin(), | |
272 value.begin() + chrome_proxy_fingerprint_prefix.size(), | |
273 chrome_proxy_fingerprint_prefix.c_str())) { | |
274 values->push_back(value); | |
275 } | |
276 } | |
277 } | |
278 | |
279 } // namespace data_reduction_proxy | 173 } // namespace data_reduction_proxy |
OLD | NEW |