Index: chrome/common/extensions/url_pattern.cc |
diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc |
index 6749ab8632bef945643b225c8f250476ffc58717..bde9e6d201794beba9143894bd6877b1b1235250 100644 |
--- a/chrome/common/extensions/url_pattern.cc |
+++ b/chrome/common/extensions/url_pattern.cc |
@@ -4,6 +4,7 @@ |
#include "chrome/common/extensions/url_pattern.h" |
+#include "base/string_number_conversions.h" |
#include "base/string_piece.h" |
#include "base/string_split.h" |
#include "base/string_util.h" |
@@ -48,6 +49,8 @@ const char* kParseErrorInvalidHostWildcard = "Invalid host wildcard."; |
const char* kParseErrorEmptyPath = "Empty path."; |
const char* kParseErrorHasColon = |
"Ports are not supported in URL patterns. ':' may not be used in a host."; |
+const char* kParseErrorInvalidPort = |
+ "Invalid port."; |
// Message explaining each URLPattern::ParseResult. |
const char* kParseResultMessages[] = { |
@@ -58,7 +61,8 @@ const char* kParseResultMessages[] = { |
kParseErrorEmptyHost, |
kParseErrorInvalidHostWildcard, |
kParseErrorEmptyPath, |
- kParseErrorHasColon |
+ kParseErrorHasColon, |
+ kParseErrorInvalidPort, |
}; |
COMPILE_ASSERT(URLPattern::NUM_PARSE_RESULTS == arraysize(kParseResultMessages), |
@@ -75,6 +79,15 @@ bool IsStandardScheme(const std::string& scheme) { |
url_parse::Component(0, static_cast<int>(scheme.length()))); |
} |
+bool IsValidPort(const std::string& port) { |
+ if (port.empty()) |
+ return true; |
+ int parsed_port; |
+ if (!base::StringToInt(port, &parsed_port)) |
+ return false; |
+ return (parsed_port >= 0) && (parsed_port < 65536); |
+} |
+ |
} // namespace |
URLPattern::URLPattern() |
@@ -191,8 +204,16 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, |
SetPath(pattern.substr(path_start_pos)); |
- if (strictness == PARSE_STRICT && host_.find(':') != std::string::npos) |
- return PARSE_ERROR_HAS_COLON; |
+ size_t port_pos = host_.find(':'); |
+ if (port_pos != std::string::npos) { |
+ if (strictness == PARSE_STRICT) |
+ return PARSE_ERROR_HAS_COLON; |
+ |
+ 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
|
+ return PARSE_ERROR_INVALID_PORT; |
+ |
+ host_ = host_.substr(0, port_pos); |
+ } |
return PARSE_SUCCESS; |
} |
@@ -226,6 +247,14 @@ void URLPattern::SetPath(const std::string& path) { |
ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); |
} |
+bool URLPattern::SetPort(const std::string& port) { |
+ if (IsValidPort(port)) { |
+ port_ = port; |
+ return true; |
+ } |
+ return false; |
+} |
+ |
bool URLPattern::MatchesURL(const GURL &test) const { |
if (!MatchesScheme(test.scheme())) |
return false; |
@@ -314,6 +343,11 @@ std::string URLPattern::GetAsString() const { |
if (!host_.empty()) |
spec += host_; |
+ |
+ if (!port_.empty()) { |
+ spec += ":"; |
+ spec += port_; |
+ } |
} |
if (!path_.empty()) |