Chromium Code Reviews| 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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_status_code.h" | 15 #include "net/http/http_status_code.h" |
| 15 #include "net/proxy/proxy_service.h" | 16 #include "net/proxy/proxy_service.h" |
| 16 | 17 |
| 17 using base::StringPiece; | 18 using base::StringPiece; |
| 18 using base::TimeDelta; | 19 using base::TimeDelta; |
| 19 using net::ProxyService; | 20 using net::ProxyService; |
| 20 | 21 |
| 21 namespace data_reduction_proxy { | 22 namespace data_reduction_proxy { |
| 22 | 23 |
| 23 bool GetDataReductionProxyBypassDuration( | 24 bool ParseHeadersAndSetProxyInfoBypassDuration( |
| 24 const net::HttpResponseHeaders* headers, | 25 const net::HttpResponseHeaders* headers, |
| 25 const std::string& action_prefix, | 26 const std::string& action_prefix, |
| 26 base::TimeDelta* duration) { | 27 DataReductionProxyInfo* proxy_info) { |
| 27 void* iter = NULL; | 28 void* iter = NULL; |
| 28 std::string value; | 29 std::string value; |
| 29 std::string name = "chrome-proxy"; | 30 std::string name = "chrome-proxy"; |
| 30 | 31 |
| 31 while (headers->EnumerateHeader(&iter, name, &value)) { | 32 while (headers->EnumerateHeader(&iter, name, &value)) { |
| 32 if (value.size() > action_prefix.size()) { | 33 if (value.size() > action_prefix.size()) { |
| 33 if (LowerCaseEqualsASCII(value.begin(), | 34 if (LowerCaseEqualsASCII(value.begin(), |
| 34 value.begin() + action_prefix.size(), | 35 value.begin() + action_prefix.size(), |
| 35 action_prefix.c_str())) { | 36 action_prefix.c_str())) { |
| 36 int64 seconds; | 37 int64 seconds; |
| 37 if (!base::StringToInt64( | 38 if (!base::StringToInt64( |
| 38 StringPiece(value.begin() + action_prefix.size(), value.end()), | 39 StringPiece(value.begin() + action_prefix.size(), value.end()), |
| 39 &seconds) || seconds < 0) { | 40 &seconds) || seconds < 0) { |
| 40 continue; // In case there is a well formed instruction. | 41 continue; // In case there is a well formed instruction. |
| 41 } | 42 } |
| 42 *duration = TimeDelta::FromSeconds(seconds); | 43 if (seconds != 0) { |
| 44 proxy_info->bypass_duration = TimeDelta::FromSeconds(seconds); | |
| 45 } else { | |
| 46 // Server deferred to us to choose a duration. Default to a range from | |
| 47 // one to five minutes. | |
| 48 SetRandBypassDuration(proxy_info); | |
| 49 } | |
| 43 return true; | 50 return true; |
| 44 } | 51 } |
| 45 } | 52 } |
| 46 } | 53 } |
| 47 return false; | 54 return false; |
| 48 } | 55 } |
| 49 | 56 |
| 50 bool GetDataReductionProxyInfo(const net::HttpResponseHeaders* headers, | 57 bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers, |
| 51 DataReductionProxyInfo* proxy_info) { | 58 DataReductionProxyInfo* proxy_info) { |
| 52 DCHECK(proxy_info); | 59 DCHECK(proxy_info); |
| 53 proxy_info->bypass_all = false; | 60 proxy_info->bypass_all = false; |
| 54 proxy_info->bypass_duration = TimeDelta(); | 61 |
| 55 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where | 62 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where |
| 56 // <duration> is the number of seconds to wait before retrying | 63 // <duration> is the number of seconds to wait before retrying |
| 57 // the proxy. If the duration is 0, then the default proxy retry delay | 64 // the proxy. If the duration is 0, then the default proxy retry delay |
| 58 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. | 65 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. |
| 59 // 'bypass' instructs Chrome to bypass the currently connected data reduction | 66 // 'bypass' instructs Chrome to bypass the currently connected data reduction |
| 60 // proxy, whereas 'block' instructs Chrome to bypass all available data | 67 // proxy, whereas 'block' instructs Chrome to bypass all available data |
| 61 // reduction proxies. | 68 // reduction proxies. |
| 62 | 69 |
| 63 // 'block' takes precedence over 'bypass', so look for it first. | 70 // 'block' takes precedence over 'bypass', so look for it first. |
| 64 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. | 71 // TODO(bengr): Reduce checks for 'block' and 'bypass' to a single loop. |
| 65 if (GetDataReductionProxyBypassDuration( | 72 if (ParseHeadersAndSetProxyInfoBypassDuration( |
| 66 headers, "block=", &proxy_info->bypass_duration)) { | 73 headers, "block=", proxy_info)) { |
| 67 proxy_info->bypass_all = true; | 74 proxy_info->bypass_all = true; |
| 68 return true; | 75 return true; |
| 69 } | 76 } |
| 70 | 77 |
| 71 // Next, look for 'bypass'. | 78 // Next, look for 'bypass'. |
| 72 if (GetDataReductionProxyBypassDuration( | 79 if (ParseHeadersAndSetProxyInfoBypassDuration( |
| 73 headers, "bypass=", &proxy_info->bypass_duration)) { | 80 headers, "bypass=", proxy_info)) { |
| 74 return true; | 81 return true; |
| 75 } | 82 } |
| 76 return false; | 83 return false; |
| 77 } | 84 } |
| 78 | 85 |
| 79 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { | 86 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { |
| 80 const size_t kVersionSize = 4; | 87 const size_t kVersionSize = 4; |
| 81 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy"; | 88 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy"; |
| 82 size_t value_len = strlen(kDataReductionProxyViaValue); | 89 size_t value_len = strlen(kDataReductionProxyViaValue); |
| 83 void* iter = NULL; | 90 void* iter = NULL; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 103 return false; | 110 return false; |
| 104 } | 111 } |
| 105 | 112 |
| 106 const int kShortBypassMaxSeconds = 59; | 113 const int kShortBypassMaxSeconds = 59; |
| 107 const int kMediumBypassMaxSeconds = 300; | 114 const int kMediumBypassMaxSeconds = 300; |
| 108 net::ProxyService::DataReductionProxyBypassType | 115 net::ProxyService::DataReductionProxyBypassType |
| 109 GetDataReductionProxyBypassType( | 116 GetDataReductionProxyBypassType( |
| 110 const net::HttpResponseHeaders* headers, | 117 const net::HttpResponseHeaders* headers, |
| 111 DataReductionProxyInfo* data_reduction_proxy_info) { | 118 DataReductionProxyInfo* data_reduction_proxy_info) { |
| 112 DCHECK(data_reduction_proxy_info); | 119 DCHECK(data_reduction_proxy_info); |
| 113 if (GetDataReductionProxyInfo(headers, data_reduction_proxy_info)) { | 120 if (ParseHeadersAndSetProxyInfo(headers, data_reduction_proxy_info)) { |
| 114 // A chrome-proxy response header is only present in a 502. For proper | 121 // A chrome-proxy response header is only present in a 502. For proper |
| 115 // reporting, this check must come before the 5xx checks below. | 122 // reporting, this check must come before the 5xx checks below. |
| 116 const TimeDelta& duration = data_reduction_proxy_info->bypass_duration; | 123 const TimeDelta& duration = data_reduction_proxy_info->bypass_duration; |
| 117 if (duration <= TimeDelta::FromSeconds(kShortBypassMaxSeconds)) | 124 if (duration <= TimeDelta::FromSeconds(kShortBypassMaxSeconds)) |
| 118 return ProxyService::SHORT_BYPASS; | 125 return ProxyService::SHORT_BYPASS; |
| 119 if (duration <= TimeDelta::FromSeconds(kMediumBypassMaxSeconds)) | 126 if (duration <= TimeDelta::FromSeconds(kMediumBypassMaxSeconds)) |
| 120 return ProxyService::MEDIUM_BYPASS; | 127 return ProxyService::MEDIUM_BYPASS; |
| 121 return ProxyService::LONG_BYPASS; | 128 return ProxyService::LONG_BYPASS; |
| 122 } | 129 } |
| 130 | |
| 123 // Fall back if a 500, 502 or 503 is returned. | 131 // Fall back if a 500, 502 or 503 is returned. |
| 124 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR) | 132 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR || |
| 125 return ProxyService::STATUS_500_HTTP_INTERNAL_SERVER_ERROR; | 133 headers->response_code() == net::HTTP_BAD_GATEWAY || |
| 126 if (headers->response_code() == net::HTTP_BAD_GATEWAY) | 134 headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) { |
| 127 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY; | 135 SetRandBypassDuration(data_reduction_proxy_info); |
| 128 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) | 136 |
| 129 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE; | 137 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR) |
| 138 return ProxyService::STATUS_500_HTTP_INTERNAL_SERVER_ERROR; | |
| 139 if (headers->response_code() == net::HTTP_BAD_GATEWAY) | |
| 140 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY; | |
| 141 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) | |
| 142 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE; | |
| 143 } | |
| 144 | |
| 130 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be | 145 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be |
| 131 // interpreted by data reduction proxy. | 146 // interpreted by data reduction proxy. |
| 132 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED && | 147 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED && |
| 133 !headers->HasHeader("Proxy-Authenticate")) { | 148 !headers->HasHeader("Proxy-Authenticate")) { |
| 149 SetRandBypassDuration(data_reduction_proxy_info); | |
| 134 return ProxyService::MALFORMED_407; | 150 return ProxyService::MALFORMED_407; |
| 135 } | 151 } |
| 136 if (!HasDataReductionProxyViaHeader(headers) && | 152 if (!HasDataReductionProxyViaHeader(headers) && |
| 137 (headers->response_code() != net::HTTP_NOT_MODIFIED)) { | 153 (headers->response_code() != net::HTTP_NOT_MODIFIED)) { |
| 154 SetRandBypassDuration(data_reduction_proxy_info); | |
| 138 // A Via header might not be present in a 304. Since the goal of a 304 | 155 // A Via header might not be present in a 304. Since the goal of a 304 |
| 139 // response is to minimize information transfer, a sender in general | 156 // response is to minimize information transfer, a sender in general |
| 140 // should not generate representation metadata other than Cache-Control, | 157 // should not generate representation metadata other than Cache-Control, |
| 141 // Content-Location, Date, ETag, Expires, and Vary. | 158 // Content-Location, Date, ETag, Expires, and Vary. |
| 142 | 159 |
| 143 // The proxy Via header might also not be present in a 4xx response. | 160 // The proxy Via header might also not be present in a 4xx response. |
| 144 // Separate this case from other responses that are missing the header. | 161 // Separate this case from other responses that are missing the header. |
| 145 if (headers->response_code() >= net::HTTP_BAD_REQUEST && | 162 if (headers->response_code() >= net::HTTP_BAD_REQUEST && |
| 146 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { | 163 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { |
| 147 return ProxyService::MISSING_VIA_HEADER_4XX; | 164 return ProxyService::MISSING_VIA_HEADER_4XX; |
| 148 } | 165 } |
| 149 return ProxyService::MISSING_VIA_HEADER_OTHER; | 166 return ProxyService::MISSING_VIA_HEADER_OTHER; |
| 150 } | 167 } |
| 151 // There is no bypass event. | 168 // There is no bypass event. |
| 152 return ProxyService::BYPASS_EVENT_TYPE_MAX; | 169 return ProxyService::BYPASS_EVENT_TYPE_MAX; |
| 153 } | 170 } |
| 154 | 171 |
| 172 void SetRandBypassDuration(DataReductionProxyInfo* data_reduction_proxy_info) { | |
|
bengr
2014/07/17 00:11:08
This can be in an anonymous namespace.
Not at Google. Contact bengr
2014/07/17 18:10:21
Done. Removed declaration from .h.
| |
| 173 data_reduction_proxy_info->bypass_duration = TimeDelta::FromMilliseconds( | |
| 174 base::RandInt(1 * 60 * 1000, 5 * 60 * 1000)); | |
| 175 } | |
| 176 | |
| 155 } // namespace data_reduction_proxy | 177 } // namespace data_reduction_proxy |
| OLD | NEW |