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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
15 #include "net/proxy/proxy_service.h" | 15 #include "net/proxy/proxy_service.h" |
16 | 16 |
17 using base::StringPiece; | 17 using base::StringPiece; |
18 using base::TimeDelta; | 18 using base::TimeDelta; |
19 using net::ProxyService; | 19 using net::ProxyService; |
20 | 20 |
21 namespace data_reduction_proxy { | 21 namespace data_reduction_proxy { |
22 | 22 |
23 const char* kDataReductionProxyViaValues[] = {"Chrome-Compression-Proxy", | 23 const char* kDataReductionProxyViaValues[] = {"Chrome-Compression-Proxy", |
24 "Chrome Compression Proxy"}; | 24 "Chrome Compression Proxy"}; |
25 | 25 |
26 bool GetDataReductionProxyActionValue( | |
27 const net::HttpResponseHeaders* headers, | |
28 const std::string& action_prefix, | |
29 std::string* action_value) { | |
30 DCHECK(action_prefix.size()); | |
31 void* iter = NULL; | |
32 std::string value; | |
33 std::string name = "chrome-proxy"; | |
34 | |
35 while (headers->EnumerateHeader(&iter, name, &value)) { | |
36 if (value.size() > action_prefix.size()) { | |
37 if (LowerCaseEqualsASCII(value.begin(), | |
38 value.begin() + action_prefix.size(), | |
39 action_prefix.c_str()) && | |
40 value[action_prefix.size()] == '=') { | |
41 if (action_value) | |
42 *action_value = value.substr(action_prefix.size() + 1); | |
43 return true; | |
44 } | |
45 } | |
46 } | |
47 return false; | |
48 } | |
49 | |
26 bool GetDataReductionProxyBypassDuration( | 50 bool GetDataReductionProxyBypassDuration( |
bolian
2014/07/14 22:42:43
why doesn't this one call GetDataReductionProxyAct
xingx
2014/07/14 22:51:08
Need to discuss with Ben: my function (above) retu
| |
27 const net::HttpResponseHeaders* headers, | 51 const net::HttpResponseHeaders* headers, |
28 const std::string& action_prefix, | 52 const std::string& action_prefix, |
29 base::TimeDelta* duration) { | 53 base::TimeDelta* duration) { |
54 DCHECK(action_prefix.size()); | |
30 void* iter = NULL; | 55 void* iter = NULL; |
31 std::string value; | 56 std::string value; |
32 std::string name = "chrome-proxy"; | 57 std::string name = "chrome-proxy"; |
33 | 58 |
34 while (headers->EnumerateHeader(&iter, name, &value)) { | 59 while (headers->EnumerateHeader(&iter, name, &value)) { |
35 if (value.size() > action_prefix.size()) { | 60 if (value.size() > action_prefix.size()) { |
36 if (LowerCaseEqualsASCII(value.begin(), | 61 if (LowerCaseEqualsASCII(value.begin(), |
37 value.begin() + action_prefix.size(), | 62 value.begin() + action_prefix.size(), |
38 action_prefix.c_str())) { | 63 action_prefix.c_str()) && |
64 value[action_prefix.size()] == '=') { | |
39 int64 seconds; | 65 int64 seconds; |
40 if (!base::StringToInt64( | 66 if (!base::StringToInt64( |
41 StringPiece(value.begin() + action_prefix.size(), value.end()), | 67 StringPiece(value.begin() + action_prefix.size() + 1, |
68 value.end()), | |
42 &seconds) || seconds < 0) { | 69 &seconds) || seconds < 0) { |
43 continue; // In case there is a well formed instruction. | 70 continue; // In case there is a well formed instruction. |
44 } | 71 } |
45 *duration = TimeDelta::FromSeconds(seconds); | 72 *duration = TimeDelta::FromSeconds(seconds); |
46 return true; | 73 return true; |
47 } | 74 } |
48 } | 75 } |
49 } | 76 } |
50 return false; | 77 return false; |
51 } | 78 } |
52 | 79 |
53 bool GetDataReductionProxyInfo(const net::HttpResponseHeaders* headers, | 80 bool GetDataReductionProxyInfo(const net::HttpResponseHeaders* headers, |
54 DataReductionProxyInfo* proxy_info) { | 81 DataReductionProxyInfo* proxy_info) { |
55 DCHECK(proxy_info); | 82 DCHECK(proxy_info); |
56 proxy_info->bypass_all = false; | 83 proxy_info->bypass_all = false; |
57 proxy_info->bypass_duration = TimeDelta(); | 84 proxy_info->bypass_duration = TimeDelta(); |
58 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where | 85 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where |
59 // <duration> is the number of seconds to wait before retrying | 86 // <duration> is the number of seconds to wait before retrying |
60 // the proxy. If the duration is 0, then the default proxy retry delay | 87 // the proxy. If the duration is 0, then the default proxy retry delay |
61 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. | 88 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. |
62 // 'bypass' instructs Chrome to bypass the currently connected data reduction | 89 // 'bypass' instructs Chrome to bypass the currently connected data reduction |
63 // proxy, whereas 'block' instructs Chrome to bypass all available data | 90 // proxy, whereas 'block' instructs Chrome to bypass all available data |
64 // reduction proxies. | 91 // reduction proxies. |
65 | 92 |
66 // 'block' takes precedence over 'bypass', so look for it first. | 93 // 'block' takes precedence over 'bypass', so look for it first. |
67 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. | 94 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. |
68 if (GetDataReductionProxyBypassDuration( | 95 if (GetDataReductionProxyBypassDuration( |
69 headers, "block=", &proxy_info->bypass_duration)) { | 96 headers, "block", &proxy_info->bypass_duration)) { |
70 proxy_info->bypass_all = true; | 97 proxy_info->bypass_all = true; |
71 return true; | 98 return true; |
72 } | 99 } |
73 | 100 |
74 // Next, look for 'bypass'. | 101 // Next, look for 'bypass'. |
75 if (GetDataReductionProxyBypassDuration( | 102 if (GetDataReductionProxyBypassDuration( |
76 headers, "bypass=", &proxy_info->bypass_duration)) { | 103 headers, "bypass", &proxy_info->bypass_duration)) { |
77 return true; | 104 return true; |
78 } | 105 } |
79 return false; | 106 return false; |
80 } | 107 } |
81 | 108 |
82 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { | 109 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { |
83 const size_t kVersionSize = 4; | 110 const size_t kVersionSize = 4; |
84 size_t value_len = strlen(kDataReductionProxyViaValues[0]); | 111 size_t value_len = strlen(kDataReductionProxyViaValues[0]); |
85 void* iter = NULL; | 112 void* iter = NULL; |
86 std::string value; | 113 std::string value; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { | 164 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { |
138 return ProxyService::PROXY_4XX_BYPASS; | 165 return ProxyService::PROXY_4XX_BYPASS; |
139 } | 166 } |
140 return ProxyService::MISSING_VIA_HEADER; | 167 return ProxyService::MISSING_VIA_HEADER; |
141 } | 168 } |
142 // There is no bypass event. | 169 // There is no bypass event. |
143 return ProxyService::BYPASS_EVENT_TYPE_MAX; | 170 return ProxyService::BYPASS_EVENT_TYPE_MAX; |
144 } | 171 } |
145 | 172 |
146 } // namespace data_reduction_proxy | 173 } // namespace data_reduction_proxy |
OLD | NEW |