OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "url/url_util.h" | 5 #include "url/url_util.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "url/url_canon_internal.h" | 13 #include "url/url_canon_internal.h" |
14 #include "url/url_file.h" | 14 #include "url/url_file.h" |
15 #include "url/url_util_internal.h" | 15 #include "url/url_util_internal.h" |
16 | 16 |
17 namespace url { | 17 namespace url { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const int kNumStandardURLSchemes = 8; | 21 const int kNumStandardURLSchemes = 8; |
22 const char* kStandardURLSchemes[kNumStandardURLSchemes] = { | 22 const char* kStandardURLSchemes[kNumStandardURLSchemes] = { |
23 kHttpScheme, | 23 kHttpScheme, |
24 kHttpsScheme, | 24 kHttpsScheme, |
25 kFileScheme, // Yes, file urls can have a hostname! | 25 kFileScheme, // Yes, file URLs can have a hostname! |
26 kFtpScheme, | 26 kFtpScheme, |
27 kGopherScheme, | 27 kGopherScheme, |
28 kWsScheme, // WebSocket. | 28 kWsScheme, // WebSocket. |
29 kWssScheme, // WebSocket secure. | 29 kWssScheme, // WebSocket secure. |
30 kFileSystemScheme, | 30 kFileSystemScheme, |
31 }; | 31 }; |
32 | 32 |
33 // List of the currently installed standard schemes. This list is lazily | 33 // List of the currently installed standard schemes. This list is lazily |
34 // initialized by InitStandardSchemes and is leaked on shutdown to prevent | 34 // initialized by InitStandardSchemes and is leaked on shutdown to prevent |
35 // any destructors from being called that will slow us down or cause problems. | 35 // any destructors from being called that will slow us down or cause problems. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 // Remove any whitespace from the middle of the relative URL, possibly | 125 // Remove any whitespace from the middle of the relative URL, possibly |
126 // copying to the new buffer. | 126 // copying to the new buffer. |
127 RawCanonOutputT<CHAR> whitespace_buffer; | 127 RawCanonOutputT<CHAR> whitespace_buffer; |
128 int spec_len; | 128 int spec_len; |
129 const CHAR* spec = RemoveURLWhitespace(in_spec, in_spec_len, | 129 const CHAR* spec = RemoveURLWhitespace(in_spec, in_spec_len, |
130 &whitespace_buffer, &spec_len); | 130 &whitespace_buffer, &spec_len); |
131 | 131 |
132 Parsed parsed_input; | 132 Parsed parsed_input; |
133 #ifdef WIN32 | 133 #ifdef WIN32 |
134 // For Windows, we allow things that look like absolute Windows paths to be | 134 // For Windows, we allow things that look like absolute Windows paths to be |
135 // fixed up magically to file URLs. This is done for IE compatability. For | 135 // fixed up magically to file URLs. This is done for IE compatibility. For |
136 // example, this will change "c:/foo" into a file URL rather than treating | 136 // example, this will change "c:/foo" into a file URL rather than treating |
137 // it as a URL with the protocol "c". It also works for UNC ("\\foo\bar.txt"). | 137 // it as a URL with the protocol "c". It also works for UNC ("\\foo\bar.txt"). |
138 // There is similar logic in url_canon_relative.cc for | 138 // There is similar logic in url_canon_relative.cc for |
139 // | 139 // |
140 // For Max & Unix, we don't do this (the equivalent would be "/foo/bar" which | 140 // For Max & Unix, we don't do this (the equivalent would be "/foo/bar" which |
141 // has no meaning as an absolute path name. This is because browsers on Mac | 141 // has no meaning as an absolute path name. This is because browsers on Mac |
142 // & Unix don't generally do this, so there is no compatibility reason for | 142 // & Unix don't generally do this, so there is no compatibility reason for |
143 // doing so. | 143 // doing so. |
144 if (DoesBeginUNCPath(spec, 0, spec_len, false) || | 144 if (DoesBeginUNCPath(spec, 0, spec_len, false) || |
145 DoesBeginWindowsDriveSpec(spec, 0, spec_len)) { | 145 DoesBeginWindowsDriveSpec(spec, 0, spec_len)) { |
(...skipping 22 matching lines...) Expand all Loading... |
168 charset_converter, output, | 168 charset_converter, output, |
169 output_parsed); | 169 output_parsed); |
170 | 170 |
171 } else if (DoIsStandard(spec, scheme)) { | 171 } else if (DoIsStandard(spec, scheme)) { |
172 // All "normal" URLs. | 172 // All "normal" URLs. |
173 ParseStandardURL(spec, spec_len, &parsed_input); | 173 ParseStandardURL(spec, spec_len, &parsed_input); |
174 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, | 174 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, |
175 charset_converter, output, output_parsed); | 175 charset_converter, output, output_parsed); |
176 | 176 |
177 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) { | 177 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) { |
178 // Mailto are treated like a standard url with only a scheme, path, query | 178 // Mailto URLs are treated like standard URLs, with only a scheme, path, |
| 179 // and query. |
179 ParseMailtoURL(spec, spec_len, &parsed_input); | 180 ParseMailtoURL(spec, spec_len, &parsed_input); |
180 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, | 181 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output, |
181 output_parsed); | 182 output_parsed); |
182 | 183 |
183 } else { | 184 } else { |
184 // "Weird" URLs like data: and javascript: | 185 // "Weird" URLs like data: and javascript:. |
185 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); | 186 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input); |
186 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, | 187 success = CanonicalizePathURL(spec, spec_len, parsed_input, output, |
187 output_parsed); | 188 output_parsed); |
188 } | 189 } |
189 return success; | 190 return success; |
190 } | 191 } |
191 | 192 |
192 template<typename CHAR> | 193 template<typename CHAR> |
193 bool DoResolveRelative(const char* base_spec, | 194 bool DoResolveRelative(const char* base_spec, |
194 int base_spec_len, | 195 int base_spec_len, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 265 |
265 template<typename CHAR> | 266 template<typename CHAR> |
266 bool DoReplaceComponents(const char* spec, | 267 bool DoReplaceComponents(const char* spec, |
267 int spec_len, | 268 int spec_len, |
268 const Parsed& parsed, | 269 const Parsed& parsed, |
269 const Replacements<CHAR>& replacements, | 270 const Replacements<CHAR>& replacements, |
270 CharsetConverter* charset_converter, | 271 CharsetConverter* charset_converter, |
271 CanonOutput* output, | 272 CanonOutput* output, |
272 Parsed* out_parsed) { | 273 Parsed* out_parsed) { |
273 // If the scheme is overridden, just do a simple string substitution and | 274 // If the scheme is overridden, just do a simple string substitution and |
274 // reparse the whole thing. There are lots of edge cases that we really don't | 275 // re-parse the whole thing. There are lots of edge cases that we really don't |
275 // want to deal with. Like what happens if I replace "http://e:8080/foo" | 276 // want to deal with. Like what happens if I replace "http://e:8080/foo" |
276 // with a file. Does it become "file:///E:/8080/foo" where the port number | 277 // with a file. Does it become "file:///E:/8080/foo" where the port number |
277 // becomes part of the path? Parsing that string as a file URL says "yes" | 278 // becomes part of the path? Parsing that string as a file URL says "yes" |
278 // but almost no sane rule for dealing with the components individually would | 279 // but almost no sane rule for dealing with the components individually would |
279 // come up with that. | 280 // come up with that. |
280 // | 281 // |
281 // Why allow these crazy cases at all? Programatically, there is almost no | 282 // Why allow these crazy cases at all? Programatically, there is almost no |
282 // case for replacing the scheme. The most common case for hitting this is | 283 // case for replacing the scheme. The most common case for hitting this is |
283 // in JS when building up a URL using the location object. In this case, the | 284 // in JS when building up a URL using the location object. In this case, the |
284 // JS code expects the string substitution behavior: | 285 // JS code expects the string substitution behavior: |
(...skipping 26 matching lines...) Expand all Loading... |
311 | 312 |
312 // Recurse using the version with the scheme already replaced. This will now | 313 // Recurse using the version with the scheme already replaced. This will now |
313 // use the replacement rules for the new scheme. | 314 // use the replacement rules for the new scheme. |
314 // | 315 // |
315 // Warning: this code assumes that ReplaceComponents will re-check all | 316 // Warning: this code assumes that ReplaceComponents will re-check all |
316 // components for validity. This is because we can't fail if DoCanonicalize | 317 // components for validity. This is because we can't fail if DoCanonicalize |
317 // failed above since theoretically the thing making it fail could be | 318 // failed above since theoretically the thing making it fail could be |
318 // getting replaced here. If ReplaceComponents didn't re-check everything, | 319 // getting replaced here. If ReplaceComponents didn't re-check everything, |
319 // we wouldn't know if something *not* getting replaced is a problem. | 320 // we wouldn't know if something *not* getting replaced is a problem. |
320 // If the scheme-specific replacers are made more intelligent so they don't | 321 // If the scheme-specific replacers are made more intelligent so they don't |
321 // re-check everything, we should instead recanonicalize the whole thing | 322 // re-check everything, we should instead re-canonicalize the whole thing |
322 // after this call to check validity (this assumes replacing the scheme is | 323 // after this call to check validity (this assumes replacing the scheme is |
323 // much much less common than other types of replacements, like clearing the | 324 // much much less common than other types of replacements, like clearing the |
324 // ref). | 325 // ref). |
325 Replacements<CHAR> replacements_no_scheme = replacements; | 326 Replacements<CHAR> replacements_no_scheme = replacements; |
326 replacements_no_scheme.SetScheme(NULL, Component()); | 327 replacements_no_scheme.SetScheme(NULL, Component()); |
327 return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(), | 328 return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(), |
328 recanonicalized_parsed, replacements_no_scheme, | 329 recanonicalized_parsed, replacements_no_scheme, |
329 charset_converter, output, out_parsed); | 330 charset_converter, output, out_parsed); |
330 } | 331 } |
331 | 332 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 } | 365 } |
365 } | 366 } |
366 | 367 |
367 void AddStandardScheme(const char* new_scheme) { | 368 void AddStandardScheme(const char* new_scheme) { |
368 // If this assert triggers, it means you've called AddStandardScheme after | 369 // If this assert triggers, it means you've called AddStandardScheme after |
369 // LockStandardSchemes have been called (see the header file for | 370 // LockStandardSchemes have been called (see the header file for |
370 // LockStandardSchemes for more). | 371 // LockStandardSchemes for more). |
371 // | 372 // |
372 // This normally means you're trying to set up a new standard scheme too late | 373 // This normally means you're trying to set up a new standard scheme too late |
373 // in your application's init process. Locate where your app does this | 374 // in your application's init process. Locate where your app does this |
374 // initialization and calls LockStandardScheme, and add your new standard | 375 // initialization and calls LockStandardSchemes, and add your new standard |
375 // scheme there. | 376 // scheme there. |
376 DCHECK(!standard_schemes_locked) << | 377 DCHECK(!standard_schemes_locked) << |
377 "Trying to add a standard scheme after the list has been locked."; | 378 "Trying to add a standard scheme after the list has been locked."; |
378 | 379 |
379 size_t scheme_len = strlen(new_scheme); | 380 size_t scheme_len = strlen(new_scheme); |
380 if (scheme_len == 0) | 381 if (scheme_len == 0) |
381 return; | 382 return; |
382 | 383 |
383 // Dulicate the scheme into a new buffer and add it to the list of standard | 384 // Duplicate the scheme into a new buffer and add it to the list of standard |
384 // schemes. This pointer will be leaked on shutdown. | 385 // schemes. This pointer will be leaked on shutdown. |
385 char* dup_scheme = new char[scheme_len + 1]; | 386 char* dup_scheme = new char[scheme_len + 1]; |
386 ANNOTATE_LEAKING_OBJECT_PTR(dup_scheme); | 387 ANNOTATE_LEAKING_OBJECT_PTR(dup_scheme); |
387 memcpy(dup_scheme, new_scheme, scheme_len + 1); | 388 memcpy(dup_scheme, new_scheme, scheme_len + 1); |
388 | 389 |
389 InitStandardSchemes(); | 390 InitStandardSchemes(); |
390 standard_schemes->push_back(dup_scheme); | 391 standard_schemes->push_back(dup_scheme); |
391 } | 392 } |
392 | 393 |
393 void LockStandardSchemes() { | 394 void LockStandardSchemes() { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 return DoCompareSchemeComponent(spec, component, compare_to); | 551 return DoCompareSchemeComponent(spec, component, compare_to); |
551 } | 552 } |
552 | 553 |
553 bool CompareSchemeComponent(const base::char16* spec, | 554 bool CompareSchemeComponent(const base::char16* spec, |
554 const Component& component, | 555 const Component& component, |
555 const char* compare_to) { | 556 const char* compare_to) { |
556 return DoCompareSchemeComponent(spec, component, compare_to); | 557 return DoCompareSchemeComponent(spec, component, compare_to); |
557 } | 558 } |
558 | 559 |
559 } // namespace url | 560 } // namespace url |
OLD | NEW |