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/debug/leak_annotations.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "url/url_canon_internal.h"
14 #include "url/url_constants.h"
15 #include "url/url_file.h"
16 #include "url/url_util_internal.h"
22 // Pass this enum through for methods which would like to know if whitespace
23 // removal is necessary.
24 enum WhitespaceRemovalPolicy {
26 DO_NOT_REMOVE_WHITESPACE,
29 const SchemeWithType kStandardURLSchemes[] = {
30 {kHttpsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
31 {kHttpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
32 // Yes, file URLs can have a hostname, so file URLs should be handled as
33 // "standard". File URLs never have a port as specified by the SchemeType
34 // field. Unlike other SCHEME_WITH_HOST schemes, the 'host' in a file
35 // URL may be empty, a behavior which is special-cased during
37 {kFileScheme, SCHEME_WITH_HOST},
38 {kFtpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
39 {kGopherScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
41 SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION}, // WebSocket secure.
42 {kWsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION}, // WebSocket.
43 {kFileSystemScheme, SCHEME_WITHOUT_AUTHORITY},
46 const SchemeWithType kReferrerURLSchemes[] = {
47 {kHttpsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
48 {kHttpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
51 const char* kSecureSchemes[] = {
58 const char* kLocalSchemes[] = {
62 const char* kNoAccessSchemes[] = {
68 const char* kCORSEnabledSchemes[] = {
74 const char* kWebStorageSchemes[] = {
83 const char* kEmptyDocumentSchemes[] = {
87 bool initialized = false;
89 // Lists of the currently installed standard and referrer schemes. These lists
90 // are lazily initialized by Initialize and are leaked on shutdown to prevent
91 // any destructors from being called that will slow us down or cause problems.
92 std::vector<SchemeWithType>* standard_schemes = nullptr;
93 std::vector<SchemeWithType>* referrer_schemes = nullptr;
95 // Similar to above, initialized by the Init*Schemes methods.
96 std::vector<std::string>* secure_schemes = nullptr;
97 std::vector<std::string>* local_schemes = nullptr;
98 std::vector<std::string>* no_access_schemes = nullptr;
99 std::vector<std::string>* cors_enabled_schemes = nullptr;
100 std::vector<std::string>* web_storage_schemes = nullptr;
101 std::vector<std::string>* csp_bypassing_schemes = nullptr;
102 std::vector<std::string>* empty_document_schemes = nullptr;
104 // See the LockSchemeRegistries declaration in the header.
105 bool scheme_registries_locked = false;
107 // This template converts a given character type to the corresponding
109 template<typename CHAR> struct CharToStringPiece {
111 template<> struct CharToStringPiece<char> {
112 typedef base::StringPiece Piece;
114 template<> struct CharToStringPiece<base::char16> {
115 typedef base::StringPiece16 Piece;
118 void InitSchemes(std::vector<std::string>** schemes,
119 const char** initial_schemes,
121 *schemes = new std::vector<std::string>(size);
122 for (size_t i = 0; i < size; i++) {
123 (*(*schemes))[i] = initial_schemes[i];
127 void InitSchemesWithType(std::vector<SchemeWithType>** schemes,
128 const SchemeWithType* initial_schemes,
130 *schemes = new std::vector<SchemeWithType>(size);
131 for (size_t i = 0; i < size; i++) {
132 (*(*schemes))[i] = initial_schemes[i];
136 // Given a string and a range inside the string, compares it to the given
137 // lower-case |compare_to| buffer.
138 template<typename CHAR>
139 inline bool DoCompareSchemeComponent(const CHAR* spec,
140 const Component& component,
141 const char* compare_to) {
142 if (!component.is_nonempty())
143 return compare_to[0] == 0; // When component is empty, match empty scheme.
144 return base::LowerCaseEqualsASCII(
145 typename CharToStringPiece<CHAR>::Piece(
146 &spec[component.begin], component.len),
150 // Returns true and sets |type| to the SchemeType of the given scheme
151 // identified by |scheme| within |spec| if in |schemes|.
152 template<typename CHAR>
153 bool DoIsInSchemes(const CHAR* spec,
154 const Component& scheme,
156 const std::vector<SchemeWithType>& schemes) {
157 if (!scheme.is_nonempty())
158 return false; // Empty or invalid schemes are non-standard.
160 for (const SchemeWithType& scheme_with_type : schemes) {
161 if (base::LowerCaseEqualsASCII(typename CharToStringPiece<CHAR>::Piece(
162 &spec[scheme.begin], scheme.len),
163 scheme_with_type.scheme)) {
164 *type = scheme_with_type.type;
171 template<typename CHAR>
172 bool DoIsStandard(const CHAR* spec, const Component& scheme, SchemeType* type) {
174 return DoIsInSchemes(spec, scheme, type, *standard_schemes);
178 template<typename CHAR>
179 bool DoFindAndCompareScheme(const CHAR* str,
182 Component* found_scheme) {
183 // Before extracting scheme, canonicalize the URL to remove any whitespace.
184 // This matches the canonicalization done in DoCanonicalize function.
185 RawCanonOutputT<CHAR> whitespace_buffer;
188 RemoveURLWhitespace(str, str_len, &whitespace_buffer, &spec_len, nullptr);
190 Component our_scheme;
191 if (!ExtractScheme(spec, spec_len, &our_scheme)) {
194 *found_scheme = Component();
198 *found_scheme = our_scheme;
199 return DoCompareSchemeComponent(spec, our_scheme, compare);
202 template <typename CHAR>
203 bool DoCanonicalize(const CHAR* spec,
206 WhitespaceRemovalPolicy whitespace_policy,
207 CharsetConverter* charset_converter,
209 Parsed* output_parsed) {
210 output->ReserveSizeIfNeeded(spec_len);
212 // Remove any whitespace from the middle of the relative URL if necessary.
213 // Possibly this will result in copying to the new buffer.
214 RawCanonOutputT<CHAR> whitespace_buffer;
215 if (whitespace_policy == REMOVE_WHITESPACE) {
216 spec = RemoveURLWhitespace(spec, spec_len, &whitespace_buffer, &spec_len,
217 &output_parsed->potentially_dangling_markup);
222 // For Windows, we allow things that look like absolute Windows paths to be
223 // fixed up magically to file URLs. This is done for IE compatibility. For
224 // example, this will change "c:/foo" into a file URL rather than treating
225 // it as a URL with the protocol "c". It also works for UNC ("\\foo\bar.txt").
226 // There is similar logic in url_canon_relative.cc for
228 // For Max & Unix, we don't do this (the equivalent would be "/foo/bar" which
229 // has no meaning as an absolute path name. This is because browsers on Mac
230 // & Unix don't generally do this, so there is no compatibility reason for
232 if (DoesBeginUNCPath(spec, 0, spec_len, false) ||
233 DoesBeginWindowsDriveSpec(spec, 0, spec_len)) {
234 ParseFileURL(spec, spec_len, &parsed_input);
235 return CanonicalizeFileURL(spec, spec_len, parsed_input, charset_converter,
236 output, output_parsed);
241 if (!ExtractScheme(spec, spec_len, &scheme))
244 // This is the parsed version of the input URL, we have to canonicalize it
245 // before storing it in our object.
247 SchemeType scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
248 if (DoCompareSchemeComponent(spec, scheme, url::kFileScheme)) {
249 // File URLs are special.
250 ParseFileURL(spec, spec_len, &parsed_input);
251 success = CanonicalizeFileURL(spec, spec_len, parsed_input,
252 charset_converter, output, output_parsed);
253 } else if (DoCompareSchemeComponent(spec, scheme, url::kFileSystemScheme)) {
254 // Filesystem URLs are special.
255 ParseFileSystemURL(spec, spec_len, &parsed_input);
256 success = CanonicalizeFileSystemURL(spec, spec_len, parsed_input,
257 charset_converter, output,
260 } else if (DoIsStandard(spec, scheme, &scheme_type)) {
261 // All "normal" URLs.
262 ParseStandardURL(spec, spec_len, &parsed_input);
263 success = CanonicalizeStandardURL(spec, spec_len, parsed_input, scheme_type,
264 charset_converter, output, output_parsed);
266 } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) {
267 // Mailto URLs are treated like standard URLs, with only a scheme, path,
269 ParseMailtoURL(spec, spec_len, &parsed_input);
270 success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output,
274 // "Weird" URLs like data: and javascript:.
275 ParsePathURL(spec, spec_len, trim_path_end, &parsed_input);
276 success = CanonicalizePathURL(spec, spec_len, parsed_input, output,
282 template<typename CHAR>
283 bool DoResolveRelative(const char* base_spec,
285 const Parsed& base_parsed,
286 const CHAR* in_relative,
287 int in_relative_length,
288 CharsetConverter* charset_converter,
290 Parsed* output_parsed) {
291 // Remove any whitespace from the middle of the relative URL, possibly
292 // copying to the new buffer.
293 RawCanonOutputT<CHAR> whitespace_buffer;
295 const CHAR* relative = RemoveURLWhitespace(
296 in_relative, in_relative_length, &whitespace_buffer, &relative_length,
297 &output_parsed->potentially_dangling_markup);
299 bool base_is_authority_based = false;
300 bool base_is_hierarchical = false;
302 base_parsed.scheme.is_nonempty()) {
303 int after_scheme = base_parsed.scheme.end() + 1; // Skip past the colon.
304 int num_slashes = CountConsecutiveSlashes(base_spec, after_scheme,
306 base_is_authority_based = num_slashes > 1;
307 base_is_hierarchical = num_slashes > 0;
310 SchemeType unused_scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
311 bool standard_base_scheme =
312 base_parsed.scheme.is_nonempty() &&
313 DoIsStandard(base_spec, base_parsed.scheme, &unused_scheme_type);
316 Component relative_component;
317 if (!IsRelativeURL(base_spec, base_parsed, relative, relative_length,
318 (base_is_hierarchical || standard_base_scheme),
319 &is_relative, &relative_component)) {
324 // Don't reserve buffer space here. Instead, reserve in DoCanonicalize and
325 // ReserveRelativeURL, to enable more accurate buffer sizes.
327 // Pretend for a moment that |base_spec| is a standard URL. Normally
328 // non-standard URLs are treated as PathURLs, but if the base has an
329 // authority we would like to preserve it.
330 if (is_relative && base_is_authority_based && !standard_base_scheme) {
331 Parsed base_parsed_authority;
332 ParseStandardURL(base_spec, base_spec_len, &base_parsed_authority);
333 if (base_parsed_authority.host.is_nonempty()) {
334 RawCanonOutputT<char> temporary_output;
335 bool did_resolve_succeed =
336 ResolveRelativeURL(base_spec, base_parsed_authority, false, relative,
337 relative_component, charset_converter,
338 &temporary_output, output_parsed);
339 // The output_parsed is incorrect at this point (because it was built
340 // based on base_parsed_authority instead of base_parsed) and needs to be
342 DoCanonicalize(temporary_output.data(), temporary_output.length(), true,
343 REMOVE_WHITESPACE, charset_converter, output,
345 return did_resolve_succeed;
347 } else if (is_relative) {
348 // Relative, resolve and canonicalize.
349 bool file_base_scheme = base_parsed.scheme.is_nonempty() &&
350 DoCompareSchemeComponent(base_spec, base_parsed.scheme, kFileScheme);
351 return ResolveRelativeURL(base_spec, base_parsed, file_base_scheme, relative,
352 relative_component, charset_converter, output,
356 // Not relative, canonicalize the input.
357 return DoCanonicalize(relative, relative_length, true,
358 DO_NOT_REMOVE_WHITESPACE, charset_converter, output,
362 template<typename CHAR>
363 bool DoReplaceComponents(const char* spec,
365 const Parsed& parsed,
366 const Replacements<CHAR>& replacements,
367 CharsetConverter* charset_converter,
369 Parsed* out_parsed) {
370 // If the scheme is overridden, just do a simple string substitution and
371 // re-parse the whole thing. There are lots of edge cases that we really don't
372 // want to deal with. Like what happens if I replace "http://e:8080/foo"
373 // with a file. Does it become "file:///E:/8080/foo" where the port number
374 // becomes part of the path? Parsing that string as a file URL says "yes"
375 // but almost no sane rule for dealing with the components individually would
376 // come up with that.
378 // Why allow these crazy cases at all? Programatically, there is almost no
379 // case for replacing the scheme. The most common case for hitting this is
380 // in JS when building up a URL using the location object. In this case, the
381 // JS code expects the string substitution behavior:
382 // http://www.w3.org/TR/2008/WD-html5-20080610/structured.html#common3
383 if (replacements.IsSchemeOverridden()) {
384 // Canonicalize the new scheme so it is 8-bit and can be concatenated with
385 // the existing spec.
386 RawCanonOutput<128> scheme_replaced;
387 Component scheme_replaced_parsed;
388 CanonicalizeScheme(replacements.sources().scheme,
389 replacements.components().scheme,
390 &scheme_replaced, &scheme_replaced_parsed);
392 // We can assume that the input is canonicalized, which means it always has
393 // a colon after the scheme (or where the scheme would be).
394 int spec_after_colon = parsed.scheme.is_valid() ? parsed.scheme.end() + 1
396 if (spec_len - spec_after_colon > 0) {
397 scheme_replaced.Append(&spec[spec_after_colon],
398 spec_len - spec_after_colon);
401 // We now need to completely re-parse the resulting string since its meaning
402 // may have changed with the different scheme.
403 RawCanonOutput<128> recanonicalized;
404 Parsed recanonicalized_parsed;
405 DoCanonicalize(scheme_replaced.data(), scheme_replaced.length(), true,
406 REMOVE_WHITESPACE, charset_converter, &recanonicalized,
407 &recanonicalized_parsed);
409 // Recurse using the version with the scheme already replaced. This will now
410 // use the replacement rules for the new scheme.
412 // Warning: this code assumes that ReplaceComponents will re-check all
413 // components for validity. This is because we can't fail if DoCanonicalize
414 // failed above since theoretically the thing making it fail could be
415 // getting replaced here. If ReplaceComponents didn't re-check everything,
416 // we wouldn't know if something *not* getting replaced is a problem.
417 // If the scheme-specific replacers are made more intelligent so they don't
418 // re-check everything, we should instead re-canonicalize the whole thing
419 // after this call to check validity (this assumes replacing the scheme is
420 // much much less common than other types of replacements, like clearing the
422 Replacements<CHAR> replacements_no_scheme = replacements;
423 replacements_no_scheme.SetScheme(NULL, Component());
424 return DoReplaceComponents(recanonicalized.data(), recanonicalized.length(),
425 recanonicalized_parsed, replacements_no_scheme,
426 charset_converter, output, out_parsed);
429 // TODO(csharrison): We could be smarter about size to reserve if this is done
430 // in callers below, and the code checks to see which components are being
431 // replaced, and with what length. If this ends up being a hot spot it should
433 output->ReserveSizeIfNeeded(spec_len);
435 // If we get here, then we know the scheme doesn't need to be replaced, so can
436 // just key off the scheme in the spec to know how to do the replacements.
437 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileScheme)) {
438 return ReplaceFileURL(spec, parsed, replacements, charset_converter, output,
441 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileSystemScheme)) {
442 return ReplaceFileSystemURL(spec, parsed, replacements, charset_converter,
445 SchemeType scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
446 if (DoIsStandard(spec, parsed.scheme, &scheme_type)) {
447 return ReplaceStandardURL(spec, parsed, replacements, scheme_type,
448 charset_converter, output, out_parsed);
450 if (DoCompareSchemeComponent(spec, parsed.scheme, url::kMailToScheme)) {
451 return ReplaceMailtoURL(spec, parsed, replacements, output, out_parsed);
454 // Default is a path URL.
455 return ReplacePathURL(spec, parsed, replacements, output, out_parsed);
458 void DoAddScheme(const char* new_scheme, std::vector<std::string>* schemes) {
460 // If this assert triggers, it means you've called Add*Scheme after
461 // LockSchemeRegistries has been called (see the header file for
462 // LockSchemeRegistries for more).
464 // This normally means you're trying to set up a new scheme too late in your
465 // application's init process. Locate where your app does this initialization
466 // and calls LockSchemeRegistries, and add your new scheme there.
467 DCHECK(!scheme_registries_locked)
468 << "Trying to add a scheme after the lists have been locked.";
470 size_t scheme_len = strlen(new_scheme);
474 DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
475 schemes->push_back(std::string(new_scheme));
478 void DoAddSchemeWithType(const char* new_scheme,
480 std::vector<SchemeWithType>* schemes) {
482 // If this assert triggers, it means you've called Add*Scheme after
483 // LockSchemeRegistries has been called (see the header file for
484 // LockSchemeRegistries for more).
486 // This normally means you're trying to set up a new scheme too late in your
487 // application's init process. Locate where your app does this initialization
488 // and calls LockSchemeRegistries, and add your new scheme there.
489 DCHECK(!scheme_registries_locked)
490 << "Trying to add a scheme after the lists have been locked.";
492 size_t scheme_len = strlen(new_scheme);
496 DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
497 // Duplicate the scheme into a new buffer and add it to the list of standard
498 // schemes. This pointer will be leaked on shutdown.
499 char* dup_scheme = new char[scheme_len + 1];
500 ANNOTATE_LEAKING_OBJECT_PTR(dup_scheme);
501 memcpy(dup_scheme, new_scheme, scheme_len + 1);
503 SchemeWithType scheme_with_type;
504 scheme_with_type.scheme = dup_scheme;
505 scheme_with_type.type = type;
506 schemes->push_back(scheme_with_type);
514 InitSchemesWithType(&standard_schemes, kStandardURLSchemes,
515 arraysize(kStandardURLSchemes));
516 InitSchemesWithType(&referrer_schemes, kReferrerURLSchemes,
517 arraysize(kReferrerURLSchemes));
518 InitSchemes(&secure_schemes, kSecureSchemes, arraysize(kSecureSchemes));
519 InitSchemes(&local_schemes, kLocalSchemes, arraysize(kLocalSchemes));
520 InitSchemes(&no_access_schemes, kNoAccessSchemes,
521 arraysize(kNoAccessSchemes));
522 InitSchemes(&cors_enabled_schemes, kCORSEnabledSchemes,
523 arraysize(kCORSEnabledSchemes));
524 InitSchemes(&web_storage_schemes, kWebStorageSchemes,
525 arraysize(kWebStorageSchemes));
526 InitSchemes(&csp_bypassing_schemes, nullptr, 0);
527 InitSchemes(&empty_document_schemes, kEmptyDocumentSchemes,
528 arraysize(kEmptyDocumentSchemes));
534 delete standard_schemes;
535 standard_schemes = nullptr;
536 delete referrer_schemes;
537 referrer_schemes = nullptr;
538 delete secure_schemes;
539 secure_schemes = nullptr;
540 delete local_schemes;
541 local_schemes = nullptr;
542 delete no_access_schemes;
543 no_access_schemes = nullptr;
544 delete cors_enabled_schemes;
545 cors_enabled_schemes = nullptr;
546 delete web_storage_schemes;
547 web_storage_schemes = nullptr;
548 delete csp_bypassing_schemes;
549 csp_bypassing_schemes = nullptr;
550 delete empty_document_schemes;
551 empty_document_schemes = nullptr;
554 void AddStandardScheme(const char* new_scheme, SchemeType type) {
556 DoAddSchemeWithType(new_scheme, type, standard_schemes);
559 void AddReferrerScheme(const char* new_scheme, SchemeType type) {
561 DoAddSchemeWithType(new_scheme, type, referrer_schemes);
564 void AddSecureScheme(const char* new_scheme) {
566 DoAddScheme(new_scheme, secure_schemes);
569 const std::vector<std::string>& GetSecureSchemes() {
571 return *secure_schemes;
574 void AddLocalScheme(const char* new_scheme) {
576 DoAddScheme(new_scheme, local_schemes);
579 const std::vector<std::string>& GetLocalSchemes() {
581 return *local_schemes;
584 void AddNoAccessScheme(const char* new_scheme) {
586 DoAddScheme(new_scheme, no_access_schemes);
589 const std::vector<std::string>& GetNoAccessSchemes() {
591 return *no_access_schemes;
594 void AddCORSEnabledScheme(const char* new_scheme) {
596 DoAddScheme(new_scheme, cors_enabled_schemes);
599 const std::vector<std::string>& GetCORSEnabledSchemes() {
601 return *cors_enabled_schemes;
604 void AddWebStorageScheme(const char* new_scheme) {
606 DoAddScheme(new_scheme, web_storage_schemes);
609 const std::vector<std::string>& GetWebStorageSchemes() {
611 return *web_storage_schemes;
614 void AddCSPBypassingScheme(const char* new_scheme) {
616 DoAddScheme(new_scheme, csp_bypassing_schemes);
619 const std::vector<std::string>& GetCSPBypassingSchemes() {
621 return *csp_bypassing_schemes;
624 void AddEmptyDocumentScheme(const char* new_scheme) {
626 DoAddScheme(new_scheme, empty_document_schemes);
629 const std::vector<std::string>& GetEmptyDocumentSchemes() {
631 return *empty_document_schemes;
634 void LockSchemeRegistries() {
635 scheme_registries_locked = true;
638 bool IsStandard(const char* spec, const Component& scheme) {
639 SchemeType unused_scheme_type;
640 return DoIsStandard(spec, scheme, &unused_scheme_type);
643 bool GetStandardSchemeType(const char* spec,
644 const Component& scheme,
646 return DoIsStandard(spec, scheme, type);
649 bool GetStandardSchemeType(const base::char16* spec,
650 const Component& scheme,
652 return DoIsStandard(spec, scheme, type);
655 bool IsStandard(const base::char16* spec, const Component& scheme) {
656 SchemeType unused_scheme_type;
657 return DoIsStandard(spec, scheme, &unused_scheme_type);
660 bool IsReferrerScheme(const char* spec, const Component& scheme) {
662 SchemeType unused_scheme_type;
663 return DoIsInSchemes(spec, scheme, &unused_scheme_type, *referrer_schemes);
666 bool FindAndCompareScheme(const char* str,
669 Component* found_scheme) {
670 return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
673 bool FindAndCompareScheme(const base::char16* str,
676 Component* found_scheme) {
677 return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
680 bool DomainIs(base::StringPiece canonical_host,
681 base::StringPiece canonical_domain) {
682 if (canonical_host.empty() || canonical_domain.empty())
685 // If the host name ends with a dot but the input domain doesn't, then we
686 // ignore the dot in the host name.
687 size_t host_len = canonical_host.length();
688 if (canonical_host.back() == '.' && canonical_domain.back() != '.')
691 if (host_len < canonical_domain.length())
694 // |host_first_pos| is the start of the compared part of the host name, not
695 // start of the whole host name.
696 const char* host_first_pos =
697 canonical_host.data() + host_len - canonical_domain.length();
699 if (base::StringPiece(host_first_pos, canonical_domain.length()) !=
704 // Make sure there aren't extra characters in host before the compared part;
705 // if the host name is longer than the input domain name, then the character
706 // immediately before the compared part should be a dot. For example,
707 // www.google.com has domain "google.com", but www.iamnotgoogle.com does not.
708 if (canonical_domain[0] != '.' && host_len > canonical_domain.length() &&
709 *(host_first_pos - 1) != '.') {
716 bool HostIsIPAddress(base::StringPiece host) {
717 url::RawCanonOutputT<char, 128> ignored_output;
718 url::CanonHostInfo host_info;
719 url::CanonicalizeIPAddress(host.data(), Component(0, host.length()),
720 &ignored_output, &host_info);
721 return host_info.IsIPAddress();
724 bool Canonicalize(const char* spec,
727 CharsetConverter* charset_converter,
729 Parsed* output_parsed) {
730 return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
731 charset_converter, output, output_parsed);
734 bool Canonicalize(const base::char16* spec,
737 CharsetConverter* charset_converter,
739 Parsed* output_parsed) {
740 return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
741 charset_converter, output, output_parsed);
744 bool ResolveRelative(const char* base_spec,
746 const Parsed& base_parsed,
747 const char* relative,
749 CharsetConverter* charset_converter,
751 Parsed* output_parsed) {
752 return DoResolveRelative(base_spec, base_spec_len, base_parsed,
753 relative, relative_length,
754 charset_converter, output, output_parsed);
757 bool ResolveRelative(const char* base_spec,
759 const Parsed& base_parsed,
760 const base::char16* relative,
762 CharsetConverter* charset_converter,
764 Parsed* output_parsed) {
765 return DoResolveRelative(base_spec, base_spec_len, base_parsed,
766 relative, relative_length,
767 charset_converter, output, output_parsed);
770 bool ReplaceComponents(const char* spec,
772 const Parsed& parsed,
773 const Replacements<char>& replacements,
774 CharsetConverter* charset_converter,
776 Parsed* out_parsed) {
777 return DoReplaceComponents(spec, spec_len, parsed, replacements,
778 charset_converter, output, out_parsed);
781 bool ReplaceComponents(const char* spec,
783 const Parsed& parsed,
784 const Replacements<base::char16>& replacements,
785 CharsetConverter* charset_converter,
787 Parsed* out_parsed) {
788 return DoReplaceComponents(spec, spec_len, parsed, replacements,
789 charset_converter, output, out_parsed);
792 DecodeURLResult DecodeURLEscapeSequences(const char* input,
794 CanonOutputW* output) {
795 RawCanonOutputT<char> unescaped_chars;
796 for (int i = 0; i < length; i++) {
797 if (input[i] == '%') {
799 if (DecodeEscaped(input, &i, length, &ch)) {
800 unescaped_chars.push_back(ch);
802 // Invalid escape sequence, copy the percent literal.
803 unescaped_chars.push_back('%');
806 // Regular non-escaped 8-bit character.
807 unescaped_chars.push_back(input[i]);
811 bool did_utf8_decode = false;
812 bool did_isomorphic_decode = false;
813 // Convert that 8-bit to UTF-16. It's not clear IE does this at all to
814 // JavaScript URLs, but Firefox and Safari do.
815 for (int i = 0; i < unescaped_chars.length(); i++) {
816 unsigned char uch = static_cast<unsigned char>(unescaped_chars.at(i));
818 // Non-UTF-8, just append directly
819 output->push_back(uch);
821 // next_ch will point to the last character of the decoded
823 int next_character = i;
825 if (ReadUTFChar(unescaped_chars.data(), &next_character,
826 unescaped_chars.length(), &code_point)) {
827 // Valid UTF-8 character, convert to UTF-16.
828 AppendUTF16Value(code_point, output);
830 did_utf8_decode = true;
832 // If there are any sequences that are not valid UTF-8, we keep
833 // invalid code points and promote to UTF-16. We copy all characters
834 // from the current position to the end of the identified sequence.
835 while (i < next_character) {
836 output->push_back(static_cast<unsigned char>(unescaped_chars.at(i)));
839 output->push_back(static_cast<unsigned char>(unescaped_chars.at(i)));
840 did_isomorphic_decode = true;
845 if (did_utf8_decode && did_isomorphic_decode)
846 return DecodeURLResult::kMixed;
847 if (did_isomorphic_decode)
848 return DecodeURLResult::kIsomorphic;
850 return DecodeURLResult::kUTF8;
851 return DecodeURLResult::kAsciiOnly;
854 void EncodeURIComponent(const char* input, int length, CanonOutput* output) {
855 for (int i = 0; i < length; ++i) {
856 unsigned char c = static_cast<unsigned char>(input[i]);
857 if (IsComponentChar(c))
858 output->push_back(c);
860 AppendEscapedChar(c, output);
864 bool CompareSchemeComponent(const char* spec,
865 const Component& component,
866 const char* compare_to) {
867 return DoCompareSchemeComponent(spec, component, compare_to);
870 bool CompareSchemeComponent(const base::char16* spec,
871 const Component& component,
872 const char* compare_to) {
873 return DoCompareSchemeComponent(spec, component, compare_to);