Skip to content

Commit

Permalink
Split out the handling of proxy bypass rules into ProxyBypassRules. T…
Browse files Browse the repository at this point in the history
…here are some pretty complicated rules, and this helps isolate that code and better test it.

This also lays a framework for addressing bug 9835 (IP/CIDR matching)

Lastly, adds support for the exclusion format ".domain" on all platforms, which is interpreted as "*.domain".

BUG=28112
TEST=ProxyBypassRulesTest.*
Review URL: http://codereview.chromium.org/601070

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39486 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
eroman@chromium.org committed Feb 19, 2010
1 parent d68a04d commit 7541206
Show file tree
Hide file tree
Showing 21 changed files with 729 additions and 585 deletions.
2 changes: 1 addition & 1 deletion chrome/browser/automation/automation_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ class SetProxyConfigTask : public Task {
}
std::string proxy_bypass_list;
if (dict.GetString(automation::kJSONProxyBypassList, &proxy_bypass_list)) {
pc->ParseNoProxyList(proxy_bypass_list);
pc->bypass_rules.ParseFromString(proxy_bypass_list);
}
std::string proxy_server;
if (dict.GetString(automation::kJSONProxyServer, &proxy_server)) {
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/net/chrome_url_request_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ net::ProxyConfig* CreateProxyConfig(const CommandLine& command_line) {
}

if (command_line.HasSwitch(switches::kProxyBypassList)) {
proxy_config->ParseNoProxyList(
proxy_config->bypass_rules.ParseFromString(
WideToASCII(command_line.GetSwitchValue(
switches::kProxyBypassList)));
}
Expand Down
18 changes: 5 additions & 13 deletions chrome/browser/net/chrome_url_request_context_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL pac_url;
net::ProxyConfig::ProxyRules proxy_rules;
const char* proxy_bypass_list; // newline separated
bool bypass_local_names;
} tests[] = {
{
TEST_DESC("Empty command line"),
Expand All @@ -71,7 +70,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL(), // pac_url
net::ProxyConfig::ProxyRules(), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
},
{
TEST_DESC("No proxy"),
Expand All @@ -83,7 +81,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL(), // pac_url
net::ProxyConfig::ProxyRules(), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
},
{
TEST_DESC("No proxy with extra parameters."),
Expand All @@ -95,7 +92,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL(), // pac_url
net::ProxyConfig::ProxyRules(), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
},
{
TEST_DESC("Single proxy."),
Expand All @@ -107,7 +103,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL(), // pac_url
net::MakeSingleProxyRules("http://proxy:8888"), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
},
{
TEST_DESC("Per scheme proxy."),
Expand All @@ -121,7 +116,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
"",
"ftpproxy:8889"), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
},
{
TEST_DESC("Per scheme proxy with bypass URLs."),
Expand All @@ -134,8 +128,8 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
net::MakeProxyPerSchemeRules("httpproxy:8888",
"",
"ftpproxy:8889"), // proxy_rules
"*.google.com\n*foo.com:99\n1.2.3.4:22\n127.0.0.1/8\n",
false // bypass_local_names
// TODO(eroman): 127.0.0.1/8 is unsupported, so it was dropped
"*.google.com\nfoo.com:99\n1.2.3.4:22\n",
},
{
TEST_DESC("Pac URL with proxy bypass URLs"),
Expand All @@ -146,8 +140,8 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
false, // auto_detect
GURL("http://wpad/wpad.dat"), // pac_url
net::ProxyConfig::ProxyRules(), // proxy_rules
"*.google.com\n*foo.com:99\n1.2.3.4:22\n127.0.0.1/8\n",
false // bypass_local_names
// TODO(eroman): 127.0.0.1/8 is unsupported, so it was dropped
"*.google.com\nfoo.com:99\n1.2.3.4:22\n",
},
{
TEST_DESC("Autodetect"),
Expand All @@ -159,7 +153,6 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
GURL(), // pac_url
net::ProxyConfig::ProxyRules(), // proxy_rules
"", // proxy_bypass_list
false // bypass_local_names
}
};

