Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / url_pattern.cc
blob502c459d43353ac94c3443d9a7d6d724e53074ea
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
3 // found in the LICENSE file.
5 #include "extensions/common/url_pattern.h"
7 #include <ostream>
9 #include "base/strings/pattern.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "content/public/common/url_constants.h"
16 #include "extensions/common/constants.h"
17 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
18 #include "url/gurl.h"
19 #include "url/url_util.h"
21 const char URLPattern::kAllUrlsPattern[] = "<all_urls>";
23 namespace {
25 // TODO(aa): What about more obscure schemes like data: and javascript: ?
26 // Note: keep this array in sync with kValidSchemeMasks.
27 const char* kValidSchemes[] = {
28 url::kHttpScheme,
29 url::kHttpsScheme,
30 url::kFileScheme,
31 url::kFtpScheme,
32 content::kChromeUIScheme,
33 extensions::kExtensionScheme,
34 url::kFileSystemScheme,
37 const int kValidSchemeMasks[] = {
38 URLPattern::SCHEME_HTTP,
39 URLPattern::SCHEME_HTTPS,
40 URLPattern::SCHEME_FILE,
41 URLPattern::SCHEME_FTP,
42 URLPattern::SCHEME_CHROMEUI,
43 URLPattern::SCHEME_EXTENSION,
44 URLPattern::SCHEME_FILESYSTEM,
47 static_assert(arraysize(kValidSchemes) == arraysize(kValidSchemeMasks),
48 "must keep these arrays in sync");
50 const char kParseSuccess[] = "Success.";
51 const char kParseErrorMissingSchemeSeparator[] = "Missing scheme separator.";
52 const char kParseErrorInvalidScheme[] = "Invalid scheme.";
53 const char kParseErrorWrongSchemeType[] = "Wrong scheme type.";
54 const char kParseErrorEmptyHost[] = "Host can not be empty.";
55 const char kParseErrorInvalidHostWildcard[] = "Invalid host wildcard.";
56 const char kParseErrorEmptyPath[] = "Empty path.";
57 const char kParseErrorInvalidPort[] = "Invalid port.";
58 const char kParseErrorInvalidHost[] = "Invalid host.";
60 // Message explaining each URLPattern::ParseResult.
61 const char* const kParseResultMessages[] = {
62 kParseSuccess,
63 kParseErrorMissingSchemeSeparator,
64 kParseErrorInvalidScheme,
65 kParseErrorWrongSchemeType,
66 kParseErrorEmptyHost,
67 kParseErrorInvalidHostWildcard,
68 kParseErrorEmptyPath,
69 kParseErrorInvalidPort,
70 kParseErrorInvalidHost,
73 static_assert(URLPattern::NUM_PARSE_RESULTS == arraysize(kParseResultMessages),
74 "must add message for each parse result");
76 const char kPathSeparator[] = "/";
78 bool IsStandardScheme(const std::string& scheme) {
79 // "*" gets the same treatment as a standard scheme.
80 if (scheme == "*")
81 return true;
83 return url::IsStandard(scheme.c_str(),
84 url::Component(0, static_cast<int>(scheme.length())));
87 bool IsValidPortForScheme(const std::string& scheme, const std::string& port) {
88 if (port == "*")
89 return true;
91 // Only accept non-wildcard ports if the scheme uses ports.
92 if (url::DefaultPortForScheme(scheme.c_str(), scheme.length()) ==
93 url::PORT_UNSPECIFIED) {
94 return false;
97 int parsed_port = url::PORT_UNSPECIFIED;
98 if (!base::StringToInt(port, &parsed_port))
99 return false;
100 return (parsed_port >= 0) && (parsed_port < 65536);
103 // Returns |path| with the trailing wildcard stripped if one existed.
105 // The functions that rely on this (OverlapsWith and Contains) are only
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
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.
110 std::string StripTrailingWildcard(const std::string& path) {
111 size_t wildcard_index = path.find('*');
112 size_t path_last = path.size() - 1;
113 DCHECK(wildcard_index == std::string::npos || wildcard_index == path_last);
114 return wildcard_index == path_last ? path.substr(0, path_last) : path;
117 } // namespace
119 // static
120 bool URLPattern::IsValidSchemeForExtensions(const std::string& scheme) {
121 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) {
122 if (scheme == kValidSchemes[i])
123 return true;
125 return false;
128 URLPattern::URLPattern()
129 : valid_schemes_(SCHEME_NONE),
130 match_all_urls_(false),
131 match_subdomains_(false),
132 port_("*") {}
134 URLPattern::URLPattern(int valid_schemes)
135 : valid_schemes_(valid_schemes),
136 match_all_urls_(false),
137 match_subdomains_(false),
138 port_("*") {}
140 URLPattern::URLPattern(int valid_schemes, const std::string& pattern)
141 // Strict error checking is used, because this constructor is only
142 // appropriate when we know |pattern| is valid.
143 : valid_schemes_(valid_schemes),
144 match_all_urls_(false),
145 match_subdomains_(false),
146 port_("*") {
147 ParseResult result = Parse(pattern);
148 if (PARSE_SUCCESS != result)
149 NOTREACHED() << "URLPattern invalid: " << pattern << " result " << result;
152 URLPattern::~URLPattern() {
155 bool URLPattern::operator<(const URLPattern& other) const {
156 return GetAsString() < other.GetAsString();
159 bool URLPattern::operator>(const URLPattern& other) const {
160 return GetAsString() > other.GetAsString();
163 bool URLPattern::operator==(const URLPattern& other) const {
164 return GetAsString() == other.GetAsString();
167 std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) {
168 return out << '"' << url_pattern.GetAsString() << '"';
171 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) {
172 spec_.clear();
173 SetMatchAllURLs(false);
174 SetMatchSubdomains(false);
175 SetPort("*");
177 // Special case pattern to match every valid URL.
178 if (pattern == kAllUrlsPattern) {
179 SetMatchAllURLs(true);
180 return PARSE_SUCCESS;
183 // Parse out the scheme.
184 size_t scheme_end_pos = pattern.find(url::kStandardSchemeSeparator);
185 bool has_standard_scheme_separator = true;
187 // Some urls also use ':' alone as the scheme separator.
188 if (scheme_end_pos == std::string::npos) {
189 scheme_end_pos = pattern.find(':');
190 has_standard_scheme_separator = false;
193 if (scheme_end_pos == std::string::npos)
194 return PARSE_ERROR_MISSING_SCHEME_SEPARATOR;
196 if (!SetScheme(pattern.substr(0, scheme_end_pos)))
197 return PARSE_ERROR_INVALID_SCHEME;
199 bool standard_scheme = IsStandardScheme(scheme_);
200 if (standard_scheme != has_standard_scheme_separator)
201 return PARSE_ERROR_WRONG_SCHEME_SEPARATOR;
203 // Advance past the scheme separator.
204 scheme_end_pos +=
205 (standard_scheme ? strlen(url::kStandardSchemeSeparator) : 1);
206 if (scheme_end_pos >= pattern.size())
207 return PARSE_ERROR_EMPTY_HOST;
209 // Parse out the host and path.
210 size_t host_start_pos = scheme_end_pos;
211 size_t path_start_pos = 0;
213 if (!standard_scheme) {
214 path_start_pos = host_start_pos;
215 } else if (scheme_ == url::kFileScheme) {
216 size_t host_end_pos = pattern.find(kPathSeparator, host_start_pos);
217 if (host_end_pos == std::string::npos) {
218 // Allow hostname omission.
219 // e.g. file://* is interpreted as file:///*,
220 // file://foo* is interpreted as file:///foo*.
221 path_start_pos = host_start_pos - 1;
222 } else {
223 // Ignore hostname if scheme is file://.
224 // e.g. file://localhost/foo is equal to file:///foo.
225 path_start_pos = host_end_pos;
227 } else {
228 size_t host_end_pos = pattern.find(kPathSeparator, host_start_pos);
230 // Host is required.
231 if (host_start_pos == host_end_pos)
232 return PARSE_ERROR_EMPTY_HOST;
234 if (host_end_pos == std::string::npos)
235 return PARSE_ERROR_EMPTY_PATH;
237 host_ = pattern.substr(host_start_pos, host_end_pos - host_start_pos);
239 // The first component can optionally be '*' to match all subdomains.
240 std::vector<std::string> host_components;
241 base::SplitString(host_, '.', &host_components);
243 // Could be empty if the host only consists of whitespace characters.
244 if (host_components.empty())
245 return PARSE_ERROR_EMPTY_HOST;
247 if (host_components[0] == "*") {
248 match_subdomains_ = true;
249 host_components.erase(host_components.begin(),
250 host_components.begin() + 1);
252 host_ = base::JoinString(host_components, ".");
254 path_start_pos = host_end_pos;
257 SetPath(pattern.substr(path_start_pos));
259 size_t port_pos = host_.find(':');
260 if (port_pos != std::string::npos) {
261 if (!SetPort(host_.substr(port_pos + 1)))
262 return PARSE_ERROR_INVALID_PORT;
263 host_ = host_.substr(0, port_pos);
266 // No other '*' can occur in the host, though. This isn't necessary, but is
267 // done as a convenience to developers who might otherwise be confused and
268 // think '*' works as a glob in the host.
269 if (host_.find('*') != std::string::npos)
270 return PARSE_ERROR_INVALID_HOST_WILDCARD;
272 // Null characters are not allowed in hosts.
273 if (host_.find('\0') != std::string::npos)
274 return PARSE_ERROR_INVALID_HOST;
276 return PARSE_SUCCESS;
279 void URLPattern::SetValidSchemes(int valid_schemes) {
280 spec_.clear();
281 valid_schemes_ = valid_schemes;
284 void URLPattern::SetHost(const std::string& host) {
285 spec_.clear();
286 host_ = host;
289 void URLPattern::SetMatchAllURLs(bool val) {
290 spec_.clear();
291 match_all_urls_ = val;
293 if (val) {
294 match_subdomains_ = true;
295 scheme_ = "*";
296 host_.clear();
297 SetPath("/*");
301 void URLPattern::SetMatchSubdomains(bool val) {
302 spec_.clear();
303 match_subdomains_ = val;
306 bool URLPattern::SetScheme(const std::string& scheme) {
307 spec_.clear();
308 scheme_ = scheme;
309 if (scheme_ == "*") {
310 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
311 } else if (!IsValidScheme(scheme_)) {
312 return false;
314 return true;
317 bool URLPattern::IsValidScheme(const std::string& scheme) const {
318 if (valid_schemes_ == SCHEME_ALL)
319 return true;
321 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) {
322 if (scheme == kValidSchemes[i] && (valid_schemes_ & kValidSchemeMasks[i]))
323 return true;
326 return false;
329 void URLPattern::SetPath(const std::string& path) {
330 spec_.clear();
331 path_ = path;
332 path_escaped_ = path_;
333 base::ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\");
334 base::ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?");
337 bool URLPattern::SetPort(const std::string& port) {
338 spec_.clear();
339 if (IsValidPortForScheme(scheme_, port)) {
340 port_ = port;
341 return true;
343 return false;
346 bool URLPattern::MatchesURL(const GURL& test) const {
347 const GURL* test_url = &test;
348 bool has_inner_url = test.inner_url() != NULL;
350 if (has_inner_url) {
351 if (!test.SchemeIsFileSystem())
352 return false; // The only nested URLs we handle are filesystem URLs.
353 test_url = test.inner_url();
356 if (!MatchesScheme(test_url->scheme()))
357 return false;
359 if (match_all_urls_)
360 return true;
362 std::string path_for_request = test.PathForRequest();
363 if (has_inner_url)
364 path_for_request = test_url->path() + path_for_request;
366 return MatchesSecurityOriginHelper(*test_url) &&
367 MatchesPath(path_for_request);
370 bool URLPattern::MatchesSecurityOrigin(const GURL& test) const {
371 const GURL* test_url = &test;
372 bool has_inner_url = test.inner_url() != NULL;
374 if (has_inner_url) {
375 if (!test.SchemeIsFileSystem())
376 return false; // The only nested URLs we handle are filesystem URLs.
377 test_url = test.inner_url();
380 if (!MatchesScheme(test_url->scheme()))
381 return false;
383 if (match_all_urls_)
384 return true;
386 return MatchesSecurityOriginHelper(*test_url);
389 bool URLPattern::MatchesScheme(const std::string& test) const {
390 if (!IsValidScheme(test))
391 return false;
393 return scheme_ == "*" || test == scheme_;
396 bool URLPattern::MatchesHost(const std::string& host) const {
397 std::string test(url::kHttpScheme);
398 test += url::kStandardSchemeSeparator;
399 test += host;
400 test += "/";
401 return MatchesHost(GURL(test));
404 bool URLPattern::MatchesHost(const GURL& test) const {
405 // If the hosts are exactly equal, we have a match.
406 if (test.host() == host_)
407 return true;
409 // If we're matching subdomains, and we have no host in the match pattern,
410 // that means that we're matching all hosts, which means we have a match no
411 // matter what the test host is.
412 if (match_subdomains_ && host_.empty())
413 return true;
415 // Otherwise, we can only match if our match pattern matches subdomains.
416 if (!match_subdomains_)
417 return false;
419 // We don't do subdomain matching against IP addresses, so we can give up now
420 // if the test host is an IP address.
421 if (test.HostIsIPAddress())
422 return false;
424 // Check if the test host is a subdomain of our host.
425 if (test.host().length() <= (host_.length() + 1))
426 return false;
428 if (test.host().compare(test.host().length() - host_.length(),
429 host_.length(), host_) != 0)
430 return false;
432 return test.host()[test.host().length() - host_.length() - 1] == '.';
435 bool URLPattern::ImpliesAllHosts() const {
436 // Check if it matches all urls or is a pattern like http://*/*.
437 if (match_all_urls_ ||
438 (match_subdomains_ && host_.empty() && port_ == "*" && path_ == "/*")) {
439 return true;
442 // If this doesn't even match subdomains, it can't possibly imply all hosts.
443 if (!match_subdomains_)
444 return false;
446 // If |host_| is a recognized TLD, this will be 0. We don't include private
447 // TLDs, so that, e.g., *.appspot.com does not imply all hosts.
448 size_t registry_length = net::registry_controlled_domains::GetRegistryLength(
449 host_,
450 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
451 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
452 // If there was more than just a TLD in the host (e.g., *.foobar.com), it
453 // doesn't imply all hosts.
454 if (registry_length > 0)
455 return false;
457 // At this point the host could either be just a TLD ("com") or some unknown
458 // TLD-like string ("notatld"). To disambiguate between them construct a
459 // fake URL, and check the registry. This returns 0 if the TLD is
460 // unrecognized, or the length of the recognized TLD.
461 registry_length = net::registry_controlled_domains::GetRegistryLength(
462 base::StringPrintf("foo.%s", host_.c_str()),
463 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
464 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
465 // If we recognized this TLD, then this is a pattern like *.com, and it
466 // should imply all hosts. Otherwise, this doesn't imply all hosts.
467 return registry_length > 0;
470 bool URLPattern::MatchesSingleOrigin() const {
471 // Strictly speaking, the port is part of the origin, but in URLPattern it
472 // defaults to *. It's not very interesting anyway, so leave it out.
473 return !ImpliesAllHosts() && scheme_ != "*" && !match_subdomains_;
476 bool URLPattern::MatchesPath(const std::string& test) const {
477 // Make the behaviour of OverlapsWith consistent with MatchesURL, which is
478 // need to match hosted apps on e.g. 'google.com' also run on 'google.com/'.
479 if (test + "/*" == path_escaped_)
480 return true;
482 return base::MatchPattern(test, path_escaped_);
485 const std::string& URLPattern::GetAsString() const {
486 if (!spec_.empty())
487 return spec_;
489 if (match_all_urls_) {
490 spec_ = kAllUrlsPattern;
491 return spec_;
494 bool standard_scheme = IsStandardScheme(scheme_);
496 std::string spec = scheme_ +
497 (standard_scheme ? url::kStandardSchemeSeparator : ":");
499 if (scheme_ != url::kFileScheme && standard_scheme) {
500 if (match_subdomains_) {
501 spec += "*";
502 if (!host_.empty())
503 spec += ".";
506 if (!host_.empty())
507 spec += host_;
509 if (port_ != "*") {
510 spec += ":";
511 spec += port_;
515 if (!path_.empty())
516 spec += path_;
518 spec_ = spec;
519 return spec_;
522 bool URLPattern::OverlapsWith(const URLPattern& other) const {
523 if (match_all_urls() || other.match_all_urls())
524 return true;
525 return (MatchesAnyScheme(other.GetExplicitSchemes()) ||
526 other.MatchesAnyScheme(GetExplicitSchemes()))
527 && (MatchesHost(other.host()) || other.MatchesHost(host()))
528 && (MatchesPortPattern(other.port()) || other.MatchesPortPattern(port()))
529 && (MatchesPath(StripTrailingWildcard(other.path())) ||
530 other.MatchesPath(StripTrailingWildcard(path())));
533 bool URLPattern::Contains(const URLPattern& other) const {
534 if (match_all_urls())
535 return true;
536 return MatchesAllSchemes(other.GetExplicitSchemes())
537 && MatchesHost(other.host())
538 && MatchesPortPattern(other.port())
539 && MatchesPath(StripTrailingWildcard(other.path()));
542 bool URLPattern::MatchesAnyScheme(
543 const std::vector<std::string>& schemes) const {
544 for (std::vector<std::string>::const_iterator i = schemes.begin();
545 i != schemes.end(); ++i) {
546 if (MatchesScheme(*i))
547 return true;
550 return false;
553 bool URLPattern::MatchesAllSchemes(
554 const std::vector<std::string>& schemes) const {
555 for (std::vector<std::string>::const_iterator i = schemes.begin();
556 i != schemes.end(); ++i) {
557 if (!MatchesScheme(*i))
558 return false;
561 return true;
564 bool URLPattern::MatchesSecurityOriginHelper(const GURL& test) const {
565 // Ignore hostname if scheme is file://.
566 if (scheme_ != url::kFileScheme && !MatchesHost(test))
567 return false;
569 if (!MatchesPortPattern(base::IntToString(test.EffectiveIntPort())))
570 return false;
572 return true;
575 bool URLPattern::MatchesPortPattern(const std::string& port) const {
576 return port_ == "*" || port_ == port;
579 std::vector<std::string> URLPattern::GetExplicitSchemes() const {
580 std::vector<std::string> result;
582 if (scheme_ != "*" && !match_all_urls_ && IsValidScheme(scheme_)) {
583 result.push_back(scheme_);
584 return result;
587 for (size_t i = 0; i < arraysize(kValidSchemes); ++i) {
588 if (MatchesScheme(kValidSchemes[i])) {
589 result.push_back(kValidSchemes[i]);
593 return result;
596 std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
597 std::vector<std::string> explicit_schemes = GetExplicitSchemes();
598 std::vector<URLPattern> result;
600 for (std::vector<std::string>::const_iterator i = explicit_schemes.begin();
601 i != explicit_schemes.end(); ++i) {
602 URLPattern temp = *this;
603 temp.SetScheme(*i);
604 temp.SetMatchAllURLs(false);
605 result.push_back(temp);
608 return result;
611 // static
612 const char* URLPattern::GetParseResultString(
613 URLPattern::ParseResult parse_result) {
614 return kParseResultMessages[parse_result];