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

Side by Side Diff: net/proxy/proxy_script_decider.cc

Issue 23181010: Fast-fail WPAD detection. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Undo API change Created 7 years, 3 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
« no previous file with comments | « net/proxy/proxy_script_decider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/proxy/proxy_script_decider.h" 5 #include "net/proxy/proxy_script_decider.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
12 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
16 #include "net/proxy/dhcp_proxy_script_fetcher.h" 17 #include "net/proxy/dhcp_proxy_script_fetcher.h"
17 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" 18 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
18 #include "net/proxy/proxy_script_fetcher.h" 19 #include "net/proxy/proxy_script_fetcher.h"
20 #include "net/url_request/url_request_context.h"
19 21
20 namespace net { 22 namespace net {
21 23
22 namespace { 24 namespace {
23 25
24 bool LooksLikePacScript(const base::string16& script) { 26 bool LooksLikePacScript(const base::string16& script) {
25 // Note: this is only an approximation! It may not always work correctly, 27 // Note: this is only an approximation! It may not always work correctly,
26 // however it is very likely that legitimate scripts have this exact string, 28 // however it is very likely that legitimate scripts have this exact string,
27 // since they must minimally define a function of this name. Conversely, a 29 // since they must minimally define a function of this name. Conversely, a
28 // file not containing the string is not likely to be a PAC script. 30 // file not containing the string is not likely to be a PAC script.
29 // 31 //
30 // An exact test would have to load the script in a javascript evaluator. 32 // An exact test would have to load the script in a javascript evaluator.
31 return script.find(ASCIIToUTF16("FindProxyForURL")) != base::string16::npos; 33 return script.find(ASCIIToUTF16("FindProxyForURL")) != base::string16::npos;
32 } 34 }
33 35
34 } 36 }
35 37
36 // This is the hard-coded location used by the DNS portion of web proxy 38 // This is the hard-coded location used by the DNS portion of web proxy
37 // auto-discovery. 39 // auto-discovery.
38 // 40 //
39 // Note that we not use DNS devolution to find the WPAD host, since that could 41 // Note that we not use DNS devolution to find the WPAD host, since that could
40 // be dangerous should our top level domain registry become out of date. 42 // be dangerous should our top level domain registry become out of date.
41 // 43 //
42 // Instead we directly resolve "wpad", and let the operating system apply the 44 // Instead we directly resolve "wpad", and let the operating system apply the
43 // DNS suffix search paths. This is the same approach taken by Firefox, and 45 // DNS suffix search paths. This is the same approach taken by Firefox, and
44 // compatibility hasn't been an issue. 46 // compatibility hasn't been an issue.
45 // 47 //
46 // For more details, also check out this comment: 48 // For more details, also check out this comment:
47 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 49 // http://code.google.com/p/chromium/issues/detail?id=18575#c20
48 static const char kWpadUrl[] = "http://wpad/wpad.dat"; 50 namespace {
51 const char kWpadUrl[] = "http://wpad/wpad.dat";
52 const int kQuickCheckDelayMs = 1000;
53 };
49 54
50 base::Value* ProxyScriptDecider::PacSource::NetLogCallback( 55 base::Value* ProxyScriptDecider::PacSource::NetLogCallback(
51 const GURL* effective_pac_url, 56 const GURL* effective_pac_url,
52 NetLog::LogLevel /* log_level */) const { 57 NetLog::LogLevel /* log_level */) const {
53 base::DictionaryValue* dict = new base::DictionaryValue(); 58 base::DictionaryValue* dict = new base::DictionaryValue();
54 std::string source; 59 std::string source;
55 switch (type) { 60 switch (type) {
56 case PacSource::WPAD_DHCP: 61 case PacSource::WPAD_DHCP:
57 source = "WPAD DHCP"; 62 source = "WPAD DHCP";
58 break; 63 break;
(...skipping 16 matching lines...) Expand all
75 NetLog* net_log) 80 NetLog* net_log)
76 : resolver_(NULL), 81 : resolver_(NULL),
77 proxy_script_fetcher_(proxy_script_fetcher), 82 proxy_script_fetcher_(proxy_script_fetcher),
78 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), 83 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher),
79 current_pac_source_index_(0u), 84 current_pac_source_index_(0u),
80 pac_mandatory_(false), 85 pac_mandatory_(false),
81 next_state_(STATE_NONE), 86 next_state_(STATE_NONE),
82 net_log_(BoundNetLog::Make( 87 net_log_(BoundNetLog::Make(
83 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), 88 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)),
84 fetch_pac_bytes_(false) { 89 fetch_pac_bytes_(false) {
90 if ( proxy_script_fetcher
91 && proxy_script_fetcher->GetRequestContext()
szym 2013/09/09 23:58:51 && on the previous line, and don't use extra space
Elly Fong-Jones 2013/09/10 17:04:16 Done.
92 && proxy_script_fetcher->GetRequestContext()->host_resolver())
93 host_resolver_.reset(new SingleRequestHostResolver(
94 proxy_script_fetcher->GetRequestContext()->host_resolver()));
85 } 95 }
86 96
87 ProxyScriptDecider::~ProxyScriptDecider() { 97 ProxyScriptDecider::~ProxyScriptDecider() {
88 if (next_state_ != STATE_NONE) 98 if (next_state_ != STATE_NONE)
89 Cancel(); 99 Cancel();
90 } 100 }
91 101
92 int ProxyScriptDecider::Start( 102 int ProxyScriptDecider::Start(
93 const ProxyConfig& config, const base::TimeDelta wait_delay, 103 const ProxyConfig& config, const base::TimeDelta wait_delay,
94 bool fetch_pac_bytes, const CompletionCallback& callback) { 104 bool fetch_pac_bytes, const CompletionCallback& callback) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 State state = next_state_; 175 State state = next_state_;
166 next_state_ = STATE_NONE; 176 next_state_ = STATE_NONE;
167 switch (state) { 177 switch (state) {
168 case STATE_WAIT: 178 case STATE_WAIT:
169 DCHECK_EQ(OK, rv); 179 DCHECK_EQ(OK, rv);
170 rv = DoWait(); 180 rv = DoWait();
171 break; 181 break;
172 case STATE_WAIT_COMPLETE: 182 case STATE_WAIT_COMPLETE:
173 rv = DoWaitComplete(rv); 183 rv = DoWaitComplete(rv);
174 break; 184 break;
185 case STATE_QUICK_CHECK:
186 DCHECK_EQ(OK, rv);
187 rv = DoQuickCheck();
188 break;
189 case STATE_QUICK_CHECK_COMPLETE:
190 rv = DoQuickCheckComplete(rv);
191 break;
175 case STATE_FETCH_PAC_SCRIPT: 192 case STATE_FETCH_PAC_SCRIPT:
176 DCHECK_EQ(OK, rv); 193 DCHECK_EQ(OK, rv);
177 rv = DoFetchPacScript(); 194 rv = DoFetchPacScript();
178 break; 195 break;
179 case STATE_FETCH_PAC_SCRIPT_COMPLETE: 196 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
180 rv = DoFetchPacScriptComplete(rv); 197 rv = DoFetchPacScriptComplete(rv);
181 break; 198 break;
182 case STATE_VERIFY_PAC_SCRIPT: 199 case STATE_VERIFY_PAC_SCRIPT:
183 DCHECK_EQ(OK, rv); 200 DCHECK_EQ(OK, rv);
184 rv = DoVerifyPacScript(); 201 rv = DoVerifyPacScript();
(...skipping 29 matching lines...) Expand all
214 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT); 231 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT);
215 return ERR_IO_PENDING; 232 return ERR_IO_PENDING;
216 } 233 }
217 234
218 int ProxyScriptDecider::DoWaitComplete(int result) { 235 int ProxyScriptDecider::DoWaitComplete(int result) {
219 DCHECK_EQ(OK, result); 236 DCHECK_EQ(OK, result);
220 if (wait_delay_.ToInternalValue() != 0) { 237 if (wait_delay_.ToInternalValue() != 0) {
221 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, 238 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT,
222 result); 239 result);
223 } 240 }
224 next_state_ = GetStartState(); 241 next_state_ = STATE_QUICK_CHECK;
225 return OK; 242 return OK;
226 } 243 }
227 244
245 int ProxyScriptDecider::DoQuickCheck() {
246 quick_check_start_time_ = base::Time::Now();
247 HostResolver::RequestInfo reqinfo(HostPortPair("wpad", 80));
248 CompletionCallback callback = base::Bind(
249 &ProxyScriptDecider::OnIOCompletion,
250 base::Unretained(this));
251
252 if (host_resolver_.get() == NULL) {
szym 2013/09/09 23:58:51 Move it to top of function.
Elly Fong-Jones 2013/09/10 17:04:16 Done.
253 // if we have no resolver, skip QuickCheck altogether.
szym 2013/09/09 23:58:51 nit: If
Elly Fong-Jones 2013/09/10 17:04:16 Done.
254 next_state_ = GetStartState();
255 return OK;
256 }
257
258 // We use HIGHEST here because proxy decision blocks doing any other requests.
259 int rv = host_resolver_->Resolve(reqinfo, HIGHEST, &wpad_addresses_,
260 callback, net_log_);
261
262 // We can't get an error response - the name is known to be valid, and we
263 // don't cache negative dns responses.
264 DCHECK(rv == OK || rv == ERR_IO_PENDING);
265
266 if (rv == OK) {
267 next_state_ = GetStartState();
268 } else {
269 quick_check_timer_.Start(FROM_HERE,
270 base::TimeDelta::FromMilliseconds(
271 kQuickCheckDelayMs),
272 base::Bind(callback, ERR_NAME_NOT_RESOLVED));
273 next_state_ = STATE_QUICK_CHECK_COMPLETE;
274 }
275 return rv;
276 }
277
278 int ProxyScriptDecider::DoQuickCheckComplete(int result) {
279 base::TimeDelta delta = base::Time::Now()
280 - quick_check_start_time_;
szym 2013/09/09 23:58:51 - on previous line, but it fits on one line anyway
Elly Fong-Jones 2013/09/10 17:04:16 Done.
281 if (result == OK)
282 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta);
283 else
284 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta);
285 host_resolver_->Cancel();
286 quick_check_timer_.Stop();
287 if (result == OK)
288 next_state_ = GetStartState();
289 return result;
290 }
291
228 int ProxyScriptDecider::DoFetchPacScript() { 292 int ProxyScriptDecider::DoFetchPacScript() {
229 DCHECK(fetch_pac_bytes_); 293 DCHECK(fetch_pac_bytes_);
230 294
231 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; 295 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE;
232 296
233 const PacSource& pac_source = current_pac_source(); 297 const PacSource& pac_source = current_pac_source();
234 298
235 GURL effective_pac_url; 299 GURL effective_pac_url;
236 DetermineURL(pac_source, &effective_pac_url); 300 DetermineURL(pac_source, &effective_pac_url);
237 301
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 469 }
406 470
407 // This is safe to call in any state. 471 // This is safe to call in any state.
408 if (dhcp_proxy_script_fetcher_) 472 if (dhcp_proxy_script_fetcher_)
409 dhcp_proxy_script_fetcher_->Cancel(); 473 dhcp_proxy_script_fetcher_->Cancel();
410 474
411 DidComplete(); 475 DidComplete();
412 } 476 }
413 477
414 } // namespace net 478 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/proxy_script_decider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698