OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/extensions/url_pattern.h" | 5 #include "chrome/common/extensions/url_pattern.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 91 |
92 int parsed_port = url_parse::PORT_UNSPECIFIED; | 92 int parsed_port = url_parse::PORT_UNSPECIFIED; |
93 if (!base::StringToInt(port, &parsed_port)) | 93 if (!base::StringToInt(port, &parsed_port)) |
94 return false; | 94 return false; |
95 return (parsed_port >= 0) && (parsed_port < 65536); | 95 return (parsed_port >= 0) && (parsed_port < 65536); |
96 } | 96 } |
97 | 97 |
98 } // namespace | 98 } // namespace |
99 | 99 |
100 URLPattern::URLPattern() | 100 URLPattern::URLPattern() |
101 : valid_schemes_(SCHEME_NONE), | 101 : parse_option_(ERROR_ON_PORTS), |
| 102 valid_schemes_(SCHEME_NONE), |
102 match_all_urls_(false), | 103 match_all_urls_(false), |
103 match_subdomains_(false), | 104 match_subdomains_(false), |
104 port_("*") {} | 105 port_("*") {} |
105 | 106 |
106 URLPattern::URLPattern(int valid_schemes) | 107 URLPattern::URLPattern(URLPattern::ParseOption parse_option, int valid_schemes) |
107 : valid_schemes_(valid_schemes), | 108 : parse_option_(parse_option), |
| 109 valid_schemes_(valid_schemes), |
108 match_all_urls_(false), | 110 match_all_urls_(false), |
109 match_subdomains_(false), | 111 match_subdomains_(false), |
110 port_("*") {} | 112 port_("*") {} |
111 | 113 |
112 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) | 114 URLPattern::URLPattern(int valid_schemes, const std::string& pattern) |
113 : valid_schemes_(valid_schemes), | 115 // Strict error checking is used, because this constructor is only |
| 116 // appropriate when we know |pattern| is valid. |
| 117 : parse_option_(ERROR_ON_PORTS), |
| 118 valid_schemes_(valid_schemes), |
114 match_all_urls_(false), | 119 match_all_urls_(false), |
115 match_subdomains_(false), | 120 match_subdomains_(false), |
116 port_("*") { | 121 port_("*") { |
117 | 122 if (PARSE_SUCCESS != Parse(pattern)) |
118 // Strict error checking is used, because this constructor is only | |
119 // appropriate when we know |pattern| is valid. | |
120 if (PARSE_SUCCESS != Parse(pattern, ERROR_ON_PORTS)) | |
121 NOTREACHED() << "URLPattern is invalid: " << pattern; | 123 NOTREACHED() << "URLPattern is invalid: " << pattern; |
122 } | 124 } |
123 | 125 |
124 URLPattern::~URLPattern() { | 126 URLPattern::~URLPattern() { |
125 } | 127 } |
126 | 128 |
127 bool URLPattern::operator<(const URLPattern& other) const { | 129 bool URLPattern::operator<(const URLPattern& other) const { |
128 return GetAsString() < other.GetAsString(); | 130 return GetAsString() < other.GetAsString(); |
129 } | 131 } |
130 | 132 |
131 bool URLPattern::operator==(const URLPattern& other) const { | 133 bool URLPattern::operator==(const URLPattern& other) const { |
132 return GetAsString() == other.GetAsString(); | 134 return GetAsString() == other.GetAsString(); |
133 } | 135 } |
134 | 136 |
135 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, | 137 URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { |
136 ParseOption strictness) { | |
137 spec_.clear(); | 138 spec_.clear(); |
138 | 139 |
139 // Special case pattern to match every valid URL. | 140 // Special case pattern to match every valid URL. |
140 if (pattern == kAllUrlsPattern) { | 141 if (pattern == kAllUrlsPattern) { |
141 SetMatchAllURLs(true); | 142 SetMatchAllURLs(true); |
142 return PARSE_SUCCESS; | 143 return PARSE_SUCCESS; |
143 } | 144 } |
144 | 145 |
145 // Parse out the scheme. | 146 // Parse out the scheme. |
146 size_t scheme_end_pos = pattern.find(chrome::kStandardSchemeSeparator); | 147 size_t scheme_end_pos = pattern.find(chrome::kStandardSchemeSeparator); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 } | 209 } |
209 host_ = JoinString(host_components, '.'); | 210 host_ = JoinString(host_components, '.'); |
210 | 211 |
211 path_start_pos = host_end_pos; | 212 path_start_pos = host_end_pos; |
212 } | 213 } |
213 | 214 |
214 SetPath(pattern.substr(path_start_pos)); | 215 SetPath(pattern.substr(path_start_pos)); |
215 | 216 |
216 size_t port_pos = host_.find(':'); | 217 size_t port_pos = host_.find(':'); |
217 if (port_pos != std::string::npos) { | 218 if (port_pos != std::string::npos) { |
218 switch (strictness) { | 219 switch (parse_option_) { |
219 case USE_PORTS: { | 220 case USE_PORTS: { |
220 if (!SetPort(host_.substr(port_pos + 1))) | 221 if (!SetPort(host_.substr(port_pos + 1))) |
221 return PARSE_ERROR_INVALID_PORT; | 222 return PARSE_ERROR_INVALID_PORT; |
222 host_ = host_.substr(0, port_pos); | 223 host_ = host_.substr(0, port_pos); |
223 break; | 224 break; |
224 } | 225 } |
225 case ERROR_ON_PORTS: { | 226 case ERROR_ON_PORTS: { |
226 return PARSE_ERROR_HAS_COLON; | 227 return PARSE_ERROR_HAS_COLON; |
227 } | 228 } |
228 case IGNORE_PORTS: { | 229 case IGNORE_PORTS: { |
229 // Do nothing, i.e. leave the colon in the host. | 230 // Do nothing, i.e. leave the colon in the host. |
230 } | 231 } |
231 } | 232 } |
232 } | 233 } |
233 | 234 |
234 // No other '*' can occur in the host, though. This isn't necessary, but is | 235 // No other '*' can occur in the host, though. This isn't necessary, but is |
235 // done as a convenience to developers who might otherwise be confused and | 236 // done as a convenience to developers who might otherwise be confused and |
236 // think '*' works as a glob in the host. | 237 // think '*' works as a glob in the host. |
237 if (host_.find('*') != std::string::npos) | 238 if (host_.find('*') != std::string::npos) |
238 return PARSE_ERROR_INVALID_HOST_WILDCARD; | 239 return PARSE_ERROR_INVALID_HOST_WILDCARD; |
239 | 240 |
240 return PARSE_SUCCESS; | 241 return PARSE_SUCCESS; |
241 } | 242 } |
242 | 243 |
243 void URLPattern::SetValidSchemes(int valid_schemes) { | 244 void URLPattern::SetValidSchemes(int valid_schemes) { |
244 spec_.clear(); | 245 spec_.clear(); |
245 valid_schemes_ = valid_schemes; | 246 valid_schemes_ = valid_schemes; |
246 } | 247 } |
247 | 248 |
| 249 void URLPattern::SetParseOption(URLPattern::ParseOption parse_option) { |
| 250 spec_.clear(); |
| 251 parse_option_ = parse_option; |
| 252 } |
| 253 |
248 void URLPattern::SetHost(const std::string& host) { | 254 void URLPattern::SetHost(const std::string& host) { |
249 spec_.clear(); | 255 spec_.clear(); |
250 host_ = host; | 256 host_ = host; |
251 } | 257 } |
252 | 258 |
253 void URLPattern::SetMatchAllURLs(bool val) { | 259 void URLPattern::SetMatchAllURLs(bool val) { |
254 spec_.clear(); | 260 spec_.clear(); |
255 match_all_urls_ = val; | 261 match_all_urls_ = val; |
256 | 262 |
257 if (val) { | 263 if (val) { |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 } | 511 } |
506 | 512 |
507 return result; | 513 return result; |
508 } | 514 } |
509 | 515 |
510 // static | 516 // static |
511 const char* URLPattern::GetParseResultString( | 517 const char* URLPattern::GetParseResultString( |
512 URLPattern::ParseResult parse_result) { | 518 URLPattern::ParseResult parse_result) { |
513 return kParseResultMessages[parse_result]; | 519 return kParseResultMessages[parse_result]; |
514 } | 520 } |
OLD | NEW |