Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/url_pattern.h" | 5 #include "extensions/common/url_pattern.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 | 10 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 // called for the patterns inside URLPatternSet. In those cases, we know that | 106 // called for the patterns inside URLPatternSet. In those cases, we know that |
| 107 // the path will have only a single wildcard at the end. This makes figuring | 107 // the path will have only a single wildcard at the end. This makes figuring |
| 108 // out overlap much easier. It seems like there is probably a computer-sciency | 108 // out overlap much easier. It seems like there is probably a computer-sciency |
| 109 // way to solve the general case, but we don't need that yet. | 109 // way to solve the general case, but we don't need that yet. |
| 110 std::string StripTrailingWildcard(const std::string& path) { | 110 std::string StripTrailingWildcard(const std::string& path) { |
| 111 size_t wildcard_index = path.find('*'); | 111 size_t wildcard_index = path.find('*'); |
| 112 size_t path_last = path.size() - 1; | 112 size_t path_last = path.size() - 1; |
| 113 return wildcard_index == path_last ? path.substr(0, path_last) : path; | 113 return wildcard_index == path_last ? path.substr(0, path_last) : path; |
| 114 } | 114 } |
| 115 | 115 |
| 116 // Removes trailing dot from the host if any. |host| must outlive a call to this | |
| 117 // function. | |
| 118 base::StringPiece CanonicalizeHostForMatching(const std::string& host) { | |
| 119 base::StringPiece host_piece(host); | |
| 120 if (host_piece.ends_with(".")) | |
| 121 host_piece.remove_suffix(1); | |
| 122 return host_piece; | |
| 123 } | |
| 124 | |
| 116 } // namespace | 125 } // namespace |
| 117 | 126 |
| 118 // static | 127 // static |
| 119 bool URLPattern::IsValidSchemeForExtensions(const std::string& scheme) { | 128 bool URLPattern::IsValidSchemeForExtensions(const std::string& scheme) { |
| 120 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { | 129 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { |
| 121 if (scheme == kValidSchemes[i]) | 130 if (scheme == kValidSchemes[i]) |
| 122 return true; | 131 return true; |
| 123 } | 132 } |
| 124 return false; | 133 return false; |
| 125 } | 134 } |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 397 | 406 |
| 398 bool URLPattern::MatchesHost(const std::string& host) const { | 407 bool URLPattern::MatchesHost(const std::string& host) const { |
| 399 std::string test(url::kHttpScheme); | 408 std::string test(url::kHttpScheme); |
| 400 test += url::kStandardSchemeSeparator; | 409 test += url::kStandardSchemeSeparator; |
| 401 test += host; | 410 test += host; |
| 402 test += "/"; | 411 test += "/"; |
| 403 return MatchesHost(GURL(test)); | 412 return MatchesHost(GURL(test)); |
| 404 } | 413 } |
| 405 | 414 |
| 406 bool URLPattern::MatchesHost(const GURL& test) const { | 415 bool URLPattern::MatchesHost(const GURL& test) const { |
| 416 // Explicit std::string to outlive the call to CanonicalizeHostForMatching. | |
| 417 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
| |
| 418 const base::StringPiece test_host(CanonicalizeHostForMatching(test_host_str)); | |
| 419 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); | |
| 420 | |
| 407 // If the hosts are exactly equal, we have a match. | 421 // If the hosts are exactly equal, we have a match. |
| 408 if (test.host() == host_) | 422 if (test_host == pattern_host) |
| 409 return true; | 423 return true; |
| 410 | 424 |
| 411 // If we're matching subdomains, and we have no host in the match pattern, | 425 // If we're matching subdomains, and we have no host in the match pattern, |
| 412 // that means that we're matching all hosts, which means we have a match no | 426 // that means that we're matching all hosts, which means we have a match no |
| 413 // matter what the test host is. | 427 // matter what the test host is. |
| 414 if (match_subdomains_ && host_.empty()) | 428 if (match_subdomains_ && pattern_host.empty()) |
| 415 return true; | 429 return true; |
| 416 | 430 |
| 417 // Otherwise, we can only match if our match pattern matches subdomains. | 431 // Otherwise, we can only match if our match pattern matches subdomains. |
| 418 if (!match_subdomains_) | 432 if (!match_subdomains_) |
| 419 return false; | 433 return false; |
| 420 | 434 |
| 421 // We don't do subdomain matching against IP addresses, so we can give up now | 435 // We don't do subdomain matching against IP addresses, so we can give up now |
| 422 // if the test host is an IP address. | 436 // if the test host is an IP address. |
| 423 if (test.HostIsIPAddress()) | 437 if (test.HostIsIPAddress()) |
| 424 return false; | 438 return false; |
| 425 | 439 |
| 426 // Check if the test host is a subdomain of our host. | 440 // Check if the test host is a subdomain of our host. |
| 427 if (test.host().length() <= (host_.length() + 1)) | 441 if (test_host.length() <= (pattern_host.length() + 1)) |
| 428 return false; | 442 return false; |
| 429 | 443 |
| 430 if (test.host().compare(test.host().length() - host_.length(), | 444 if (test_host.find(pattern_host, |
|
Devlin
2016/10/31 16:13:17
simpler:
if (!test_host.ends_with(pattern_host))
| |
| 431 host_.length(), host_) != 0) | 445 test_host.length() - pattern_host.length()) == |
| 446 base::StringPiece::npos) | |
| 432 return false; | 447 return false; |
| 433 | 448 |
| 434 return test.host()[test.host().length() - host_.length() - 1] == '.'; | 449 return test_host[test_host.length() - pattern_host.length() - 1] == '.'; |
| 435 } | 450 } |
| 436 | 451 |
| 437 bool URLPattern::ImpliesAllHosts() const { | 452 bool URLPattern::ImpliesAllHosts() const { |
| 438 // Check if it matches all urls or is a pattern like http://*/*. | 453 // Check if it matches all urls or is a pattern like http://*/*. |
| 439 if (match_all_urls_ || | 454 if (match_all_urls_ || |
| 440 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) { | 455 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) { |
| 441 return true; | 456 return true; |
| 442 } | 457 } |
| 443 | 458 |
| 444 // If this doesn't even match subdomains, it can't possibly imply all hosts. | 459 // If this doesn't even match subdomains, it can't possibly imply all hosts. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 } | 620 } |
| 606 | 621 |
| 607 return result; | 622 return result; |
| 608 } | 623 } |
| 609 | 624 |
| 610 // static | 625 // static |
| 611 const char* URLPattern::GetParseResultString( | 626 const char* URLPattern::GetParseResultString( |
| 612 URLPattern::ParseResult parse_result) { | 627 URLPattern::ParseResult parse_result) { |
| 613 return kParseResultMessages[parse_result]; | 628 return kParseResultMessages[parse_result]; |
| 614 } | 629 } |
| OLD | NEW |