Index: net/base/registry_controlled_domains/registry_controlled_domain.cc |
diff --git a/net/base/registry_controlled_domains/registry_controlled_domain.cc b/net/base/registry_controlled_domains/registry_controlled_domain.cc |
index 607d735f3080be6b37354b5963064df6e78bd7fa..ece6c7927ef61388af59f98fbedea549158bdb8c 100644 |
--- a/net/base/registry_controlled_domains/registry_controlled_domain.cc |
+++ b/net/base/registry_controlled_domains/registry_controlled_domain.cc |
@@ -66,10 +66,19 @@ namespace { |
const unsigned char* g_graph = kDafsa; |
size_t g_graph_length = sizeof(kDafsa); |
+struct MappedHostComponent { |
+ size_t original_begin; |
+ size_t original_end; |
+ |
+ size_t canonical_begin; |
+ size_t canonical_end; |
+}; |
+ |
size_t GetRegistryLengthImpl(base::StringPiece host, |
UnknownRegistryFilter unknown_filter, |
PrivateRegistryFilter private_filter) { |
- DCHECK(!host.empty()); |
+ if (host.empty()) |
+ return std::string::npos; |
// Skip leading dots. |
const size_t host_check_begin = host.find_first_not_of('.'); |
@@ -189,6 +198,106 @@ base::StringPiece GetDomainAndRegistryAsStringPiece( |
return GetDomainAndRegistryImpl(host, filter); |
} |
+// Backend for PermissiveGetHostRegistryLength that handles both UTF-8 and |
+// UTF-16 input. The template type is the std::string type to use (it makes the |
+// typedefs easier than using the character type). |
+template <typename Str> |
+size_t DoPermissiveGetHostRegistryLength(base::BasicStringPiece<Str> host, |
+ UnknownRegistryFilter unknown_filter, |
+ PrivateRegistryFilter private_filter) { |
+ std::string canonical_host; // No not modify outside of canon_output. |
Peter Kasting
2016/10/22 05:04:19
Nit: No -> Do ?
|
+ canonical_host.reserve(host.size()); |
Peter Kasting
2016/10/22 05:04:19
Nit: The rest of this file, and the majority of Ch
brettw
2016/10/24 21:45:24
I've always written size() but will use length() f
|
+ url::StdStringCanonOutput canon_output(&canonical_host); |
+ |
+ std::vector<MappedHostComponent> components; |
+ |
+ size_t current = 0; |
+ while (current < host.size()) { |
Peter Kasting
2016/10/22 05:04:19
Nit: Shorter, narrows scope of |current|:
for (
brettw
2016/10/24 21:45:24
Done.
|
+ // Advance to next "." or end. |
Peter Kasting
2016/10/22 05:04:19
Nit: Technically, this comment belongs on the whil
brettw
2016/10/24 21:45:24
Done.
|
+ size_t begin = current; |
Peter Kasting
2016/10/22 05:04:19
Nit: Can be const
brettw
2016/10/24 21:45:24
I prefer not to mark random local variables const
Peter Kasting
2016/10/24 23:04:32
There's no Chromium-wide pattern, so that's fine.
|
+ while (current < host.size() && host[current] != '.') |
+ current++; |
Peter Kasting
2016/10/22 05:04:19
Nit: Shorter:
current = std::min(host.find('.
brettw
2016/10/24 21:45:24
This seems less readable to me.
Peter Kasting
2016/10/24 23:04:32
What about:
current = host.find('.', begin);
if (
brettw
2016/10/25 20:28:17
I can live with this.
|
+ |
+ MappedHostComponent mapping; |
+ mapping.original_begin = begin; |
+ mapping.original_end = current; |
+ mapping.canonical_begin = static_cast<size_t>(canon_output.length()); |
Peter Kasting
2016/10/22 05:04:19
Nit: If you save this in a temp here instead of cr
brettw
2016/10/24 21:45:24
This feels icky to me, I don't like implicitly dep
|
+ |
+ // Append the canonicalized version of this component. |
Peter Kasting
2016/10/22 05:04:19
Nit: Append -> Try to append ?
brettw
2016/10/24 21:45:24
Done.
|
+ if (!url::CanonicalizeHostSubstring( |
+ host.data(), url::Component(static_cast<int>(begin), |
+ static_cast<int>(current - begin)), |
+ &canon_output)) { |
+ // Failure to canonicalize this component, append as-is. |
Peter Kasting
2016/10/22 05:04:19
Nit: Failure -> Failed; comma -> semicolon
|
+ for (size_t i = begin; i < current; i++) |
+ canon_output.push_back(host[i]); |
Peter Kasting
2016/10/22 05:04:19
Nit: Shorter and more efficient:
canon_outp
brettw
2016/10/24 21:45:24
Actually this old code was wrong in the UTF-16 cas
|
+ } |
+ |
+ mapping.canonical_end = static_cast<size_t>(canon_output.length()); |
+ components.push_back(mapping); |
+ |
+ if (current < host.size()) { |
+ canon_output.push_back('.'); |
+ current++; // Skip over '.' in input. |
+ } |
+ } |
+ canon_output.Complete(); |
+ |
+ size_t canonical_rcd_len = |
+ GetRegistryLengthImpl(canonical_host, unknown_filter, private_filter); |
+ if (canonical_rcd_len == 0 || canonical_rcd_len == std::string::npos) |
+ return canonical_rcd_len; // Error or no registry controlled domain. |
+ |
+ // Find which host component the result started in. |
+ size_t canonical_rcd_begin = canonical_host.size() - canonical_rcd_len; |
+ for (const auto& mapping : components) { |
+ // In the common case, GetRegistryLengthImpl will identify the beginning |
+ // of a component and we can just return where that component was in the |
+ // original string. |
+ if (canonical_rcd_begin == mapping.canonical_begin) |
+ return host.size() - mapping.original_begin; |
+ |
+ if (canonical_rcd_begin < mapping.canonical_end) { |
Peter Kasting
2016/10/22 05:04:19
Nit: If you change this to this, at the top of the
brettw
2016/10/24 21:45:24
Done.
|
+ // The registry controlled domain begin was identified as being in the |
+ // middle of this dot-separated domain component in the non-canonical |
+ // input. This indicates some form of escaped dot, or a non-ASCII |
+ // character that was canonicalized to a dot. |
+ // |
+ // Brute-force search from the end by repeatedly canonicalizing longer |
+ // substrings until we get a match for the canonicalized version. This |
+ // depends on the canonicalization process not changing the order of the |
+ // characters. Punycode can change the order of characters, but it |
+ // doesn't work across dots so this is safe. |
+ |
+ // Expected canonical registry controlled domain. |
+ base::StringPiece canonical_rcd(&canonical_host[canonical_rcd_begin], |
+ canonical_rcd_len); |
+ |
+ for (int current_try = static_cast<int>(mapping.original_end) - 1; |
Peter Kasting
2016/10/22 05:04:19
Can't we binary-search this instead of linear-sear
brettw
2016/10/24 21:45:24
That doesn't work. Canonicalization can make the r
Peter Kasting
2016/10/24 23:04:32
Yeah, you're right. I thought about this some but
brettw
2016/10/25 20:28:17
Done.
|
+ current_try >= static_cast<int>(mapping.original_begin); |
Peter Kasting
2016/10/22 05:04:19
Seems like we could get by with ">" instead of ">=
Peter Kasting
2016/10/25 01:33:32
Still wondering about this.
brettw
2016/10/25 20:28:17
You're correct, but in the context of a loop count
Peter Kasting
2016/10/25 20:37:53
Maybe write '>' with a comment saying we already c
|
+ current_try--) { |
Peter Kasting
2016/10/22 05:04:19
Nit: Predecrement
brettw
2016/10/24 21:45:24
I don't understand this comment. Are you asking be
Peter Kasting
2016/10/24 23:04:32
There's no logic problem, I was suggesting using p
brettw
2016/10/25 20:28:17
I always write postfix for scalars because I think
Peter Kasting
2016/10/25 20:37:53
This surprised me so I tried to look myself:
http
|
+ std::string try_string; |
+ url::StdStringCanonOutput try_output(&try_string); |
+ |
+ if (!url::CanonicalizeHostSubstring( |
+ host.data(), |
+ url::Component( |
+ current_try, |
+ static_cast<int>(mapping.original_end) - current_try), |
+ &try_output)) |
+ continue; // Invalid substring, skip. |
+ |
+ try_output.Complete(); |
+ if (try_string == canonical_rcd) |
+ return host.size() - current_try; |
+ } |
+ } |
+ } |
+ |
+ NOTREACHED(); |
+ return canonical_rcd_len; |
+} |
+ |
} // namespace |
std::string GetDomainAndRegistry(const GURL& gurl, |
@@ -238,26 +347,59 @@ size_t GetRegistryLength( |
const GURL& gurl, |
UnknownRegistryFilter unknown_filter, |
PrivateRegistryFilter private_filter) { |
- base::StringPiece host = gurl.host_piece(); |
- if (host.empty()) |
- return std::string::npos; |
- if (gurl.HostIsIPAddress()) |
Peter Kasting
2016/10/22 05:04:19
I assume deleting this is not a behavior change be
brettw
2016/10/24 21:45:24
I did this because it can never match, and decodin
|
- return 0; |
- return GetRegistryLengthImpl(host, unknown_filter, private_filter); |
+ return GetRegistryLengthImpl(gurl.host_piece(), unknown_filter, |
+ private_filter); |
} |
-size_t GetRegistryLength(base::StringPiece host, |
- UnknownRegistryFilter unknown_filter, |
- PrivateRegistryFilter private_filter) { |
+bool HostHasRegistryControlledDomain(base::StringPiece host, |
+ UnknownRegistryFilter unknown_filter, |
+ PrivateRegistryFilter private_filter) { |
url::CanonHostInfo host_info; |
const std::string canon_host(CanonicalizeHost(host, &host_info)); |
- if (canon_host.empty()) |
- return std::string::npos; |
- if (host_info.IsIPAddress()) |
- return 0; |
+ switch (host_info.family) { |
+ case url::CanonHostInfo::IPV4: |
+ case url::CanonHostInfo::IPV6: |
+ // IP addresses don't have R.C.D.'s. |
+ return false; |
+ case url::CanonHostInfo::BROKEN: |
+ // Host is not canonicalizable. Fall back to the slower "permissive" |
+ // version. |
+ return PermissiveGetHostRegistryLength(host, unknown_filter, |
+ private_filter) > 0; |
Peter Kasting
2016/10/22 05:04:19
"> 0" doesn't seem right; we want to return false
brettw
2016/10/24 21:45:23
Doth done, thanks for catching. I added some unit
|
+ case url::CanonHostInfo::NEUTRAL: |
+ return GetRegistryLengthImpl(host, unknown_filter, private_filter) > 0; |
Peter Kasting
2016/10/22 05:04:19
Same question.
|
+ default: |
+ NOTREACHED(); |
+ return false |
Peter Kasting
2016/10/22 05:04:19
Semicolon
|
+ } |
+} |
+ |
+size_t GetCanonicalHostRegistryLength(base::StringPiece canon_host, |
+ UnknownRegistryFilter unknown_filter, |
+ PrivateRegistryFilter private_filter) { |
+#ifndef NDEBUG |
+ // Ensure passed-in host name is canonical. |
+ url::CanonHostInfo host_info; |
+ DCHECK_EQ(net::CanonicalizeHost(canon_host, &host_info), canon_host); |
+#endif |
+ |
return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); |
} |
+size_t PermissiveGetHostRegistryLength(base::StringPiece host, |
+ UnknownRegistryFilter unknown_filter, |
+ PrivateRegistryFilter private_filter) { |
+ return DoPermissiveGetHostRegistryLength<std::string>(host, unknown_filter, |
+ private_filter); |
+} |
+ |
+size_t PermissiveGetHostRegistryLength(base::StringPiece16 host, |
+ UnknownRegistryFilter unknown_filter, |
+ PrivateRegistryFilter private_filter) { |
+ return DoPermissiveGetHostRegistryLength<base::string16>(host, unknown_filter, |
+ private_filter); |
+} |
+ |
void SetFindDomainGraph() { |
g_graph = kDafsa; |
g_graph_length = sizeof(kDafsa); |