1//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Effective-TLD Service
16 *
17 * The Initial Developer of the Original Code is
18 * Google Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *   Pamela Greene <pamg.bugs@gmail.com> (original author)
24 *   Daniel Witte <dwitte@stanford.edu>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40#include "net/base/registry_controlled_domain.h"
41
42#include "base/logging.h"
43#include "base/memory/singleton.h"
44#include "base/string_util.h"
45#include "googleurl/src/gurl.h"
46#include "googleurl/src/url_parse.h"
47#include "net/base/net_module.h"
48#include "net/base/net_util.h"
49
50#include "effective_tld_names.cc"
51
52namespace net {
53
54namespace {
55
56const int kExceptionRule = 1;
57const int kWildcardRule = 2;
58
59RegistryControlledDomainService* test_instance_;
60
61}  // namespace
62
63// static
64std::string RegistryControlledDomainService::GetDomainAndRegistry(
65    const GURL& gurl) {
66  const url_parse::Component host =
67      gurl.parsed_for_possibly_invalid_spec().host;
68  if ((host.len <= 0) || gurl.HostIsIPAddress())
69    return std::string();
70  return GetDomainAndRegistryImpl(std::string(
71      gurl.possibly_invalid_spec().data() + host.begin, host.len));
72}
73
74// static
75std::string RegistryControlledDomainService::GetDomainAndRegistry(
76    const std::string& host) {
77  url_canon::CanonHostInfo host_info;
78  const std::string canon_host(CanonicalizeHost(host, &host_info));
79  if (canon_host.empty() || host_info.IsIPAddress())
80    return std::string();
81  return GetDomainAndRegistryImpl(canon_host);
82}
83
84// static
85std::string RegistryControlledDomainService::GetDomainAndRegistry(
86    const std::wstring& host) {
87  url_canon::CanonHostInfo host_info;
88  const std::string canon_host(CanonicalizeHost(host, &host_info));
89  if (canon_host.empty() || host_info.IsIPAddress())
90    return std::string();
91  return GetDomainAndRegistryImpl(canon_host);
92}
93
94// static
95bool RegistryControlledDomainService::SameDomainOrHost(const GURL& gurl1,
96                                                       const GURL& gurl2) {
97  // See if both URLs have a known domain + registry, and those values are the
98  // same.
99  const std::string domain1(GetDomainAndRegistry(gurl1));
100  const std::string domain2(GetDomainAndRegistry(gurl2));
101  if (!domain1.empty() || !domain2.empty())
102    return domain1 == domain2;
103
104  // No domains.  See if the hosts are identical.
105  const url_parse::Component host1 =
106      gurl1.parsed_for_possibly_invalid_spec().host;
107  const url_parse::Component host2 =
108      gurl2.parsed_for_possibly_invalid_spec().host;
109  if ((host1.len <= 0) || (host1.len != host2.len))
110    return false;
111  return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin,
112                  gurl2.possibly_invalid_spec().data() + host2.begin,
113                  host1.len);
114}
115
116// static
117size_t RegistryControlledDomainService::GetRegistryLength(
118    const GURL& gurl,
119    bool allow_unknown_registries) {
120  const url_parse::Component host =
121      gurl.parsed_for_possibly_invalid_spec().host;
122  if (host.len <= 0)
123    return std::string::npos;
124  if (gurl.HostIsIPAddress())
125    return 0;
126  return GetInstance()->GetRegistryLengthImpl(
127      std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len),
128      allow_unknown_registries);
129}
130
131// static
132size_t RegistryControlledDomainService::GetRegistryLength(
133    const std::string& host,
134    bool allow_unknown_registries) {
135  url_canon::CanonHostInfo host_info;
136  const std::string canon_host(CanonicalizeHost(host, &host_info));
137  if (canon_host.empty())
138    return std::string::npos;
139  if (host_info.IsIPAddress())
140    return 0;
141  return GetInstance()->GetRegistryLengthImpl(canon_host,
142                                              allow_unknown_registries);
143}
144
145// static
146size_t RegistryControlledDomainService::GetRegistryLength(
147    const std::wstring& host,
148    bool allow_unknown_registries) {
149  url_canon::CanonHostInfo host_info;
150  const std::string canon_host(CanonicalizeHost(host, &host_info));
151  if (canon_host.empty())
152    return std::string::npos;
153  if (host_info.IsIPAddress())
154    return 0;
155  return GetInstance()->GetRegistryLengthImpl(canon_host,
156                                              allow_unknown_registries);
157}
158
159// static
160RegistryControlledDomainService* RegistryControlledDomainService::GetInstance()
161{
162  if (test_instance_)
163    return test_instance_;
164
165  return Singleton<RegistryControlledDomainService>::get();
166}
167
168RegistryControlledDomainService::RegistryControlledDomainService()
169    : find_domain_function_(Perfect_Hash::FindDomain) {
170}
171
172// static
173RegistryControlledDomainService* RegistryControlledDomainService::SetInstance(
174    RegistryControlledDomainService* instance) {
175  RegistryControlledDomainService* old_instance = test_instance_;
176  test_instance_ = instance;
177  return old_instance;
178}
179
180// static
181void RegistryControlledDomainService::UseFindDomainFunction(
182    FindDomainPtr function) {
183  RegistryControlledDomainService* instance = GetInstance();
184  instance->find_domain_function_ = function;
185}
186
187// static
188std::string RegistryControlledDomainService::GetDomainAndRegistryImpl(
189    const std::string& host) {
190  DCHECK(!host.empty());
191
192  // Find the length of the registry for this host.
193  const size_t registry_length =
194      GetInstance()->GetRegistryLengthImpl(host, true);
195  if ((registry_length == std::string::npos) || (registry_length == 0))
196    return std::string();  // No registry.
197  // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding
198  // subcomponent length.
199  DCHECK(host.length() >= 2);
200  if (registry_length > (host.length() - 2)) {
201    NOTREACHED() <<
202        "Host does not have at least one subcomponent before registry!";
203    return std::string();
204  }
205
206  // Move past the dot preceding the registry, and search for the next previous
207  // dot.  Return the host from after that dot, or the whole host when there is
208  // no dot.
209  const size_t dot = host.rfind('.', host.length() - registry_length - 2);
210  if (dot == std::string::npos)
211    return host;
212  return host.substr(dot + 1);
213}
214
215size_t RegistryControlledDomainService::GetRegistryLengthImpl(
216    const std::string& host,
217    bool allow_unknown_registries) {
218  DCHECK(!host.empty());
219
220  // Skip leading dots.
221  const size_t host_check_begin = host.find_first_not_of('.');
222  if (host_check_begin == std::string::npos)
223    return 0;  // Host is only dots.
224
225  // A single trailing dot isn't relevant in this determination, but does need
226  // to be included in the final returned length.
227  size_t host_check_len = host.length();
228  if (host[host_check_len - 1] == '.') {
229    --host_check_len;
230    DCHECK(host_check_len > 0);  // If this weren't true, the host would be ".",
231                                 // and we'd have already returned above.
232    if (host[host_check_len - 1] == '.')
233      return 0;  // Multiple trailing dots.
234  }
235
236  // Walk up the domain tree, most specific to least specific,
237  // looking for matches at each level.
238  size_t prev_start = std::string::npos;
239  size_t curr_start = host_check_begin;
240  size_t next_dot = host.find('.', curr_start);
241  if (next_dot >= host_check_len)  // Catches std::string::npos as well.
242    return 0;  // This can't have a registry + domain.
243  while (1) {
244    const char* domain_str = host.data() + curr_start;
245    int domain_length = host_check_len - curr_start;
246    const DomainRule* rule = find_domain_function_(domain_str, domain_length);
247
248    // We need to compare the string after finding a match because the
249    // no-collisions of perfect hashing only refers to items in the set.  Since
250    // we're searching for arbitrary domains, there could be collisions.
251    if (rule &&
252        base::strncasecmp(domain_str, rule->name, domain_length) == 0) {
253      // Exception rules override wildcard rules when the domain is an exact
254      // match, but wildcards take precedence when there's a subdomain.
255      if (rule->type == kWildcardRule && (prev_start != std::string::npos)) {
256        // If prev_start == host_check_begin, then the host is the registry
257        // itself, so return 0.
258        return (prev_start == host_check_begin) ?
259            0 : (host.length() - prev_start);
260      }
261
262      if (rule->type == kExceptionRule) {
263        if (next_dot == std::string::npos) {
264          // If we get here, we had an exception rule with no dots (e.g.
265          // "!foo").  This would only be valid if we had a corresponding
266          // wildcard rule, which would have to be "*".  But we explicitly
267          // disallow that case, so this kind of rule is invalid.
268          NOTREACHED() << "Invalid exception rule";
269          return 0;
270        }
271        return host.length() - next_dot - 1;
272      }
273
274      // If curr_start == host_check_begin, then the host is the registry
275      // itself, so return 0.
276      return (curr_start == host_check_begin) ?
277          0 : (host.length() - curr_start);
278    }
279
280    if (next_dot >= host_check_len)  // Catches std::string::npos as well.
281      break;
282
283    prev_start = curr_start;
284    curr_start = next_dot + 1;
285    next_dot = host.find('.', curr_start);
286  }
287
288  // No rule found in the registry.  curr_start now points to the first
289  // character of the last subcomponent of the host, so if we allow unknown
290  // registries, return the length of this subcomponent.
291  return allow_unknown_registries ? (host.length() - curr_start) : 0;
292}
293
294}  // namespace net
295