summaryrefslogtreecommitdiffstats
path: root/components/url_matcher
diff options
context:
space:
mode:
authormnissler <mnissler@chromium.org>2015-05-29 06:57:41 -0700
committerCommit bot <commit-bot@chromium.org>2015-05-29 13:58:09 +0000
commitd9cdcd8702111079c27787a2d49b48470bc10ae7 (patch)
tree21b8612c0bf4f8b5c805e4a652db400a18d2080b /components/url_matcher
parent673eda698a02bbc74c07b790e52eb5185a693bde (diff)
downloadchromium_src-d9cdcd8702111079c27787a2d49b48470bc10ae7.zip
chromium_src-d9cdcd8702111079c27787a2d49b48470bc10ae7.tar.gz
chromium_src-d9cdcd8702111079c27787a2d49b48470bc10ae7.tar.bz2
url_matcher: Treat FQDN hosts as if they were relative.
This changes URLMatcher to not make a difference between fully-qualified and relative URLs. Patterns that specify fully-qualified host names now also match the corresponding relative host name in URLs and fully-qualified host names in URLs now match patterns that use relative host names. BUG=chromium:493142 TEST=Added unit and browser test coverage. Review URL: https://codereview.chromium.org/1167483002 Cr-Commit-Position: refs/heads/master@{#331963}
Diffstat (limited to 'components/url_matcher')
-rw-r--r--components/url_matcher/url_matcher.cc27
-rw-r--r--components/url_matcher/url_matcher.h6
-rw-r--r--components/url_matcher/url_matcher_unittest.cc31
3 files changed, 54 insertions, 10 deletions
diff --git a/components/url_matcher/url_matcher.cc b/components/url_matcher/url_matcher.cc
index 3798f7e..5cdf4a42 100644
--- a/components/url_matcher/url_matcher.cc
+++ b/components/url_matcher/url_matcher.cc
@@ -277,13 +277,13 @@ std::string URLMatcherConditionFactory::CanonicalizeURLForComponentSearches(
URLMatcherCondition URLMatcherConditionFactory::CreateHostPrefixCondition(
const std::string& prefix) {
return CreateCondition(URLMatcherCondition::HOST_PREFIX,
- kBeginningOfURL + CanonicalizeHostname(prefix));
+ kBeginningOfURL + CanonicalizeHostPrefix(prefix));
}
URLMatcherCondition URLMatcherConditionFactory::CreateHostSuffixCondition(
const std::string& suffix) {
return CreateCondition(URLMatcherCondition::HOST_SUFFIX,
- suffix + kEndOfDomain);
+ CanonicalizeHostSuffix(suffix) + kEndOfDomain);
}
URLMatcherCondition URLMatcherConditionFactory::CreateHostContainsCondition(
@@ -365,7 +365,7 @@ URLMatcherCondition
const std::string& host_suffix,
const std::string& path_prefix) {
return CreateCondition(URLMatcherCondition::HOST_SUFFIX_PATH_PREFIX,
- host_suffix + kEndOfDomain + path_prefix);
+ CanonicalizeHostSuffix(host_suffix) + kEndOfDomain + path_prefix);
}
URLMatcherCondition
@@ -521,12 +521,25 @@ URLMatcherCondition URLMatcherConditionFactory::CreateCondition(
}
}
+std::string URLMatcherConditionFactory::CanonicalizeHostSuffix(
+ const std::string& suffix) const {
+ if (!suffix.empty() && suffix[suffix.size() - 1] == '.')
+ return suffix;
+ else
+ return suffix + ".";
+}
+
+std::string URLMatcherConditionFactory::CanonicalizeHostPrefix(
+ const std::string& prefix) const {
+ if (!prefix.empty() && prefix[0] == '.')
+ return prefix;
+ else
+ return "." + prefix;
+}
+
std::string URLMatcherConditionFactory::CanonicalizeHostname(
const std::string& hostname) const {
- if (!hostname.empty() && hostname[0] == '.')
- return hostname;
- else
- return "." + hostname;
+ return CanonicalizeHostPrefix(CanonicalizeHostSuffix(hostname));
}
// This function prepares the query string by replacing query separator with a
diff --git a/components/url_matcher/url_matcher.h b/components/url_matcher/url_matcher.h
index d8fe032..a0b13d9 100644
--- a/components/url_matcher/url_matcher.h
+++ b/components/url_matcher/url_matcher.h
@@ -190,7 +190,11 @@ class URL_MATCHER_EXPORT URLMatcherConditionFactory {
URLMatcherCondition CreateCondition(URLMatcherCondition::Criterion criterion,
const std::string& pattern);
- // Prepends a "." to the hostname if it does not start with one.
+ // Prepends a "." to the prefix if it does not start with one.
+ std::string CanonicalizeHostPrefix(const std::string& prefix) const;
+ // Appends a "." to the hostname if it does not start with one.
+ std::string CanonicalizeHostSuffix(const std::string& suffix) const;
+ // Adds "." to either side of the hostname if not present yet.
std::string CanonicalizeHostname(const std::string& hostname) const;
// Convert the query string to canonical form suitable for key token search.
diff --git a/components/url_matcher/url_matcher_unittest.cc b/components/url_matcher/url_matcher_unittest.cc
index b69364c..328c633 100644
--- a/components/url_matcher/url_matcher_unittest.cc
+++ b/components/url_matcher/url_matcher_unittest.cc
@@ -241,10 +241,14 @@ TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
}
TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
+ URLMatcherConditionFactory factory;
GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
"&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
- URLMatcherConditionFactory factory;
std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
+ GURL gurl2("https://www.google.com.:1234/webhp?sourceid=chrome-instant"
+ "&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab"
+ "&q=chrome%20is%20awesome");
+ std::string url2 = factory.CanonicalizeURLForComponentSearches(gurl2);
// Test host component.
EXPECT_TRUE(Matches(factory.CreateHostPrefixCondition(std::string()), url));
@@ -259,12 +263,18 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
EXPECT_FALSE(Matches(factory.CreateHostPrefixCondition("webhp"), url));
EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url));
+ EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(std::string()), url2));
EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url));
+ EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition("com"), url2));
EXPECT_TRUE(Matches(factory.CreateHostSuffixCondition(".com"), url));
EXPECT_TRUE(
Matches(factory.CreateHostSuffixCondition("www.google.com"), url));
EXPECT_TRUE(
Matches(factory.CreateHostSuffixCondition(".www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(factory.CreateHostSuffixCondition(".www.google.com"), url2));
+ EXPECT_TRUE(
+ Matches(factory.CreateHostSuffixCondition(".www.google.com."), url));
EXPECT_FALSE(Matches(factory.CreateHostSuffixCondition("www"), url));
EXPECT_FALSE(
Matches(factory.CreateHostSuffixCondition("www.google.com/"), url));
@@ -274,9 +284,14 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
EXPECT_FALSE(Matches(factory.CreateHostEqualsCondition("www"), url));
EXPECT_TRUE(
Matches(factory.CreateHostEqualsCondition("www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(factory.CreateHostEqualsCondition("www.google.com"), url2));
EXPECT_FALSE(
Matches(factory.CreateHostEqualsCondition("www.google.com/"), url));
-
+ EXPECT_TRUE(
+ Matches(factory.CreateHostEqualsCondition(".www.google.com."), url));
+ EXPECT_TRUE(
+ Matches(factory.CreateHostEqualsCondition(".www.google.com."), url2));
// Test path component.
EXPECT_TRUE(Matches(factory.CreatePathPrefixCondition(std::string()), url));
@@ -330,6 +345,12 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
// Test adjacent components
EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
"google.com", "/webhp"), url));
+ EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
+ "google.com", "/webhp"), url2));
+ EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
+ "google.com.", "/webhp"), url));
+ EXPECT_TRUE(Matches(factory.CreateHostSuffixPathPrefixCondition(
+ "google.com.", "/webhp"), url2));
EXPECT_TRUE(Matches(
factory.CreateHostSuffixPathPrefixCondition(std::string(), "/webhp"),
url));
@@ -341,6 +362,12 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
"www.google.com", "/webhp"), url));
+ EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
+ "www.google.com", "/webhp"), url2));
+ EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
+ ".www.google.com.", "/webhp"), url));
+ EXPECT_TRUE(Matches(factory.CreateHostEqualsPathPrefixCondition(
+ ".www.google.com.", "/webhp"), url2));
EXPECT_FALSE(Matches(
factory.CreateHostEqualsPathPrefixCondition(std::string(), "/webhp"),
url));