| 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 |host_piece| if any. |
| 117 base::StringPiece CanonicalizeHostForMatching(base::StringPiece host_piece) { |
| 118 if (host_piece.ends_with(".")) |
| 119 host_piece.remove_suffix(1); |
| 120 return host_piece; |
| 121 } |
| 122 |
| 116 } // namespace | 123 } // namespace |
| 117 | 124 |
| 118 // static | 125 // static |
| 119 bool URLPattern::IsValidSchemeForExtensions(const std::string& scheme) { | 126 bool URLPattern::IsValidSchemeForExtensions(const std::string& scheme) { |
| 120 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { | 127 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) { |
| 121 if (scheme == kValidSchemes[i]) | 128 if (scheme == kValidSchemes[i]) |
| 122 return true; | 129 return true; |
| 123 } | 130 } |
| 124 return false; | 131 return false; |
| 125 } | 132 } |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 | 404 |
| 398 bool URLPattern::MatchesHost(const std::string& host) const { | 405 bool URLPattern::MatchesHost(const std::string& host) const { |
| 399 std::string test(url::kHttpScheme); | 406 std::string test(url::kHttpScheme); |
| 400 test += url::kStandardSchemeSeparator; | 407 test += url::kStandardSchemeSeparator; |
| 401 test += host; | 408 test += host; |
| 402 test += "/"; | 409 test += "/"; |
| 403 return MatchesHost(GURL(test)); | 410 return MatchesHost(GURL(test)); |
| 404 } | 411 } |
| 405 | 412 |
| 406 bool URLPattern::MatchesHost(const GURL& test) const { | 413 bool URLPattern::MatchesHost(const GURL& test) const { |
| 414 const base::StringPiece test_host( |
| 415 CanonicalizeHostForMatching(test.host_piece())); |
| 416 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); |
| 417 |
| 407 // If the hosts are exactly equal, we have a match. | 418 // If the hosts are exactly equal, we have a match. |
| 408 if (test.host() == host_) | 419 if (test_host == pattern_host) |
| 409 return true; | 420 return true; |
| 410 | 421 |
| 411 // If we're matching subdomains, and we have no host in the match pattern, | 422 // 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 | 423 // that means that we're matching all hosts, which means we have a match no |
| 413 // matter what the test host is. | 424 // matter what the test host is. |
| 414 if (match_subdomains_ && host_.empty()) | 425 if (match_subdomains_ && pattern_host.empty()) |
| 415 return true; | 426 return true; |
| 416 | 427 |
| 417 // Otherwise, we can only match if our match pattern matches subdomains. | 428 // Otherwise, we can only match if our match pattern matches subdomains. |
| 418 if (!match_subdomains_) | 429 if (!match_subdomains_) |
| 419 return false; | 430 return false; |
| 420 | 431 |
| 421 // We don't do subdomain matching against IP addresses, so we can give up now | 432 // We don't do subdomain matching against IP addresses, so we can give up now |
| 422 // if the test host is an IP address. | 433 // if the test host is an IP address. |
| 423 if (test.HostIsIPAddress()) | 434 if (test.HostIsIPAddress()) |
| 424 return false; | 435 return false; |
| 425 | 436 |
| 426 // Check if the test host is a subdomain of our host. | 437 // Check if the test host is a subdomain of our host. |
| 427 if (test.host().length() <= (host_.length() + 1)) | 438 if (test_host.length() <= (pattern_host.length() + 1)) |
| 428 return false; | 439 return false; |
| 429 | 440 |
| 430 if (test.host().compare(test.host().length() - host_.length(), | 441 if (!test_host.ends_with(pattern_host)) |
| 431 host_.length(), host_) != 0) | |
| 432 return false; | 442 return false; |
| 433 | 443 |
| 434 return test.host()[test.host().length() - host_.length() - 1] == '.'; | 444 return test_host[test_host.length() - pattern_host.length() - 1] == '.'; |
| 435 } | 445 } |
| 436 | 446 |
| 437 bool URLPattern::ImpliesAllHosts() const { | 447 bool URLPattern::ImpliesAllHosts() const { |
| 438 // Check if it matches all urls or is a pattern like http://*/*. | 448 // Check if it matches all urls or is a pattern like http://*/*. |
| 439 if (match_all_urls_ || | 449 if (match_all_urls_ || |
| 440 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) { | 450 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) { |
| 441 return true; | 451 return true; |
| 442 } | 452 } |
| 443 | 453 |
| 444 // If this doesn't even match subdomains, it can't possibly imply all hosts. | 454 // 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 } | 615 } |
| 606 | 616 |
| 607 return result; | 617 return result; |
| 608 } | 618 } |
| 609 | 619 |
| 610 // static | 620 // static |
| 611 const char* URLPattern::GetParseResultString( | 621 const char* URLPattern::GetParseResultString( |
| 612 URLPattern::ParseResult parse_result) { | 622 URLPattern::ParseResult parse_result) { |
| 613 return kParseResultMessages[parse_result]; | 623 return kParseResultMessages[parse_result]; |
| 614 } | 624 } |
| OLD | NEW |