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 "components/content_settings/core/common/embedder_variables.h" |
9 #include "extensions/common/constants.h" | 9 #include "url/url_constants.h" |
10 #include "net/base/net_util.h" | |
11 #include "url/gurl.h" | |
12 #include "url/url_canon.h" | |
13 | 10 |
14 namespace { | 11 namespace { |
15 | 12 |
16 const char* kUrlPathSeparator = "/"; | 13 const char kDomainWildcard[] = "[*.]"; |
17 const char* kUrlPortSeparator = ":"; | 14 const size_t kDomainWildcardLength = 4; |
| 15 const char kHostWildcard[] = "*"; |
| 16 const char kPathWildcard[] = "*"; |
| 17 const char kPortWildcard[] = "*"; |
| 18 const char kSchemeWildcard[] = "*"; |
| 19 const char kUrlPathSeparator[] = "/"; |
| 20 const char kUrlPortSeparator[] = ":"; |
18 | 21 |
19 class Component { | 22 class Component { |
20 public: | 23 public: |
21 Component() : start(0), len(0) {} | 24 Component() : start(0), len(0) {} |
22 Component(size_t s, size_t l) : start(s), len(l) {} | 25 Component(size_t s, size_t l) : start(s), len(l) {} |
23 | 26 |
24 bool IsNonEmpty() { | 27 bool IsNonEmpty() { |
25 return len > 0; | 28 return len > 0; |
26 } | 29 } |
27 | 30 |
28 size_t start; | 31 size_t start; |
29 size_t len; | 32 size_t len; |
30 }; | 33 }; |
31 | 34 |
32 } // namespace | 35 } // namespace |
33 | 36 |
34 namespace content_settings { | 37 namespace content_settings { |
35 | 38 |
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, | 39 void PatternParser::Parse(const std::string& pattern_spec, |
50 ContentSettingsPattern::BuilderInterface* builder) { | 40 ContentSettingsPattern::BuilderInterface* builder) { |
51 if (pattern_spec == "*") { | 41 if (pattern_spec == "*") { |
52 builder->WithSchemeWildcard(); | 42 builder->WithSchemeWildcard(); |
53 builder->WithDomainWildcard(); | 43 builder->WithDomainWildcard(); |
54 builder->WithPortWildcard(); | 44 builder->WithPortWildcard(); |
55 return; | 45 return; |
56 } | 46 } |
57 | 47 |
58 // Initialize components for the individual patterns parts to empty | 48 // Initialize components for the individual patterns parts to empty |
59 // sub-strings. | 49 // sub-strings. |
60 Component scheme_component; | 50 Component scheme_component; |
61 Component host_component; | 51 Component host_component; |
62 Component port_component; | 52 Component port_component; |
63 Component path_component; | 53 Component path_component; |
64 | 54 |
65 size_t start = 0; | 55 size_t start = 0; |
66 size_t current_pos = 0; | 56 size_t current_pos = 0; |
67 | 57 |
68 if (pattern_spec.empty()) | 58 if (pattern_spec.empty()) |
69 return; | 59 return; |
70 | 60 |
71 // Test if a scheme pattern is in the spec. | 61 // Test if a scheme pattern is in the spec. |
72 current_pos = pattern_spec.find( | 62 const std::string standard_scheme_separator(url::kStandardSchemeSeparator); |
73 std::string(url::kStandardSchemeSeparator), start); | 63 current_pos = pattern_spec.find(standard_scheme_separator, start); |
74 if (current_pos != std::string::npos) { | 64 if (current_pos != std::string::npos) { |
75 scheme_component = Component(start, current_pos); | 65 scheme_component = Component(start, current_pos); |
76 start = current_pos + strlen(url::kStandardSchemeSeparator); | 66 start = current_pos + standard_scheme_separator.size(); |
77 current_pos = start; | 67 current_pos = start; |
78 } else { | 68 } else { |
79 current_pos = start; | 69 current_pos = start; |
80 } | 70 } |
81 | 71 |
82 if (start >= pattern_spec.size()) | 72 if (start >= pattern_spec.size()) |
83 return; // Bad pattern spec. | 73 return; // Bad pattern spec. |
84 | 74 |
85 // Jump to the end of domain wildcards or an IPv6 addresses. IPv6 addresses | 75 // 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 | 76 // 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) { | 155 for (size_t i = 0; i < port.size(); ++i) { |
166 if (!IsAsciiDigit(port[i])) { | 156 if (!IsAsciiDigit(port[i])) { |
167 builder->Invalid(); | 157 builder->Invalid(); |
168 return; | 158 return; |
169 } | 159 } |
170 } | 160 } |
171 // TODO(markusheintz): Check port range. | 161 // TODO(markusheintz): Check port range. |
172 builder->WithPort(port); | 162 builder->WithPort(port); |
173 } | 163 } |
174 } else { | 164 } else { |
175 if (scheme != std::string(extensions::kExtensionScheme) && | 165 if (!content_settings::IsNonPortScheme(scheme) && |
176 scheme != std::string(url::kFileScheme)) | 166 scheme != url::kFileScheme) |
177 builder->WithPortWildcard(); | 167 builder->WithPortWildcard(); |
178 } | 168 } |
179 | 169 |
180 if (path_component.IsNonEmpty()) { | 170 if (path_component.IsNonEmpty()) { |
181 const std::string path = pattern_spec.substr(path_component.start, | 171 const std::string path = pattern_spec.substr(path_component.start, |
182 path_component.len); | 172 path_component.len); |
183 if (path.substr(1) == kPathWildcard) | 173 if (path.substr(1) == kPathWildcard) |
184 builder->WithPathWildcard(); | 174 builder->WithPathWildcard(); |
185 else | 175 else |
186 builder->WithPath(path); | 176 builder->WithPath(path); |
187 } | 177 } |
188 } | 178 } |
189 | 179 |
190 // static | |
191 std::string PatternParser::ToString( | 180 std::string PatternParser::ToString( |
192 const ContentSettingsPattern::PatternParts& parts) { | 181 const ContentSettingsPattern::PatternParts& parts) { |
193 // Return the most compact form to support legacy code and legacy pattern | 182 // Return the most compact form to support legacy code and legacy pattern |
194 // strings. | 183 // strings. |
195 if (parts.is_scheme_wildcard && | 184 if (parts.is_scheme_wildcard && |
196 parts.has_domain_wildcard && | 185 parts.has_domain_wildcard && |
197 parts.host.empty() && | 186 parts.host.empty() && |
198 parts.is_port_wildcard) | 187 parts.is_port_wildcard) |
199 return "*"; | 188 return "*"; |
200 | 189 |
201 std::string str; | 190 std::string str; |
202 if (!parts.is_scheme_wildcard) | 191 if (!parts.is_scheme_wildcard) |
203 str += parts.scheme + url::kStandardSchemeSeparator; | 192 str += parts.scheme + url::kStandardSchemeSeparator; |
204 | 193 |
205 if (parts.scheme == url::kFileScheme) { | 194 if (parts.scheme == url::kFileScheme) { |
206 if (parts.is_path_wildcard) | 195 if (parts.is_path_wildcard) |
207 return str + kUrlPathSeparator + kPathWildcard; | 196 return str + kUrlPathSeparator + kPathWildcard; |
208 else | 197 else |
209 return str + parts.path; | 198 return str + parts.path; |
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 (content_settings::IsNonPortScheme(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 |