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

Unified Diff: net/proxy/init_proxy_resolver.cc

Issue 6831025: Adds support for the DHCP portion of the WPAD (proxy auto-discovery) protocol. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add timeout on Win32 DHCP API. Created 9 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: net/proxy/init_proxy_resolver.cc
diff --git a/net/proxy/init_proxy_resolver.cc b/net/proxy/init_proxy_resolver.cc
index 90911688b2618340fc5272ca86a004b941280d10..48b2c8c686d6728cc0b243a5ea3cd2b0db57241a 100644
--- a/net/proxy/init_proxy_resolver.cc
+++ b/net/proxy/init_proxy_resolver.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -10,6 +10,8 @@
#include "base/string_util.h"
#include "net/base/net_log.h"
#include "net/base/net_errors.h"
+#include "net/proxy/dhcp_proxy_script_fetcher.h"
+#include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
#include "net/proxy/proxy_config.h"
#include "net/proxy/proxy_resolver.h"
#include "net/proxy/proxy_script_fetcher.h"
@@ -30,15 +32,16 @@ namespace net {
// http://code.google.com/p/chromium/issues/detail?id=18575#c20
static const char kWpadUrl[] = "http://wpad/wpad.dat";
-InitProxyResolver::InitProxyResolver(ProxyResolver* resolver,
- ProxyScriptFetcher* proxy_script_fetcher,
- NetLog* net_log)
+InitProxyResolver::InitProxyResolver(
+ ProxyResolver* resolver,
+ URLProxyScriptFetcher* proxy_script_fetcher,
+ NetLog* net_log)
: resolver_(resolver),
proxy_script_fetcher_(proxy_script_fetcher),
ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_(
this, &InitProxyResolver::OnIOCompletion)),
user_callback_(NULL),
- current_pac_url_index_(0u),
+ current_pac_source_index_(0u),
next_state_(STATE_NONE),
net_log_(BoundNetLog::Make(
net_log, NetLog::SOURCE_INIT_PROXY_RESOLVER)),
@@ -67,8 +70,8 @@ int InitProxyResolver::Init(const ProxyConfig& config,
effective_config_ = effective_config;
- pac_urls_ = BuildPacUrlsFallbackList(config);
- DCHECK(!pac_urls_.empty());
+ pac_sources_ = BuildPacSourcesFallbackList(config);
+ DCHECK(!pac_sources_.empty());
next_state_ = STATE_WAIT;
@@ -81,17 +84,26 @@ int InitProxyResolver::Init(const ProxyConfig& config,
return rv;
}
+DhcpProxyScriptFetcher* InitProxyResolver::ImplCreateDhcpProxyScriptFetcher(
+ URLRequestContext* url_request_context) {
+ return DhcpProxyScriptFetcherFactory::GetInstance()->Create(
+ url_request_context);
+}
+
// Initialize the fallback rules.
-// (1) WPAD (DNS).
-// (2) Custom PAC URL.
-InitProxyResolver::UrlList InitProxyResolver::BuildPacUrlsFallbackList(
+// (1) WPAD (DHCP).
+// (2) WPAD (DNS).
+// (3) Custom PAC URL.
+InitProxyResolver::PacSourceList InitProxyResolver::BuildPacSourcesFallbackList(
const ProxyConfig& config) const {
- UrlList pac_urls;
- if (config.auto_detect())
- pac_urls.push_back(PacURL(true, GURL()));
+ PacSourceList pac_sources;
+ if (config.auto_detect()) {
+ pac_sources.push_back(PacSource(PacSource::WPAD_DHCP, GURL()));
+ pac_sources.push_back(PacSource(PacSource::WPAD_DNS, GURL()));
+ }
if (config.has_pac_url())
- pac_urls.push_back(PacURL(false, config.pac_url()));
- return pac_urls;
+ pac_sources.push_back(PacSource(PacSource::CUSTOM, config.pac_url()));
+ return pac_sources;
}
void InitProxyResolver::OnIOCompletion(int result) {
@@ -174,24 +186,43 @@ int InitProxyResolver::DoFetchPacScript() {
next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE;
- const PacURL& pac_url = current_pac_url();
+ const PacSource& pac_source = current_pac_source();
- const GURL effective_pac_url =
- pac_url.auto_detect ? GURL(kWpadUrl) : pac_url.url;
+ std::string source_field;
eroman 2011/04/21 05:22:48 I suggest moving this code into a helper function.
Jói 2011/05/03 21:20:59 Done.
+ GURL effective_pac_url;
+ switch (pac_source.type) {
+ case PacSource::WPAD_DHCP:
+ source_field = "WPAD DHCP";
+ break;
+ case PacSource::WPAD_DNS:
+ effective_pac_url = GURL(kWpadUrl);
+ source_field = "WPAD DNS: ";
+ source_field += effective_pac_url.possibly_invalid_spec();
+ break;
+ case PacSource::CUSTOM:
+ effective_pac_url = pac_source.url;
+ source_field = "Custom PAC URL: ";
+ source_field += effective_pac_url.possibly_invalid_spec();
+ break;
+ }
net_log_.BeginEvent(
NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT,
- make_scoped_refptr(new NetLogStringParameter(
- "url", effective_pac_url.possibly_invalid_spec())));
+ make_scoped_refptr(new NetLogStringParameter("source", source_field)));
if (!proxy_script_fetcher_) {
net_log_.AddEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_HAS_NO_FETCHER, NULL);
return ERR_UNEXPECTED;
}
- return proxy_script_fetcher_->Fetch(effective_pac_url,
- &pac_script_,
- &io_callback_);
+ if (pac_source.type == PacSource::WPAD_DHCP) {
+ dhcp_proxy_script_fetcher_.reset(ImplCreateDhcpProxyScriptFetcher(
eroman 2011/04/21 05:22:48 Can this instead be a dependency to InitProxyResol
Jói 2011/05/03 21:20:59 Done.
+ proxy_script_fetcher_->GetRequestContext()));
+ return dhcp_proxy_script_fetcher_->Fetch(&pac_script_, &io_callback_);
+ } else {
+ proxy_script_fetcher_->SetURL(effective_pac_url);
eroman 2011/04/21 05:22:48 Why split off SetURL() as separate from Fetch() ?
Jói 2011/05/03 21:20:59 Reverted as discussed.
+ return proxy_script_fetcher_->Fetch(&pac_script_, &io_callback_);
+ }
}
int InitProxyResolver::DoFetchPacScriptComplete(int result) {
@@ -200,7 +231,7 @@ int InitProxyResolver::DoFetchPacScriptComplete(int result) {
net_log_.EndEventWithNetErrorCode(
NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT, result);
if (result != OK)
- return TryToFallbackPacUrl(result);
+ return TryToFallbackPacSource(result);
next_state_ = STATE_SET_PAC_SCRIPT;
return result;
@@ -209,7 +240,7 @@ int InitProxyResolver::DoFetchPacScriptComplete(int result) {
int InitProxyResolver::DoSetPacScript() {
net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT, NULL);
- const PacURL& pac_url = current_pac_url();
+ const PacSource& pac_source = current_pac_source();
next_state_ = STATE_SET_PAC_SCRIPT_COMPLETE;
@@ -218,9 +249,9 @@ int InitProxyResolver::DoSetPacScript() {
if (resolver_->expects_pac_bytes()) {
script_data = ProxyResolverScriptData::FromUTF16(pac_script_);
} else {
- script_data = pac_url.auto_detect ?
- ProxyResolverScriptData::ForAutoDetect() :
- ProxyResolverScriptData::FromURL(pac_url.url);
+ script_data = pac_source.type == PacSource::CUSTOM ?
+ ProxyResolverScriptData::FromURL(pac_source.url) :
+ ProxyResolverScriptData::ForAutoDetect();
}
return resolver_->SetPacScript(script_data, &io_callback_);
@@ -230,38 +261,58 @@ int InitProxyResolver::DoSetPacScriptComplete(int result) {
net_log_.EndEventWithNetErrorCode(
NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT, result);
if (result != OK)
- return TryToFallbackPacUrl(result);
+ return TryToFallbackPacSource(result);
// Let the caller know which automatic setting we ended up initializing the
// resolver for (there may have been multiple fallbacks to choose from.)
if (effective_config_) {
- if (current_pac_url().auto_detect && resolver_->expects_pac_bytes()) {
+ if (current_pac_source().type == PacSource::CUSTOM) {
*effective_config_ =
- ProxyConfig::CreateFromCustomPacURL(GURL(kWpadUrl));
- } else if (current_pac_url().auto_detect) {
- *effective_config_ = ProxyConfig::CreateAutoDetect();
+ ProxyConfig::CreateFromCustomPacURL(current_pac_source().url);
} else {
- *effective_config_ =
- ProxyConfig::CreateFromCustomPacURL(current_pac_url().url);
+ if (resolver_->expects_pac_bytes()) {
+ GURL auto_detected_url;
+
+ switch (current_pac_source().type) {
+ case PacSource::WPAD_DHCP:
+ auto_detected_url = dhcp_proxy_script_fetcher_->GetPacURL();
+ break;
+
+ case PacSource::WPAD_DNS:
+ auto_detected_url = GURL(kWpadUrl);
+ break;
+
+ default:
+ NOTREACHED();
+ }
+
+ *effective_config_ =
+ ProxyConfig::CreateFromCustomPacURL(auto_detected_url);
+ } else {
+ // The resolver does its own resolution so we cannot know the
+ // URL. Just do the best we can and state that the configuration
+ // is to auto-detect proxy settings.
+ *effective_config_ = ProxyConfig::CreateAutoDetect();
+ }
}
}
return result;
}
-int InitProxyResolver::TryToFallbackPacUrl(int error) {
+int InitProxyResolver::TryToFallbackPacSource(int error) {
DCHECK_LT(error, 0);
- if (current_pac_url_index_ + 1 >= pac_urls_.size()) {
+ if (current_pac_source_index_ + 1 >= pac_sources_.size()) {
// Nothing left to fall back to.
return error;
}
// Advance to next URL in our list.
- ++current_pac_url_index_;
+ ++current_pac_source_index_;
net_log_.AddEvent(
- NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_URL, NULL);
+ NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE, NULL);
next_state_ = GetStartState();
@@ -273,9 +324,10 @@ InitProxyResolver::State InitProxyResolver::GetStartState() const {
STATE_FETCH_PAC_SCRIPT : STATE_SET_PAC_SCRIPT;
}
-const InitProxyResolver::PacURL& InitProxyResolver::current_pac_url() const {
- DCHECK_LT(current_pac_url_index_, pac_urls_.size());
- return pac_urls_[current_pac_url_index_];
+const InitProxyResolver::PacSource&
+ InitProxyResolver::current_pac_source() const {
+ DCHECK_LT(current_pac_source_index_, pac_sources_.size());
+ return pac_sources_[current_pac_source_index_];
}
void InitProxyResolver::OnWaitTimerFired() {

Powered by Google App Engine
This is Rietveld 408576698