| 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 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 int result = 0; | 134 int result = 0; |
| 135 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i) | 135 for (size_t i = 0; i < arraysize(kValidSchemeMasks); ++i) |
| 136 result |= kValidSchemeMasks[i]; | 136 result |= kValidSchemeMasks[i]; |
| 137 return result; | 137 return result; |
| 138 } | 138 } |
| 139 | 139 |
| 140 URLPattern::URLPattern() | 140 URLPattern::URLPattern() |
| 141 : valid_schemes_(SCHEME_NONE), | 141 : valid_schemes_(SCHEME_NONE), |
| 142 match_all_urls_(false), | 142 match_all_urls_(false), |
| 143 match_subdomains_(false), | 143 match_subdomains_(false), |
| 144 match_effective_tld_(true), | |
| 145 port_("*") {} | 144 port_("*") {} |
| 146 | 145 |
| 147 URLPattern::URLPattern(int valid_schemes) | 146 URLPattern::URLPattern(int valid_schemes) |
| 148 : valid_schemes_(valid_schemes), | 147 : valid_schemes_(valid_schemes), |
| 149 match_all_urls_(false), | 148 match_all_urls_(false), |
| 150 match_subdomains_(false), | 149 match_subdomains_(false), |
| 151 match_effective_tld_(true), | |
| 152 port_("*") {} | 150 port_("*") {} |
| 153 | 151 |
| 154 URLPattern::URLPattern(int valid_schemes, base::StringPiece pattern) | 152 URLPattern::URLPattern(int valid_schemes, base::StringPiece pattern) |
| 155 // Strict error checking is used, because this constructor is only | 153 // Strict error checking is used, because this constructor is only |
| 156 // appropriate when we know |pattern| is valid. | 154 // appropriate when we know |pattern| is valid. |
| 157 : valid_schemes_(valid_schemes), | 155 : valid_schemes_(valid_schemes), |
| 158 match_all_urls_(false), | 156 match_all_urls_(false), |
| 159 match_subdomains_(false), | 157 match_subdomains_(false), |
| 160 match_effective_tld_(true), | |
| 161 port_("*") { | 158 port_("*") { |
| 162 ParseResult result = Parse(pattern); | 159 ParseResult result = Parse(pattern); |
| 163 if (PARSE_SUCCESS != result) | 160 if (PARSE_SUCCESS != result) |
| 164 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result; | 161 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result; |
| 165 } | 162 } |
| 166 | 163 |
| 167 URLPattern::URLPattern(const URLPattern& other) = default; | 164 URLPattern::URLPattern(const URLPattern& other) = default; |
| 168 | 165 |
| 169 URLPattern::~URLPattern() { | 166 URLPattern::~URLPattern() { |
| 170 } | 167 } |
| 171 | 168 |
| 172 bool URLPattern::operator<(const URLPattern& other) const { | 169 bool URLPattern::operator<(const URLPattern& other) const { |
| 173 return GetAsString() < other.GetAsString(); | 170 return GetAsString() < other.GetAsString(); |
| 174 } | 171 } |
| 175 | 172 |
| 176 bool URLPattern::operator>(const URLPattern& other) const { | 173 bool URLPattern::operator>(const URLPattern& other) const { |
| 177 return GetAsString() > other.GetAsString(); | 174 return GetAsString() > other.GetAsString(); |
| 178 } | 175 } |
| 179 | 176 |
| 180 bool URLPattern::operator==(const URLPattern& other) const { | 177 bool URLPattern::operator==(const URLPattern& other) const { |
| 181 return GetAsString() == other.GetAsString(); | 178 return GetAsString() == other.GetAsString(); |
| 182 } | 179 } |
| 183 | 180 |
| 184 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { | 181 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { |
| 185 return out << '"' << url_pattern.GetAsString() << '"'; | 182 return out << '"' << url_pattern.GetAsString() << '"'; |
| 186 } | 183 } |
| 187 | 184 |
| 188 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern) { | 185 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern) { |
| 189 return Parse(pattern, DENY_WILDCARD_FOR_EFFECTIVE_TLD); | |
| 190 } | |
| 191 | |
| 192 URLPattern::ParseResult URLPattern::Parse(base::StringPiece pattern, | |
| 193 ParseOptions parse_options) { | |
| 194 spec_.clear(); | 186 spec_.clear(); |
| 195 SetMatchAllURLs(false); | 187 SetMatchAllURLs(false); |
| 196 SetMatchSubdomains(false); | 188 SetMatchSubdomains(false); |
| 197 SetMatchEffectiveTld(true); | |
| 198 SetPort("*"); | 189 SetPort("*"); |
| 199 | 190 |
| 200 // Special case pattern to match every valid URL. | 191 // Special case pattern to match every valid URL. |
| 201 if (pattern == kAllUrlsPattern) { | 192 if (pattern == kAllUrlsPattern) { |
| 202 SetMatchAllURLs(true); | 193 SetMatchAllURLs(true); |
| 203 return PARSE_SUCCESS; | 194 return PARSE_SUCCESS; |
| 204 } | 195 } |
| 205 | 196 |
| 206 // Parse out the scheme. | 197 // Parse out the scheme. |
| 207 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); | 198 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 // Could be empty if the host only consists of whitespace characters. | 260 // Could be empty if the host only consists of whitespace characters. |
| 270 if (host_components.empty() || | 261 if (host_components.empty() || |
| 271 (host_components.size() == 1 && host_components[0].empty())) | 262 (host_components.size() == 1 && host_components[0].empty())) |
| 272 return PARSE_ERROR_EMPTY_HOST; | 263 return PARSE_ERROR_EMPTY_HOST; |
| 273 | 264 |
| 274 if (host_components[0] == "*") { | 265 if (host_components[0] == "*") { |
| 275 match_subdomains_ = true; | 266 match_subdomains_ = true; |
| 276 host_components.erase(host_components.begin(), | 267 host_components.erase(host_components.begin(), |
| 277 host_components.begin() + 1); | 268 host_components.begin() + 1); |
| 278 } | 269 } |
| 279 | |
| 280 // If explicitly allowed, the last component can optionally be '*' to | |
| 281 // match all effective TLDs. | |
| 282 if (parse_options == ALLOW_WILDCARD_FOR_EFFECTIVE_TLD && | |
| 283 host_components.size() > 1 && host_components.back() == "*") { | |
| 284 match_effective_tld_ = false; | |
| 285 host_components.pop_back(); | |
| 286 } | |
| 287 host_ = base::JoinString(host_components, "."); | 270 host_ = base::JoinString(host_components, "."); |
| 288 | 271 |
| 289 path_start_pos = host_end_pos; | 272 path_start_pos = host_end_pos; |
| 290 } | 273 } |
| 291 | 274 |
| 292 SetPath(pattern.substr(path_start_pos)); | 275 SetPath(pattern.substr(path_start_pos)); |
| 293 | 276 |
| 294 size_t port_pos = host_.find(':'); | 277 size_t port_pos = host_.find(':'); |
| 295 if (port_pos != std::string::npos) { | 278 if (port_pos != std::string::npos) { |
| 296 if (!SetPort(host_.substr(port_pos + 1))) | 279 if (!SetPort(host_.substr(port_pos + 1))) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 host_.clear(); | 314 host_.clear(); |
| 332 SetPath("/*"); | 315 SetPath("/*"); |
| 333 } | 316 } |
| 334 } | 317 } |
| 335 | 318 |
| 336 void URLPattern::SetMatchSubdomains(bool val) { | 319 void URLPattern::SetMatchSubdomains(bool val) { |
| 337 spec_.clear(); | 320 spec_.clear(); |
| 338 match_subdomains_ = val; | 321 match_subdomains_ = val; |
| 339 } | 322 } |
| 340 | 323 |
| 341 void URLPattern::SetMatchEffectiveTld(bool val) { | |
| 342 spec_.clear(); | |
| 343 match_effective_tld_ = val; | |
| 344 } | |
| 345 | |
| 346 bool URLPattern::SetScheme(base::StringPiece scheme) { | 324 bool URLPattern::SetScheme(base::StringPiece scheme) { |
| 347 spec_.clear(); | 325 spec_.clear(); |
| 348 scheme.CopyToString(&scheme_); | 326 scheme.CopyToString(&scheme_); |
| 349 if (scheme_ == "*") { | 327 if (scheme_ == "*") { |
| 350 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); | 328 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); |
| 351 } else if (!IsValidScheme(scheme_)) { | 329 } else if (!IsValidScheme(scheme_)) { |
| 352 return false; | 330 return false; |
| 353 } | 331 } |
| 354 return true; | 332 return true; |
| 355 } | 333 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 } | 414 } |
| 437 | 415 |
| 438 bool URLPattern::MatchesHost(base::StringPiece host) const { | 416 bool URLPattern::MatchesHost(base::StringPiece host) const { |
| 439 // TODO(devlin): This is a bit sad. Parsing urls is expensive. | 417 // TODO(devlin): This is a bit sad. Parsing urls is expensive. |
| 440 return MatchesHost( | 418 return MatchesHost( |
| 441 GURL(base::StringPrintf("%s%s%s/", url::kHttpScheme, | 419 GURL(base::StringPrintf("%s%s%s/", url::kHttpScheme, |
| 442 url::kStandardSchemeSeparator, host.data()))); | 420 url::kStandardSchemeSeparator, host.data()))); |
| 443 } | 421 } |
| 444 | 422 |
| 445 bool URLPattern::MatchesHost(const GURL& test) const { | 423 bool URLPattern::MatchesHost(const GURL& test) const { |
| 446 base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece())); | 424 const base::StringPiece test_host( |
| 425 CanonicalizeHostForMatching(test.host_piece())); |
| 447 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); | 426 const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); |
| 448 | 427 |
| 449 // If we don't care about matching the effective TLD, remove it. | |
| 450 if (!match_effective_tld_) { | |
| 451 int reg_length = net::registry_controlled_domains::GetRegistryLength( | |
| 452 test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, | |
| 453 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | |
| 454 if (reg_length > 0) { | |
| 455 test_host = test_host.substr(0, test_host.size() - reg_length - 1); | |
| 456 } | |
| 457 } | |
| 458 | |
| 459 // If the hosts are exactly equal, we have a match. | 428 // If the hosts are exactly equal, we have a match. |
| 460 if (test_host == pattern_host) | 429 if (test_host == pattern_host) |
| 461 return true; | 430 return true; |
| 462 | 431 |
| 463 // If we're matching subdomains, and we have no host in the match pattern, | 432 // If we're matching subdomains, and we have no host in the match pattern, |
| 464 // that means that we're matching all hosts, which means we have a match no | 433 // that means that we're matching all hosts, which means we have a match no |
| 465 // matter what the test host is. | 434 // matter what the test host is. |
| 466 if (match_subdomains_ && pattern_host.empty()) | 435 if (match_subdomains_ && pattern_host.empty()) |
| 467 return true; | 436 return true; |
| 468 | 437 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 if (scheme_ != url::kFileScheme && standard_scheme) { | 522 if (scheme_ != url::kFileScheme && standard_scheme) { |
| 554 if (match_subdomains_) { | 523 if (match_subdomains_) { |
| 555 spec += "*"; | 524 spec += "*"; |
| 556 if (!host_.empty()) | 525 if (!host_.empty()) |
| 557 spec += "."; | 526 spec += "."; |
| 558 } | 527 } |
| 559 | 528 |
| 560 if (!host_.empty()) | 529 if (!host_.empty()) |
| 561 spec += host_; | 530 spec += host_; |
| 562 | 531 |
| 563 if (!match_effective_tld_) { | |
| 564 if (!host_.empty()) | |
| 565 spec += "."; | |
| 566 spec += "*"; | |
| 567 } | |
| 568 | |
| 569 if (port_ != "*") { | 532 if (port_ != "*") { |
| 570 spec += ":"; | 533 spec += ":"; |
| 571 spec += port_; | 534 spec += port_; |
| 572 } | 535 } |
| 573 } | 536 } |
| 574 | 537 |
| 575 if (!path_.empty()) | 538 if (!path_.empty()) |
| 576 spec += path_; | 539 spec += path_; |
| 577 | 540 |
| 578 spec_ = std::move(spec); | 541 spec_ = std::move(spec); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 } | 630 } |
| 668 | 631 |
| 669 return result; | 632 return result; |
| 670 } | 633 } |
| 671 | 634 |
| 672 // static | 635 // static |
| 673 const char* URLPattern::GetParseResultString( | 636 const char* URLPattern::GetParseResultString( |
| 674 URLPattern::ParseResult parse_result) { | 637 URLPattern::ParseResult parse_result) { |
| 675 return kParseResultMessages[parse_result]; | 638 return kParseResultMessages[parse_result]; |
| 676 } | 639 } |
| OLD | NEW |