Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1372)

Side by Side Diff: net/proxy/proxy_bypass_rules.cc

Issue 1810183002: Migrate net/proxy/* to net::IPAddress. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: re-add a comment Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/stl_util.h"
8 #include "base/strings/pattern.h" 8 #include "base/strings/pattern.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_tokenizer.h" 11 #include "base/strings/string_tokenizer.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "net/base/host_port_pair.h" 14 #include "net/base/host_port_pair.h"
15 #include "net/base/ip_address_number.h" 15 #include "net/base/ip_address.h"
16 #include "net/base/url_util.h" 16 #include "net/base/url_util.h"
17 17
18 namespace net { 18 namespace net {
19 19
20 namespace { 20 namespace {
21 21
22 class HostnamePatternRule : public ProxyBypassRules::Rule { 22 class HostnamePatternRule : public ProxyBypassRules::Rule {
23 public: 23 public:
24 HostnamePatternRule(const std::string& optional_scheme, 24 HostnamePatternRule(const std::string& optional_scheme,
25 const std::string& hostname_pattern, 25 const std::string& hostname_pattern,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 }; 77 };
78 78
79 // Rule for matching a URL that is an IP address, if that IP address falls 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 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. 81 // match all the IPs in the CIDR block 10.10.3.4/24.
82 class BypassIPBlockRule : public ProxyBypassRules::Rule { 82 class BypassIPBlockRule : public ProxyBypassRules::Rule {
83 public: 83 public:
84 // |ip_prefix| + |prefix_length| define the IP block to match. 84 // |ip_prefix| + |prefix_length| define the IP block to match.
85 BypassIPBlockRule(const std::string& description, 85 BypassIPBlockRule(const std::string& description,
86 const std::string& optional_scheme, 86 const std::string& optional_scheme,
87 const IPAddressNumber& ip_prefix, 87 const IPAddress& ip_prefix,
88 size_t prefix_length_in_bits) 88 size_t prefix_length_in_bits)
89 : description_(description), 89 : description_(description),
90 optional_scheme_(optional_scheme), 90 optional_scheme_(optional_scheme),
91 ip_prefix_(ip_prefix), 91 ip_prefix_(ip_prefix),
92 prefix_length_in_bits_(prefix_length_in_bits) { 92 prefix_length_in_bits_(prefix_length_in_bits) {}
93 }
94 93
95 bool Matches(const GURL& url) const override { 94 bool Matches(const GURL& url) const override {
96 if (!url.HostIsIPAddress()) 95 if (!url.HostIsIPAddress())
97 return false; 96 return false;
98 97
99 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) 98 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_)
100 return false; // Didn't match scheme expectation. 99 return false; // Didn't match scheme expectation.
101 100
102 // Parse the input IP literal to a number. 101 // Parse the input IP literal to a number.
103 IPAddressNumber ip_number; 102 IPAddress ip_address;
104 if (!ParseIPLiteralToNumber(url.HostNoBrackets(), &ip_number)) 103 if (!ip_address.AssignFromIPLiteral(url.HostNoBrackets()))
105 return false; 104 return false;
106 105
107 // Test if it has the expected prefix. 106 // Test if it has the expected prefix.
108 return IPNumberMatchesPrefix(ip_number, ip_prefix_, 107 return IPAddressMatchesPrefix(ip_address, ip_prefix_,
109 prefix_length_in_bits_); 108 prefix_length_in_bits_);
110 } 109 }
111 110
112 std::string ToString() const override { return description_; } 111 std::string ToString() const override { return description_; }
113 112
114 Rule* Clone() const override { 113 Rule* Clone() const override {
115 return new BypassIPBlockRule(description_, 114 return new BypassIPBlockRule(description_,
116 optional_scheme_, 115 optional_scheme_,
117 ip_prefix_, 116 ip_prefix_,
118 prefix_length_in_bits_); 117 prefix_length_in_bits_);
119 } 118 }
120 119
121 private: 120 private:
122 const std::string description_; 121 const std::string description_;
123 const std::string optional_scheme_; 122 const std::string optional_scheme_;
124 const IPAddressNumber ip_prefix_; 123 const IPAddress ip_prefix_;
125 const size_t prefix_length_in_bits_; 124 const size_t prefix_length_in_bits_;
126 }; 125 };
127 126
128 // Returns true if the given string represents an IP address. 127 // Returns true if the given string represents an IP address.
129 // IPv6 addresses are expected to be bracketed. 128 // IPv6 addresses are expected to be bracketed.
130 bool IsIPAddress(const std::string& domain) { 129 bool IsIPAddress(const std::string& domain) {
131 // From GURL::HostIsIPAddress() 130 // From GURL::HostIsIPAddress()
132 url::RawCanonOutputT<char, 128> ignored_output; 131 url::RawCanonOutputT<char, 128> ignored_output;
133 url::CanonHostInfo host_info; 132 url::CanonHostInfo host_info;
134 url::Component domain_comp(0, domain.size()); 133 url::Component domain_comp(0, domain.size());
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 if (scheme.empty()) 276 if (scheme.empty())
278 return false; 277 return false;
279 } 278 }
280 279
281 if (raw.empty()) 280 if (raw.empty())
282 return false; 281 return false;
283 282
284 // If there is a forward slash in the input, it is probably a CIDR style 283 // If there is a forward slash in the input, it is probably a CIDR style
285 // mask. 284 // mask.
286 if (raw.find('/') != std::string::npos) { 285 if (raw.find('/') != std::string::npos) {
287 IPAddressNumber ip_prefix; 286 IPAddress ip_prefix;
288 size_t prefix_length_in_bits; 287 size_t prefix_length_in_bits;
289 288
290 if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits)) 289 if (!ParseCIDRBlock(raw, ip_prefix, prefix_length_in_bits))
291 return false; 290 return false;
292 291
293 rules_.push_back( 292 rules_.push_back(
294 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); 293 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits));
295 294
296 return true; 295 return true;
297 } 296 }
298 297
299 // Check if we have an <ip-address>[:port] input. We need to treat this 298 // 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. 299 // separately since the IP literal may not be in a canonical form.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 return AddRuleForHostname(scheme, raw, port); 343 return AddRuleForHostname(scheme, raw, port);
345 } 344 }
346 345
347 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( 346 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging(
348 const std::string& raw, 347 const std::string& raw,
349 bool use_hostname_suffix_matching) { 348 bool use_hostname_suffix_matching) {
350 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); 349 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching);
351 } 350 }
352 351
353 } // namespace net 352 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698