Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Unified Diff: url/url_util.cc

Issue 2287483002: Provide the equivalent of GURL::DomainIs for url::Origin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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) {
« url/url_util.h ('K') | « url/url_util.h ('k') | url/url_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698