| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "net/proxy/init_proxy_resolver.h" | |
| 6 | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "base/format_macros.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "net/base/net_log.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 #include "net/proxy/dhcp_proxy_script_fetcher.h" | |
| 14 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" | |
| 15 #include "net/proxy/proxy_config.h" | |
| 16 #include "net/proxy/proxy_resolver.h" | |
| 17 #include "net/proxy/proxy_script_fetcher.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 // This is the hard-coded location used by the DNS portion of web proxy | |
| 22 // auto-discovery. | |
| 23 // | |
| 24 // Note that we not use DNS devolution to find the WPAD host, since that could | |
| 25 // be dangerous should our top level domain registry become out of date. | |
| 26 // | |
| 27 // Instead we directly resolve "wpad", and let the operating system apply the | |
| 28 // DNS suffix search paths. This is the same approach taken by Firefox, and | |
| 29 // compatibility hasn't been an issue. | |
| 30 // | |
| 31 // For more details, also check out this comment: | |
| 32 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 | |
| 33 static const char kWpadUrl[] = "http://wpad/wpad.dat"; | |
| 34 | |
| 35 InitProxyResolver::InitProxyResolver( | |
| 36 ProxyResolver* resolver, | |
| 37 ProxyScriptFetcher* proxy_script_fetcher, | |
| 38 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | |
| 39 NetLog* net_log) | |
| 40 : resolver_(resolver), | |
| 41 proxy_script_fetcher_(proxy_script_fetcher), | |
| 42 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), | |
| 43 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | |
| 44 this, &InitProxyResolver::OnIOCompletion)), | |
| 45 user_callback_(NULL), | |
| 46 current_pac_source_index_(0u), | |
| 47 pac_mandatory_(false), | |
| 48 next_state_(STATE_NONE), | |
| 49 net_log_(BoundNetLog::Make( | |
| 50 net_log, NetLog::SOURCE_INIT_PROXY_RESOLVER)), | |
| 51 effective_config_(NULL) { | |
| 52 } | |
| 53 | |
| 54 InitProxyResolver::~InitProxyResolver() { | |
| 55 if (next_state_ != STATE_NONE) | |
| 56 Cancel(); | |
| 57 } | |
| 58 | |
| 59 int InitProxyResolver::Init(const ProxyConfig& config, | |
| 60 const base::TimeDelta wait_delay, | |
| 61 ProxyConfig* effective_config, | |
| 62 OldCompletionCallback* callback) { | |
| 63 DCHECK_EQ(STATE_NONE, next_state_); | |
| 64 DCHECK(callback); | |
| 65 DCHECK(config.HasAutomaticSettings()); | |
| 66 | |
| 67 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER, NULL); | |
| 68 | |
| 69 // Save the |wait_delay| as a non-negative value. | |
| 70 wait_delay_ = wait_delay; | |
| 71 if (wait_delay_ < base::TimeDelta()) | |
| 72 wait_delay_ = base::TimeDelta(); | |
| 73 | |
| 74 effective_config_ = effective_config; | |
| 75 | |
| 76 pac_mandatory_ = config.pac_mandatory(); | |
| 77 | |
| 78 pac_sources_ = BuildPacSourcesFallbackList(config); | |
| 79 DCHECK(!pac_sources_.empty()); | |
| 80 | |
| 81 next_state_ = STATE_WAIT; | |
| 82 | |
| 83 int rv = DoLoop(OK); | |
| 84 if (rv == ERR_IO_PENDING) | |
| 85 user_callback_ = callback; | |
| 86 else | |
| 87 DidCompleteInit(); | |
| 88 | |
| 89 return rv; | |
| 90 } | |
| 91 | |
| 92 // Initialize the fallback rules. | |
| 93 // (1) WPAD (DHCP). | |
| 94 // (2) WPAD (DNS). | |
| 95 // (3) Custom PAC URL. | |
| 96 InitProxyResolver::PacSourceList InitProxyResolver::BuildPacSourcesFallbackList( | |
| 97 const ProxyConfig& config) const { | |
| 98 PacSourceList pac_sources; | |
| 99 if (config.auto_detect()) { | |
| 100 pac_sources.push_back(PacSource(PacSource::WPAD_DHCP, GURL())); | |
| 101 pac_sources.push_back(PacSource(PacSource::WPAD_DNS, GURL())); | |
| 102 } | |
| 103 if (config.has_pac_url()) | |
| 104 pac_sources.push_back(PacSource(PacSource::CUSTOM, config.pac_url())); | |
| 105 return pac_sources; | |
| 106 } | |
| 107 | |
| 108 void InitProxyResolver::OnIOCompletion(int result) { | |
| 109 DCHECK_NE(STATE_NONE, next_state_); | |
| 110 int rv = DoLoop(result); | |
| 111 if (rv != ERR_IO_PENDING) { | |
| 112 DidCompleteInit(); | |
| 113 DoCallback(rv); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 int InitProxyResolver::DoLoop(int result) { | |
| 118 DCHECK_NE(next_state_, STATE_NONE); | |
| 119 int rv = result; | |
| 120 do { | |
| 121 State state = next_state_; | |
| 122 next_state_ = STATE_NONE; | |
| 123 switch (state) { | |
| 124 case STATE_WAIT: | |
| 125 DCHECK_EQ(OK, rv); | |
| 126 rv = DoWait(); | |
| 127 break; | |
| 128 case STATE_WAIT_COMPLETE: | |
| 129 rv = DoWaitComplete(rv); | |
| 130 break; | |
| 131 case STATE_FETCH_PAC_SCRIPT: | |
| 132 DCHECK_EQ(OK, rv); | |
| 133 rv = DoFetchPacScript(); | |
| 134 break; | |
| 135 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | |
| 136 rv = DoFetchPacScriptComplete(rv); | |
| 137 break; | |
| 138 case STATE_SET_PAC_SCRIPT: | |
| 139 DCHECK_EQ(OK, rv); | |
| 140 rv = DoSetPacScript(); | |
| 141 break; | |
| 142 case STATE_SET_PAC_SCRIPT_COMPLETE: | |
| 143 rv = DoSetPacScriptComplete(rv); | |
| 144 break; | |
| 145 default: | |
| 146 NOTREACHED() << "bad state"; | |
| 147 rv = ERR_UNEXPECTED; | |
| 148 break; | |
| 149 } | |
| 150 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | |
| 151 return rv; | |
| 152 } | |
| 153 | |
| 154 void InitProxyResolver::DoCallback(int result) { | |
| 155 DCHECK_NE(ERR_IO_PENDING, result); | |
| 156 DCHECK(user_callback_); | |
| 157 user_callback_->Run(result); | |
| 158 } | |
| 159 | |
| 160 int InitProxyResolver::DoWait() { | |
| 161 next_state_ = STATE_WAIT_COMPLETE; | |
| 162 | |
| 163 // If no waiting is required, continue on to the next state. | |
| 164 if (wait_delay_.ToInternalValue() == 0) | |
| 165 return OK; | |
| 166 | |
| 167 // Otherwise wait the specified amount of time. | |
| 168 wait_timer_.Start(FROM_HERE, wait_delay_, this, | |
| 169 &InitProxyResolver::OnWaitTimerFired); | |
| 170 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT, NULL); | |
| 171 return ERR_IO_PENDING; | |
| 172 } | |
| 173 | |
| 174 int InitProxyResolver::DoWaitComplete(int result) { | |
| 175 DCHECK_EQ(OK, result); | |
| 176 if (wait_delay_.ToInternalValue() != 0) { | |
| 177 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT, | |
| 178 result); | |
| 179 } | |
| 180 next_state_ = GetStartState(); | |
| 181 return OK; | |
| 182 } | |
| 183 | |
| 184 int InitProxyResolver::DoFetchPacScript() { | |
| 185 DCHECK(resolver_->expects_pac_bytes()); | |
| 186 | |
| 187 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; | |
| 188 | |
| 189 const PacSource& pac_source = current_pac_source(); | |
| 190 | |
| 191 GURL effective_pac_url; | |
| 192 NetLogStringParameter* log_parameter = | |
| 193 CreateNetLogParameterAndDetermineURL(pac_source, &effective_pac_url); | |
| 194 | |
| 195 net_log_.BeginEvent( | |
| 196 NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT, | |
| 197 make_scoped_refptr(log_parameter)); | |
| 198 | |
| 199 if (pac_source.type == PacSource::WPAD_DHCP) { | |
| 200 if (!dhcp_proxy_script_fetcher_) { | |
| 201 net_log_.AddEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_HAS_NO_FETCHER, NULL); | |
| 202 return ERR_UNEXPECTED; | |
| 203 } | |
| 204 | |
| 205 return dhcp_proxy_script_fetcher_->Fetch(&pac_script_, &io_callback_); | |
| 206 } | |
| 207 | |
| 208 if (!proxy_script_fetcher_) { | |
| 209 net_log_.AddEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_HAS_NO_FETCHER, NULL); | |
| 210 return ERR_UNEXPECTED; | |
| 211 } | |
| 212 | |
| 213 return proxy_script_fetcher_->Fetch( | |
| 214 effective_pac_url, &pac_script_, &io_callback_); | |
| 215 } | |
| 216 | |
| 217 int InitProxyResolver::DoFetchPacScriptComplete(int result) { | |
| 218 DCHECK(resolver_->expects_pac_bytes()); | |
| 219 | |
| 220 net_log_.EndEventWithNetErrorCode( | |
| 221 NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT, result); | |
| 222 if (result != OK) | |
| 223 return TryToFallbackPacSource(result); | |
| 224 | |
| 225 next_state_ = STATE_SET_PAC_SCRIPT; | |
| 226 return result; | |
| 227 } | |
| 228 | |
| 229 int InitProxyResolver::DoSetPacScript() { | |
| 230 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT, NULL); | |
| 231 | |
| 232 const PacSource& pac_source = current_pac_source(); | |
| 233 | |
| 234 next_state_ = STATE_SET_PAC_SCRIPT_COMPLETE; | |
| 235 | |
| 236 scoped_refptr<ProxyResolverScriptData> script_data; | |
| 237 | |
| 238 if (resolver_->expects_pac_bytes()) { | |
| 239 script_data = ProxyResolverScriptData::FromUTF16(pac_script_); | |
| 240 } else { | |
| 241 script_data = pac_source.type == PacSource::CUSTOM ? | |
| 242 ProxyResolverScriptData::FromURL(pac_source.url) : | |
| 243 ProxyResolverScriptData::ForAutoDetect(); | |
| 244 } | |
| 245 | |
| 246 return resolver_->SetPacScript(script_data, &io_callback_); | |
| 247 } | |
| 248 | |
| 249 int InitProxyResolver::DoSetPacScriptComplete(int result) { | |
| 250 net_log_.EndEventWithNetErrorCode( | |
| 251 NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT, result); | |
| 252 if (result != OK) | |
| 253 return TryToFallbackPacSource(result); | |
| 254 | |
| 255 // Let the caller know which automatic setting we ended up initializing the | |
| 256 // resolver for (there may have been multiple fallbacks to choose from.) | |
| 257 if (effective_config_) { | |
| 258 if (current_pac_source().type == PacSource::CUSTOM) { | |
| 259 *effective_config_ = | |
| 260 ProxyConfig::CreateFromCustomPacURL(current_pac_source().url); | |
| 261 effective_config_->set_pac_mandatory(pac_mandatory_); | |
| 262 } else { | |
| 263 if (resolver_->expects_pac_bytes()) { | |
| 264 GURL auto_detected_url; | |
| 265 | |
| 266 switch (current_pac_source().type) { | |
| 267 case PacSource::WPAD_DHCP: | |
| 268 auto_detected_url = dhcp_proxy_script_fetcher_->GetPacURL(); | |
| 269 break; | |
| 270 | |
| 271 case PacSource::WPAD_DNS: | |
| 272 auto_detected_url = GURL(kWpadUrl); | |
| 273 break; | |
| 274 | |
| 275 default: | |
| 276 NOTREACHED(); | |
| 277 } | |
| 278 | |
| 279 *effective_config_ = | |
| 280 ProxyConfig::CreateFromCustomPacURL(auto_detected_url); | |
| 281 } else { | |
| 282 // The resolver does its own resolution so we cannot know the | |
| 283 // URL. Just do the best we can and state that the configuration | |
| 284 // is to auto-detect proxy settings. | |
| 285 *effective_config_ = ProxyConfig::CreateAutoDetect(); | |
| 286 } | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 return result; | |
| 291 } | |
| 292 | |
| 293 int InitProxyResolver::TryToFallbackPacSource(int error) { | |
| 294 DCHECK_LT(error, 0); | |
| 295 | |
| 296 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { | |
| 297 // Nothing left to fall back to. | |
| 298 return error; | |
| 299 } | |
| 300 | |
| 301 // Advance to next URL in our list. | |
| 302 ++current_pac_source_index_; | |
| 303 | |
| 304 net_log_.AddEvent( | |
| 305 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE, NULL); | |
| 306 | |
| 307 next_state_ = GetStartState(); | |
| 308 | |
| 309 return OK; | |
| 310 } | |
| 311 | |
| 312 InitProxyResolver::State InitProxyResolver::GetStartState() const { | |
| 313 return resolver_->expects_pac_bytes() ? | |
| 314 STATE_FETCH_PAC_SCRIPT : STATE_SET_PAC_SCRIPT; | |
| 315 } | |
| 316 | |
| 317 NetLogStringParameter* InitProxyResolver::CreateNetLogParameterAndDetermineURL( | |
| 318 const PacSource& pac_source, | |
| 319 GURL* effective_pac_url) { | |
| 320 DCHECK(effective_pac_url); | |
| 321 | |
| 322 std::string source_field; | |
| 323 switch (pac_source.type) { | |
| 324 case PacSource::WPAD_DHCP: | |
| 325 source_field = "WPAD DHCP"; | |
| 326 break; | |
| 327 case PacSource::WPAD_DNS: | |
| 328 *effective_pac_url = GURL(kWpadUrl); | |
| 329 source_field = "WPAD DNS: "; | |
| 330 source_field += effective_pac_url->possibly_invalid_spec(); | |
| 331 break; | |
| 332 case PacSource::CUSTOM: | |
| 333 *effective_pac_url = pac_source.url; | |
| 334 source_field = "Custom PAC URL: "; | |
| 335 source_field += effective_pac_url->possibly_invalid_spec(); | |
| 336 break; | |
| 337 } | |
| 338 return new NetLogStringParameter("source", source_field); | |
| 339 } | |
| 340 | |
| 341 const InitProxyResolver::PacSource& | |
| 342 InitProxyResolver::current_pac_source() const { | |
| 343 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); | |
| 344 return pac_sources_[current_pac_source_index_]; | |
| 345 } | |
| 346 | |
| 347 void InitProxyResolver::OnWaitTimerFired() { | |
| 348 OnIOCompletion(OK); | |
| 349 } | |
| 350 | |
| 351 void InitProxyResolver::DidCompleteInit() { | |
| 352 net_log_.EndEvent(NetLog::TYPE_INIT_PROXY_RESOLVER, NULL); | |
| 353 } | |
| 354 | |
| 355 void InitProxyResolver::Cancel() { | |
| 356 DCHECK_NE(STATE_NONE, next_state_); | |
| 357 | |
| 358 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | |
| 359 | |
| 360 switch (next_state_) { | |
| 361 case STATE_WAIT_COMPLETE: | |
| 362 wait_timer_.Stop(); | |
| 363 break; | |
| 364 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | |
| 365 proxy_script_fetcher_->Cancel(); | |
| 366 break; | |
| 367 case STATE_SET_PAC_SCRIPT_COMPLETE: | |
| 368 resolver_->CancelSetPacScript(); | |
| 369 break; | |
| 370 default: | |
| 371 NOTREACHED(); | |
| 372 break; | |
| 373 } | |
| 374 | |
| 375 // This is safe to call in any state. | |
| 376 if (dhcp_proxy_script_fetcher_) | |
| 377 dhcp_proxy_script_fetcher_->Cancel(); | |
| 378 | |
| 379 DidCompleteInit(); | |
| 380 } | |
| 381 | |
| 382 } // namespace net | |
| OLD | NEW |