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" | 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/net_util.h" | 14 #include "net/base/net_util.h" |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 class HostnamePatternRule : public ProxyBypassRules::Rule { | 20 class HostnamePatternRule : public ProxyBypassRules::Rule { |
21 public: | 21 public: |
22 HostnamePatternRule(const std::string& optional_scheme, | 22 HostnamePatternRule(const std::string& optional_scheme, |
23 const std::string& hostname_pattern, | 23 const std::string& hostname_pattern, |
24 int optional_port) | 24 int optional_port) |
25 : optional_scheme_(base::StringToLowerASCII(optional_scheme)), | 25 : optional_scheme_(base::StringToLowerASCII(optional_scheme)), |
26 hostname_pattern_(base::StringToLowerASCII(hostname_pattern)), | 26 hostname_pattern_(base::StringToLowerASCII(hostname_pattern)), |
27 optional_port_(optional_port) { | 27 optional_port_(optional_port) { |
28 } | 28 } |
29 | 29 |
30 virtual bool Matches(const GURL& url) const OVERRIDE { | 30 virtual bool Matches(const GURL& url) const override { |
31 if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_) | 31 if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_) |
32 return false; // Didn't match port expectation. | 32 return false; // Didn't match port expectation. |
33 | 33 |
34 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) | 34 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) |
35 return false; // Didn't match scheme expectation. | 35 return false; // Didn't match scheme expectation. |
36 | 36 |
37 // Note it is necessary to lower-case the host, since GURL uses capital | 37 // Note it is necessary to lower-case the host, since GURL uses capital |
38 // letters for percent-escaped characters. | 38 // letters for percent-escaped characters. |
39 return MatchPattern(base::StringToLowerASCII(url.host()), | 39 return MatchPattern(base::StringToLowerASCII(url.host()), |
40 hostname_pattern_); | 40 hostname_pattern_); |
41 } | 41 } |
42 | 42 |
43 virtual std::string ToString() const OVERRIDE { | 43 virtual 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 virtual Rule* Clone() const OVERRIDE { | 53 virtual Rule* Clone() const override { |
54 return new HostnamePatternRule(optional_scheme_, | 54 return new HostnamePatternRule(optional_scheme_, |
55 hostname_pattern_, | 55 hostname_pattern_, |
56 optional_port_); | 56 optional_port_); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 const std::string optional_scheme_; | 60 const std::string optional_scheme_; |
61 const std::string hostname_pattern_; | 61 const std::string hostname_pattern_; |
62 const int optional_port_; | 62 const int optional_port_; |
63 }; | 63 }; |
64 | 64 |
65 class BypassLocalRule : public ProxyBypassRules::Rule { | 65 class BypassLocalRule : public ProxyBypassRules::Rule { |
66 public: | 66 public: |
67 virtual bool Matches(const GURL& url) const OVERRIDE { | 67 virtual bool Matches(const GURL& url) const override { |
68 const std::string& host = url.host(); | 68 const std::string& host = url.host(); |
69 if (host == "127.0.0.1" || host == "[::1]") | 69 if (host == "127.0.0.1" || host == "[::1]") |
70 return true; | 70 return true; |
71 return host.find('.') == std::string::npos; | 71 return host.find('.') == std::string::npos; |
72 } | 72 } |
73 | 73 |
74 virtual std::string ToString() const OVERRIDE { | 74 virtual std::string ToString() const override { |
75 return "<local>"; | 75 return "<local>"; |
76 } | 76 } |
77 | 77 |
78 virtual Rule* Clone() const OVERRIDE { | 78 virtual Rule* Clone() const override { |
79 return new BypassLocalRule(); | 79 return new BypassLocalRule(); |
80 } | 80 } |
81 }; | 81 }; |
82 | 82 |
83 // Rule for matching a URL that is an IP address, if that IP address falls | 83 // Rule for matching a URL that is an IP address, if that IP address falls |
84 // within a certain numeric range. For example, you could use this rule to | 84 // within a certain numeric range. For example, you could use this rule to |
85 // match all the IPs in the CIDR block 10.10.3.4/24. | 85 // match all the IPs in the CIDR block 10.10.3.4/24. |
86 class BypassIPBlockRule : public ProxyBypassRules::Rule { | 86 class BypassIPBlockRule : public ProxyBypassRules::Rule { |
87 public: | 87 public: |
88 // |ip_prefix| + |prefix_length| define the IP block to match. | 88 // |ip_prefix| + |prefix_length| define the IP block to match. |
89 BypassIPBlockRule(const std::string& description, | 89 BypassIPBlockRule(const std::string& description, |
90 const std::string& optional_scheme, | 90 const std::string& optional_scheme, |
91 const IPAddressNumber& ip_prefix, | 91 const IPAddressNumber& ip_prefix, |
92 size_t prefix_length_in_bits) | 92 size_t prefix_length_in_bits) |
93 : description_(description), | 93 : description_(description), |
94 optional_scheme_(optional_scheme), | 94 optional_scheme_(optional_scheme), |
95 ip_prefix_(ip_prefix), | 95 ip_prefix_(ip_prefix), |
96 prefix_length_in_bits_(prefix_length_in_bits) { | 96 prefix_length_in_bits_(prefix_length_in_bits) { |
97 } | 97 } |
98 | 98 |
99 virtual bool Matches(const GURL& url) const OVERRIDE { | 99 virtual bool Matches(const GURL& url) const override { |
100 if (!url.HostIsIPAddress()) | 100 if (!url.HostIsIPAddress()) |
101 return false; | 101 return false; |
102 | 102 |
103 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) | 103 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) |
104 return false; // Didn't match scheme expectation. | 104 return false; // Didn't match scheme expectation. |
105 | 105 |
106 // Parse the input IP literal to a number. | 106 // Parse the input IP literal to a number. |
107 IPAddressNumber ip_number; | 107 IPAddressNumber ip_number; |
108 if (!ParseIPLiteralToNumber(url.HostNoBrackets(), &ip_number)) | 108 if (!ParseIPLiteralToNumber(url.HostNoBrackets(), &ip_number)) |
109 return false; | 109 return false; |
110 | 110 |
111 // Test if it has the expected prefix. | 111 // Test if it has the expected prefix. |
112 return IPNumberMatchesPrefix(ip_number, ip_prefix_, | 112 return IPNumberMatchesPrefix(ip_number, ip_prefix_, |
113 prefix_length_in_bits_); | 113 prefix_length_in_bits_); |
114 } | 114 } |
115 | 115 |
116 virtual std::string ToString() const OVERRIDE { | 116 virtual std::string ToString() const override { |
117 return description_; | 117 return description_; |
118 } | 118 } |
119 | 119 |
120 virtual Rule* Clone() const OVERRIDE { | 120 virtual Rule* Clone() const override { |
121 return new BypassIPBlockRule(description_, | 121 return new BypassIPBlockRule(description_, |
122 optional_scheme_, | 122 optional_scheme_, |
123 ip_prefix_, | 123 ip_prefix_, |
124 prefix_length_in_bits_); | 124 prefix_length_in_bits_); |
125 } | 125 } |
126 | 126 |
127 private: | 127 private: |
128 const std::string description_; | 128 const std::string description_; |
129 const std::string optional_scheme_; | 129 const std::string optional_scheme_; |
130 const IPAddressNumber ip_prefix_; | 130 const IPAddressNumber ip_prefix_; |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 return AddRuleForHostname(scheme, raw, port); | 344 return AddRuleForHostname(scheme, raw, port); |
345 } | 345 } |
346 | 346 |
347 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( | 347 bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( |
348 const std::string& raw, | 348 const std::string& raw, |
349 bool use_hostname_suffix_matching) { | 349 bool use_hostname_suffix_matching) { |
350 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); | 350 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); |
351 } | 351 } |
352 | 352 |
353 } // namespace net | 353 } // namespace net |
OLD | NEW |