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
3 // found in the LICENSE file.
5 #include "url/url_util.h"
10 #include "base/logging.h"
11 #include "url/url_canon_internal.h"
12 #include "url/url_file.h"
13 #include "url/url_util_internal.h"
17 const char kFileScheme
[] = "file";
18 const char kFileSystemScheme
[] = "filesystem";
19 const char kMailtoScheme
[] = "mailto";
23 // ASCII-specific tolower. The standard library's tolower is locale sensitive,
24 // so we don't want to use it here.
25 template <class Char
> inline Char
ToLowerASCII(Char c
) {
26 return (c
>= 'A' && c
<= 'Z') ? (c
+ ('a' - 'A')) : c
;
29 // Backend for LowerCaseEqualsASCII.
30 template<typename Iter
>
31 inline bool DoLowerCaseEqualsASCII(Iter a_begin
, Iter a_end
, const char* b
) {
32 for (Iter it
= a_begin
; it
!= a_end
; ++it
, ++b
) {
33 if (!*b
|| ToLowerASCII(*it
) != *b
)
39 const int kNumStandardURLSchemes
= 8;
40 const char* kStandardURLSchemes
[kNumStandardURLSchemes
] = {
43 kFileScheme
, // Yes, file urls can have a hostname!
47 "wss", // WebSocket secure.
51 // List of the currently installed standard schemes. This list is lazily
52 // initialized by InitStandardSchemes and is leaked on shutdown to prevent
53 // any destructors from being called that will slow us down or cause problems.
54 std::vector
<const char*>* standard_schemes
= NULL
;
56 // See the LockStandardSchemes declaration in the header.
57 bool standard_schemes_locked
= false;
59 // Ensures that the standard_schemes list is initialized, does nothing if it
60 // already has values.
61 void InitStandardSchemes() {
64 standard_schemes
= new std::vector
<const char*>;
65 for (int i
= 0; i
< kNumStandardURLSchemes
; i
++)
66 standard_schemes
->push_back(kStandardURLSchemes
[i
]);
69 // Given a string and a range inside the string, compares it to the given
70 // lower-case |compare_to| buffer.
71 template<typename CHAR
>
72 inline bool DoCompareSchemeComponent(const CHAR
* spec
,
73 const url_parse::Component
& component
,
74 const char* compare_to
) {
75 if (!component
.is_nonempty())
76 return compare_to
[0] == 0; // When component is empty, match empty scheme.
77 return LowerCaseEqualsASCII(&spec
[component
.begin
],
78 &spec
[component
.end()],
82 // Returns true if the given scheme identified by |scheme| within |spec| is one
83 // of the registered "standard" schemes.
84 template<typename CHAR
>
85 bool DoIsStandard(const CHAR
* spec
, const url_parse::Component
& scheme
) {
86 if (!scheme
.is_nonempty())
87 return false; // Empty or invalid schemes are non-standard.
89 InitStandardSchemes();
90 for (size_t i
= 0; i
< standard_schemes
->size(); i
++) {
91 if (LowerCaseEqualsASCII(&spec
[scheme
.begin
], &spec
[scheme
.end()],
92 standard_schemes
->at(i
)))
98 template<typename CHAR
>
99 bool DoFindAndCompareScheme(const CHAR
* str
,
102 url_parse::Component
* found_scheme
) {
103 // Before extracting scheme, canonicalize the URL to remove any whitespace.
104 // This matches the canonicalization done in DoCanonicalize function.
105 url_canon::RawCanonOutputT
<CHAR
> whitespace_buffer
;
107 const CHAR
* spec
= RemoveURLWhitespace(str
, str_len
,
108 &whitespace_buffer
, &spec_len
);
110 url_parse::Component our_scheme
;
111 if (!url_parse::ExtractScheme(spec
, spec_len
, &our_scheme
)) {
114 *found_scheme
= url_parse::Component();
118 *found_scheme
= our_scheme
;
119 return DoCompareSchemeComponent(spec
, our_scheme
, compare
);
122 template<typename CHAR
>
123 bool DoCanonicalize(const CHAR
* in_spec
, int in_spec_len
,
124 url_canon::CharsetConverter
* charset_converter
,
125 url_canon::CanonOutput
* output
,
126 url_parse::Parsed
* output_parsed
) {
127 // Remove any whitespace from the middle of the relative URL, possibly
128 // copying to the new buffer.
129 url_canon::RawCanonOutputT
<CHAR
> whitespace_buffer
;
131 const CHAR
* spec
= RemoveURLWhitespace(in_spec
, in_spec_len
,
132 &whitespace_buffer
, &spec_len
);
134 url_parse::Parsed parsed_input
;
136 // For Windows, we allow things that look like absolute Windows paths to be
137 // fixed up magically to file URLs. This is done for IE compatability. For
138 // example, this will change "c:/foo" into a file URL rather than treating
139 // it as a URL with the protocol "c". It also works for UNC ("\\foo\bar.txt").
140 // There is similar logic in url_canon_relative.cc for
142 // For Max & Unix, we don't do this (the equivalent would be "/foo/bar" which
143 // has no meaning as an absolute path name. This is because browsers on Mac
144 // & Unix don't generally do this, so there is no compatibility reason for
146 if (url_parse::DoesBeginUNCPath(spec
, 0, spec_len
, false) ||
147 url_parse::DoesBeginWindowsDriveSpec(spec
, 0, spec_len
)) {
148 url_parse::ParseFileURL(spec
, spec_len
, &parsed_input
);
149 return url_canon::CanonicalizeFileURL(spec
, spec_len
, parsed_input
,
151 output
, output_parsed
);
155 url_parse::Component scheme
;
156 if (!url_parse::ExtractScheme(spec
, spec_len
, &scheme
))
159 // This is the parsed version of the input URL, we have to canonicalize it
160 // before storing it in our object.
162 if (DoCompareSchemeComponent(spec
, scheme
, kFileScheme
)) {
163 // File URLs are special.
164 url_parse::ParseFileURL(spec
, spec_len
, &parsed_input
);
165 success
= url_canon::CanonicalizeFileURL(spec
, spec_len
, parsed_input
,
166 charset_converter
, output
,
168 } else if (DoCompareSchemeComponent(spec
, scheme
, kFileSystemScheme
)) {
169 // Filesystem URLs are special.
170 url_parse::ParseFileSystemURL(spec
, spec_len
, &parsed_input
);
171 success
= url_canon::CanonicalizeFileSystemURL(spec
, spec_len
,
174 output
, output_parsed
);
176 } else if (DoIsStandard(spec
, scheme
)) {
177 // All "normal" URLs.
178 url_parse::ParseStandardURL(spec
, spec_len
, &parsed_input
);
179 success
= url_canon::CanonicalizeStandardURL(spec
, spec_len
, parsed_input
,
181 output
, output_parsed
);
183 } else if (DoCompareSchemeComponent(spec
, scheme
, kMailtoScheme
)) {
184 // Mailto are treated like a standard url with only a scheme, path, query
185 url_parse::ParseMailtoURL(spec
, spec_len
, &parsed_input
);
186 success
= url_canon::CanonicalizeMailtoURL(spec
, spec_len
, parsed_input
,
187 output
, output_parsed
);
190 // "Weird" URLs like data: and javascript:
191 url_parse::ParsePathURL(spec
, spec_len
, &parsed_input
);
192 success
= url_canon::CanonicalizePathURL(spec
, spec_len
, parsed_input
,
193 output
, output_parsed
);
198 template<typename CHAR
>
199 bool DoResolveRelative(const char* base_spec
,
201 const url_parse::Parsed
& base_parsed
,
202 const CHAR
* in_relative
,
203 int in_relative_length
,
204 url_canon::CharsetConverter
* charset_converter
,
205 url_canon::CanonOutput
* output
,
206 url_parse::Parsed
* output_parsed
) {
207 // Remove any whitespace from the middle of the relative URL, possibly
208 // copying to the new buffer.
209 url_canon::RawCanonOutputT
<CHAR
> whitespace_buffer
;
211 const CHAR
* relative
= RemoveURLWhitespace(in_relative
, in_relative_length
,
214 bool base_is_authority_based
= false;
215 bool base_is_hierarchical
= false;
217 base_parsed
.scheme
.is_nonempty()) {
218 int after_scheme
= base_parsed
.scheme
.end() + 1; // Skip past the colon.
219 int num_slashes
= url_parse::CountConsecutiveSlashes(
220 base_spec
, after_scheme
, base_spec_len
);
221 base_is_authority_based
= num_slashes
> 1;
222 base_is_hierarchical
= num_slashes
> 0;
225 bool standard_base_scheme
=
226 base_parsed
.scheme
.is_nonempty() &&
227 DoIsStandard(base_spec
, base_parsed
.scheme
);
230 url_parse::Component relative_component
;
231 if (!url_canon::IsRelativeURL(base_spec
, base_parsed
,
232 relative
, relative_length
,
233 (base_is_hierarchical
|| standard_base_scheme
),
235 &relative_component
)) {
240 // Pretend for a moment that |base_spec| is a standard URL. Normally
241 // non-standard URLs are treated as PathURLs, but if the base has an
242 // authority we would like to preserve it.
243 if (is_relative
&& base_is_authority_based
&& !standard_base_scheme
) {
244 url_parse::Parsed base_parsed_authority
;
245 ParseStandardURL(base_spec
, base_spec_len
, &base_parsed_authority
);
246 if (base_parsed_authority
.host
.is_nonempty()) {
247 bool did_resolve_succeed
=
248 url_canon::ResolveRelativeURL(base_spec
, base_parsed_authority
,
250 relative_component
, charset_converter
,
251 output
, output_parsed
);
252 // The output_parsed is incorrect at this point (because it was built
253 // based on base_parsed_authority instead of base_parsed) and needs to be
255 ParsePathURL(output
->data(), output
->length(), output_parsed
);
256 return did_resolve_succeed
;
258 } else if (is_relative
) {
259 // Relative, resolve and canonicalize.
260 bool file_base_scheme
= base_parsed
.scheme
.is_nonempty() &&
261 DoCompareSchemeComponent(base_spec
, base_parsed
.scheme
, kFileScheme
);
262 return url_canon::ResolveRelativeURL(base_spec
, base_parsed
,
263 file_base_scheme
, relative
,
264 relative_component
, charset_converter
,
265 output
, output_parsed
);
268 // Not relative, canonicalize the input.
269 return DoCanonicalize(relative
, relative_length
, charset_converter
,
270 output
, output_parsed
);
273 template<typename CHAR
>
274 bool DoReplaceComponents(const char* spec
,
276 const url_parse::Parsed
& parsed
,
277 const url_canon::Replacements
<CHAR
>& replacements
,
278 url_canon::CharsetConverter
* charset_converter
,
279 url_canon::CanonOutput
* output
,
280 url_parse::Parsed
* out_parsed
) {
281 // If the scheme is overridden, just do a simple string substitution and
282 // reparse the whole thing. There are lots of edge cases that we really don't
283 // want to deal with. Like what happens if I replace "http://e:8080/foo"
284 // with a file. Does it become "file:///E:/8080/foo" where the port number
285 // becomes part of the path? Parsing that string as a file URL says "yes"
286 // but almost no sane rule for dealing with the components individually would
287 // come up with that.
289 // Why allow these crazy cases at all? Programatically, there is almost no
290 // case for replacing the scheme. The most common case for hitting this is
291 // in JS when building up a URL using the location object. In this case, the
292 // JS code expects the string substitution behavior:
293 // http://www.w3.org/TR/2008/WD-html5-20080610/structured.html#common3
294 if (replacements
.IsSchemeOverridden()) {
295 // Canonicalize the new scheme so it is 8-bit and can be concatenated with
296 // the existing spec.
297 url_canon::RawCanonOutput
<128> scheme_replaced
;
298 url_parse::Component scheme_replaced_parsed
;
299 url_canon::CanonicalizeScheme(
300 replacements
.sources().scheme
,
301 replacements
.components().scheme
,
302 &scheme_replaced
, &scheme_replaced_parsed
);
304 // We can assume that the input is canonicalized, which means it always has
305 // a colon after the scheme (or where the scheme would be).
306 int spec_after_colon
= parsed
.scheme
.is_valid() ? parsed
.scheme
.end() + 1
308 if (spec_len
- spec_after_colon
> 0) {
309 scheme_replaced
.Append(&spec
[spec_after_colon
],
310 spec_len
- spec_after_colon
);
313 // We now need to completely re-parse the resulting string since its meaning
314 // may have changed with the different scheme.
315 url_canon::RawCanonOutput
<128> recanonicalized
;
316 url_parse::Parsed recanonicalized_parsed
;
317 DoCanonicalize(scheme_replaced
.data(), scheme_replaced
.length(),
319 &recanonicalized
, &recanonicalized_parsed
);
321 // Recurse using the version with the scheme already replaced. This will now
322 // use the replacement rules for the new scheme.
324 // Warning: this code assumes that ReplaceComponents will re-check all
325 // components for validity. This is because we can't fail if DoCanonicalize
326 // failed above since theoretically the thing making it fail could be
327 // getting replaced here. If ReplaceComponents didn't re-check everything,
328 // we wouldn't know if something *not* getting replaced is a problem.
329 // If the scheme-specific replacers are made more intelligent so they don't
330 // re-check everything, we should instead recanonicalize the whole thing
331 // after this call to check validity (this assumes replacing the scheme is
332 // much much less common than other types of replacements, like clearing the
334 url_canon::Replacements
<CHAR
> replacements_no_scheme
= replacements
;
335 replacements_no_scheme
.SetScheme(NULL
, url_parse::Component());
336 return DoReplaceComponents(recanonicalized
.data(), recanonicalized
.length(),
337 recanonicalized_parsed
, replacements_no_scheme
,
338 charset_converter
, output
, out_parsed
);
341 // If we get here, then we know the scheme doesn't need to be replaced, so can
342 // just key off the scheme in the spec to know how to do the replacements.
343 if (DoCompareSchemeComponent(spec
, parsed
.scheme
, kFileScheme
)) {
344 return url_canon::ReplaceFileURL(spec
, parsed
, replacements
,
345 charset_converter
, output
, out_parsed
);
347 if (DoCompareSchemeComponent(spec
, parsed
.scheme
, kFileSystemScheme
)) {
348 return url_canon::ReplaceFileSystemURL(spec
, parsed
, replacements
,
349 charset_converter
, output
,
352 if (DoIsStandard(spec
, parsed
.scheme
)) {
353 return url_canon::ReplaceStandardURL(spec
, parsed
, replacements
,
354 charset_converter
, output
, out_parsed
);
356 if (DoCompareSchemeComponent(spec
, parsed
.scheme
, kMailtoScheme
)) {
357 return url_canon::ReplaceMailtoURL(spec
, parsed
, replacements
,
361 // Default is a path URL.
362 return url_canon::ReplacePathURL(spec
, parsed
, replacements
,
369 InitStandardSchemes();
373 if (standard_schemes
) {
374 delete standard_schemes
;
375 standard_schemes
= NULL
;
379 void AddStandardScheme(const char* new_scheme
) {
380 // If this assert triggers, it means you've called AddStandardScheme after
381 // LockStandardSchemes have been called (see the header file for
382 // LockStandardSchemes for more).
384 // This normally means you're trying to set up a new standard scheme too late
385 // in your application's init process. Locate where your app does this
386 // initialization and calls LockStandardScheme, and add your new standard
388 DCHECK(!standard_schemes_locked
) <<
389 "Trying to add a standard scheme after the list has been locked.";
391 size_t scheme_len
= strlen(new_scheme
);
395 // Dulicate the scheme into a new buffer and add it to the list of standard
396 // schemes. This pointer will be leaked on shutdown.
397 char* dup_scheme
= new char[scheme_len
+ 1];
398 memcpy(dup_scheme
, new_scheme
, scheme_len
+ 1);
400 InitStandardSchemes();
401 standard_schemes
->push_back(dup_scheme
);
404 void LockStandardSchemes() {
405 standard_schemes_locked
= true;
408 bool IsStandard(const char* spec
, const url_parse::Component
& scheme
) {
409 return DoIsStandard(spec
, scheme
);
412 bool IsStandard(const base::char16
* spec
, const url_parse::Component
& scheme
) {
413 return DoIsStandard(spec
, scheme
);
416 bool FindAndCompareScheme(const char* str
,
419 url_parse::Component
* found_scheme
) {
420 return DoFindAndCompareScheme(str
, str_len
, compare
, found_scheme
);
423 bool FindAndCompareScheme(const base::char16
* str
,
426 url_parse::Component
* found_scheme
) {
427 return DoFindAndCompareScheme(str
, str_len
, compare
, found_scheme
);
430 bool Canonicalize(const char* spec
,
432 url_canon::CharsetConverter
* charset_converter
,
433 url_canon::CanonOutput
* output
,
434 url_parse::Parsed
* output_parsed
) {
435 return DoCanonicalize(spec
, spec_len
, charset_converter
,
436 output
, output_parsed
);
439 bool Canonicalize(const base::char16
* spec
,
441 url_canon::CharsetConverter
* charset_converter
,
442 url_canon::CanonOutput
* output
,
443 url_parse::Parsed
* output_parsed
) {
444 return DoCanonicalize(spec
, spec_len
, charset_converter
,
445 output
, output_parsed
);
448 bool ResolveRelative(const char* base_spec
,
450 const url_parse::Parsed
& base_parsed
,
451 const char* relative
,
453 url_canon::CharsetConverter
* charset_converter
,
454 url_canon::CanonOutput
* output
,
455 url_parse::Parsed
* output_parsed
) {
456 return DoResolveRelative(base_spec
, base_spec_len
, base_parsed
,
457 relative
, relative_length
,
458 charset_converter
, output
, output_parsed
);
461 bool ResolveRelative(const char* base_spec
,
463 const url_parse::Parsed
& base_parsed
,
464 const base::char16
* relative
,
466 url_canon::CharsetConverter
* charset_converter
,
467 url_canon::CanonOutput
* output
,
468 url_parse::Parsed
* output_parsed
) {
469 return DoResolveRelative(base_spec
, base_spec_len
, base_parsed
,
470 relative
, relative_length
,
471 charset_converter
, output
, output_parsed
);
474 bool ReplaceComponents(const char* spec
,
476 const url_parse::Parsed
& parsed
,
477 const url_canon::Replacements
<char>& replacements
,
478 url_canon::CharsetConverter
* charset_converter
,
479 url_canon::CanonOutput
* output
,
480 url_parse::Parsed
* out_parsed
) {
481 return DoReplaceComponents(spec
, spec_len
, parsed
, replacements
,
482 charset_converter
, output
, out_parsed
);
485 bool ReplaceComponents(
488 const url_parse::Parsed
& parsed
,
489 const url_canon::Replacements
<base::char16
>& replacements
,
490 url_canon::CharsetConverter
* charset_converter
,
491 url_canon::CanonOutput
* output
,
492 url_parse::Parsed
* out_parsed
) {
493 return DoReplaceComponents(spec
, spec_len
, parsed
, replacements
,
494 charset_converter
, output
, out_parsed
);
497 // Front-ends for LowerCaseEqualsASCII.
498 bool LowerCaseEqualsASCII(const char* a_begin
,
501 return DoLowerCaseEqualsASCII(a_begin
, a_end
, b
);
504 bool LowerCaseEqualsASCII(const char* a_begin
,
508 while (a_begin
!= a_end
&& b_begin
!= b_end
&&
509 ToLowerASCII(*a_begin
) == *b_begin
) {
513 return a_begin
== a_end
&& b_begin
== b_end
;
516 bool LowerCaseEqualsASCII(const base::char16
* a_begin
,
517 const base::char16
* a_end
,
519 return DoLowerCaseEqualsASCII(a_begin
, a_end
, b
);
522 void DecodeURLEscapeSequences(const char* input
, int length
,
523 url_canon::CanonOutputW
* output
) {
524 url_canon::RawCanonOutputT
<char> unescaped_chars
;
525 for (int i
= 0; i
< length
; i
++) {
526 if (input
[i
] == '%') {
528 if (url_canon::DecodeEscaped(input
, &i
, length
, &ch
)) {
529 unescaped_chars
.push_back(ch
);
531 // Invalid escape sequence, copy the percent literal.
532 unescaped_chars
.push_back('%');
535 // Regular non-escaped 8-bit character.
536 unescaped_chars
.push_back(input
[i
]);
540 // Convert that 8-bit to UTF-16. It's not clear IE does this at all to
541 // JavaScript URLs, but Firefox and Safari do.
542 for (int i
= 0; i
< unescaped_chars
.length(); i
++) {
543 unsigned char uch
= static_cast<unsigned char>(unescaped_chars
.at(i
));
545 // Non-UTF-8, just append directly
546 output
->push_back(uch
);
548 // next_ch will point to the last character of the decoded
550 int next_character
= i
;
552 if (url_canon::ReadUTFChar(unescaped_chars
.data(), &next_character
,
553 unescaped_chars
.length(), &code_point
)) {
554 // Valid UTF-8 character, convert to UTF-16.
555 url_canon::AppendUTF16Value(code_point
, output
);
558 // If there are any sequences that are not valid UTF-8, we keep
559 // invalid code points and promote to UTF-16. We copy all characters
560 // from the current position to the end of the identified sequence.
561 while (i
< next_character
) {
562 output
->push_back(static_cast<unsigned char>(unescaped_chars
.at(i
)));
565 output
->push_back(static_cast<unsigned char>(unescaped_chars
.at(i
)));
571 void EncodeURIComponent(const char* input
, int length
,
572 url_canon::CanonOutput
* output
) {
573 for (int i
= 0; i
< length
; ++i
) {
574 unsigned char c
= static_cast<unsigned char>(input
[i
]);
575 if (url_canon::IsComponentChar(c
))
576 output
->push_back(c
);
578 AppendEscapedChar(c
, output
);
582 bool CompareSchemeComponent(const char* spec
,
583 const url_parse::Component
& component
,
584 const char* compare_to
) {
585 return DoCompareSchemeComponent(spec
, component
, compare_to
);
588 bool CompareSchemeComponent(const base::char16
* spec
,
589 const url_parse::Component
& component
,
590 const char* compare_to
) {
591 return DoCompareSchemeComponent(spec
, component
, compare_to
);
594 } // namespace url_util