Upload upstream chromium 85.0.4183.84
[platform/framework/web/chromium-efl.git] / url / url_canon_pathurl.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 // Functions for canonicalizing "path" URLs. Not to be confused with the path
6 // of a URL, these are URLs that have no authority section, only a path. For
7 // example, "javascript:" and "data:".
8
9 #include "url/url_canon.h"
10 #include "url/url_canon_internal.h"
11
12 namespace url {
13
14 namespace {
15
16 // Canonicalize the given |component| from |source| into |output| and
17 // |new_component|. If |separator| is non-zero, it is pre-pended to |output|
18 // prior to the canonicalized component; i.e. for the '?' or '#' characters.
19 template <typename CHAR, typename UCHAR>
20 void DoCanonicalizePathComponent(const CHAR* source,
21                                  const Component& component,
22                                  char separator,
23                                  CanonOutput* output,
24                                  Component* new_component) {
25   if (component.is_valid()) {
26     if (separator)
27       output->push_back(separator);
28     // Copy the path using path URL's more lax escaping rules (think for
29     // javascript:). We convert to UTF-8 and escape non-ASCII, but leave all
30     // ASCII characters alone. This helps readability of JavaStript.
31     new_component->begin = output->length();
32     int end = component.end();
33     for (int i = component.begin; i < end; i++) {
34       UCHAR uch = static_cast<UCHAR>(source[i]);
35       if (uch < 0x20 || uch >= 0x80)
36         AppendUTF8EscapedChar(source, &i, end, output);
37       else
38         output->push_back(static_cast<char>(uch));
39     }
40     new_component->len = output->length() - new_component->begin;
41   } else {
42     // Empty part.
43     new_component->reset();
44   }
45 }
46
47 template <typename CHAR, typename UCHAR>
48 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
49                            const Parsed& parsed,
50                            CanonOutput* output,
51                            Parsed* new_parsed) {
52   // Scheme: this will append the colon.
53   bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
54                                     output, &new_parsed->scheme);
55
56   // We assume there's no authority for path URLs. Note that hosts should never
57   // have -1 length.
58   new_parsed->username.reset();
59   new_parsed->password.reset();
60   new_parsed->host.reset();
61   new_parsed->port.reset();
62   // We allow path URLs to have the path, query and fragment components, but we
63   // will canonicalize each of the via the weaker path URL rules.
64   //
65   // Note: parsing the path part should never cause a failure, see
66   // https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state
67   DoCanonicalizePathComponent<CHAR, UCHAR>(source.path, parsed.path, '\0',
68                                            output, &new_parsed->path);
69   DoCanonicalizePathComponent<CHAR, UCHAR>(source.query, parsed.query, '?',
70                                            output, &new_parsed->query);
71   DoCanonicalizePathComponent<CHAR, UCHAR>(source.ref, parsed.ref, '#', output,
72                                            &new_parsed->ref);
73
74   return success;
75 }
76
77 }  // namespace
78
79 bool CanonicalizePathURL(const char* spec,
80                          int spec_len,
81                          const Parsed& parsed,
82                          CanonOutput* output,
83                          Parsed* new_parsed) {
84   return DoCanonicalizePathURL<char, unsigned char>(
85       URLComponentSource<char>(spec), parsed, output, new_parsed);
86 }
87
88 bool CanonicalizePathURL(const base::char16* spec,
89                          int spec_len,
90                          const Parsed& parsed,
91                          CanonOutput* output,
92                          Parsed* new_parsed) {
93   return DoCanonicalizePathURL<base::char16, base::char16>(
94       URLComponentSource<base::char16>(spec), parsed, output, new_parsed);
95 }
96
97 bool ReplacePathURL(const char* base,
98                     const Parsed& base_parsed,
99                     const Replacements<char>& replacements,
100                     CanonOutput* output,
101                     Parsed* new_parsed) {
102   URLComponentSource<char> source(base);
103   Parsed parsed(base_parsed);
104   SetupOverrideComponents(base, replacements, &source, &parsed);
105   return DoCanonicalizePathURL<char, unsigned char>(
106       source, parsed, output, new_parsed);
107 }
108
109 bool ReplacePathURL(const char* base,
110                     const Parsed& base_parsed,
111                     const Replacements<base::char16>& replacements,
112                     CanonOutput* output,
113                     Parsed* new_parsed) {
114   RawCanonOutput<1024> utf8;
115   URLComponentSource<char> source(base);
116   Parsed parsed(base_parsed);
117   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
118   return DoCanonicalizePathURL<char, unsigned char>(
119       source, parsed, output, new_parsed);
120 }
121
122 }  // namespace url