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_piece.h" | 8 #include "base/string_piece.h" |
8 #include "base/string_split.h" | 9 #include "base/string_split.h" |
9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
10 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
11 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
12 #include "googleurl/src/url_util.h" | 13 #include "googleurl/src/url_util.h" |
13 | 14 |
14 const char URLPattern::kAllUrlsPattern[] = "<all_urls>"; | 15 const char URLPattern::kAllUrlsPattern[] = "<all_urls>"; |
15 | 16 |
16 namespace { | 17 namespace { |
(...skipping 24 matching lines...) Expand all Loading... | |
41 | 42 |
42 const char* kParseSuccess = "Success."; | 43 const char* kParseSuccess = "Success."; |
43 const char* kParseErrorMissingSchemeSeparator = "Missing scheme separator."; | 44 const char* kParseErrorMissingSchemeSeparator = "Missing scheme separator."; |
44 const char* kParseErrorInvalidScheme = "Invalid scheme."; | 45 const char* kParseErrorInvalidScheme = "Invalid scheme."; |
45 const char* kParseErrorWrongSchemeType = "Wrong scheme type."; | 46 const char* kParseErrorWrongSchemeType = "Wrong scheme type."; |
46 const char* kParseErrorEmptyHost = "Host can not be empty."; | 47 const char* kParseErrorEmptyHost = "Host can not be empty."; |
47 const char* kParseErrorInvalidHostWildcard = "Invalid host wildcard."; | 48 const char* kParseErrorInvalidHostWildcard = "Invalid host wildcard."; |
48 const char* kParseErrorEmptyPath = "Empty path."; | 49 const char* kParseErrorEmptyPath = "Empty path."; |
49 const char* kParseErrorHasColon = | 50 const char* kParseErrorHasColon = |
50 "Ports are not supported in URL patterns. ':' may not be used in a host."; | 51 "Ports are not supported in URL patterns. ':' may not be used in a host."; |
52 const char* kParseErrorInvalidPort = | |
53 "Invalid port."; | |
51 | 54 |
52 // Message explaining each URLPattern::ParseResult. | 55 // Message explaining each URLPattern::ParseResult. |
53 const char* kParseResultMessages[] = { | 56 const char* kParseResultMessages[] = { |
54 kParseSuccess, | 57 kParseSuccess, |
55 kParseErrorMissingSchemeSeparator, | 58 kParseErrorMissingSchemeSeparator, |
56 kParseErrorInvalidScheme, | 59 kParseErrorInvalidScheme, |
57 kParseErrorWrongSchemeType, | 60 kParseErrorWrongSchemeType, |
58 kParseErrorEmptyHost, | 61 kParseErrorEmptyHost, |
59 kParseErrorInvalidHostWildcard, | 62 kParseErrorInvalidHostWildcard, |
60 kParseErrorEmptyPath, | 63 kParseErrorEmptyPath, |
61 kParseErrorHasColon | 64 kParseErrorHasColon, |
65 kParseErrorInvalidPort, | |
62 }; | 66 }; |
63 | 67 |
64 COMPILE_ASSERT(URLPattern::NUM_PARSE_RESULTS == arraysize(kParseResultMessages), | 68 COMPILE_ASSERT(URLPattern::NUM_PARSE_RESULTS == arraysize(kParseResultMessages), |
65 must_add_message_for_each_parse_result); | 69 must_add_message_for_each_parse_result); |
66 | 70 |
67 const char kPathSeparator[] = "/"; | 71 const char kPathSeparator[] = "/"; |
68 | 72 |
69 bool IsStandardScheme(const std::string& scheme) { | 73 bool IsStandardScheme(const std::string& scheme) { |
70 // "*" gets the same treatment as a standard scheme. | 74 // "*" gets the same treatment as a standard scheme. |
71 if (scheme == "*") | 75 if (scheme == "*") |
72 return true; | 76 return true; |
73 | 77 |
74 return url_util::IsStandard(scheme.c_str(), | 78 return url_util::IsStandard(scheme.c_str(), |
75 url_parse::Component(0, static_cast<int>(scheme.length()))); | 79 url_parse::Component(0, static_cast<int>(scheme.length()))); |
76 } | 80 } |
77 | 81 |
82 bool IsValidPort(const std::string& port) { | |
83 if (port.empty()) | |
84 return true; | |
85 int parsed_port; | |
86 if (!base::StringToInt(port, &parsed_port)) | |
87 return false; | |
88 return (parsed_port >= 0) && (parsed_port < 65536); | |
89 } | |
90 | |
78 } // namespace | 91 } // namespace |
79 | 92 |
80 URLPattern::URLPattern() | 93 URLPattern::URLPattern() |
81 : valid_schemes_(SCHEME_NONE), | 94 : valid_schemes_(SCHEME_NONE), |
82 match_all_urls_(false), | 95 match_all_urls_(false), |
83 match_subdomains_(false) {} | 96 match_subdomains_(false) {} |
84 | 97 |
85 URLPattern::URLPattern(int valid_schemes) | 98 URLPattern::URLPattern(int valid_schemes) |
86 : valid_schemes_(valid_schemes), match_all_urls_(false), | 99 : valid_schemes_(valid_schemes), match_all_urls_(false), |
87 match_subdomains_(false) {} | 100 match_subdomains_(false) {} |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 // done as a convenience to developers who might otherwise be confused and | 197 // done as a convenience to developers who might otherwise be confused and |
185 // think '*' works as a glob in the host. | 198 // think '*' works as a glob in the host. |
186 if (host_.find('*') != std::string::npos) | 199 if (host_.find('*') != std::string::npos) |
187 return PARSE_ERROR_INVALID_HOST_WILDCARD; | 200 return PARSE_ERROR_INVALID_HOST_WILDCARD; |
188 | 201 |
189 path_start_pos = host_end_pos; | 202 path_start_pos = host_end_pos; |
190 } | 203 } |
191 | 204 |
192 SetPath(pattern.substr(path_start_pos)); | 205 SetPath(pattern.substr(path_start_pos)); |
193 | 206 |
194 if (strictness == PARSE_STRICT && host_.find(':') != std::string::npos) | 207 size_t port_pos = host_.find(':'); |
195 return PARSE_ERROR_HAS_COLON; | 208 if (port_pos != std::string::npos) { |
209 if (strictness == PARSE_STRICT) | |
210 return PARSE_ERROR_HAS_COLON; | |
211 | |
212 if (!SetPort(host_.substr(port_pos + 1))) | |
Matt Perry
2011/06/24 18:53:58
This does change the behavior slightly. Previously
Bernhard Bauer
2011/06/24 21:49:53
I'd like to keep the changes to GetAsString, so we
| |
213 return PARSE_ERROR_INVALID_PORT; | |
214 | |
215 host_ = host_.substr(0, port_pos); | |
216 } | |
196 | 217 |
197 return PARSE_SUCCESS; | 218 return PARSE_SUCCESS; |
198 } | 219 } |
199 | 220 |
200 bool URLPattern::SetScheme(const std::string& scheme) { | 221 bool URLPattern::SetScheme(const std::string& scheme) { |
201 scheme_ = scheme; | 222 scheme_ = scheme; |
202 if (scheme_ == "*") { | 223 if (scheme_ == "*") { |
203 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); | 224 valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS); |
204 } else if (!IsValidScheme(scheme_)) { | 225 } else if (!IsValidScheme(scheme_)) { |
205 return false; | 226 return false; |
(...skipping 13 matching lines...) Expand all Loading... | |
219 return false; | 240 return false; |
220 } | 241 } |
221 | 242 |
222 void URLPattern::SetPath(const std::string& path) { | 243 void URLPattern::SetPath(const std::string& path) { |
223 path_ = path; | 244 path_ = path; |
224 path_escaped_ = path_; | 245 path_escaped_ = path_; |
225 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); | 246 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\"); |
226 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); | 247 ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); |
227 } | 248 } |
228 | 249 |
250 bool URLPattern::SetPort(const std::string& port) { | |
251 if (IsValidPort(port)) { | |
252 port_ = port; | |
253 return true; | |
254 } | |
255 return false; | |
256 } | |
257 | |
229 bool URLPattern::MatchesURL(const GURL &test) const { | 258 bool URLPattern::MatchesURL(const GURL &test) const { |
230 if (!MatchesScheme(test.scheme())) | 259 if (!MatchesScheme(test.scheme())) |
231 return false; | 260 return false; |
232 | 261 |
233 if (match_all_urls_) | 262 if (match_all_urls_) |
234 return true; | 263 return true; |
235 | 264 |
236 // Ignore hostname if scheme is file://. | 265 // Ignore hostname if scheme is file://. |
237 if (scheme_ != chrome::kFileScheme && !MatchesHost(test)) | 266 if (scheme_ != chrome::kFileScheme && !MatchesHost(test)) |
238 return false; | 267 return false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 | 336 |
308 if (scheme_ != chrome::kFileScheme && standard_scheme) { | 337 if (scheme_ != chrome::kFileScheme && standard_scheme) { |
309 if (match_subdomains_) { | 338 if (match_subdomains_) { |
310 spec += "*"; | 339 spec += "*"; |
311 if (!host_.empty()) | 340 if (!host_.empty()) |
312 spec += "."; | 341 spec += "."; |
313 } | 342 } |
314 | 343 |
315 if (!host_.empty()) | 344 if (!host_.empty()) |
316 spec += host_; | 345 spec += host_; |
346 | |
347 if (!port_.empty()) { | |
348 spec += ":"; | |
349 spec += port_; | |
350 } | |
317 } | 351 } |
318 | 352 |
319 if (!path_.empty()) | 353 if (!path_.empty()) |
320 spec += path_; | 354 spec += path_; |
321 | 355 |
322 return spec; | 356 return spec; |
323 } | 357 } |
324 | 358 |
325 bool URLPattern::OverlapsWith(const URLPattern& other) const { | 359 bool URLPattern::OverlapsWith(const URLPattern& other) const { |
326 if (!MatchesAnyScheme(other.GetExplicitSchemes()) && | 360 if (!MatchesAnyScheme(other.GetExplicitSchemes()) && |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 } | 421 } |
388 | 422 |
389 return result; | 423 return result; |
390 } | 424 } |
391 | 425 |
392 // static | 426 // static |
393 const char* URLPattern::GetParseResultString( | 427 const char* URLPattern::GetParseResultString( |
394 URLPattern::ParseResult parse_result) { | 428 URLPattern::ParseResult parse_result) { |
395 return kParseResultMessages[parse_result]; | 429 return kParseResultMessages[parse_result]; |
396 } | 430 } |
OLD | NEW |