Expand All @@ -176,8 +169,7 @@ TEST(ChromeUrlRequestContextTest, CreateProxyConfigTest) {
EXPECT_EQ(tests[i].auto_detect, config->auto_detect);
EXPECT_EQ(tests[i].pac_url, config->pac_url);
EXPECT_EQ(tests[i].proxy_bypass_list,
net::FlattenProxyBypass(config->proxy_bypass));
EXPECT_EQ(tests[i].bypass_local_names, config->proxy_bypass_local_names);
net::FlattenProxyBypass(config->bypass_rules));
EXPECT_EQ(tests[i].proxy_rules, config->proxy_rules);
}
}
Expand Down
3 changes: 2 additions & 1 deletion chrome/common/chrome_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ const char kProxyAutoDetect[] = "proxy-auto-detect";
// Specify a list of hosts for whom we bypass proxy settings and use direct
// connections. Ignored if --proxy-auto-detect or --no-proxy-server are
// also specified.
// TODO(robertshield): Specify host format.
// This is a comma separated list of bypass rules. See:
// "net/proxy/proxy_bypass_rules.h" for the format of these rules.
const char kProxyBypassList[] = "proxy-bypass-list";

// Use the pac script at the given URL
Expand Down
3 changes: 3 additions & 0 deletions net/net.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@
'ocsp/nss_ocsp.h',
'proxy/init_proxy_resolver.cc',
'proxy/init_proxy_resolver.h',
'proxy/proxy_bypass_rules.cc',
'proxy/proxy_bypass_rules.h',
'proxy/proxy_config.cc',
'proxy/proxy_config.h',
'proxy/proxy_config_service.h',
Expand Down Expand Up @@ -659,6 +661,7 @@
'http/http_vary_data_unittest.cc',
'proxy/init_proxy_resolver_unittest.cc',
'proxy/mock_proxy_resolver.h',
'proxy/proxy_bypass_rules_unittest.cc',
'proxy/proxy_config_service_linux_unittest.cc',
'proxy/proxy_config_service_win_unittest.cc',
'proxy/proxy_config_unittest.cc',
Expand Down
228 changes: 228 additions & 0 deletions net/proxy/proxy_bypass_rules.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// Copyright (c) 2010 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.

#include "net/proxy/proxy_bypass_rules.h"

#include "base/logging.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "net/base/net_util.h"

namespace net {

namespace {

class HostnamePatternRule : public ProxyBypassRules::Rule {
public:
HostnamePatternRule(const std::string& optional_scheme,
const std::string& hostname_pattern,
int optional_port)
: optional_scheme_(StringToLowerASCII(optional_scheme)),
hostname_pattern_(StringToLowerASCII(hostname_pattern)),
optional_port_(optional_port) {
}

virtual bool Matches(const GURL& url) const {
if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_)
return false; // Didn't match port expectation.

if (!optional_scheme_.empty() && url.scheme() != optional_scheme_)
return false; // Didn't match scheme expectation.

// Note it is necessary to lower-case the host, since GURL uses capital
// letters for percent-escaped characters.
return MatchPatternASCII(StringToLowerASCII(url.host()),
hostname_pattern_);
}

virtual std::string ToString() const {
std::string str;
if (!optional_scheme_.empty())
StringAppendF(&str, "%s://", optional_scheme_.c_str());
str += hostname_pattern_;
if (optional_port_ != -1)
StringAppendF(&str, ":%d", optional_port_);
return str;
}

private:
const std::string optional_scheme_;
const std::string hostname_pattern_;
const int optional_port_;
};

class BypassLocalRule : public ProxyBypassRules::Rule {
public:
virtual bool Matches(const GURL& url) const {
const std::string& host = url.host();
if (host == "127.0.0.1" || host == "[::1]")
return true;
return host.find('.') == std::string::npos;
}

virtual std::string ToString() const {
return "<local>";
}
};

// Returns true if the given string represents an IP address.
bool IsIPAddress(const std::string& domain) {
// From GURL::HostIsIPAddress()
url_canon::RawCanonOutputT<char, 128> ignored_output;
url_canon::CanonHostInfo host_info;
url_parse::Component domain_comp(0, domain.size());
url_canon::CanonicalizeIPAddress(domain.c_str(), domain_comp,
&ignored_output, &host_info);
return host_info.IsIPAddress();
}

} // namespace

ProxyBypassRules::~ProxyBypassRules() {
}

