Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: components/data_reduction_proxy/common/data_reduction_proxy_headers.cc

Issue 382313003: Add data reduction functionality to all platforms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed code review comments by asvitkine@. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 {
bengr 2014/07/21 22:45:15 nit: this doesn't have to be inside data_reduction
Not at Google. Contact bengr 2014/07/22 00:12:09 Done.
25
26 // Returns a random bypass duration between 1 and 5 minutes.
27 base::TimeDelta GetRandBypassDuration() {
bengr 2014/07/21 22:45:14 Suggest changing name to GetDefaultBypassDuration(
Not at Google. Contact bengr 2014/07/22 00:12:09 Done.
28 const int64 delta_ms = base::RandInt(
29 base::TimeDelta::FromMinutes(1).InMilliseconds(),
30 base::TimeDelta::FromMinutes(5).InMilliseconds());
31 return TimeDelta::FromMilliseconds(delta_ms);
32 }
33
34 } // namespace anonymous
35
36 bool ParseHeadersAndSetBypassDuration(
24 const net::HttpResponseHeaders* headers, 37 const net::HttpResponseHeaders* headers,
25 const std::string& action_prefix, 38 const std::string& action_prefix,
26 base::TimeDelta* duration) { 39 base::TimeDelta* bypass_duration) {
27 void* iter = NULL; 40 void* iter = NULL;
28 std::string value; 41 std::string value;
29 std::string name = "chrome-proxy"; 42 std::string name = "chrome-proxy";
30 43
31 while (headers->EnumerateHeader(&iter, name, &value)) { 44 while (headers->EnumerateHeader(&iter, name, &value)) {
32 if (value.size() > action_prefix.size()) { 45 if (value.size() > action_prefix.size()) {
33 if (LowerCaseEqualsASCII(value.begin(), 46 if (LowerCaseEqualsASCII(value.begin(),
34 value.begin() + action_prefix.size(), 47 value.begin() + action_prefix.size(),
35 action_prefix.c_str())) { 48 action_prefix.c_str())) {
36 int64 seconds; 49 int64 seconds;
37 if (!base::StringToInt64( 50 if (!base::StringToInt64(
38 StringPiece(value.begin() + action_prefix.size(), value.end()), 51 StringPiece(value.begin() + action_prefix.size(), value.end()),
39 &seconds) || seconds < 0) { 52 &seconds) || seconds < 0) {
40 continue; // In case there is a well formed instruction. 53 continue; // In case there is a well formed instruction.
41 } 54 }
42 *duration = TimeDelta::FromSeconds(seconds); 55 if (seconds != 0) {
56 *bypass_duration = TimeDelta::FromSeconds(seconds);
57 } else {
58 // Server deferred to us to choose a duration. Default to a range from
bengr 2014/07/21 22:45:16 range from... -> random duration between one and f
Not at Google. Contact bengr 2014/07/22 00:12:09 Done.
59 // one to five minutes.
60 *bypass_duration = GetRandBypassDuration();
61 }
43 return true; 62 return true;
44 } 63 }
45 } 64 }
46 } 65 }
47 return false; 66 return false;
48 } 67 }
49 68
50 bool GetDataReductionProxyInfo(const net::HttpResponseHeaders* headers, 69 bool ParseHeadersAndSetProxyInfo(const net::HttpResponseHeaders* headers,
51 DataReductionProxyInfo* proxy_info) { 70 DataReductionProxyInfo* proxy_info) {
52 DCHECK(proxy_info); 71 DCHECK(proxy_info);
53 proxy_info->bypass_all = false; 72 proxy_info->bypass_all = false;
54 proxy_info->bypass_duration = TimeDelta(); 73
55 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where 74 // Support header of the form Chrome-Proxy: bypass|block=<duration>, where
56 // <duration> is the number of seconds to wait before retrying 75 // <duration> is the number of seconds to wait before retrying
57 // 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
58 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used. 77 // (specified in |ProxyList::UpdateRetryInfoOnFallback|) will be used.
59 // 'bypass' instructs Chrome to bypass the currently connected data reduction 78 // 'bypass' instructs Chrome to bypass the currently connected data reduction
60 // proxy, whereas 'block' instructs Chrome to bypass all available data 79 // proxy, whereas 'block' instructs Chrome to bypass all available data
61 // reduction proxies. 80 // reduction proxies.
62 81
63 // 'block' takes precedence over 'bypass', so look for it first. 82 // 'block' takes precedence over 'bypass', so look for it first.
64 // 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.
65 if (GetDataReductionProxyBypassDuration( 84 if (ParseHeadersAndSetBypassDuration(
66 headers, "block=", &proxy_info->bypass_duration)) { 85 headers, "block=", &proxy_info->bypass_duration)) {
67 proxy_info->bypass_all = true; 86 proxy_info->bypass_all = true;
68 return true; 87 return true;
69 } 88 }
70 89
71 // Next, look for 'bypass'. 90 // Next, look for 'bypass'.
72 if (GetDataReductionProxyBypassDuration( 91 if (ParseHeadersAndSetBypassDuration(
73 headers, "bypass=", &proxy_info->bypass_duration)) { 92 headers, "bypass=", &proxy_info->bypass_duration)) {
74 return true; 93 return true;
75 } 94 }
76 return false; 95 return false;
77 } 96 }
78 97
79 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) { 98 bool HasDataReductionProxyViaHeader(const net::HttpResponseHeaders* headers) {
80 const size_t kVersionSize = 4; 99 const size_t kVersionSize = 4;
81 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy"; 100 const char kDataReductionProxyViaValue[] = "Chrome-Compression-Proxy";
82 size_t value_len = strlen(kDataReductionProxyViaValue); 101 size_t value_len = strlen(kDataReductionProxyViaValue);
(...skipping 20 matching lines...) Expand all
103 return false; 122 return false;
104 } 123 }
105 124
106 const int kShortBypassMaxSeconds = 59; 125 const int kShortBypassMaxSeconds = 59;
107 const int kMediumBypassMaxSeconds = 300; 126 const int kMediumBypassMaxSeconds = 300;
108 net::ProxyService::DataReductionProxyBypassType 127 net::ProxyService::DataReductionProxyBypassType
109 GetDataReductionProxyBypassType( 128 GetDataReductionProxyBypassType(
110 const net::HttpResponseHeaders* headers, 129 const net::HttpResponseHeaders* headers,
111 DataReductionProxyInfo* data_reduction_proxy_info) { 130 DataReductionProxyInfo* data_reduction_proxy_info) {
112 DCHECK(data_reduction_proxy_info); 131 DCHECK(data_reduction_proxy_info);
113 if (GetDataReductionProxyInfo(headers, data_reduction_proxy_info)) { 132 if (ParseHeadersAndSetProxyInfo(headers, data_reduction_proxy_info)) {
114 // A chrome-proxy response header is only present in a 502. For proper 133 // A chrome-proxy response header is only present in a 502. For proper
115 // reporting, this check must come before the 5xx checks below. 134 // reporting, this check must come before the 5xx checks below.
116 const TimeDelta& duration = data_reduction_proxy_info->bypass_duration; 135 const TimeDelta& duration = data_reduction_proxy_info->bypass_duration;
117 if (duration <= TimeDelta::FromSeconds(kShortBypassMaxSeconds)) 136 if (duration <= TimeDelta::FromSeconds(kShortBypassMaxSeconds))
118 return ProxyService::SHORT_BYPASS; 137 return ProxyService::SHORT_BYPASS;
119 if (duration <= TimeDelta::FromSeconds(kMediumBypassMaxSeconds)) 138 if (duration <= TimeDelta::FromSeconds(kMediumBypassMaxSeconds))
120 return ProxyService::MEDIUM_BYPASS; 139 return ProxyService::MEDIUM_BYPASS;
121 return ProxyService::LONG_BYPASS; 140 return ProxyService::LONG_BYPASS;
122 } 141 }
142
123 // Fall back if a 500, 502 or 503 is returned. 143 // Fall back if a 500, 502 or 503 is returned.
124 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR) 144 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR ||
bengr 2014/07/21 22:45:15 Coordinate with megjablon when landing.
Not at Google. Contact bengr 2014/07/22 00:12:09 Acknowledged.
125 return ProxyService::STATUS_500_HTTP_INTERNAL_SERVER_ERROR; 145 headers->response_code() == net::HTTP_BAD_GATEWAY ||
126 if (headers->response_code() == net::HTTP_BAD_GATEWAY) 146 headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) {
127 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY; 147 data_reduction_proxy_info->bypass_duration = GetRandBypassDuration();
128 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE) 148
129 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE; 149 if (headers->response_code() == net::HTTP_INTERNAL_SERVER_ERROR)
150 return ProxyService::STATUS_500_HTTP_INTERNAL_SERVER_ERROR;
151 if (headers->response_code() == net::HTTP_BAD_GATEWAY)
152 return ProxyService::STATUS_502_HTTP_BAD_GATEWAY;
153 if (headers->response_code() == net::HTTP_SERVICE_UNAVAILABLE)
154 return ProxyService::STATUS_503_HTTP_SERVICE_UNAVAILABLE;
155 }
156
130 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be 157 // TODO(kundaji): Bypass if Proxy-Authenticate header value cannot be
131 // interpreted by data reduction proxy. 158 // interpreted by data reduction proxy.
132 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED && 159 if (headers->response_code() == net::HTTP_PROXY_AUTHENTICATION_REQUIRED &&
133 !headers->HasHeader("Proxy-Authenticate")) { 160 !headers->HasHeader("Proxy-Authenticate")) {
161 data_reduction_proxy_info->bypass_duration = GetRandBypassDuration();
134 return ProxyService::MALFORMED_407; 162 return ProxyService::MALFORMED_407;
135 } 163 }
136 if (!HasDataReductionProxyViaHeader(headers) && 164 if (!HasDataReductionProxyViaHeader(headers) &&
137 (headers->response_code() != net::HTTP_NOT_MODIFIED)) { 165 (headers->response_code() != net::HTTP_NOT_MODIFIED)) {
166 data_reduction_proxy_info->bypass_duration = GetRandBypassDuration();
138 // A Via header might not be present in a 304. Since the goal of a 304 167 // 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 168 // response is to minimize information transfer, a sender in general
140 // should not generate representation metadata other than Cache-Control, 169 // should not generate representation metadata other than Cache-Control,
141 // Content-Location, Date, ETag, Expires, and Vary. 170 // Content-Location, Date, ETag, Expires, and Vary.
142 171
143 // The proxy Via header might also not be present in a 4xx response. 172 // 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. 173 // Separate this case from other responses that are missing the header.
145 if (headers->response_code() >= net::HTTP_BAD_REQUEST && 174 if (headers->response_code() >= net::HTTP_BAD_REQUEST &&
146 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) { 175 headers->response_code() < net::HTTP_INTERNAL_SERVER_ERROR) {
147 return ProxyService::MISSING_VIA_HEADER_4XX; 176 return ProxyService::MISSING_VIA_HEADER_4XX;
148 } 177 }
149 return ProxyService::MISSING_VIA_HEADER_OTHER; 178 return ProxyService::MISSING_VIA_HEADER_OTHER;
150 } 179 }
151 // There is no bypass event. 180 // There is no bypass event.
152 return ProxyService::BYPASS_EVENT_TYPE_MAX; 181 return ProxyService::BYPASS_EVENT_TYPE_MAX;
153 } 182 }
154 183
155 } // namespace data_reduction_proxy 184 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698