OLD | NEW |
---|---|
1 //* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | 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 | |
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) | |
2 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
4 * | 10 * |
5 * The contents of this file are subject to the Mozilla Public License Version | 11 * 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 | 12 * 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 | 13 * the License. You may obtain a copy of the License at |
8 * http://www.mozilla.org/MPL/ | 14 * http://www.mozilla.org/MPL/ |
9 * | 15 * |
10 * Software distributed under the License is distributed on an "AS IS" basis, | 16 * 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 | 17 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 | 56 |
51 #include "effective_tld_names.cc" | 57 #include "effective_tld_names.cc" |
52 | 58 |
53 namespace net { | 59 namespace net { |
54 | 60 |
55 namespace { | 61 namespace { |
56 | 62 |
57 const int kExceptionRule = 1; | 63 const int kExceptionRule = 1; |
58 const int kWildcardRule = 2; | 64 const int kWildcardRule = 2; |
59 | 65 |
60 RegistryControlledDomainService* test_instance_; | 66 } // namespace |
wtc
2011/10/27 22:44:09
Is test_instance_ not used?
Ryan Sleevi
2011/10/27 22:48:04
Not in the refactor, no. Previously it was used by
| |
61 | 67 |
62 } // namespace | 68 RegistryControlledDomainService::FindDomainPtr |
69 RegistryControlledDomainService::find_domain_function_ = | |
70 Perfect_Hash::FindDomain; | |
63 | 71 |
64 // static | 72 // static |
65 std::string RegistryControlledDomainService::GetDomainAndRegistry( | 73 std::string RegistryControlledDomainService::GetDomainAndRegistry( |
66 const GURL& gurl) { | 74 const GURL& gurl) { |
67 const url_parse::Component host = | 75 const url_parse::Component host = |
68 gurl.parsed_for_possibly_invalid_spec().host; | 76 gurl.parsed_for_possibly_invalid_spec().host; |
69 if ((host.len <= 0) || gurl.HostIsIPAddress()) | 77 if ((host.len <= 0) || gurl.HostIsIPAddress()) |
70 return std::string(); | 78 return std::string(); |
71 return GetDomainAndRegistryImpl(std::string( | 79 return GetDomainAndRegistryImpl(std::string( |
72 gurl.possibly_invalid_spec().data() + host.begin, host.len)); | 80 gurl.possibly_invalid_spec().data() + host.begin, host.len)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 // static | 115 // static |
108 size_t RegistryControlledDomainService::GetRegistryLength( | 116 size_t RegistryControlledDomainService::GetRegistryLength( |
109 const GURL& gurl, | 117 const GURL& gurl, |
110 bool allow_unknown_registries) { | 118 bool allow_unknown_registries) { |
111 const url_parse::Component host = | 119 const url_parse::Component host = |
112 gurl.parsed_for_possibly_invalid_spec().host; | 120 gurl.parsed_for_possibly_invalid_spec().host; |
113 if (host.len <= 0) | 121 if (host.len <= 0) |
114 return std::string::npos; | 122 return std::string::npos; |
115 if (gurl.HostIsIPAddress()) | 123 if (gurl.HostIsIPAddress()) |
116 return 0; | 124 return 0; |
117 return GetInstance()->GetRegistryLengthImpl( | 125 return GetRegistryLengthImpl( |
118 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), | 126 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), |
119 allow_unknown_registries); | 127 allow_unknown_registries); |
120 } | 128 } |
121 | 129 |
122 // static | 130 // static |
123 size_t RegistryControlledDomainService::GetRegistryLength( | 131 size_t RegistryControlledDomainService::GetRegistryLength( |
124 const std::string& host, | 132 const std::string& host, |
125 bool allow_unknown_registries) { | 133 bool allow_unknown_registries) { |
126 url_canon::CanonHostInfo host_info; | 134 url_canon::CanonHostInfo host_info; |
127 const std::string canon_host(CanonicalizeHost(host, &host_info)); | 135 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
128 if (canon_host.empty()) | 136 if (canon_host.empty()) |
129 return std::string::npos; | 137 return std::string::npos; |
130 if (host_info.IsIPAddress()) | 138 if (host_info.IsIPAddress()) |
131 return 0; | 139 return 0; |
132 return GetInstance()->GetRegistryLengthImpl(canon_host, | 140 return GetRegistryLengthImpl(canon_host, allow_unknown_registries); |
133 allow_unknown_registries); | |
134 } | |
135 | |
136 // static | |
137 RegistryControlledDomainService* RegistryControlledDomainService::GetInstance() | |
138 { | |
139 if (test_instance_) | |
140 return test_instance_; | |
141 | |
142 return Singleton<RegistryControlledDomainService>::get(); | |
143 } | |
144 | |
145 RegistryControlledDomainService::RegistryControlledDomainService() | |
146 : find_domain_function_(Perfect_Hash::FindDomain) { | |
147 } | |
148 | |
149 // static | |
150 RegistryControlledDomainService* RegistryControlledDomainService::SetInstance( | |
151 RegistryControlledDomainService* instance) { | |
152 RegistryControlledDomainService* old_instance = test_instance_; | |
153 test_instance_ = instance; | |
154 return old_instance; | |
155 } | 141 } |
156 | 142 |
157 // static | 143 // static |
158 void RegistryControlledDomainService::UseFindDomainFunction( | 144 void RegistryControlledDomainService::UseFindDomainFunction( |
159 FindDomainPtr function) { | 145 FindDomainPtr function) { |
160 RegistryControlledDomainService* instance = GetInstance(); | 146 if (function) { |
161 instance->find_domain_function_ = function; | 147 find_domain_function_ = function; |
148 } else { | |
149 find_domain_function_ = Perfect_Hash::FindDomain; | |
150 } | |
162 } | 151 } |
163 | 152 |
164 // static | 153 // static |
165 std::string RegistryControlledDomainService::GetDomainAndRegistryImpl( | 154 std::string RegistryControlledDomainService::GetDomainAndRegistryImpl( |
166 const std::string& host) { | 155 const std::string& host) { |
167 DCHECK(!host.empty()); | 156 DCHECK(!host.empty()); |
168 | 157 |
169 // Find the length of the registry for this host. | 158 // Find the length of the registry for this host. |
170 const size_t registry_length = | 159 const size_t registry_length = GetRegistryLengthImpl(host, true); |
171 GetInstance()->GetRegistryLengthImpl(host, true); | |
172 if ((registry_length == std::string::npos) || (registry_length == 0)) | 160 if ((registry_length == std::string::npos) || (registry_length == 0)) |
173 return std::string(); // No registry. | 161 return std::string(); // No registry. |
174 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding | 162 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding |
175 // subcomponent length. | 163 // subcomponent length. |
176 DCHECK(host.length() >= 2); | 164 DCHECK(host.length() >= 2); |
177 if (registry_length > (host.length() - 2)) { | 165 if (registry_length > (host.length() - 2)) { |
178 NOTREACHED() << | 166 NOTREACHED() << |
179 "Host does not have at least one subcomponent before registry!"; | 167 "Host does not have at least one subcomponent before registry!"; |
180 return std::string(); | 168 return std::string(); |
181 } | 169 } |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 next_dot = host.find('.', curr_start); | 250 next_dot = host.find('.', curr_start); |
263 } | 251 } |
264 | 252 |
265 // No rule found in the registry. curr_start now points to the first | 253 // No rule found in the registry. curr_start now points to the first |
266 // character of the last subcomponent of the host, so if we allow unknown | 254 // character of the last subcomponent of the host, so if we allow unknown |
267 // registries, return the length of this subcomponent. | 255 // registries, return the length of this subcomponent. |
268 return allow_unknown_registries ? (host.length() - curr_start) : 0; | 256 return allow_unknown_registries ? (host.length() - curr_start) : 0; |
269 } | 257 } |
270 | 258 |
271 } // namespace net | 259 } // namespace net |
OLD | NEW |