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/proxy_bypass_rules.h" | |
6 | |
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/net_util.h" | |
15 | |
16 namespace net { | |
17 | |
18 namespace { | |
19 | |
20 class HostnamePatternRule : public ProxyBypassRules::Rule { | |
21 public: | |
22 HostnamePatternRule(const std::string& optional_scheme, | |
23 const std::string& hostname_pattern, | |
24 int optional_port) | |
25 : optional_scheme_(base::StringToLowerASCII(optional_scheme)), | |
26 hostname_pattern_(base::StringToLowerASCII(hostname_pattern)), | |
27 optional_port_(optional_port) { | |
28 } | |
29 | |
30 bool Matches(const GURL& url) const override { | |
31 if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_) | |
32 return false; // Didn't match port expectation. | |
33 | |
34 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) | |
35 return false; // Didn't match scheme expectation. | |
36 | |
37 // Note it is necessary to lower-case the host, since GURL uses capital | |
38 // letters for percent-escaped characters. | |
39 return MatchPattern(base::StringToLowerASCII(url.host()), | |
40 hostname_pattern_); | |
41 } | |
42 | |
43 std::string ToString() const override { | |
44 std::string str; | |
45 if (!optional_scheme_.empty()) | |
46 base::StringAppendF(&str, "%s://", optional_scheme_.c_str()); | |
47 str += hostname_pattern_; | |
48 if (optional_port_ != -1) | |
49 base::StringAppendF(&str, ":%d", optional_port_); | |
50 return str; | |
51 } | |
52 | |
53 Rule* Clone() const override { | |
54 return new HostnamePatternRule(optional_scheme_, | |
55 hostname_pattern_, | |
56 optional_port_); | |
57 } | |
58 | |
59 private: | |
60 const std::string optional_scheme_; | |
61 const std::string hostname_pattern_; | |
62 const int optional_port_; | |
63 }; | |
64 | |
65 class BypassLocalRule : public ProxyBypassRules::Rule { | |
66 public: | |
67 bool Matches(const GURL& url) const override { | |
68 const std::string& host = url.host(); | |
69 if (host == "127.0.0.1" || host == "[::1]") | |
70 return true; | |
71 return host.find('.') == std::string::npos; | |
72 } | |
73 | |
74 std::string ToString() const override { return "<local>"; } | |
75 | |
76 Rule* Clone() const override { return new BypassLocalRule(); } | |
77 }; | |
78 | |
79 // Rule for matching a URL that is an IP address, if that IP address falls | |
80 // within a certain numeric range. For example, you could use this rule to | |
81 // match all the IPs in the CIDR block 10.10.3.4/24. | |
82 class BypassIPBlockRule : public ProxyBypassRules::Rule { | |
83 public: | |
84 // |ip_prefix| + |prefix_length| define the IP block to match. | |
85 BypassIPBlockRule(const std::string& description, | |
86 const std::string& optional_scheme, | |
87 const IPAddressNumber& ip_prefix, | |
88 size_t prefix_length_in_bits) | |
89 : description_(description), | |
90 optional_scheme_(optional_scheme), | |
91 ip_prefix_(ip_prefix), | |
92 prefix_length_in_bits_(prefix_length_in_bits) { | |
93 } | |
94 | |
95 bool Matches(const GURL& url) const override { | |
96 if (!url.HostIsIPAddress()) | |
97 return false; | |
98 | |
99 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) | |
100 return false; // Didn't match scheme expectation. | |
101 | |
102 // Parse the input IP literal to a number. | |
103 IPAddressNumber ip_number; | |
104 if (!ParseIPLiteralToNumber(url.HostNoBrackets(), &ip_number)) | |
105 return false; | |
106 | |
107 // Test if it has the expected prefix. | |
108 return IPNumberMatchesPrefix(ip_number, ip_prefix_, | |
109 prefix_length_in_bits_); | |
110 } | |
111 | |
112 std::string ToString() const override { return description_; } | |
113 | |
114 Rule* Clone() const override { | |
115 return new BypassIPBlockRule(description_, | |
116 optional_scheme_, | |
117 ip_prefix_, | |
118 prefix_length_in_bits_); | |
119 } | |
120 | |
121 private: | |
122 const std::string description_; | |
123 const std::string optional_scheme_; | |
124 const IPAddressNumber ip_prefix_; | |
125 const size_t prefix_length_in_bits_; | |
126 }; | |
127 | |
128 // Returns true if the given string represents an IP address. | |
129 // IPv6 addresses are expected to be bracketed. | |
130 bool IsIPAddress(const std::string& domain) { | |
131 // From GURL::HostIsIPAddress() | |
132 url::RawCanonOutputT<char, 128> ignored_output; | |
133 url::CanonHostInfo host_info; | |
134 url::Component domain_comp(0, domain.size()); | |
135 url::CanonicalizeIPAddress(domain.c_str(), domain_comp, &ignored_output, | |
136 &host_info); | |
137 return host_info.IsIPAddress(); | |
138 } | |
139 | |
140 } // namespace | |
141 | |
142 ProxyBypassRules::Rule::Rule() { | |
143 } | |
144 | |
145 ProxyBypassRules::Rule::~Rule() { | |
146 } | |
147 | |
148 bool ProxyBypassRules::Rule::Equals(const Rule& rule) const { | |
149 return ToString() == rule.ToString(); | |
150 } | |
151 | |
152 ProxyBypassRules::ProxyBypassRules() { | |
153 } | |
154 | |
155 ProxyBypassRules::ProxyBypassRules(const ProxyBypassRules& rhs) { | |
156 AssignFrom(rhs); | |
157 } | |
158 | |
159 ProxyBypassRules::~ProxyBypassRules() { | |
160 Clear(); | |
161 } | |
162 | |
163 ProxyBypassRules& ProxyBypassRules::operator=(const ProxyBypassRules& rhs) { | |
164 AssignFrom(rhs); | |
165 return *this; | |
166 } | |
167 | |
168 bool ProxyBypassRules::Matches(const GURL& url) const { | |
169 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); ++it) { | |
170 if ((*it)->Matches(url)) | |
171 return true; | |
172 } | |
173 return false; | |
174 } | |
175 | |
176 bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const { | |
177 if (rules_.size() != other.rules_.size()) | |
178 return false; | |
179 | |
180 for (size_t i = 0; i < rules_.size(); ++i) { | |
181 if (!rules_[i]->Equals(*other.rules_[i])) | |
182 return false; | |
183 } | |
184 return true; | |
185 } | |
186 | |
187 void ProxyBypassRules::ParseFromString(const std::string& raw) { | |
188 ParseFromStringInternal(raw, false); | |
189 } | |
190 | |
191 void ProxyBypassRules::ParseFromStringUsingSuffixMatching( | |
192 const std::string& raw) { | |
193 ParseFromStringInternal(raw, true); | |
194 } | |
195 | |
196 bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme, | |
197 const std::string& hostname_pattern, | |
198 int optional_port) { | |
199 if (hostname_pattern.empty()) | |
200 return false; | |
201 | |
202 rules_.push_back(new HostnamePatternRule(optional_scheme, | |
203 hostname_pattern, | |
204 optional_port)); | |
205 return true; | |
206 } | |
207 | |
208 void ProxyBypassRules::AddRuleToBypassLocal() { | |
209 rules_.push_back(new BypassLocalRule); | |
210 } | |
211 | |
212 bool ProxyBypassRules::AddRuleFromString(const std::string& raw) { | |
213 return AddRuleFromStringInternalWithLogging(raw, false); | |
214 } | |
215 | |
216 bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching( | |
217 const std::string& raw) { | |
218 return AddRuleFromStringInternalWithLogging(raw, true); | |
219 } | |
220 | |
221 std::string ProxyBypassRules::ToString() const { | |
222 std::string result; | |
223 for (RuleList::const_iterator rule(rules_.begin()); | |
224 rule != rules_.end(); | |
225 ++rule) { | |
226 result += (*rule)->ToString(); | |
227 result += ";"; | |
228 } | |
229 return result; | |
230 } | |
231 | |
232 void ProxyBypassRules::Clear() { | |
233 STLDeleteElements(&rules_); | |
234 } | |
235 | |
236 void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) { | |
237 Clear(); | |
238 | |
239 // Make a copy of the rules list. | |
240 for (RuleList::const_iterator it = other.rules_.begin(); | |
241 it != other.rules_.end(); ++it) { | |
242 rules_.push_back((*it)->Clone()); | |
243 } | |
244 } | |
245 | |
246 void ProxyBypassRules::ParseFromStringInternal( | |
247 const std::string& raw, | |
248 bool use_hostname_suffix_matching) { | |
249 Clear(); | |
250 | |
251 base::StringTokenizer entries(raw, ",;"); | |
252 while (entries.GetNext()) { | |
253 AddRuleFromStringInternalWithLogging(entries.token(), | |
254 use_hostname_suffix_matching); | |
255 } | |
256 } | |
257 | |
258 bool ProxyBypassRules::AddRuleFromStringInternal( | |
259 const std::string& raw_untrimmed, | |
260 bool use_hostname_suffix_matching) { | |
261 std::string raw; | |
262 base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL, &raw); | |
263 | |
264 // This is the special syntax used by WinInet's bypass list -- we allow it | |
265 // on all platforms and interpret it the same way. | |
266 if (LowerCaseEqualsASCII(raw, "<local>")) { | |
267 AddRuleToBypassLocal(); | |
268 return true; | |
269 } | |
270 | |
271 // Extract any scheme-restriction. | |
272 std::string::size_type scheme_pos = raw.find("://"); | |
273 std::string scheme; | |
274 if (scheme_pos != std::string::npos) { | |
275 scheme = raw.substr(0, scheme_pos); | |
276 raw = raw.substr(scheme_pos + 3); | |
277 if (scheme.empty()) | |
278 return false; | |
279 } | |
280 | |
281 if (raw.empty()) | |
282 return false; | |
283 | |
284 // If there is a forward slash in the input, it is probably a CIDR style | |
285 // mask. | |
286 if (raw.find('/') != std::string::npos) { | |
287 IPAddressNumber ip_prefix; | |
288 size_t prefix_length_in_bits; | |
289 | |
290 if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits)) | |
291 return false; | |
292 | |
293 rules_.push_back( | |
294 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); | |
295 | |
296 return true; | |
297 } | |
298 | |
299 // Check if we have an <ip-address>[:port] input. We need to treat this | |
300 // separately since the IP literal may not be in a canonical form. | |
301 std::string host; | |
302 int port; | |
303 if (ParseHostAndPort(raw, &host, &port)) { | |
304 // Note that HostPortPair is used to merely to convert any IPv6 literals to | |
305 // a URL-safe format that can be used by canonicalization below. | |
306 std::string bracketed_host = HostPortPair(host, 80).HostForURL(); | |
307 if (IsIPAddress(bracketed_host)) { | |
308 // Canonicalize the IP literal before adding it as a string pattern. | |
309 GURL tmp_url("http://" + bracketed_host); | |
310 return AddRuleForHostname(scheme, tmp_url.host(), port); | |
311 } | |
312 } | |
313 | |
314 // Otherwise assume we have <hostname-pattern>[:port]. | |
315 std::string::size_type pos_colon = raw.rfind(':'); | |
316 host = raw; | |
317 port = -1; | |
318 if (pos_colon != std::string::npos) { | |
319 if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1, | |
320 raw.end()), | |
321 &port) || | |
322 (port < 0 || port > 0xFFFF)) { | |
323 return false; // Port was invalid. | |
324 } | |
325 raw = raw.substr(0, pos_colon); | |
326 } | |
327 | |
328 // Special-case hostnames that begin with a period. | |
329 // For example, we remap ".google.com" --> "*.google.com". | |
330 if (StartsWithASCII(raw, ".", false)) | |
331 raw = "*" + raw; | |
332 | |
333 // If suffix matching was asked for, make sure the pattern starts with a | |
334 // wildcard. | |
335 if (use_hostname_suffix_matching && !StartsWithASCII(raw, "*", false)) | |
336 raw = "*" + raw; | |
337 | |
338 return AddRuleForHostname(scheme, raw, port); | |
339 } | |
340 | |
341 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( | |
342 const std::string& raw, | |
343 bool use_hostname_suffix_matching) { | |
344 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); | |
345 } | |
346 | |
347 } // namespace net | |
OLD | NEW |