Chromium Code Reviews| Index: url/url_util.cc |
| diff --git a/url/url_util.cc b/url/url_util.cc |
| index c1e997992d3569496b96e64014a79e692bc01bcf..9322d9606a5e16b2b54a5ac6c40024966129b8e5 100644 |
| --- a/url/url_util.cc |
| +++ b/url/url_util.cc |
| @@ -616,6 +616,43 @@ void EncodeURIComponent(const char* input, int length, CanonOutput* output) { |
| } |
| } |
| +bool DomainIs(base::StringPiece canonicalized_host, |
| + base::StringPiece lower_ascii_domain) { |
| + DCHECK(!canonicalized_host.empty()); |
|
engedy
2016/08/26 16:48:14
I think it would be safer if this shared stuff did
pkalinnikov
2016/08/29 09:53:00
Done.
|
| + DCHECK(!lower_ascii_domain.empty()); |
| + |
| + // If the host name ends with a dot but the input domain doesn't, then we |
| + // ignore the dot in the host name. |
| + size_t host_len = canonicalized_host.length(); |
| + if (canonicalized_host.back() == '.' && lower_ascii_domain.back() != '.') |
| + --host_len; |
| + |
| + if (host_len < lower_ascii_domain.length()) |
| + return false; |
| + |
| + // |host_first_pos| is the start of the compared part of the host name, not |
| + // start of the whole host name. |
| + const char* host_first_pos = |
| + canonicalized_host.data() + host_len - lower_ascii_domain.length(); |
| + |
| + if (!base::LowerCaseEqualsASCII( |
| + base::StringPiece(host_first_pos, lower_ascii_domain.length()), |
| + lower_ascii_domain)) { |
| + return false; |
| + } |
| + |
| + // Make sure there aren't extra characters in host before the compared part; |
| + // if the host name is longer than the input domain name, then the character |
| + // immediately before the compared part should be a dot. For example, |
| + // www.google.com has domain "google.com", but www.iamnotgoogle.com does not. |
| + if (lower_ascii_domain[0] != '.' && host_len > lower_ascii_domain.length() && |
| + *(host_first_pos - 1) != '.') { |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| bool CompareSchemeComponent(const char* spec, |
| const Component& component, |
| const char* compare_to) { |