Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / url / url_util.cc
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.
4
5 #include "url/url_util.h"
6
7 #include <stddef.h>
8 #include <string.h>
9
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"
17
18 namespace url {
19
20 namespace {
21
22 // Pass this enum through for methods which would like to know if whitespace
23 // removal is necessary.
24 enum WhitespaceRemovalPolicy {
25   REMOVE_WHITESPACE,
26   DO_NOT_REMOVE_WHITESPACE,
27 };
28
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
36     // canonicalization.
37     {kFileScheme, SCHEME_WITH_HOST},
38     {kFtpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
39     {kGopherScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
40     {kWssScheme,
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},
44 };
45
46 const SchemeWithType kReferrerURLSchemes[] = {
47     {kHttpsScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
48     {kHttpScheme, SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION},
49 };
50
51 const char* kSecureSchemes[] = {
52   kHttpsScheme,
53   kAboutScheme,
54   kDataScheme,
55   kWssScheme,
56 };
57
58 const char* kLocalSchemes[] = {
59   kFileScheme,
60 };
61
62 const char* kNoAccessSchemes[] = {
63   kAboutScheme,
64   kJavaScriptScheme,
65   kDataScheme,
66 };
67
68 const char* kCORSEnabledSchemes[] = {
69   kHttpsScheme,
70   kHttpScheme,
71   kDataScheme,
72 };
73
74 const char* kWebStorageSchemes[] = {
75   kHttpsScheme,
76   kHttpScheme,
77   kFileScheme,
78   kFtpScheme,
79   kWssScheme,
80   kWsScheme,
81 };
82
83 const char* kEmptyDocumentSchemes[] = {
84     kAboutScheme,
85 };
86
87 bool initialized = false;
88
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;
94
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;
103
104 // See the LockSchemeRegistries declaration in the header.
105 bool scheme_registries_locked = false;
106
107 // This template converts a given character type to the corresponding
108 // StringPiece type.
109 template<typename CHAR> struct CharToStringPiece {
110 };
111 template<> struct CharToStringPiece<char> {
112   typedef base::StringPiece Piece;
113 };
114 template<> struct CharToStringPiece<base::char16> {
115   typedef base::StringPiece16 Piece;
116 };
117
118 void InitSchemes(std::vector<std::string>** schemes,
119                  const char** initial_schemes,
120                  size_t size) {
121   *schemes = new std::vector<std::string>(size);
122   for (size_t i = 0; i < size; i++) {
123     (*(*schemes))[i] = initial_schemes[i];
124   }
125 }
126
127 void InitSchemesWithType(std::vector<SchemeWithType>** schemes,
128                          const SchemeWithType* initial_schemes,
129                          size_t size) {
130   *schemes = new std::vector<SchemeWithType>(size);
131   for (size_t i = 0; i < size; i++) {
132     (*(*schemes))[i] = initial_schemes[i];
133   }
134 }
135
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),
147       compare_to);
148 }
149
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,
155                    SchemeType* type,
156                    const std::vector<SchemeWithType>& schemes) {
157   if (!scheme.is_nonempty())
158     return false;  // Empty or invalid schemes are non-standard.
159
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;
165       return true;
166     }
167   }
168   return false;
169 }
170
171 template<typename CHAR>
172 bool DoIsStandard(const CHAR* spec, const Component& scheme, SchemeType* type) {
173   Initialize();
174   return DoIsInSchemes(spec, scheme, type, *standard_schemes);
175 }
176
177
178 template<typename CHAR>
179 bool DoFindAndCompareScheme(const CHAR* str,
180                             int str_len,
181                             const char* compare,
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;
186   int spec_len;
187   const CHAR* spec =
188       RemoveURLWhitespace(str, str_len, &whitespace_buffer, &spec_len, nullptr);
189
190   Component our_scheme;
191   if (!ExtractScheme(spec, spec_len, &our_scheme)) {
192     // No scheme.
193     if (found_scheme)
194       *found_scheme = Component();
195     return false;
196   }
197   if (found_scheme)
198     *found_scheme = our_scheme;
199   return DoCompareSchemeComponent(spec, our_scheme, compare);
200 }
201
202 template <typename CHAR>
203 bool DoCanonicalize(const CHAR* spec,
204                     int spec_len,
205                     bool trim_path_end,
206                     WhitespaceRemovalPolicy whitespace_policy,
207                     CharsetConverter* charset_converter,
208                     CanonOutput* output,
209                     Parsed* output_parsed) {
210   output->ReserveSizeIfNeeded(spec_len);
211
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);
218   }
219
220   Parsed parsed_input;
221 #ifdef WIN32
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
227   //
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
231   // doing so.
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);
237   }
238 #endif
239
240   Component scheme;
241   if (!ExtractScheme(spec, spec_len, &scheme))
242     return false;
243
244   // This is the parsed version of the input URL, we have to canonicalize it
245   // before storing it in our object.
246   bool success;
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,
258                                         output_parsed);
259
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);
265
266   } else if (DoCompareSchemeComponent(spec, scheme, url::kMailToScheme)) {
267     // Mailto URLs are treated like standard URLs, with only a scheme, path,
268     // and query.
269     ParseMailtoURL(spec, spec_len, &parsed_input);
270     success = CanonicalizeMailtoURL(spec, spec_len, parsed_input, output,
271                                     output_parsed);
272
273   } else {
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,
277                                   output_parsed);
278   }
279   return success;
280 }
281
282 template<typename CHAR>
283 bool DoResolveRelative(const char* base_spec,
284                        int base_spec_len,
285                        const Parsed& base_parsed,
286                        const CHAR* in_relative,
287                        int in_relative_length,
288                        CharsetConverter* charset_converter,
289                        CanonOutput* output,
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;
294   int relative_length;
295   const CHAR* relative = RemoveURLWhitespace(
296       in_relative, in_relative_length, &whitespace_buffer, &relative_length,
297       &output_parsed->potentially_dangling_markup);
298
299   bool base_is_authority_based = false;
300   bool base_is_hierarchical = false;
301   if (base_spec &&
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,
305                                               base_spec_len);
306     base_is_authority_based = num_slashes > 1;
307     base_is_hierarchical = num_slashes > 0;
308   }
309
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);
314
315   bool is_relative;
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)) {
320     // Error resolving.
321     return false;
322   }
323
324   // Don't reserve buffer space here. Instead, reserve in DoCanonicalize and
325   // ReserveRelativeURL, to enable more accurate buffer sizes.
326
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
341       // re-created.
342       DoCanonicalize(temporary_output.data(), temporary_output.length(), true,
343                      REMOVE_WHITESPACE, charset_converter, output,
344                      output_parsed);
345       return did_resolve_succeed;
346     }
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,
353                               output_parsed);
354   }
355
356   // Not relative, canonicalize the input.
357   return DoCanonicalize(relative, relative_length, true,
358                         DO_NOT_REMOVE_WHITESPACE, charset_converter, output,
359                         output_parsed);
360 }
361
362 template<typename CHAR>
363 bool DoReplaceComponents(const char* spec,
364                          int spec_len,
365                          const Parsed& parsed,
366                          const Replacements<CHAR>& replacements,
367                          CharsetConverter* charset_converter,
368                          CanonOutput* output,
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.
377   //
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);
391
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
395                                                     : 1;
396     if (spec_len - spec_after_colon > 0) {
397       scheme_replaced.Append(&spec[spec_after_colon],
398                              spec_len - spec_after_colon);
399     }
400
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);
408
409     // Recurse using the version with the scheme already replaced. This will now
410     // use the replacement rules for the new scheme.
411     //
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
421     // ref).
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);
427   }
428
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
432   // be changed.
433   output->ReserveSizeIfNeeded(spec_len);
434
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,
439                           out_parsed);
440   }
441   if (DoCompareSchemeComponent(spec, parsed.scheme, url::kFileSystemScheme)) {
442     return ReplaceFileSystemURL(spec, parsed, replacements, charset_converter,
443                                 output, out_parsed);
444   }
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);
449   }
450   if (DoCompareSchemeComponent(spec, parsed.scheme, url::kMailToScheme)) {
451     return ReplaceMailtoURL(spec, parsed, replacements, output, out_parsed);
452   }
453
454   // Default is a path URL.
455   return ReplacePathURL(spec, parsed, replacements, output, out_parsed);
456 }
457
458 void DoAddScheme(const char* new_scheme, std::vector<std::string>* schemes) {
459   DCHECK(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).
463   //
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.";
469
470   size_t scheme_len = strlen(new_scheme);
471   if (scheme_len == 0)
472     return;
473
474   DCHECK_EQ(base::ToLowerASCII(new_scheme), new_scheme);
475   schemes->push_back(std::string(new_scheme));
476 }
477
478 void DoAddSchemeWithType(const char* new_scheme,
479                          SchemeType type,
480                          std::vector<SchemeWithType>* schemes) {
481   DCHECK(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).
485   //
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.";
491
492   size_t scheme_len = strlen(new_scheme);
493   if (scheme_len == 0)
494     return;
495
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);
502
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);
507 }
508
509 }  // namespace
510
511 void Initialize() {
512   if (initialized)
513     return;
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));
529   initialized = true;
530 }
531
532 void Shutdown() {
533   initialized = false;
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;
552 }
553
554 void AddStandardScheme(const char* new_scheme, SchemeType type) {
555   Initialize();
556   DoAddSchemeWithType(new_scheme, type, standard_schemes);
557 }
558
559 void AddReferrerScheme(const char* new_scheme, SchemeType type) {
560   Initialize();
561   DoAddSchemeWithType(new_scheme, type, referrer_schemes);
562 }
563
564 void AddSecureScheme(const char* new_scheme) {
565   Initialize();
566   DoAddScheme(new_scheme, secure_schemes);
567 }
568
569 const std::vector<std::string>& GetSecureSchemes() {
570   Initialize();
571   return *secure_schemes;
572 }
573
574 void AddLocalScheme(const char* new_scheme) {
575   Initialize();
576   DoAddScheme(new_scheme, local_schemes);
577 }
578
579 const std::vector<std::string>& GetLocalSchemes() {
580   Initialize();
581   return *local_schemes;
582 }
583
584 void AddNoAccessScheme(const char* new_scheme) {
585   Initialize();
586   DoAddScheme(new_scheme, no_access_schemes);
587 }
588
589 const std::vector<std::string>& GetNoAccessSchemes() {
590   Initialize();
591   return *no_access_schemes;
592 }
593
594 void AddCORSEnabledScheme(const char* new_scheme) {
595   Initialize();
596   DoAddScheme(new_scheme, cors_enabled_schemes);
597 }
598
599 const std::vector<std::string>& GetCORSEnabledSchemes() {
600   Initialize();
601   return *cors_enabled_schemes;
602 }
603
604 void AddWebStorageScheme(const char* new_scheme) {
605   Initialize();
606   DoAddScheme(new_scheme, web_storage_schemes);
607 }
608
609 const std::vector<std::string>& GetWebStorageSchemes() {
610   Initialize();
611   return *web_storage_schemes;
612 }
613
614 void AddCSPBypassingScheme(const char* new_scheme) {
615   Initialize();
616   DoAddScheme(new_scheme, csp_bypassing_schemes);
617 }
618
619 const std::vector<std::string>& GetCSPBypassingSchemes() {
620   Initialize();
621   return *csp_bypassing_schemes;
622 }
623
624 void AddEmptyDocumentScheme(const char* new_scheme) {
625   Initialize();
626   DoAddScheme(new_scheme, empty_document_schemes);
627 }
628
629 const std::vector<std::string>& GetEmptyDocumentSchemes() {
630   Initialize();
631   return *empty_document_schemes;
632 }
633
634 void LockSchemeRegistries() {
635   scheme_registries_locked = true;
636 }
637
638 bool IsStandard(const char* spec, const Component& scheme) {
639   SchemeType unused_scheme_type;
640   return DoIsStandard(spec, scheme, &unused_scheme_type);
641 }
642
643 bool GetStandardSchemeType(const char* spec,
644                            const Component& scheme,
645                            SchemeType* type) {
646   return DoIsStandard(spec, scheme, type);
647 }
648
649 bool GetStandardSchemeType(const base::char16* spec,
650                            const Component& scheme,
651                            SchemeType* type) {
652   return DoIsStandard(spec, scheme, type);
653 }
654
655 bool IsStandard(const base::char16* spec, const Component& scheme) {
656   SchemeType unused_scheme_type;
657   return DoIsStandard(spec, scheme, &unused_scheme_type);
658 }
659
660 bool IsReferrerScheme(const char* spec, const Component& scheme) {
661   Initialize();
662   SchemeType unused_scheme_type;
663   return DoIsInSchemes(spec, scheme, &unused_scheme_type, *referrer_schemes);
664 }
665
666 bool FindAndCompareScheme(const char* str,
667                           int str_len,
668                           const char* compare,
669                           Component* found_scheme) {
670   return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
671 }
672
673 bool FindAndCompareScheme(const base::char16* str,
674                           int str_len,
675                           const char* compare,
676                           Component* found_scheme) {
677   return DoFindAndCompareScheme(str, str_len, compare, found_scheme);
678 }
679
680 bool DomainIs(base::StringPiece canonical_host,
681               base::StringPiece canonical_domain) {
682   if (canonical_host.empty() || canonical_domain.empty())
683     return false;
684
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() != '.')
689     --host_len;
690
691   if (host_len < canonical_domain.length())
692     return false;
693
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();
698
699   if (base::StringPiece(host_first_pos, canonical_domain.length()) !=
700       canonical_domain) {
701     return false;
702   }
703
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) != '.') {
710     return false;
711   }
712
713   return true;
714 }
715
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();
722 }
723
724 bool Canonicalize(const char* spec,
725                   int spec_len,
726                   bool trim_path_end,
727                   CharsetConverter* charset_converter,
728                   CanonOutput* output,
729                   Parsed* output_parsed) {
730   return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
731                         charset_converter, output, output_parsed);
732 }
733
734 bool Canonicalize(const base::char16* spec,
735                   int spec_len,
736                   bool trim_path_end,
737                   CharsetConverter* charset_converter,
738                   CanonOutput* output,
739                   Parsed* output_parsed) {
740   return DoCanonicalize(spec, spec_len, trim_path_end, REMOVE_WHITESPACE,
741                         charset_converter, output, output_parsed);
742 }
743
744 bool ResolveRelative(const char* base_spec,
745                      int base_spec_len,
746                      const Parsed& base_parsed,
747                      const char* relative,
748                      int relative_length,
749                      CharsetConverter* charset_converter,
750                      CanonOutput* output,
751                      Parsed* output_parsed) {
752   return DoResolveRelative(base_spec, base_spec_len, base_parsed,
753                            relative, relative_length,
754                            charset_converter, output, output_parsed);
755 }
756
757 bool ResolveRelative(const char* base_spec,
758                      int base_spec_len,
759                      const Parsed& base_parsed,
760                      const base::char16* relative,
761                      int relative_length,
762                      CharsetConverter* charset_converter,
763                      CanonOutput* output,
764                      Parsed* output_parsed) {
765   return DoResolveRelative(base_spec, base_spec_len, base_parsed,
766                            relative, relative_length,
767                            charset_converter, output, output_parsed);
768 }
769
770 bool ReplaceComponents(const char* spec,
771                        int spec_len,
772                        const Parsed& parsed,
773                        const Replacements<char>& replacements,
774                        CharsetConverter* charset_converter,
775                        CanonOutput* output,
776                        Parsed* out_parsed) {
777   return DoReplaceComponents(spec, spec_len, parsed, replacements,
778                              charset_converter, output, out_parsed);
779 }
780
781 bool ReplaceComponents(const char* spec,
782                        int spec_len,
783                        const Parsed& parsed,
784                        const Replacements<base::char16>& replacements,
785                        CharsetConverter* charset_converter,
786                        CanonOutput* output,
787                        Parsed* out_parsed) {
788   return DoReplaceComponents(spec, spec_len, parsed, replacements,
789                              charset_converter, output, out_parsed);
790 }
791
792 DecodeURLResult DecodeURLEscapeSequences(const char* input,
793                                          int length,
794                                          CanonOutputW* output) {
795   RawCanonOutputT<char> unescaped_chars;
796   for (int i = 0; i < length; i++) {
797     if (input[i] == '%') {
798       unsigned char ch;
799       if (DecodeEscaped(input, &i, length, &ch)) {
800         unescaped_chars.push_back(ch);
801       } else {
802         // Invalid escape sequence, copy the percent literal.
803         unescaped_chars.push_back('%');
804       }
805     } else {
806       // Regular non-escaped 8-bit character.
807       unescaped_chars.push_back(input[i]);
808     }
809   }
810
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));
817     if (uch < 0x80) {
818       // Non-UTF-8, just append directly
819       output->push_back(uch);
820     } else {
821       // next_ch will point to the last character of the decoded
822       // character.
823       int next_character = i;
824       unsigned code_point;
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);
829         i = next_character;
830         did_utf8_decode = true;
831       } else {
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)));
837           i++;
838         }
839         output->push_back(static_cast<unsigned char>(unescaped_chars.at(i)));
840         did_isomorphic_decode = true;
841       }
842     }
843   }
844
845   if (did_utf8_decode && did_isomorphic_decode)
846     return DecodeURLResult::kMixed;
847   if (did_isomorphic_decode)
848     return DecodeURLResult::kIsomorphic;
849   if (did_utf8_decode)
850     return DecodeURLResult::kUTF8;
851   return DecodeURLResult::kAsciiOnly;
852 }
853
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);
859     else
860       AppendEscapedChar(c, output);
861   }
862 }
863
864 bool CompareSchemeComponent(const char* spec,
865                             const Component& component,
866                             const char* compare_to) {
867   return DoCompareSchemeComponent(spec, component, compare_to);
868 }
869
870 bool CompareSchemeComponent(const base::char16* spec,
871                             const Component& component,
872                             const char* compare_to) {
873   return DoCompareSchemeComponent(spec, component, compare_to);
874 }
875
876 }  // namespace url