Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7733)

Unified Diff: chrome/common/extensions/url_pattern.cc

Issue 7229012: Use extension match pattern syntax in content settings extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: unit test Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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())

Powered by Google App Engine
This is Rietveld 408576698