| OLD | NEW |
| 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 Loading... |
| 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 && proxy_script_fetcher->GetRequestContext() && |
| 91 proxy_script_fetcher->GetRequestContext()->host_resolver()) |
| 92 host_resolver_.reset(new SingleRequestHostResolver( |
| 93 proxy_script_fetcher->GetRequestContext()->host_resolver())); |
| 85 } | 94 } |
| 86 | 95 |
| 87 ProxyScriptDecider::~ProxyScriptDecider() { | 96 ProxyScriptDecider::~ProxyScriptDecider() { |
| 88 if (next_state_ != STATE_NONE) | 97 if (next_state_ != STATE_NONE) |
| 89 Cancel(); | 98 Cancel(); |
| 90 } | 99 } |
| 91 | 100 |
| 92 int ProxyScriptDecider::Start( | 101 int ProxyScriptDecider::Start( |
| 93 const ProxyConfig& config, const base::TimeDelta wait_delay, | 102 const ProxyConfig& config, const base::TimeDelta wait_delay, |
| 94 bool fetch_pac_bytes, const CompletionCallback& callback) { | 103 bool fetch_pac_bytes, const CompletionCallback& callback) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 State state = next_state_; | 174 State state = next_state_; |
| 166 next_state_ = STATE_NONE; | 175 next_state_ = STATE_NONE; |
| 167 switch (state) { | 176 switch (state) { |
| 168 case STATE_WAIT: | 177 case STATE_WAIT: |
| 169 DCHECK_EQ(OK, rv); | 178 DCHECK_EQ(OK, rv); |
| 170 rv = DoWait(); | 179 rv = DoWait(); |
| 171 break; | 180 break; |
| 172 case STATE_WAIT_COMPLETE: | 181 case STATE_WAIT_COMPLETE: |
| 173 rv = DoWaitComplete(rv); | 182 rv = DoWaitComplete(rv); |
| 174 break; | 183 break; |
| 184 case STATE_QUICK_CHECK: |
| 185 DCHECK_EQ(OK, rv); |
| 186 rv = DoQuickCheck(); |
| 187 break; |
| 188 case STATE_QUICK_CHECK_COMPLETE: |
| 189 rv = DoQuickCheckComplete(rv); |
| 190 break; |
| 175 case STATE_FETCH_PAC_SCRIPT: | 191 case STATE_FETCH_PAC_SCRIPT: |
| 176 DCHECK_EQ(OK, rv); | 192 DCHECK_EQ(OK, rv); |
| 177 rv = DoFetchPacScript(); | 193 rv = DoFetchPacScript(); |
| 178 break; | 194 break; |
| 179 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | 195 case STATE_FETCH_PAC_SCRIPT_COMPLETE: |
| 180 rv = DoFetchPacScriptComplete(rv); | 196 rv = DoFetchPacScriptComplete(rv); |
| 181 break; | 197 break; |
| 182 case STATE_VERIFY_PAC_SCRIPT: | 198 case STATE_VERIFY_PAC_SCRIPT: |
| 183 DCHECK_EQ(OK, rv); | 199 DCHECK_EQ(OK, rv); |
| 184 rv = DoVerifyPacScript(); | 200 rv = DoVerifyPacScript(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 214 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT); | 230 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT); |
| 215 return ERR_IO_PENDING; | 231 return ERR_IO_PENDING; |
| 216 } | 232 } |
| 217 | 233 |
| 218 int ProxyScriptDecider::DoWaitComplete(int result) { | 234 int ProxyScriptDecider::DoWaitComplete(int result) { |
| 219 DCHECK_EQ(OK, result); | 235 DCHECK_EQ(OK, result); |
| 220 if (wait_delay_.ToInternalValue() != 0) { | 236 if (wait_delay_.ToInternalValue() != 0) { |
| 221 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, | 237 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, |
| 222 result); | 238 result); |
| 223 } | 239 } |
| 224 next_state_ = GetStartState(); | 240 next_state_ = STATE_QUICK_CHECK; |
| 225 return OK; | 241 return OK; |
| 226 } | 242 } |
| 227 | 243 |
| 244 int ProxyScriptDecider::DoQuickCheck() { |
| 245 if (host_resolver_.get() == NULL) { |
| 246 // If we have no resolver, skip QuickCheck altogether. |
| 247 next_state_ = GetStartState(); |
| 248 return OK; |
| 249 } |
| 250 |
| 251 quick_check_start_time_ = base::Time::Now(); |
| 252 HostResolver::RequestInfo reqinfo(HostPortPair("wpad", 80)); |
| 253 CompletionCallback callback = base::Bind( |
| 254 &ProxyScriptDecider::OnIOCompletion, |
| 255 base::Unretained(this)); |
| 256 |
| 257 // We use HIGHEST here because proxy decision blocks doing any other requests. |
| 258 int rv = host_resolver_->Resolve(reqinfo, HIGHEST, &wpad_addresses_, |
| 259 callback, net_log_); |
| 260 |
| 261 // We can't get an error response - the name is known to be valid, and we |
| 262 // don't cache negative dns responses. |
| 263 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
| 264 |
| 265 if (rv == OK) { |
| 266 next_state_ = GetStartState(); |
| 267 } else { |
| 268 quick_check_timer_.Start(FROM_HERE, |
| 269 base::TimeDelta::FromMilliseconds( |
| 270 kQuickCheckDelayMs), |
| 271 base::Bind(callback, ERR_NAME_NOT_RESOLVED)); |
| 272 next_state_ = STATE_QUICK_CHECK_COMPLETE; |
| 273 } |
| 274 return rv; |
| 275 } |
| 276 |
| 277 int ProxyScriptDecider::DoQuickCheckComplete(int result) { |
| 278 base::TimeDelta delta = base::Time::Now() - quick_check_start_time_; |
| 279 if (result == OK) |
| 280 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta); |
| 281 else |
| 282 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta); |
| 283 host_resolver_->Cancel(); |
| 284 quick_check_timer_.Stop(); |
| 285 if (result == OK) |
| 286 next_state_ = GetStartState(); |
| 287 return result; |
| 288 } |
| 289 |
| 228 int ProxyScriptDecider::DoFetchPacScript() { | 290 int ProxyScriptDecider::DoFetchPacScript() { |
| 229 DCHECK(fetch_pac_bytes_); | 291 DCHECK(fetch_pac_bytes_); |
| 230 | 292 |
| 231 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; | 293 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; |
| 232 | 294 |
| 233 const PacSource& pac_source = current_pac_source(); | 295 const PacSource& pac_source = current_pac_source(); |
| 234 | 296 |
| 235 GURL effective_pac_url; | 297 GURL effective_pac_url; |
| 236 DetermineURL(pac_source, &effective_pac_url); | 298 DetermineURL(pac_source, &effective_pac_url); |
| 237 | 299 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 467 } |
| 406 | 468 |
| 407 // This is safe to call in any state. | 469 // This is safe to call in any state. |
| 408 if (dhcp_proxy_script_fetcher_) | 470 if (dhcp_proxy_script_fetcher_) |
| 409 dhcp_proxy_script_fetcher_->Cancel(); | 471 dhcp_proxy_script_fetcher_->Cancel(); |
| 410 | 472 |
| 411 DidComplete(); | 473 DidComplete(); |
| 412 } | 474 } |
| 413 | 475 |
| 414 } // namespace net | 476 } // namespace net |
| OLD | NEW |