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