| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 next_dot = host.find('.', curr_start); | 141 next_dot = host.find('.', curr_start); |
| 142 } | 142 } |
| 143 | 143 |
| 144 // No rule found in the registry. curr_start now points to the first | 144 // No rule found in the registry. curr_start now points to the first |
| 145 // character of the last subcomponent of the host, so if we allow unknown | 145 // character of the last subcomponent of the host, so if we allow unknown |
| 146 // registries, return the length of this subcomponent. | 146 // registries, return the length of this subcomponent. |
| 147 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ? | 147 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ? |
| 148 (host.length() - curr_start) : 0; | 148 (host.length() - curr_start) : 0; |
| 149 } | 149 } |
| 150 | 150 |
| 151 std::string GetDomainAndRegistryImpl(base::StringPiece host, | 151 base::StringPiece GetDomainAndRegistryImpl( |
| 152 PrivateRegistryFilter private_filter) { | 152 base::StringPiece host, |
| 153 PrivateRegistryFilter private_filter) { |
| 153 DCHECK(!host.empty()); | 154 DCHECK(!host.empty()); |
| 154 | 155 |
| 155 // Find the length of the registry for this host. | 156 // Find the length of the registry for this host. |
| 156 const size_t registry_length = | 157 const size_t registry_length = |
| 157 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); | 158 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); |
| 158 if ((registry_length == std::string::npos) || (registry_length == 0)) | 159 if ((registry_length == std::string::npos) || (registry_length == 0)) |
| 159 return std::string(); // No registry. | 160 return base::StringPiece(); // No registry. |
| 160 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding | 161 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding |
| 161 // subcomponent length. | 162 // subcomponent length. |
| 162 DCHECK(host.length() >= 2); | 163 DCHECK(host.length() >= 2); |
| 163 if (registry_length > (host.length() - 2)) { | 164 if (registry_length > (host.length() - 2)) { |
| 164 NOTREACHED() << | 165 NOTREACHED() << |
| 165 "Host does not have at least one subcomponent before registry!"; | 166 "Host does not have at least one subcomponent before registry!"; |
| 166 return std::string(); | 167 return base::StringPiece(); |
| 167 } | 168 } |
| 168 | 169 |
| 169 // Move past the dot preceding the registry, and search for the next previous | 170 // Move past the dot preceding the registry, and search for the next previous |
| 170 // dot. Return the host from after that dot, or the whole host when there is | 171 // dot. Return the host from after that dot, or the whole host when there is |
| 171 // no dot. | 172 // no dot. |
| 172 const size_t dot = host.rfind('.', host.length() - registry_length - 2); | 173 const size_t dot = host.rfind('.', host.length() - registry_length - 2); |
| 173 if (dot == std::string::npos) | 174 if (dot == std::string::npos) |
| 174 return host.as_string(); | 175 return host; |
| 175 return host.substr(dot + 1).as_string(); | 176 return host.substr(dot + 1); |
| 177 } |
| 178 |
| 179 // Same as GetDomainAndRegistry, but returns the domain and registry as a |
| 180 // StringPiece that references the underlying string of the passed-in |gurl|. |
| 181 // TODO(pkalinnikov): Eliminate this helper by exposing StringPiece as the |
| 182 // interface type for all the APIs. |
| 183 base::StringPiece GetDomainAndRegistryAsStringPiece( |
| 184 const GURL& gurl, |
| 185 PrivateRegistryFilter filter) { |
| 186 base::StringPiece host = gurl.host_piece(); |
| 187 if (host.empty() || gurl.HostIsIPAddress()) |
| 188 return base::StringPiece(); |
| 189 return GetDomainAndRegistryImpl(host, filter); |
| 176 } | 190 } |
| 177 | 191 |
| 178 } // namespace | 192 } // namespace |
| 179 | 193 |
| 180 std::string GetDomainAndRegistry( | 194 std::string GetDomainAndRegistry(const GURL& gurl, |
| 181 const GURL& gurl, | 195 PrivateRegistryFilter filter) { |
| 182 PrivateRegistryFilter filter) { | 196 return GetDomainAndRegistryAsStringPiece(gurl, filter).as_string(); |
| 183 base::StringPiece host = gurl.host_piece(); | |
| 184 if (host.empty() || gurl.HostIsIPAddress()) | |
| 185 return std::string(); | |
| 186 return GetDomainAndRegistryImpl(host, filter); | |
| 187 } | 197 } |
| 188 | 198 |
| 189 std::string GetDomainAndRegistry(base::StringPiece host, | 199 std::string GetDomainAndRegistry(base::StringPiece host, |
| 190 PrivateRegistryFilter filter) { | 200 PrivateRegistryFilter filter) { |
| 191 url::CanonHostInfo host_info; | 201 url::CanonHostInfo host_info; |
| 192 const std::string canon_host(CanonicalizeHost(host, &host_info)); | 202 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
| 193 if (canon_host.empty() || host_info.IsIPAddress()) | 203 if (canon_host.empty() || host_info.IsIPAddress()) |
| 194 return std::string(); | 204 return std::string(); |
| 195 return GetDomainAndRegistryImpl(canon_host, filter); | 205 return GetDomainAndRegistryImpl(canon_host, filter).as_string(); |
| 196 } | 206 } |
| 197 | 207 |
| 198 bool SameDomainOrHost( | 208 bool SameDomainOrHost( |
| 199 const GURL& gurl1, | 209 const GURL& gurl1, |
| 200 const GURL& gurl2, | 210 const GURL& gurl2, |
| 201 PrivateRegistryFilter filter) { | 211 PrivateRegistryFilter filter) { |
| 202 // See if both URLs have a known domain + registry, and those values are the | 212 // See if both URLs have a known domain + registry, and those values are the |
| 203 // same. | 213 // same. |
| 204 const std::string domain1(GetDomainAndRegistry(gurl1, filter)); | 214 const base::StringPiece domain1 = |
| 205 const std::string domain2(GetDomainAndRegistry(gurl2, filter)); | 215 GetDomainAndRegistryAsStringPiece(gurl1, filter); |
| 216 const base::StringPiece domain2 = |
| 217 GetDomainAndRegistryAsStringPiece(gurl2, filter); |
| 206 if (!domain1.empty() || !domain2.empty()) | 218 if (!domain1.empty() || !domain2.empty()) |
| 207 return domain1 == domain2; | 219 return domain1 == domain2; |
| 208 | 220 |
| 209 // No domains. See if the hosts are identical. | 221 // No domains. See if the hosts are identical. |
| 210 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host; | 222 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host; |
| 211 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host; | 223 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host; |
| 212 if ((host1.len <= 0) || (host1.len != host2.len)) | 224 if ((host1.len <= 0) || (host1.len != host2.len)) |
| 213 return false; | 225 return false; |
| 214 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, | 226 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, |
| 215 gurl2.possibly_invalid_spec().data() + host2.begin, | 227 gurl2.possibly_invalid_spec().data() + host2.begin, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 | 266 |
| 255 void SetFindDomainGraph(const unsigned char* domains, size_t length) { | 267 void SetFindDomainGraph(const unsigned char* domains, size_t length) { |
| 256 CHECK(domains); | 268 CHECK(domains); |
| 257 CHECK_NE(length, 0u); | 269 CHECK_NE(length, 0u); |
| 258 g_graph = domains; | 270 g_graph = domains; |
| 259 g_graph_length = length; | 271 g_graph_length = length; |
| 260 } | 272 } |
| 261 | 273 |
| 262 } // namespace registry_controlled_domains | 274 } // namespace registry_controlled_domains |
| 263 } // namespace net | 275 } // namespace net |
| OLD | NEW |