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

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: Minor changes. 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
« no previous file with comments | « url/url_util.h ('k') | url/url_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: url/url_util.cc
diff --git a/url/url_util.cc b/url/url_util.cc
index c1e997992d3569496b96e64014a79e692bc01bcf..7a0a1f833f961a4aa616dc731a5763457866882a 100644
--- a/url/url_util.cc
+++ b/url/url_util.cc
@@ -488,6 +488,43 @@ bool FindAndCompareScheme(const base::char16* str,
return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
}
+bool DomainIs(base::StringPiece canonicalized_host,
+ base::StringPiece lower_ascii_domain) {
+ if (canonicalized_host.empty() || lower_ascii_domain.empty())
+ return false;
+
+ // 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 Canonicalize(const char* spec,
int spec_len,
bool trim_path_end,
« no previous file with comments | « 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