OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | |
6 // later modified by others), but almost entirely rewritten for Chrome. | |
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) | |
8 /* ***** BEGIN LICENSE BLOCK ***** | |
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
10 * | |
11 * The contents of this file are subject to the Mozilla Public License Version | |
12 * 1.1 (the "License"); you may not use this file except in compliance with | |
13 * the License. You may obtain a copy of the License at | |
14 * http://www.mozilla.org/MPL/ | |
15 * | |
16 * Software distributed under the License is distributed on an "AS IS" basis, | |
17 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
18 * for the specific language governing rights and limitations under the | |
19 * License. | |
20 * | |
21 * The Original Code is Mozilla Effective-TLD Service | |
22 * | |
23 * The Initial Developer of the Original Code is | |
24 * Google Inc. | |
25 * Portions created by the Initial Developer are Copyright (C) 2006 | |
26 * the Initial Developer. All Rights Reserved. | |
27 * | |
28 * Contributor(s): | |
29 * Pamela Greene <pamg.bugs@gmail.com> (original author) | |
30 * Daniel Witte <dwitte@stanford.edu> | |
31 * | |
32 * Alternatively, the contents of this file may be used under the terms of | |
33 * either the GNU General Public License Version 2 or later (the "GPL"), or | |
34 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
35 * in which case the provisions of the GPL or the LGPL are applicable instead | |
36 * of those above. If you wish to allow use of your version of this file only | |
37 * under the terms of either the GPL or the LGPL, and not to allow others to | |
38 * use your version of this file under the terms of the MPL, indicate your | |
39 * decision by deleting the provisions above and replace them with the notice | |
40 * and other provisions required by the GPL or the LGPL. If you do not delete | |
41 * the provisions above, a recipient may use your version of this file under | |
42 * the terms of any one of the MPL, the GPL or the LGPL. | |
43 * | |
44 * ***** END LICENSE BLOCK ***** */ | |
45 | |
46 #include "net/base/registry_controlled_domain.h" | |
47 | |
48 #include "base/logging.h" | |
49 #include "base/string_util.h" | |
50 #include "base/utf_string_conversions.h" | |
51 #include "googleurl/src/gurl.h" | |
52 #include "googleurl/src/url_parse.h" | |
53 #include "net/base/net_module.h" | |
54 #include "net/base/net_util.h" | |
55 | |
56 #include "effective_tld_names.cc" | |
57 | |
58 namespace net { | |
59 | |
60 namespace { | |
61 | |
62 const int kExceptionRule = 1; | |
63 const int kWildcardRule = 2; | |
64 | |
65 } // namespace | |
66 | |
67 RegistryControlledDomainService::FindDomainPtr | |
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, | |
132 bool allow_unknown_registries) { | |
133 url_canon::CanonHostInfo host_info; | |
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()); | |
179 | |
180 // Skip leading dots. | |
181 const size_t host_check_begin = host.find_first_not_of('.'); | |
182 if (host_check_begin == std::string::npos) | |
183 return 0; // Host is only dots. | |
184 | |
185 // A single trailing dot isn't relevant in this determination, but does need | |
186 // to be included in the final returned length. | |
187 size_t host_check_len = host.length(); | |
188 if (host[host_check_len - 1] == '.') { | |
189 --host_check_len; | |
190 DCHECK(host_check_len > 0); // If this weren't true, the host would be ".", | |
191 // and we'd have already returned above. | |
192 if (host[host_check_len - 1] == '.') | |
193 return 0; // Multiple trailing dots. | |
194 } | |
195 | |
196 // Walk up the domain tree, most specific to least specific, | |
197 // looking for matches at each level. | |
198 size_t prev_start = std::string::npos; | |
199 size_t curr_start = host_check_begin; | |
200 size_t next_dot = host.find('.', curr_start); | |
201 if (next_dot >= host_check_len) // Catches std::string::npos as well. | |
202 return 0; // This can't have a registry + domain. | |
203 while (1) { | |
204 const char* domain_str = host.data() + curr_start; | |
205 int domain_length = host_check_len - curr_start; | |
206 const DomainRule* rule = find_domain_function_(domain_str, domain_length); | |
207 | |
208 // 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 | |
210 // we're searching for arbitrary domains, there could be collisions. | |
211 if (rule && | |
212 base::strncasecmp(domain_str, rule->name, domain_length) == 0) { | |
213 // Exception rules override wildcard rules when the domain is an exact | |
214 // match, but wildcards take precedence when there's a subdomain. | |
215 if (rule->type == kWildcardRule && (prev_start != std::string::npos)) { | |
216 // If prev_start == host_check_begin, then the host is the registry | |
217 // itself, so return 0. | |
218 return (prev_start == host_check_begin) ? | |
219 0 : (host.length() - prev_start); | |
220 } | |
221 | |
222 if (rule->type == kExceptionRule) { | |
223 if (next_dot == std::string::npos) { | |
224 // If we get here, we had an exception rule with no dots (e.g. | |
225 // "!foo"). This would only be valid if we had a corresponding | |
226 // wildcard rule, which would have to be "*". But we explicitly | |
227 // disallow that case, so this kind of rule is invalid. | |
228 NOTREACHED() << "Invalid exception rule"; | |
229 return 0; | |
230 } | |
231 return host.length() - next_dot - 1; | |
232 } | |
233 | |
234 // If curr_start == host_check_begin, then the host is the registry | |
235 // itself, so return 0. | |
236 return (curr_start == host_check_begin) ? | |
237 0 : (host.length() - curr_start); | |
238 } | |
239 | |
240 if (next_dot >= host_check_len) // Catches std::string::npos as well. | |
241 break; | |
242 | |
243 prev_start = curr_start; | |
244 curr_start = next_dot + 1; | |
245 next_dot = host.find('.', curr_start); | |
246 } | |
247 | |
248 // 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 | |
250 // registries, return the length of this subcomponent. | |
251 return allow_unknown_registries ? (host.length() - curr_start) : 0; | |
252 } | |
253 | |
254 } // namespace net | |
OLD | NEW |