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

Unified Diff: extensions/common/url_pattern.cc

Issue 2455373002: Add implicit trailing dot domain matching support to URLPattern. (Closed)
Patch Set: Use StringPiece to speedup MatchesHost. Created 4 years, 1 month 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 | « no previous file | extensions/common/url_pattern_unittest.cc » ('j') | extensions/common/url_pattern_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/common/url_pattern.cc
diff --git a/extensions/common/url_pattern.cc b/extensions/common/url_pattern.cc
index 91ba65bec02583f4666ea4a68e76c96fe3ef66e6..0b5546299344e53e34a71020bb0d5b6982b746c5 100644
--- a/extensions/common/url_pattern.cc
+++ b/extensions/common/url_pattern.cc
@@ -113,6 +113,15 @@ std::string StripTrailingWildcard(const std::string& path) {
return wildcard_index == path_last ? path.substr(0, path_last) : path;
}
+// Removes trailing dot from the host if any. |host| must outlive a call to this
+// function.
+base::StringPiece CanonicalizeHostForMatching(const std::string& host) {
+ base::StringPiece host_piece(host);
+ if (host_piece.ends_with("."))
+ host_piece.remove_suffix(1);
+ return host_piece;
+}
+
} // namespace
// static
@@ -404,14 +413,19 @@ bool URLPattern::MatchesHost(const std::string& host) const {
}
bool URLPattern::MatchesHost(const GURL& test) const {
+ // Explicit std::string to outlive the call to CanonicalizeHostForMatching.
+ const std::string& test_host_str = test.host();
Devlin 2016/10/31 16:13:17 GURL::host() actually returns a std::string (rathe
Devlin 2016/10/31 16:58:47 Actually, even better yet, we have a GURL::host_pi
+ const base::StringPiece test_host(CanonicalizeHostForMatching(test_host_str));
+ const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_));
+
// If the hosts are exactly equal, we have a match.
- if (test.host() == host_)
+ if (test_host == pattern_host)
return true;
// If we're matching subdomains, and we have no host in the match pattern,
// that means that we're matching all hosts, which means we have a match no
// matter what the test host is.
- if (match_subdomains_ && host_.empty())
+ if (match_subdomains_ && pattern_host.empty())
return true;
// Otherwise, we can only match if our match pattern matches subdomains.
@@ -424,14 +438,15 @@ bool URLPattern::MatchesHost(const GURL& test) const {
return false;
// Check if the test host is a subdomain of our host.
- if (test.host().length() <= (host_.length() + 1))
+ if (test_host.length() <= (pattern_host.length() + 1))
return false;
- if (test.host().compare(test.host().length() - host_.length(),
- host_.length(), host_) != 0)
+ if (test_host.find(pattern_host,
Devlin 2016/10/31 16:13:17 simpler: if (!test_host.ends_with(pattern_host))
+ test_host.length() - pattern_host.length()) ==
+ base::StringPiece::npos)
return false;
- return test.host()[test.host().length() - host_.length() - 1] == '.';
+ return test_host[test_host.length() - pattern_host.length() - 1] == '.';
}
bool URLPattern::ImpliesAllHosts() const {
« no previous file with comments | « no previous file | extensions/common/url_pattern_unittest.cc » ('j') | extensions/common/url_pattern_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698