Switch some page set from user_agent fields to coresponding shared_state classes
[chromium-blink-merge.git] / net / proxy / proxy_bypass_rules.cc
blobbb163adc9dcdb45f66c17436d4babf3f55d85f13
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"
17 namespace net {
19 namespace {
21 class HostnamePatternRule : public ProxyBypassRules::Rule {
22 public:
23 HostnamePatternRule(const std::string& optional_scheme,
24 const std::string& hostname_pattern,
25 int optional_port)
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()),
41 hostname_pattern_);
44 std::string ToString() const override {
45 std::string str;
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_);
51 return str;
54 Rule* Clone() const override {
55 return new HostnamePatternRule(optional_scheme_,
56 hostname_pattern_,
57 optional_port_);
60 private:
61 const std::string optional_scheme_;
62 const std::string hostname_pattern_;
63 const int optional_port_;
66 class BypassLocalRule : public ProxyBypassRules::Rule {
67 public:
68 bool Matches(const GURL& url) const override {
69 const std::string& host = url.host();
70 if (host == "127.0.0.1" || host == "[::1]")
71 return true;
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 {
84 public:
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())
98 return false;
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))
106 return false;
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_,
117 optional_scheme_,
118 ip_prefix_,
119 prefix_length_in_bits_);
122 private:
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,
137 &host_info);
138 return host_info.IsIPAddress();
141 } // namespace
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) {
157 AssignFrom(rhs);
160 ProxyBypassRules::~ProxyBypassRules() {
161 Clear();
164 ProxyBypassRules& ProxyBypassRules::operator=(const ProxyBypassRules& rhs) {
165 AssignFrom(rhs);
166 return *this;
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))
172 return true;
174 return false;
177 bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const {
178 if (rules_.size() != other.rules_.size())
179 return false;
181 for (size_t i = 0; i < rules_.size(); ++i) {
182 if (!rules_[i]->Equals(*other.rules_[i]))
183 return false;
185 return true;
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,
199 int optional_port) {
200 if (hostname_pattern.empty())
201 return false;
203 rules_.push_back(new HostnamePatternRule(optional_scheme,
204 hostname_pattern,
205 optional_port));
206 return true;
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 {
223 std::string result;
224 for (RuleList::const_iterator rule(rules_.begin());
225 rule != rules_.end();
226 ++rule) {
227 result += (*rule)->ToString();
228 result += ";";
230 return result;
233 void ProxyBypassRules::Clear() {
234 STLDeleteElements(&rules_);
237 void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) {
238 Clear();
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) {
250 Clear();
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) {
262 std::string raw;
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();
269 return true;
272 // Extract any scheme-restriction.
273 std::string::size_type scheme_pos = raw.find("://");
274 std::string scheme;
275 if (scheme_pos != std::string::npos) {
276 scheme = raw.substr(0, scheme_pos);
277 raw = raw.substr(scheme_pos + 3);
278 if (scheme.empty())
279 return false;
282 if (raw.empty())
283 return false;
285 // If there is a forward slash in the input, it is probably a CIDR style
286 // mask.
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))
292 return false;
294 rules_.push_back(
295 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits));
297 return true;
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.
302 std::string host;
303 int port;
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(':');
317 host = raw;
318 port = -1;
319 if (pos_colon != std::string::npos) {
320 if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1,
321 raw.end()),
322 &port) ||
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 (StartsWithASCII(raw, ".", false))
332 raw = "*" + raw;
334 // If suffix matching was asked for, make sure the pattern starts with a
335 // wildcard.
336 if (use_hostname_suffix_matching && !StartsWithASCII(raw, "*", false))
337 raw = "*" + raw;
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);
348 } // namespace net