diff options
Diffstat (limited to 'components')
-rw-r--r-- | components/url_matcher/url_matcher.cc | 27 | ||||
-rw-r--r-- | components/url_matcher/url_matcher.h | 6 | ||||
-rw-r--r-- | components/url_matcher/url_matcher_unittest.cc | 31 |
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)); |