OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_bypass_rules.h" | 5 #include "net/proxy/proxy_bypass_rules.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/pattern.h" | 8 #include "base/strings/pattern.h" |
9 #include "base/strings/string_piece.h" | 9 #include "base/strings/string_piece.h" |
10 #include "base/strings/string_tokenizer.h" | 10 #include "base/strings/string_tokenizer.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
14 #include "net/base/ip_address.h" | 14 #include "net/base/ip_address.h" |
15 #include "net/base/parse_number.h" | 15 #include "net/base/parse_number.h" |
16 #include "net/base/url_util.h" | 16 #include "net/base/url_util.h" |
17 | 17 |
(...skipping 25 matching lines...) Expand all Loading... |
43 std::string ToString() const override { | 43 std::string ToString() const override { |
44 std::string str; | 44 std::string str; |
45 if (!optional_scheme_.empty()) | 45 if (!optional_scheme_.empty()) |
46 base::StringAppendF(&str, "%s://", optional_scheme_.c_str()); | 46 base::StringAppendF(&str, "%s://", optional_scheme_.c_str()); |
47 str += hostname_pattern_; | 47 str += hostname_pattern_; |
48 if (optional_port_ != -1) | 48 if (optional_port_ != -1) |
49 base::StringAppendF(&str, ":%d", optional_port_); | 49 base::StringAppendF(&str, ":%d", optional_port_); |
50 return str; | 50 return str; |
51 } | 51 } |
52 | 52 |
53 Rule* Clone() const override { | 53 std::unique_ptr<Rule> Clone() const override { |
54 return new HostnamePatternRule(optional_scheme_, | 54 return base::MakeUnique<HostnamePatternRule>( |
55 hostname_pattern_, | 55 optional_scheme_, hostname_pattern_, optional_port_); |
56 optional_port_); | |
57 } | 56 } |
58 | 57 |
59 private: | 58 private: |
60 const std::string optional_scheme_; | 59 const std::string optional_scheme_; |
61 const std::string hostname_pattern_; | 60 const std::string hostname_pattern_; |
62 const int optional_port_; | 61 const int optional_port_; |
63 }; | 62 }; |
64 | 63 |
65 class BypassLocalRule : public ProxyBypassRules::Rule { | 64 class BypassLocalRule : public ProxyBypassRules::Rule { |
66 public: | 65 public: |
67 bool Matches(const GURL& url) const override { | 66 bool Matches(const GURL& url) const override { |
68 const std::string& host = url.host(); | 67 const std::string& host = url.host(); |
69 if (host == "127.0.0.1" || host == "[::1]") | 68 if (host == "127.0.0.1" || host == "[::1]") |
70 return true; | 69 return true; |
71 return host.find('.') == std::string::npos; | 70 return host.find('.') == std::string::npos; |
72 } | 71 } |
73 | 72 |
74 std::string ToString() const override { return "<local>"; } | 73 std::string ToString() const override { return "<local>"; } |
75 | 74 |
76 Rule* Clone() const override { return new BypassLocalRule(); } | 75 std::unique_ptr<Rule> Clone() const override { |
| 76 return base::MakeUnique<BypassLocalRule>(); |
| 77 } |
77 }; | 78 }; |
78 | 79 |
79 // Rule for matching a URL that is an IP address, if that IP address falls | 80 // 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 // 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 // match all the IPs in the CIDR block 10.10.3.4/24. |
82 class BypassIPBlockRule : public ProxyBypassRules::Rule { | 83 class BypassIPBlockRule : public ProxyBypassRules::Rule { |
83 public: | 84 public: |
84 // |ip_prefix| + |prefix_length| define the IP block to match. | 85 // |ip_prefix| + |prefix_length| define the IP block to match. |
85 BypassIPBlockRule(const std::string& description, | 86 BypassIPBlockRule(const std::string& description, |
86 const std::string& optional_scheme, | 87 const std::string& optional_scheme, |
(...skipping 16 matching lines...) Expand all Loading... |
103 if (!ip_address.AssignFromIPLiteral(url.HostNoBrackets())) | 104 if (!ip_address.AssignFromIPLiteral(url.HostNoBrackets())) |
104 return false; | 105 return false; |
105 | 106 |
106 // Test if it has the expected prefix. | 107 // Test if it has the expected prefix. |
107 return IPAddressMatchesPrefix(ip_address, ip_prefix_, | 108 return IPAddressMatchesPrefix(ip_address, ip_prefix_, |
108 prefix_length_in_bits_); | 109 prefix_length_in_bits_); |
109 } | 110 } |
110 | 111 |
111 std::string ToString() const override { return description_; } | 112 std::string ToString() const override { return description_; } |
112 | 113 |
113 Rule* Clone() const override { | 114 std::unique_ptr<Rule> Clone() const override { |
114 return new BypassIPBlockRule(description_, | 115 return base::MakeUnique<BypassIPBlockRule>( |
115 optional_scheme_, | 116 description_, optional_scheme_, ip_prefix_, prefix_length_in_bits_); |
116 ip_prefix_, | |
117 prefix_length_in_bits_); | |
118 } | 117 } |
119 | 118 |
120 private: | 119 private: |
121 const std::string description_; | 120 const std::string description_; |
122 const std::string optional_scheme_; | 121 const std::string optional_scheme_; |
123 const IPAddress ip_prefix_; | 122 const IPAddress ip_prefix_; |
124 const size_t prefix_length_in_bits_; | 123 const size_t prefix_length_in_bits_; |
125 }; | 124 }; |
126 | 125 |
127 // Returns true if the given string represents an IP address. | 126 // Returns true if the given string represents an IP address. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 const std::string& raw) { | 190 const std::string& raw) { |
192 ParseFromStringInternal(raw, true); | 191 ParseFromStringInternal(raw, true); |
193 } | 192 } |
194 | 193 |
195 bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme, | 194 bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme, |
196 const std::string& hostname_pattern, | 195 const std::string& hostname_pattern, |
197 int optional_port) { | 196 int optional_port) { |
198 if (hostname_pattern.empty()) | 197 if (hostname_pattern.empty()) |
199 return false; | 198 return false; |
200 | 199 |
201 rules_.push_back(new HostnamePatternRule(optional_scheme, | 200 rules_.push_back(base::MakeUnique<HostnamePatternRule>( |
202 hostname_pattern, | 201 optional_scheme, hostname_pattern, optional_port)); |
203 optional_port)); | |
204 return true; | 202 return true; |
205 } | 203 } |
206 | 204 |
207 void ProxyBypassRules::AddRuleToBypassLocal() { | 205 void ProxyBypassRules::AddRuleToBypassLocal() { |
208 rules_.push_back(new BypassLocalRule); | 206 rules_.push_back(base::MakeUnique<BypassLocalRule>()); |
209 } | 207 } |
210 | 208 |
211 bool ProxyBypassRules::AddRuleFromString(const std::string& raw) { | 209 bool ProxyBypassRules::AddRuleFromString(const std::string& raw) { |
212 return AddRuleFromStringInternalWithLogging(raw, false); | 210 return AddRuleFromStringInternalWithLogging(raw, false); |
213 } | 211 } |
214 | 212 |
215 bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching( | 213 bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching( |
216 const std::string& raw) { | 214 const std::string& raw) { |
217 return AddRuleFromStringInternalWithLogging(raw, true); | 215 return AddRuleFromStringInternalWithLogging(raw, true); |
218 } | 216 } |
219 | 217 |
220 std::string ProxyBypassRules::ToString() const { | 218 std::string ProxyBypassRules::ToString() const { |
221 std::string result; | 219 std::string result; |
222 for (RuleList::const_iterator rule(rules_.begin()); | 220 for (RuleList::const_iterator rule(rules_.begin()); |
223 rule != rules_.end(); | 221 rule != rules_.end(); |
224 ++rule) { | 222 ++rule) { |
225 result += (*rule)->ToString(); | 223 result += (*rule)->ToString(); |
226 result += ";"; | 224 result += ";"; |
227 } | 225 } |
228 return result; | 226 return result; |
229 } | 227 } |
230 | 228 |
231 void ProxyBypassRules::Clear() { | 229 void ProxyBypassRules::Clear() { |
232 base::STLDeleteElements(&rules_); | 230 rules_.clear(); |
233 } | 231 } |
234 | 232 |
235 void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) { | 233 void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) { |
236 Clear(); | 234 Clear(); |
237 | 235 |
238 // Make a copy of the rules list. | 236 // Make a copy of the rules list. |
239 for (RuleList::const_iterator it = other.rules_.begin(); | 237 for (RuleList::const_iterator it = other.rules_.begin(); |
240 it != other.rules_.end(); ++it) { | 238 it != other.rules_.end(); ++it) { |
241 rules_.push_back((*it)->Clone()); | 239 rules_.push_back((*it)->Clone()); |
242 } | 240 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 280 |
283 // If there is a forward slash in the input, it is probably a CIDR style | 281 // If there is a forward slash in the input, it is probably a CIDR style |
284 // mask. | 282 // mask. |
285 if (raw.find('/') != std::string::npos) { | 283 if (raw.find('/') != std::string::npos) { |
286 IPAddress ip_prefix; | 284 IPAddress ip_prefix; |
287 size_t prefix_length_in_bits; | 285 size_t prefix_length_in_bits; |
288 | 286 |
289 if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits)) | 287 if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits)) |
290 return false; | 288 return false; |
291 | 289 |
292 rules_.push_back( | 290 rules_.push_back(base::MakeUnique<BypassIPBlockRule>( |
293 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); | 291 raw, scheme, ip_prefix, prefix_length_in_bits)); |
294 | 292 |
295 return true; | 293 return true; |
296 } | 294 } |
297 | 295 |
298 // Check if we have an <ip-address>[:port] input. We need to treat this | 296 // Check if we have an <ip-address>[:port] input. We need to treat this |
299 // separately since the IP literal may not be in a canonical form. | 297 // separately since the IP literal may not be in a canonical form. |
300 std::string host; | 298 std::string host; |
301 int port; | 299 int port; |
302 if (ParseHostAndPort(raw, &host, &port)) { | 300 if (ParseHostAndPort(raw, &host, &port)) { |
303 // TODO(eroman): HostForURL() below DCHECKs() when |host| contains an | 301 // TODO(eroman): HostForURL() below DCHECKs() when |host| contains an |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 return AddRuleForHostname(scheme, raw, port); | 339 return AddRuleForHostname(scheme, raw, port); |
342 } | 340 } |
343 | 341 |
344 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( | 342 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( |
345 const std::string& raw, | 343 const std::string& raw, |
346 bool use_hostname_suffix_matching) { | 344 bool use_hostname_suffix_matching) { |
347 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); | 345 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); |
348 } | 346 } |
349 | 347 |
350 } // namespace net | 348 } // namespace net |
OLD | NEW |