Chromium Code Reviews| 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/stl_util.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_piece.h" | |
| 10 #include "base/strings/string_tokenizer.h" | |
| 9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/string_piece.h" | 13 #include "net/base/host_port_pair.h" |
| 12 #include "base/strings/string_tokenizer.h" | |
| 13 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 14 | 15 |
| 15 namespace net { | 16 namespace net { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class HostnamePatternRule : public ProxyBypassRules::Rule { | 20 class HostnamePatternRule : public ProxyBypassRules::Rule { |
| 20 public: | 21 public: |
| 21 HostnamePatternRule(const std::string& optional_scheme, | 22 HostnamePatternRule(const std::string& optional_scheme, |
| 22 const std::string& hostname_pattern, | 23 const std::string& hostname_pattern, |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 } | 125 } |
| 125 | 126 |
| 126 private: | 127 private: |
| 127 const std::string description_; | 128 const std::string description_; |
| 128 const std::string optional_scheme_; | 129 const std::string optional_scheme_; |
| 129 const IPAddressNumber ip_prefix_; | 130 const IPAddressNumber ip_prefix_; |
| 130 const size_t prefix_length_in_bits_; | 131 const size_t prefix_length_in_bits_; |
| 131 }; | 132 }; |
| 132 | 133 |
| 133 // Returns true if the given string represents an IP address. | 134 // Returns true if the given string represents an IP address. |
| 135 // IPv6 addresses are expected to be bracketed. | |
| 134 bool IsIPAddress(const std::string& domain) { | 136 bool IsIPAddress(const std::string& domain) { |
| 135 // From GURL::HostIsIPAddress() | 137 // From GURL::HostIsIPAddress() |
| 136 url::RawCanonOutputT<char, 128> ignored_output; | 138 url::RawCanonOutputT<char, 128> ignored_output; |
| 137 url::CanonHostInfo host_info; | 139 url::CanonHostInfo host_info; |
| 138 url::Component domain_comp(0, domain.size()); | 140 url::Component domain_comp(0, domain.size()); |
| 139 url::CanonicalizeIPAddress(domain.c_str(), domain_comp, &ignored_output, | 141 url::CanonicalizeIPAddress(domain.c_str(), domain_comp, &ignored_output, |
| 140 &host_info); | 142 &host_info); |
| 141 return host_info.IsIPAddress(); | 143 return host_info.IsIPAddress(); |
| 142 } | 144 } |
| 143 | 145 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); | 300 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); |
| 299 | 301 |
| 300 return true; | 302 return true; |
| 301 } | 303 } |
| 302 | 304 |
| 303 // Check if we have an <ip-address>[:port] input. We need to treat this | 305 // Check if we have an <ip-address>[:port] input. We need to treat this |
| 304 // separately since the IP literal may not be in a canonical form. | 306 // separately since the IP literal may not be in a canonical form. |
| 305 std::string host; | 307 std::string host; |
| 306 int port; | 308 int port; |
| 307 if (ParseHostAndPort(raw, &host, &port)) { | 309 if (ParseHostAndPort(raw, &host, &port)) { |
| 308 if (IsIPAddress(host)) { | 310 std::string bracketed_host = HostPortPair(host, 80).HostForURL(); |
|
Ryan Hamilton
2014/09/24 21:48:49
nit: s/80/port/ ? (I suspect it doesn't actually
eroman
2014/09/24 21:59:28
The specific port used does not matter, I am just
| |
| 311 if (IsIPAddress(bracketed_host)) { | |
| 309 // Canonicalize the IP literal before adding it as a string pattern. | 312 // Canonicalize the IP literal before adding it as a string pattern. |
| 310 GURL tmp_url("http://" + host); | 313 GURL tmp_url("http://" + bracketed_host); |
| 311 return AddRuleForHostname(scheme, tmp_url.host(), port); | 314 return AddRuleForHostname(scheme, tmp_url.host(), port); |
| 312 } | 315 } |
| 313 } | 316 } |
| 314 | 317 |
| 315 // Otherwise assume we have <hostname-pattern>[:port]. | 318 // Otherwise assume we have <hostname-pattern>[:port]. |
| 316 std::string::size_type pos_colon = raw.rfind(':'); | 319 std::string::size_type pos_colon = raw.rfind(':'); |
| 317 host = raw; | 320 host = raw; |
| 318 port = -1; | 321 port = -1; |
| 319 if (pos_colon != std::string::npos) { | 322 if (pos_colon != std::string::npos) { |
| 320 if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1, | 323 if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 339 return AddRuleForHostname(scheme, raw, port); | 342 return AddRuleForHostname(scheme, raw, port); |
| 340 } | 343 } |
| 341 | 344 |
| 342 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( | 345 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( |
| 343 const std::string& raw, | 346 const std::string& raw, |
| 344 bool use_hostname_suffix_matching) { | 347 bool use_hostname_suffix_matching) { |
| 345 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); | 348 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); |
| 346 } | 349 } |
| 347 | 350 |
| 348 } // namespace net | 351 } // namespace net |
| OLD | NEW |