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
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_script_decider.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "net/base/net_errors.h"
18 #include "net/proxy/dhcp_proxy_script_fetcher.h"
19 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
20 #include "net/proxy/proxy_script_fetcher.h"
21 #include "net/url_request/url_request_context.h"
27 bool LooksLikePacScript(const base::string16
& script
) {
28 // Note: this is only an approximation! It may not always work correctly,
29 // however it is very likely that legitimate scripts have this exact string,
30 // since they must minimally define a function of this name. Conversely, a
31 // file not containing the string is not likely to be a PAC script.
33 // An exact test would have to load the script in a javascript evaluator.
34 return script
.find(base::ASCIIToUTF16("FindProxyForURL")) !=
40 // This is the hard-coded location used by the DNS portion of web proxy
43 // Note that we not use DNS devolution to find the WPAD host, since that could
44 // be dangerous should our top level domain registry become out of date.
46 // Instead we directly resolve "wpad", and let the operating system apply the
47 // DNS suffix search paths. This is the same approach taken by Firefox, and
48 // compatibility hasn't been an issue.
50 // For more details, also check out this comment:
51 // http://code.google.com/p/chromium/issues/detail?id=18575#c20
53 const char kWpadUrl
[] = "http://wpad/wpad.dat";
54 const int kQuickCheckDelayMs
= 1000;
57 base::Value
* ProxyScriptDecider::PacSource::NetLogCallback(
58 const GURL
* effective_pac_url
,
59 NetLog::LogLevel
/* log_level */) const {
60 base::DictionaryValue
* dict
= new base::DictionaryValue();
63 case PacSource::WPAD_DHCP
:
66 case PacSource::WPAD_DNS
:
67 source
= "WPAD DNS: ";
68 source
+= effective_pac_url
->possibly_invalid_spec();
70 case PacSource::CUSTOM
:
71 source
= "Custom PAC URL: ";
72 source
+= effective_pac_url
->possibly_invalid_spec();
75 dict
->SetString("source", source
);
79 ProxyScriptDecider::ProxyScriptDecider(
80 ProxyScriptFetcher
* proxy_script_fetcher
,
81 DhcpProxyScriptFetcher
* dhcp_proxy_script_fetcher
,
84 proxy_script_fetcher_(proxy_script_fetcher
),
85 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher
),
86 current_pac_source_index_(0u),
87 pac_mandatory_(false),
88 next_state_(STATE_NONE
),
89 net_log_(BoundNetLog::Make(
90 net_log
, NetLog::SOURCE_PROXY_SCRIPT_DECIDER
)),
91 fetch_pac_bytes_(false),
92 quick_check_enabled_(true) {
93 if (proxy_script_fetcher
&&
94 proxy_script_fetcher
->GetRequestContext() &&
95 proxy_script_fetcher
->GetRequestContext()->host_resolver()) {
96 host_resolver_
.reset(new SingleRequestHostResolver(
97 proxy_script_fetcher
->GetRequestContext()->host_resolver()));
101 ProxyScriptDecider::~ProxyScriptDecider() {
102 if (next_state_
!= STATE_NONE
)
106 int ProxyScriptDecider::Start(
107 const ProxyConfig
& config
, const base::TimeDelta wait_delay
,
108 bool fetch_pac_bytes
, const CompletionCallback
& callback
) {
109 DCHECK_EQ(STATE_NONE
, next_state_
);
110 DCHECK(!callback
.is_null());
111 DCHECK(config
.HasAutomaticSettings());
113 net_log_
.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER
);
115 fetch_pac_bytes_
= fetch_pac_bytes
;
117 // Save the |wait_delay| as a non-negative value.
118 wait_delay_
= wait_delay
;
119 if (wait_delay_
< base::TimeDelta())
120 wait_delay_
= base::TimeDelta();
122 pac_mandatory_
= config
.pac_mandatory();
123 have_custom_pac_url_
= config
.has_pac_url();
125 pac_sources_
= BuildPacSourcesFallbackList(config
);
126 DCHECK(!pac_sources_
.empty());
128 next_state_
= STATE_WAIT
;
131 if (rv
== ERR_IO_PENDING
)
132 callback_
= callback
;
139 const ProxyConfig
& ProxyScriptDecider::effective_config() const {
140 DCHECK_EQ(STATE_NONE
, next_state_
);
141 return effective_config_
;
144 // TODO(eroman): Return a const-pointer.
145 ProxyResolverScriptData
* ProxyScriptDecider::script_data() const {
146 DCHECK_EQ(STATE_NONE
, next_state_
);
147 return script_data_
.get();
150 // Initialize the fallback rules.
153 // (3) Custom PAC URL.
154 ProxyScriptDecider::PacSourceList
ProxyScriptDecider::
155 BuildPacSourcesFallbackList(
156 const ProxyConfig
& config
) const {
157 PacSourceList pac_sources
;
158 if (config
.auto_detect()) {
159 pac_sources
.push_back(PacSource(PacSource::WPAD_DHCP
, GURL(kWpadUrl
)));
160 pac_sources
.push_back(PacSource(PacSource::WPAD_DNS
, GURL(kWpadUrl
)));
162 if (config
.has_pac_url())
163 pac_sources
.push_back(PacSource(PacSource::CUSTOM
, config
.pac_url()));
167 void ProxyScriptDecider::OnIOCompletion(int result
) {
168 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
169 tracked_objects::ScopedTracker
tracking_profile(
170 FROM_HERE_WITH_EXPLICIT_FUNCTION(
171 "436634 ProxyScriptDecider::OnIOCompletion"));
173 DCHECK_NE(STATE_NONE
, next_state_
);
174 int rv
= DoLoop(result
);
175 if (rv
!= ERR_IO_PENDING
) {
181 int ProxyScriptDecider::DoLoop(int result
) {
182 DCHECK_NE(next_state_
, STATE_NONE
);
185 State state
= next_state_
;
186 next_state_
= STATE_NONE
;
192 case STATE_WAIT_COMPLETE
:
193 rv
= DoWaitComplete(rv
);
195 case STATE_QUICK_CHECK
:
199 case STATE_QUICK_CHECK_COMPLETE
:
200 rv
= DoQuickCheckComplete(rv
);
202 case STATE_FETCH_PAC_SCRIPT
:
204 rv
= DoFetchPacScript();
206 case STATE_FETCH_PAC_SCRIPT_COMPLETE
:
207 rv
= DoFetchPacScriptComplete(rv
);
209 case STATE_VERIFY_PAC_SCRIPT
:
211 rv
= DoVerifyPacScript();
213 case STATE_VERIFY_PAC_SCRIPT_COMPLETE
:
214 rv
= DoVerifyPacScriptComplete(rv
);
217 NOTREACHED() << "bad state";
221 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
225 void ProxyScriptDecider::DoCallback(int result
) {
226 DCHECK_NE(ERR_IO_PENDING
, result
);
227 DCHECK(!callback_
.is_null());
228 callback_
.Run(result
);
231 int ProxyScriptDecider::DoWait() {
232 next_state_
= STATE_WAIT_COMPLETE
;
234 // If no waiting is required, continue on to the next state.
235 if (wait_delay_
.ToInternalValue() == 0)
238 // Otherwise wait the specified amount of time.
239 wait_timer_
.Start(FROM_HERE
, wait_delay_
, this,
240 &ProxyScriptDecider::OnWaitTimerFired
);
241 net_log_
.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT
);
242 return ERR_IO_PENDING
;
245 int ProxyScriptDecider::DoWaitComplete(int result
) {
246 DCHECK_EQ(OK
, result
);
247 if (wait_delay_
.ToInternalValue() != 0) {
248 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT
,
251 if (quick_check_enabled_
&& current_pac_source().type
== PacSource::WPAD_DNS
)
252 next_state_
= STATE_QUICK_CHECK
;
254 next_state_
= GetStartState();
258 int ProxyScriptDecider::DoQuickCheck() {
259 DCHECK(quick_check_enabled_
);
260 if (host_resolver_
.get() == NULL
) {
261 // If we have no resolver, skip QuickCheck altogether.
262 next_state_
= GetStartState();
266 quick_check_start_time_
= base::Time::Now();
267 std::string host
= current_pac_source().url
.host();
268 HostResolver::RequestInfo
reqinfo(HostPortPair(host
, 80));
269 reqinfo
.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY
);
270 CompletionCallback callback
= base::Bind(
271 &ProxyScriptDecider::OnIOCompletion
,
272 base::Unretained(this));
274 next_state_
= STATE_QUICK_CHECK_COMPLETE
;
275 quick_check_timer_
.Start(FROM_HERE
,
276 base::TimeDelta::FromMilliseconds(
278 base::Bind(callback
, ERR_NAME_NOT_RESOLVED
));
280 // We use HIGHEST here because proxy decision blocks doing any other requests.
281 return host_resolver_
->Resolve(reqinfo
, HIGHEST
, &wpad_addresses_
,
285 int ProxyScriptDecider::DoQuickCheckComplete(int result
) {
286 DCHECK(quick_check_enabled_
);
287 base::TimeDelta delta
= base::Time::Now() - quick_check_start_time_
;
289 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta
);
291 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta
);
292 host_resolver_
->Cancel();
293 quick_check_timer_
.Stop();
295 return TryToFallbackPacSource(result
);
296 next_state_
= GetStartState();
300 int ProxyScriptDecider::DoFetchPacScript() {
301 DCHECK(fetch_pac_bytes_
);
303 next_state_
= STATE_FETCH_PAC_SCRIPT_COMPLETE
;
305 const PacSource
& pac_source
= current_pac_source();
307 GURL effective_pac_url
;
308 DetermineURL(pac_source
, &effective_pac_url
);
310 net_log_
.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT
,
311 base::Bind(&PacSource::NetLogCallback
,
312 base::Unretained(&pac_source
),
313 &effective_pac_url
));
315 if (pac_source
.type
== PacSource::WPAD_DHCP
) {
316 if (!dhcp_proxy_script_fetcher_
) {
317 net_log_
.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER
);
318 return ERR_UNEXPECTED
;
321 return dhcp_proxy_script_fetcher_
->Fetch(
322 &pac_script_
, base::Bind(&ProxyScriptDecider::OnIOCompletion
,
323 base::Unretained(this)));
326 if (!proxy_script_fetcher_
) {
327 net_log_
.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER
);
328 return ERR_UNEXPECTED
;
331 return proxy_script_fetcher_
->Fetch(
332 effective_pac_url
, &pac_script_
,
333 base::Bind(&ProxyScriptDecider::OnIOCompletion
, base::Unretained(this)));
336 int ProxyScriptDecider::DoFetchPacScriptComplete(int result
) {
337 DCHECK(fetch_pac_bytes_
);
339 net_log_
.EndEventWithNetErrorCode(
340 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT
, result
);
342 return TryToFallbackPacSource(result
);
344 next_state_
= STATE_VERIFY_PAC_SCRIPT
;
348 int ProxyScriptDecider::DoVerifyPacScript() {
349 next_state_
= STATE_VERIFY_PAC_SCRIPT_COMPLETE
;
351 // This is just a heuristic. Ideally we would try to parse the script.
352 if (fetch_pac_bytes_
&& !LooksLikePacScript(pac_script_
))
353 return ERR_PAC_SCRIPT_FAILED
;
358 int ProxyScriptDecider::DoVerifyPacScriptComplete(int result
) {
360 return TryToFallbackPacSource(result
);
362 const PacSource
& pac_source
= current_pac_source();
364 // Extract the current script data.
365 if (fetch_pac_bytes_
) {
366 script_data_
= ProxyResolverScriptData::FromUTF16(pac_script_
);
368 script_data_
= pac_source
.type
== PacSource::CUSTOM
?
369 ProxyResolverScriptData::FromURL(pac_source
.url
) :
370 ProxyResolverScriptData::ForAutoDetect();
373 // Let the caller know which automatic setting we ended up initializing the
374 // resolver for (there may have been multiple fallbacks to choose from.)
375 if (current_pac_source().type
== PacSource::CUSTOM
) {
377 ProxyConfig::CreateFromCustomPacURL(current_pac_source().url
);
378 effective_config_
.set_pac_mandatory(pac_mandatory_
);
380 if (fetch_pac_bytes_
) {
381 GURL auto_detected_url
;
383 switch (current_pac_source().type
) {
384 case PacSource::WPAD_DHCP
:
385 auto_detected_url
= dhcp_proxy_script_fetcher_
->GetPacURL();
388 case PacSource::WPAD_DNS
:
389 auto_detected_url
= GURL(kWpadUrl
);
397 ProxyConfig::CreateFromCustomPacURL(auto_detected_url
);
399 // The resolver does its own resolution so we cannot know the
400 // URL. Just do the best we can and state that the configuration
401 // is to auto-detect proxy settings.
402 effective_config_
= ProxyConfig::CreateAutoDetect();
409 int ProxyScriptDecider::TryToFallbackPacSource(int error
) {
412 if (current_pac_source_index_
+ 1 >= pac_sources_
.size()) {
413 // Nothing left to fall back to.
417 // Advance to next URL in our list.
418 ++current_pac_source_index_
;
421 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE
);
422 if (quick_check_enabled_
&& current_pac_source().type
== PacSource::WPAD_DNS
)
423 next_state_
= STATE_QUICK_CHECK
;
425 next_state_
= GetStartState();
430 ProxyScriptDecider::State
ProxyScriptDecider::GetStartState() const {
431 return fetch_pac_bytes_
? STATE_FETCH_PAC_SCRIPT
: STATE_VERIFY_PAC_SCRIPT
;
434 void ProxyScriptDecider::DetermineURL(const PacSource
& pac_source
,
435 GURL
* effective_pac_url
) {
436 DCHECK(effective_pac_url
);
438 switch (pac_source
.type
) {
439 case PacSource::WPAD_DHCP
:
441 case PacSource::WPAD_DNS
:
442 *effective_pac_url
= GURL(kWpadUrl
);
444 case PacSource::CUSTOM
:
445 *effective_pac_url
= pac_source
.url
;
450 const ProxyScriptDecider::PacSource
&
451 ProxyScriptDecider::current_pac_source() const {
452 DCHECK_LT(current_pac_source_index_
, pac_sources_
.size());
453 return pac_sources_
[current_pac_source_index_
];
456 void ProxyScriptDecider::OnWaitTimerFired() {
460 void ProxyScriptDecider::DidComplete() {
461 net_log_
.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER
);
464 void ProxyScriptDecider::Cancel() {
465 DCHECK_NE(STATE_NONE
, next_state_
);
467 net_log_
.AddEvent(NetLog::TYPE_CANCELLED
);
469 switch (next_state_
) {
470 case STATE_WAIT_COMPLETE
:
473 case STATE_FETCH_PAC_SCRIPT_COMPLETE
:
474 proxy_script_fetcher_
->Cancel();
481 // This is safe to call in any state.
482 if (dhcp_proxy_script_fetcher_
)
483 dhcp_proxy_script_fetcher_
->Cancel();