bool ProxyBypassRules::Matches(const GURL& url) const {
for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); ++it) {
if ((*it)->Matches(url))
return true;
}
return false;
}

bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const {
if (rules_.size() != other.rules().size())
return false;

for (size_t i = 0; i < rules_.size(); ++i) {
if (!rules_[i]->Equals(*other.rules()[i]))
return false;
}
return true;
}

void ProxyBypassRules::ParseFromString(const std::string& raw) {
ParseFromStringInternal(raw, false);
}

void ProxyBypassRules::ParseFromStringUsingSuffixMatching(
const std::string& raw) {
ParseFromStringInternal(raw, true);
}

bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme,
const std::string& hostname_pattern,
int optional_port) {
if (hostname_pattern.empty())
return false;

rules_.push_back(new HostnamePatternRule(optional_scheme,
hostname_pattern,
optional_port));
return true;
}

void ProxyBypassRules::AddRuleToBypassLocal() {
rules_.push_back(new BypassLocalRule);
}

bool ProxyBypassRules::AddRuleFromString(const std::string& raw) {
return AddRuleFromStringInternalWithLogging(raw, false);
}

void ProxyBypassRules::Clear() {
rules_.clear();
}

void ProxyBypassRules::ParseFromStringInternal(
const std::string& raw,
bool use_hostname_suffix_matching) {
Clear();

StringTokenizer entries(raw, ",;");
while (entries.GetNext()) {
AddRuleFromStringInternalWithLogging(entries.token(),
use_hostname_suffix_matching);
}
}

bool ProxyBypassRules::AddRuleFromStringInternal(
const std::string& raw_untrimmed,
bool use_hostname_suffix_matching) {
std::string raw;
TrimWhitespaceASCII(raw_untrimmed, TRIM_ALL, &raw);

// This is the special syntax used by WinInet's bypass list -- we allow it
// on all platforms and interpret it the same way.
if (LowerCaseEqualsASCII(raw, "<local>")) {
AddRuleToBypassLocal();
return true;
}

// Extract any scheme-restriction.
std::string::size_type scheme_pos = raw.find("://");
std::string scheme;
if (scheme_pos != std::string::npos) {
scheme = raw.substr(0, scheme_pos);
raw = raw.substr(scheme_pos + 3);
if (scheme.empty())
return false;
}

if (raw.empty())
return false;

// If there is a forward slash in the input, it is probably a CIDR style
// mask.
if (raw.find('/') != std::string::npos) {
LOG(WARNING) << "TODO: support CIDR-style proxy bypass entries "
"(http://crbug.com/9835)";
return false;
}

// Check if we have an <ip-address>[:port] input. We need to treat this
// separately since the IP literal may not be in a canonical form.
std::string host;
int port;
if (ParseHostAndPort(raw, &host, &port)) {
if (IsIPAddress(host)) {
// Canonicalize the IP literal before adding it as a string pattern.
GURL tmp_url("http://" + host);
return AddRuleForHostname(scheme, tmp_url.host(), port);
}
}

// Otherwise assume we have <hostname-pattern>[:port].
std::string::size_type pos_colon = raw.rfind(':');
host = raw;
port = -1;
if (pos_colon != std::string::npos) {
if (!StringToInt(raw.substr(pos_colon + 1), &port) ||
(port < 0 || port > 0xFFFF)) {
return false; // Port was invalid.
}
raw = raw.substr(0, pos_colon);
}

// Special-case hostnames that begin with a period.
// For example, we remap ".google.com" --> "*.google.com".
if (StartsWithASCII(raw, ".", false))
raw = "*" + raw;

// If suffix matching was asked for, make sure the pattern starts with a
// wildcard.
if (use_hostname_suffix_matching && !StartsWithASCII(raw, "*", false))
raw = "*" + raw;

return AddRuleForHostname(scheme, raw, port);
}

bool ProxyBypassRules::AddRuleFromStringInternalWithLogging(
const std::string& raw,
bool use_hostname_suffix_matching) {
bool ok = AddRuleFromStringInternal(raw, use_hostname_suffix_matching);
LOG_IF(WARNING, !ok) << "Unable to parse proxy bypass rule: " << raw;
return ok;
}

} // namespace net
Loading

0 comments on commit 7541206

Please sign in to comment.