OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, |
6 // later modified by others), but almost entirely rewritten for Chrome. | 6 // later modified by others), but almost entirely rewritten for Chrome. |
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) | 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) |
8 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
10 * | 10 * |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 #include "base/string_util.h" | 49 #include "base/string_util.h" |
50 #include "base/utf_string_conversions.h" | 50 #include "base/utf_string_conversions.h" |
51 #include "googleurl/src/gurl.h" | 51 #include "googleurl/src/gurl.h" |
52 #include "googleurl/src/url_parse.h" | 52 #include "googleurl/src/url_parse.h" |
53 #include "net/base/net_module.h" | 53 #include "net/base/net_module.h" |
54 #include "net/base/net_util.h" | 54 #include "net/base/net_util.h" |
55 | 55 |
56 #include "effective_tld_names.cc" | 56 #include "effective_tld_names.cc" |
57 | 57 |
58 namespace net { | 58 namespace net { |
| 59 namespace registry_controlled_domains { |
59 | 60 |
60 namespace { | 61 namespace { |
61 | 62 |
62 const int kExceptionRule = 1; | 63 const int kExceptionRule = 1; |
63 const int kWildcardRule = 2; | 64 const int kWildcardRule = 2; |
64 | 65 |
65 } // namespace | 66 const FindDomainPtr kDefaultFindDomainFunction = Perfect_Hash::FindDomain; |
| 67 FindDomainPtr g_find_domain_function = kDefaultFindDomainFunction; |
66 | 68 |
67 RegistryControlledDomainService::FindDomainPtr | 69 size_t GetRegistryLengthImpl( |
68 RegistryControlledDomainService::find_domain_function_ = | |
69 Perfect_Hash::FindDomain; | |
70 | |
71 // static | |
72 std::string RegistryControlledDomainService::GetDomainAndRegistry( | |
73 const GURL& gurl) { | |
74 const url_parse::Component host = | |
75 gurl.parsed_for_possibly_invalid_spec().host; | |
76 if ((host.len <= 0) || gurl.HostIsIPAddress()) | |
77 return std::string(); | |
78 return GetDomainAndRegistryImpl(std::string( | |
79 gurl.possibly_invalid_spec().data() + host.begin, host.len)); | |
80 } | |
81 | |
82 // static | |
83 std::string RegistryControlledDomainService::GetDomainAndRegistry( | |
84 const std::string& host) { | |
85 url_canon::CanonHostInfo host_info; | |
86 const std::string canon_host(CanonicalizeHost(host, &host_info)); | |
87 if (canon_host.empty() || host_info.IsIPAddress()) | |
88 return std::string(); | |
89 return GetDomainAndRegistryImpl(canon_host); | |
90 } | |
91 | |
92 // static | |
93 bool RegistryControlledDomainService::SameDomainOrHost(const GURL& gurl1, | |
94 const GURL& gurl2) { | |
95 // See if both URLs have a known domain + registry, and those values are the | |
96 // same. | |
97 const std::string domain1(GetDomainAndRegistry(gurl1)); | |
98 const std::string domain2(GetDomainAndRegistry(gurl2)); | |
99 if (!domain1.empty() || !domain2.empty()) | |
100 return domain1 == domain2; | |
101 | |
102 // No domains. See if the hosts are identical. | |
103 const url_parse::Component host1 = | |
104 gurl1.parsed_for_possibly_invalid_spec().host; | |
105 const url_parse::Component host2 = | |
106 gurl2.parsed_for_possibly_invalid_spec().host; | |
107 if ((host1.len <= 0) || (host1.len != host2.len)) | |
108 return false; | |
109 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, | |
110 gurl2.possibly_invalid_spec().data() + host2.begin, | |
111 host1.len); | |
112 } | |
113 | |
114 // static | |
115 size_t RegistryControlledDomainService::GetRegistryLength( | |
116 const GURL& gurl, | |
117 bool allow_unknown_registries) { | |
118 const url_parse::Component host = | |
119 gurl.parsed_for_possibly_invalid_spec().host; | |
120 if (host.len <= 0) | |
121 return std::string::npos; | |
122 if (gurl.HostIsIPAddress()) | |
123 return 0; | |
124 return GetRegistryLengthImpl( | |
125 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), | |
126 allow_unknown_registries); | |
127 } | |
128 | |
129 // static | |
130 size_t RegistryControlledDomainService::GetRegistryLength( | |
131 const std::string& host, | 70 const std::string& host, |
132 bool allow_unknown_registries) { | 71 UnknownRegistryFilter unknown_filter, |
133 url_canon::CanonHostInfo host_info; | 72 PrivateRegistryFilter private_filter) { |
134 const std::string canon_host(CanonicalizeHost(host, &host_info)); | |
135 if (canon_host.empty()) | |
136 return std::string::npos; | |
137 if (host_info.IsIPAddress()) | |
138 return 0; | |
139 return GetRegistryLengthImpl(canon_host, allow_unknown_registries); | |
140 } | |
141 | |
142 // static | |
143 void RegistryControlledDomainService::UseFindDomainFunction( | |
144 FindDomainPtr function) { | |
145 find_domain_function_ = function ? function : Perfect_Hash::FindDomain; | |
146 } | |
147 | |
148 // static | |
149 std::string RegistryControlledDomainService::GetDomainAndRegistryImpl( | |
150 const std::string& host) { | |
151 DCHECK(!host.empty()); | |
152 | |
153 // Find the length of the registry for this host. | |
154 const size_t registry_length = GetRegistryLengthImpl(host, true); | |
155 if ((registry_length == std::string::npos) || (registry_length == 0)) | |
156 return std::string(); // No registry. | |
157 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding | |
158 // subcomponent length. | |
159 DCHECK(host.length() >= 2); | |
160 if (registry_length > (host.length() - 2)) { | |
161 NOTREACHED() << | |
162 "Host does not have at least one subcomponent before registry!"; | |
163 return std::string(); | |
164 } | |
165 | |
166 // Move past the dot preceding the registry, and search for the next previous | |
167 // dot. Return the host from after that dot, or the whole host when there is | |
168 // no dot. | |
169 const size_t dot = host.rfind('.', host.length() - registry_length - 2); | |
170 if (dot == std::string::npos) | |
171 return host; | |
172 return host.substr(dot + 1); | |
173 } | |
174 | |
175 size_t RegistryControlledDomainService::GetRegistryLengthImpl( | |
176 const std::string& host, | |
177 bool allow_unknown_registries) { | |
178 DCHECK(!host.empty()); | 73 DCHECK(!host.empty()); |
179 | 74 |
180 // Skip leading dots. | 75 // Skip leading dots. |
181 const size_t host_check_begin = host.find_first_not_of('.'); | 76 const size_t host_check_begin = host.find_first_not_of('.'); |
182 if (host_check_begin == std::string::npos) | 77 if (host_check_begin == std::string::npos) |
183 return 0; // Host is only dots. | 78 return 0; // Host is only dots. |
184 | 79 |
185 // A single trailing dot isn't relevant in this determination, but does need | 80 // A single trailing dot isn't relevant in this determination, but does need |
186 // to be included in the final returned length. | 81 // to be included in the final returned length. |
187 size_t host_check_len = host.length(); | 82 size_t host_check_len = host.length(); |
188 if (host[host_check_len - 1] == '.') { | 83 if (host[host_check_len - 1] == '.') { |
189 --host_check_len; | 84 --host_check_len; |
190 DCHECK(host_check_len > 0); // If this weren't true, the host would be ".", | 85 DCHECK(host_check_len > 0); // If this weren't true, the host would be ".", |
191 // and we'd have already returned above. | 86 // and we'd have already returned above. |
192 if (host[host_check_len - 1] == '.') | 87 if (host[host_check_len - 1] == '.') |
193 return 0; // Multiple trailing dots. | 88 return 0; // Multiple trailing dots. |
194 } | 89 } |
195 | 90 |
196 // Walk up the domain tree, most specific to least specific, | 91 // Walk up the domain tree, most specific to least specific, |
197 // looking for matches at each level. | 92 // looking for matches at each level. |
198 size_t prev_start = std::string::npos; | 93 size_t prev_start = std::string::npos; |
199 size_t curr_start = host_check_begin; | 94 size_t curr_start = host_check_begin; |
200 size_t next_dot = host.find('.', curr_start); | 95 size_t next_dot = host.find('.', curr_start); |
201 if (next_dot >= host_check_len) // Catches std::string::npos as well. | 96 if (next_dot >= host_check_len) // Catches std::string::npos as well. |
202 return 0; // This can't have a registry + domain. | 97 return 0; // This can't have a registry + domain. |
203 while (1) { | 98 while (1) { |
204 const char* domain_str = host.data() + curr_start; | 99 const char* domain_str = host.data() + curr_start; |
205 int domain_length = host_check_len - curr_start; | 100 int domain_length = host_check_len - curr_start; |
206 const DomainRule* rule = find_domain_function_(domain_str, domain_length); | 101 const DomainRule* rule = g_find_domain_function(domain_str, domain_length); |
207 | 102 |
208 // We need to compare the string after finding a match because the | 103 // We need to compare the string after finding a match because the |
209 // no-collisions of perfect hashing only refers to items in the set. Since | 104 // no-collisions of perfect hashing only refers to items in the set. Since |
210 // we're searching for arbitrary domains, there could be collisions. | 105 // we're searching for arbitrary domains, there could be collisions. |
| 106 // Furthermore, if the apparent match is a private registry and we're not |
| 107 // including those, it can't be an actual match. |
211 if (rule && | 108 if (rule && |
| 109 (private_filter == INCLUDE_PRIVATE_REGISTRIES || !rule->is_private) && |
212 base::strncasecmp(domain_str, rule->name, domain_length) == 0) { | 110 base::strncasecmp(domain_str, rule->name, domain_length) == 0) { |
213 // Exception rules override wildcard rules when the domain is an exact | 111 // Exception rules override wildcard rules when the domain is an exact |
214 // match, but wildcards take precedence when there's a subdomain. | 112 // match, but wildcards take precedence when there's a subdomain. |
215 if (rule->type == kWildcardRule && (prev_start != std::string::npos)) { | 113 if (rule->type == kWildcardRule && (prev_start != std::string::npos)) { |
216 // If prev_start == host_check_begin, then the host is the registry | 114 // If prev_start == host_check_begin, then the host is the registry |
217 // itself, so return 0. | 115 // itself, so return 0. |
218 return (prev_start == host_check_begin) ? | 116 return (prev_start == host_check_begin) ? |
219 0 : (host.length() - prev_start); | 117 0 : (host.length() - prev_start); |
220 } | 118 } |
221 | 119 |
(...skipping 19 matching lines...) Expand all Loading... |
241 break; | 139 break; |
242 | 140 |
243 prev_start = curr_start; | 141 prev_start = curr_start; |
244 curr_start = next_dot + 1; | 142 curr_start = next_dot + 1; |
245 next_dot = host.find('.', curr_start); | 143 next_dot = host.find('.', curr_start); |
246 } | 144 } |
247 | 145 |
248 // No rule found in the registry. curr_start now points to the first | 146 // No rule found in the registry. curr_start now points to the first |
249 // character of the last subcomponent of the host, so if we allow unknown | 147 // character of the last subcomponent of the host, so if we allow unknown |
250 // registries, return the length of this subcomponent. | 148 // registries, return the length of this subcomponent. |
251 return allow_unknown_registries ? (host.length() - curr_start) : 0; | 149 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ? |
| 150 (host.length() - curr_start) : 0; |
252 } | 151 } |
253 | 152 |
| 153 std::string GetDomainAndRegistryImpl( |
| 154 const std::string& host, PrivateRegistryFilter private_filter) { |
| 155 DCHECK(!host.empty()); |
| 156 |
| 157 // Find the length of the registry for this host. |
| 158 const size_t registry_length = |
| 159 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); |
| 160 if ((registry_length == std::string::npos) || (registry_length == 0)) |
| 161 return std::string(); // No registry. |
| 162 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding |
| 163 // subcomponent length. |
| 164 DCHECK(host.length() >= 2); |
| 165 if (registry_length > (host.length() - 2)) { |
| 166 NOTREACHED() << |
| 167 "Host does not have at least one subcomponent before registry!"; |
| 168 return std::string(); |
| 169 } |
| 170 |
| 171 // Move past the dot preceding the registry, and search for the next previous |
| 172 // dot. Return the host from after that dot, or the whole host when there is |
| 173 // no dot. |
| 174 const size_t dot = host.rfind('.', host.length() - registry_length - 2); |
| 175 if (dot == std::string::npos) |
| 176 return host; |
| 177 return host.substr(dot + 1); |
| 178 } |
| 179 |
| 180 } // namespace |
| 181 |
| 182 std::string GetDomainAndRegistry( |
| 183 const GURL& gurl, |
| 184 PrivateRegistryFilter filter) { |
| 185 const url_parse::Component host = |
| 186 gurl.parsed_for_possibly_invalid_spec().host; |
| 187 if ((host.len <= 0) || gurl.HostIsIPAddress()) |
| 188 return std::string(); |
| 189 return GetDomainAndRegistryImpl(std::string( |
| 190 gurl.possibly_invalid_spec().data() + host.begin, host.len), filter); |
| 191 } |
| 192 |
| 193 std::string GetDomainAndRegistry( |
| 194 const std::string& host, |
| 195 PrivateRegistryFilter filter) { |
| 196 url_canon::CanonHostInfo host_info; |
| 197 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
| 198 if (canon_host.empty() || host_info.IsIPAddress()) |
| 199 return std::string(); |
| 200 return GetDomainAndRegistryImpl(canon_host, filter); |
| 201 } |
| 202 |
| 203 bool SameDomainOrHost( |
| 204 const GURL& gurl1, |
| 205 const GURL& gurl2, |
| 206 PrivateRegistryFilter filter) { |
| 207 // See if both URLs have a known domain + registry, and those values are the |
| 208 // same. |
| 209 const std::string domain1(GetDomainAndRegistry(gurl1, filter)); |
| 210 const std::string domain2(GetDomainAndRegistry(gurl2, filter)); |
| 211 if (!domain1.empty() || !domain2.empty()) |
| 212 return domain1 == domain2; |
| 213 |
| 214 // No domains. See if the hosts are identical. |
| 215 const url_parse::Component host1 = |
| 216 gurl1.parsed_for_possibly_invalid_spec().host; |
| 217 const url_parse::Component host2 = |
| 218 gurl2.parsed_for_possibly_invalid_spec().host; |
| 219 if ((host1.len <= 0) || (host1.len != host2.len)) |
| 220 return false; |
| 221 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, |
| 222 gurl2.possibly_invalid_spec().data() + host2.begin, |
| 223 host1.len); |
| 224 } |
| 225 |
| 226 size_t GetRegistryLength( |
| 227 const GURL& gurl, |
| 228 UnknownRegistryFilter unknown_filter, |
| 229 PrivateRegistryFilter private_filter) { |
| 230 const url_parse::Component host = |
| 231 gurl.parsed_for_possibly_invalid_spec().host; |
| 232 if (host.len <= 0) |
| 233 return std::string::npos; |
| 234 if (gurl.HostIsIPAddress()) |
| 235 return 0; |
| 236 return GetRegistryLengthImpl( |
| 237 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), |
| 238 unknown_filter, |
| 239 private_filter); |
| 240 } |
| 241 |
| 242 size_t GetRegistryLength( |
| 243 const std::string& host, |
| 244 UnknownRegistryFilter unknown_filter, |
| 245 PrivateRegistryFilter private_filter) { |
| 246 url_canon::CanonHostInfo host_info; |
| 247 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
| 248 if (canon_host.empty()) |
| 249 return std::string::npos; |
| 250 if (host_info.IsIPAddress()) |
| 251 return 0; |
| 252 return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); |
| 253 } |
| 254 |
| 255 void SetFindDomainFunctionForTesting(FindDomainPtr function) { |
| 256 g_find_domain_function = function ? function : kDefaultFindDomainFunction; |
| 257 } |
| 258 |
| 259 } // namespace registry_controlled_domains |
254 } // namespace net | 260 } // namespace net |
OLD | NEW |