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.
5 #include "net/proxy/proxy_bypass_rules.h"
7 #include "base/stl_util.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/ip_address_number.h"
15 #include "net/base/net_util.h"
21 class HostnamePatternRule
: public ProxyBypassRules::Rule
{
23 HostnamePatternRule(const std::string
& optional_scheme
,
24 const std::string
& hostname_pattern
,
26 : optional_scheme_(base::StringToLowerASCII(optional_scheme
)),
27 hostname_pattern_(base::StringToLowerASCII(hostname_pattern
)),
28 optional_port_(optional_port
) {
31 bool Matches(const GURL
& url
) const override
{
32 if (optional_port_
!= -1 && url
.EffectiveIntPort() != optional_port_
)
33 return false; // Didn't match port expectation.
35 if (!optional_scheme_
.empty() && url
.scheme() != optional_scheme_
)
36 return false; // Didn't match scheme expectation.
38 // Note it is necessary to lower-case the host, since GURL uses capital
39 // letters for percent-escaped characters.
40 return MatchPattern(base::StringToLowerASCII(url
.host()),
44 std::string
ToString() const override
{
46 if (!optional_scheme_
.empty())
47 base::StringAppendF(&str
, "%s://", optional_scheme_
.c_str());
48 str
+= hostname_pattern_
;
49 if (optional_port_
!= -1)
50 base::StringAppendF(&str
, ":%d", optional_port_
);
54 Rule
* Clone() const override
{
55 return new HostnamePatternRule(optional_scheme_
,
61 const std::string optional_scheme_
;
62 const std::string hostname_pattern_
;
63 const int optional_port_
;
66 class BypassLocalRule
: public ProxyBypassRules::Rule
{
68 bool Matches(const GURL
& url
) const override
{
69 const std::string
& host
= url
.host();
70 if (host
== "127.0.0.1" || host
== "[::1]")
72 return host
.find('.') == std::string::npos
;
75 std::string
ToString() const override
{ return "<local>"; }
77 Rule
* Clone() const override
{ return new BypassLocalRule(); }
80 // Rule for matching a URL that is an IP address, if that IP address falls
81 // within a certain numeric range. For example, you could use this rule to
82 // match all the IPs in the CIDR block 10.10.3.4/24.
83 class BypassIPBlockRule
: public ProxyBypassRules::Rule
{
85 // |ip_prefix| + |prefix_length| define the IP block to match.
86 BypassIPBlockRule(const std::string
& description
,
87 const std::string
& optional_scheme
,
88 const IPAddressNumber
& ip_prefix
,
89 size_t prefix_length_in_bits
)
90 : description_(description
),
91 optional_scheme_(optional_scheme
),
92 ip_prefix_(ip_prefix
),
93 prefix_length_in_bits_(prefix_length_in_bits
) {
96 bool Matches(const GURL
& url
) const override
{
97 if (!url
.HostIsIPAddress())
100 if (!optional_scheme_
.empty() && url
.scheme() != optional_scheme_
)
101 return false; // Didn't match scheme expectation.
103 // Parse the input IP literal to a number.
104 IPAddressNumber ip_number
;
105 if (!ParseIPLiteralToNumber(url
.HostNoBrackets(), &ip_number
))
108 // Test if it has the expected prefix.
109 return IPNumberMatchesPrefix(ip_number
, ip_prefix_
,
110 prefix_length_in_bits_
);
113 std::string
ToString() const override
{ return description_
; }
115 Rule
* Clone() const override
{
116 return new BypassIPBlockRule(description_
,
119 prefix_length_in_bits_
);
123 const std::string description_
;
124 const std::string optional_scheme_
;
125 const IPAddressNumber ip_prefix_
;
126 const size_t prefix_length_in_bits_
;
129 // Returns true if the given string represents an IP address.
130 // IPv6 addresses are expected to be bracketed.
131 bool IsIPAddress(const std::string
& domain
) {
132 // From GURL::HostIsIPAddress()
133 url::RawCanonOutputT
<char, 128> ignored_output
;
134 url::CanonHostInfo host_info
;
135 url::Component
domain_comp(0, domain
.size());
136 url::CanonicalizeIPAddress(domain
.c_str(), domain_comp
, &ignored_output
,
138 return host_info
.IsIPAddress();
143 ProxyBypassRules::Rule::Rule() {
146 ProxyBypassRules::Rule::~Rule() {
149 bool ProxyBypassRules::Rule::Equals(const Rule
& rule
) const {
150 return ToString() == rule
.ToString();
153 ProxyBypassRules::ProxyBypassRules() {
156 ProxyBypassRules::ProxyBypassRules(const ProxyBypassRules
& rhs
) {
160 ProxyBypassRules::~ProxyBypassRules() {
164 ProxyBypassRules
& ProxyBypassRules::operator=(const ProxyBypassRules
& rhs
) {
169 bool ProxyBypassRules::Matches(const GURL
& url
) const {
170 for (RuleList::const_iterator it
= rules_
.begin(); it
!= rules_
.end(); ++it
) {
171 if ((*it
)->Matches(url
))
177 bool ProxyBypassRules::Equals(const ProxyBypassRules
& other
) const {
178 if (rules_
.size() != other
.rules_
.size())
181 for (size_t i
= 0; i
< rules_
.size(); ++i
) {
182 if (!rules_
[i
]->Equals(*other
.rules_
[i
]))
188 void ProxyBypassRules::ParseFromString(const std::string
& raw
) {
189 ParseFromStringInternal(raw
, false);
192 void ProxyBypassRules::ParseFromStringUsingSuffixMatching(
193 const std::string
& raw
) {
194 ParseFromStringInternal(raw
, true);
197 bool ProxyBypassRules::AddRuleForHostname(const std::string
& optional_scheme
,
198 const std::string
& hostname_pattern
,
200 if (hostname_pattern
.empty())
203 rules_
.push_back(new HostnamePatternRule(optional_scheme
,
209 void ProxyBypassRules::AddRuleToBypassLocal() {
210 rules_
.push_back(new BypassLocalRule
);
213 bool ProxyBypassRules::AddRuleFromString(const std::string
& raw
) {
214 return AddRuleFromStringInternalWithLogging(raw
, false);
217 bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching(
218 const std::string
& raw
) {
219 return AddRuleFromStringInternalWithLogging(raw
, true);
222 std::string
ProxyBypassRules::ToString() const {
224 for (RuleList::const_iterator
rule(rules_
.begin());
225 rule
!= rules_
.end();
227 result
+= (*rule
)->ToString();
233 void ProxyBypassRules::Clear() {
234 STLDeleteElements(&rules_
);
237 void ProxyBypassRules::AssignFrom(const ProxyBypassRules
& other
) {
240 // Make a copy of the rules list.
241 for (RuleList::const_iterator it
= other
.rules_
.begin();
242 it
!= other
.rules_
.end(); ++it
) {
243 rules_
.push_back((*it
)->Clone());
247 void ProxyBypassRules::ParseFromStringInternal(
248 const std::string
& raw
,
249 bool use_hostname_suffix_matching
) {
252 base::StringTokenizer
entries(raw
, ",;");
253 while (entries
.GetNext()) {
254 AddRuleFromStringInternalWithLogging(entries
.token(),
255 use_hostname_suffix_matching
);
259 bool ProxyBypassRules::AddRuleFromStringInternal(
260 const std::string
& raw_untrimmed
,
261 bool use_hostname_suffix_matching
) {
263 base::TrimWhitespaceASCII(raw_untrimmed
, base::TRIM_ALL
, &raw
);
265 // This is the special syntax used by WinInet's bypass list -- we allow it
266 // on all platforms and interpret it the same way.
267 if (base::LowerCaseEqualsASCII(raw
, "<local>")) {
268 AddRuleToBypassLocal();
272 // Extract any scheme-restriction.
273 std::string::size_type scheme_pos
= raw
.find("://");
275 if (scheme_pos
!= std::string::npos
) {
276 scheme
= raw
.substr(0, scheme_pos
);
277 raw
= raw
.substr(scheme_pos
+ 3);
285 // If there is a forward slash in the input, it is probably a CIDR style
287 if (raw
.find('/') != std::string::npos
) {
288 IPAddressNumber ip_prefix
;
289 size_t prefix_length_in_bits
;
291 if (!ParseCIDRBlock(raw
, &ip_prefix
, &prefix_length_in_bits
))
295 new BypassIPBlockRule(raw
, scheme
, ip_prefix
, prefix_length_in_bits
));
300 // Check if we have an <ip-address>[:port] input. We need to treat this
301 // separately since the IP literal may not be in a canonical form.
304 if (ParseHostAndPort(raw
, &host
, &port
)) {
305 // Note that HostPortPair is used to merely to convert any IPv6 literals to
306 // a URL-safe format that can be used by canonicalization below.
307 std::string bracketed_host
= HostPortPair(host
, 80).HostForURL();
308 if (IsIPAddress(bracketed_host
)) {
309 // Canonicalize the IP literal before adding it as a string pattern.
310 GURL
tmp_url("http://" + bracketed_host
);
311 return AddRuleForHostname(scheme
, tmp_url
.host(), port
);
315 // Otherwise assume we have <hostname-pattern>[:port].
316 std::string::size_type pos_colon
= raw
.rfind(':');
319 if (pos_colon
!= std::string::npos
) {
320 if (!base::StringToInt(base::StringPiece(raw
.begin() + pos_colon
+ 1,
323 (port
< 0 || port
> 0xFFFF)) {
324 return false; // Port was invalid.
326 raw
= raw
.substr(0, pos_colon
);
329 // Special-case hostnames that begin with a period.
330 // For example, we remap ".google.com" --> "*.google.com".
331 if (base::StartsWithASCII(raw
, ".", false))
334 // If suffix matching was asked for, make sure the pattern starts with a
336 if (use_hostname_suffix_matching
&& !base::StartsWithASCII(raw
, "*", false))
339 return AddRuleForHostname(scheme
, raw
, port
);
342 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging(
343 const std::string
& raw
,
344 bool use_hostname_suffix_matching
) {
345 return AddRuleFromStringInternal(raw
, use_hostname_suffix_matching
);