| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/content_settings_pattern_parser.h" | 5 #include "chrome/common/content_settings_pattern_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "chrome/common/url_constants.h" | 8 #include "url/url_constants.h" |
| 9 #include "extensions/common/constants.h" | |
| 10 #include "net/base/net_util.h" | |
| 11 #include "url/gurl.h" | |
| 12 #include "url/url_canon.h" | |
| 13 | 9 |
| 14 namespace { | 10 namespace { |
| 15 | 11 |
| 16 const char* kUrlPathSeparator = "/"; | 12 const char kDomainWildcard[] = "[*.]"; |
| 17 const char* kUrlPortSeparator = ":"; | 13 const size_t kDomainWildcardLength = 4; |
| 14 const char kHostWildcard[] = "*"; |
| 15 const char kPathWildcard[] = "*"; |
| 16 const char kPortWildcard[] = "*"; |
| 17 const char kSchemeWildcard[] = "*"; |
| 18 const char kUrlPathSeparator[] = "/"; |
| 19 const char kUrlPortSeparator[] = ":"; |
| 18 | 20 |
| 19 class Component { | 21 class Component { |
| 20 public: | 22 public: |
| 21 Component() : start(0), len(0) {} | 23 Component() : start(0), len(0) {} |
| 22 Component(size_t s, size_t l) : start(s), len(l) {} | 24 Component(size_t s, size_t l) : start(s), len(l) {} |
| 23 | 25 |
| 24 bool IsNonEmpty() { | 26 bool IsNonEmpty() { |
| 25 return len > 0; | 27 return len > 0; |
| 26 } | 28 } |
| 27 | 29 |
| 28 size_t start; | 30 size_t start; |
| 29 size_t len; | 31 size_t len; |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 } // namespace | 34 } // namespace |
| 33 | 35 |
| 34 namespace content_settings { | 36 namespace content_settings { |
| 35 | 37 |
| 36 const char* PatternParser::kDomainWildcard = "[*.]"; | |
| 37 | |
| 38 const size_t PatternParser::kDomainWildcardLength = 4; | |
| 39 | |
| 40 const char* PatternParser::kSchemeWildcard = "*"; | |
| 41 | |
| 42 const char* PatternParser::kHostWildcard = "*"; | |
| 43 | |
| 44 const char* PatternParser::kPortWildcard = "*"; | |
| 45 | |
| 46 const char* PatternParser::kPathWildcard = "*"; | |
| 47 | |
| 48 // static | |
| 49 void PatternParser::Parse(const std::string& pattern_spec, | 38 void PatternParser::Parse(const std::string& pattern_spec, |
| 50 ContentSettingsPattern::BuilderInterface* builder) { | 39 ContentSettingsPattern::BuilderInterface* builder) { |
| 51 if (pattern_spec == "*") { | 40 if (pattern_spec == "*") { |
| 52 builder->WithSchemeWildcard(); | 41 builder->WithSchemeWildcard(); |
| 53 builder->WithDomainWildcard(); | 42 builder->WithDomainWildcard(); |
| 54 builder->WithPortWildcard(); | 43 builder->WithPortWildcard(); |
| 55 return; | 44 return; |
| 56 } | 45 } |
| 57 | 46 |
| 58 // Initialize components for the individual patterns parts to empty | 47 // Initialize components for the individual patterns parts to empty |
| 59 // sub-strings. | 48 // sub-strings. |
| 60 Component scheme_component; | 49 Component scheme_component; |
| 61 Component host_component; | 50 Component host_component; |
| 62 Component port_component; | 51 Component port_component; |
| 63 Component path_component; | 52 Component path_component; |
| 64 | 53 |
| 65 size_t start = 0; | 54 size_t start = 0; |
| 66 size_t current_pos = 0; | 55 size_t current_pos = 0; |
| 67 | 56 |
| 68 if (pattern_spec.empty()) | 57 if (pattern_spec.empty()) |
| 69 return; | 58 return; |
| 70 | 59 |
| 71 // Test if a scheme pattern is in the spec. | 60 // Test if a scheme pattern is in the spec. |
| 72 current_pos = pattern_spec.find( | 61 const std::string standard_scheme_separator(url::kStandardSchemeSeparator); |
| 73 std::string(url::kStandardSchemeSeparator), start); | 62 current_pos = pattern_spec.find(standard_scheme_separator, start); |
| 74 if (current_pos != std::string::npos) { | 63 if (current_pos != std::string::npos) { |
| 75 scheme_component = Component(start, current_pos); | 64 scheme_component = Component(start, current_pos); |
| 76 start = current_pos + strlen(url::kStandardSchemeSeparator); | 65 start = current_pos + standard_scheme_separator.size(); |
| 77 current_pos = start; | 66 current_pos = start; |
| 78 } else { | 67 } else { |
| 79 current_pos = start; | 68 current_pos = start; |
| 80 } | 69 } |
| 81 | 70 |
| 82 if (start >= pattern_spec.size()) | 71 if (start >= pattern_spec.size()) |
| 83 return; // Bad pattern spec. | 72 return; // Bad pattern spec. |
| 84 | 73 |
| 85 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses | 74 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses |
| 86 // contain ':'. So first move to the end of an IPv6 address befor searching | 75 // contain ':'. So first move to the end of an IPv6 address befor searching |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 for (size_t i = 0; i < port.size(); ++i) { | 154 for (size_t i = 0; i < port.size(); ++i) { |
| 166 if (!IsAsciiDigit(port[i])) { | 155 if (!IsAsciiDigit(port[i])) { |
| 167 builder->Invalid(); | 156 builder->Invalid(); |
| 168 return; | 157 return; |
| 169 } | 158 } |
| 170 } | 159 } |
| 171 // TODO(markusheintz): Check port range. | 160 // TODO(markusheintz): Check port range. |
| 172 builder->WithPort(port); | 161 builder->WithPort(port); |
| 173 } | 162 } |
| 174 } else { | 163 } else { |
| 175 if (scheme != std::string(extensions::kExtensionScheme) && | 164 if (!ContentSettingsPattern::IsNonWildcardDomainNonPortScheme(scheme) && |
| 176 scheme != std::string(url::kFileScheme)) | 165 scheme != url::kFileScheme) |
| 177 builder->WithPortWildcard(); | 166 builder->WithPortWildcard(); |
| 178 } | 167 } |
| 179 | 168 |
| 180 if (path_component.IsNonEmpty()) { | 169 if (path_component.IsNonEmpty()) { |
| 181 const std::string path = pattern_spec.substr(path_component.start, | 170 const std::string path = pattern_spec.substr(path_component.start, |
| 182 path_component.len); | 171 path_component.len); |
| 183 if (path.substr(1) == kPathWildcard) | 172 if (path.substr(1) == kPathWildcard) |
| 184 builder->WithPathWildcard(); | 173 builder->WithPathWildcard(); |
| 185 else | 174 else |
| 186 builder->WithPath(path); | 175 builder->WithPath(path); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 210 } | 199 } |
| 211 | 200 |
| 212 if (parts.has_domain_wildcard) { | 201 if (parts.has_domain_wildcard) { |
| 213 if (parts.host.empty()) | 202 if (parts.host.empty()) |
| 214 str += kHostWildcard; | 203 str += kHostWildcard; |
| 215 else | 204 else |
| 216 str += kDomainWildcard; | 205 str += kDomainWildcard; |
| 217 } | 206 } |
| 218 str += parts.host; | 207 str += parts.host; |
| 219 | 208 |
| 220 if (parts.scheme == std::string(extensions::kExtensionScheme)) { | 209 if (ContentSettingsPattern::IsNonWildcardDomainNonPortScheme(parts.scheme)) { |
| 221 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; | 210 str += parts.path.empty() ? std::string(kUrlPathSeparator) : parts.path; |
| 222 return str; | 211 return str; |
| 223 } | 212 } |
| 224 | 213 |
| 225 if (!parts.is_port_wildcard) { | 214 if (!parts.is_port_wildcard) { |
| 226 str += std::string(kUrlPortSeparator) + parts.port; | 215 str += std::string(kUrlPortSeparator) + parts.port; |
| 227 } | 216 } |
| 228 | 217 |
| 229 return str; | 218 return str; |
| 230 } | 219 } |
| 231 | 220 |
| 232 } // namespace content_settings | 221 } // namespace content_settings |
| OLD | NEW |