1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/350788890): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 // Functions for canonicalizing "path" URLs. Not to be confused with the path
11 // of a URL, these are URLs that have no authority section, only a path. For
12 // example, "javascript:" and "data:".
13
14 #include "url/url_canon.h"
15 #include "url/url_canon_internal.h"
16
17 namespace url {
18
19 namespace {
20
21 // Canonicalize the given |component| from |source| into |output| and
22 // |new_component|. If |separator| is non-zero, it is pre-pended to |output|
23 // prior to the canonicalized component; i.e. for the '?' or '#' characters.
24 template <typename CHAR, typename UCHAR>
DoCanonicalizePathComponent(const CHAR * source,const Component & component,char separator,CanonOutput * output,Component * new_component)25 void DoCanonicalizePathComponent(const CHAR* source,
26 const Component& component,
27 char separator,
28 CanonOutput* output,
29 Component* new_component) {
30 if (component.is_valid()) {
31 if (separator)
32 output->push_back(separator);
33 // Copy the path using path URL's more lax escaping rules (think for
34 // javascript:). We convert to UTF-8 and escape characters from the
35 // C0 control percent-encode set, but leave all other characters alone.
36 // This helps readability of JavaScript.
37 // https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state
38 // https://url.spec.whatwg.org/#c0-control-percent-encode-set
39 new_component->begin = output->length();
40 size_t end = static_cast<size_t>(component.end());
41 for (size_t i = static_cast<size_t>(component.begin); i < end; i++) {
42 UCHAR uch = static_cast<UCHAR>(source[i]);
43 if (IsInC0ControlPercentEncodeSet(uch)) {
44 AppendUTF8EscapedChar(source, &i, end, output);
45 } else {
46 output->push_back(static_cast<char>(uch));
47 }
48 }
49 new_component->len = output->length() - new_component->begin;
50 } else {
51 // Empty part.
52 new_component->reset();
53 }
54 }
55
56 template <typename CHAR, typename UCHAR>
DoCanonicalizePathURL(const URLComponentSource<CHAR> & source,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)57 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
58 const Parsed& parsed,
59 CanonOutput* output,
60 Parsed* new_parsed) {
61 // Scheme: this will append the colon.
62 bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
63 output, &new_parsed->scheme);
64
65 // We assume there's no authority for path URLs. Note that hosts should never
66 // have -1 length.
67 new_parsed->username.reset();
68 new_parsed->password.reset();
69 new_parsed->host.reset();
70 new_parsed->port.reset();
71
72 // Canonicalize path via the weaker path URL rules.
73 //
74 // Note: parsing the path part should never cause a failure, see
75 // https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state
76 DoCanonicalizePathComponent<CHAR, UCHAR>(source.path, parsed.path, '\0',
77 output, &new_parsed->path);
78
79 // Similar to mailto:, always use the default UTF-8 charset converter for
80 // query.
81 CanonicalizeQuery(source.query, parsed.query, nullptr, output,
82 &new_parsed->query);
83
84 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
85
86 return success;
87 }
88
89 } // namespace
90
CanonicalizePathURL(const char * spec,int spec_len,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)91 bool CanonicalizePathURL(const char* spec,
92 int spec_len,
93 const Parsed& parsed,
94 CanonOutput* output,
95 Parsed* new_parsed) {
96 return DoCanonicalizePathURL<char, unsigned char>(
97 URLComponentSource<char>(spec), parsed, output, new_parsed);
98 }
99
CanonicalizePathURL(const char16_t * spec,int spec_len,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)100 bool CanonicalizePathURL(const char16_t* spec,
101 int spec_len,
102 const Parsed& parsed,
103 CanonOutput* output,
104 Parsed* new_parsed) {
105 return DoCanonicalizePathURL<char16_t, char16_t>(
106 URLComponentSource<char16_t>(spec), parsed, output, new_parsed);
107 }
108
CanonicalizePathURLPath(const char * source,const Component & component,CanonOutput * output,Component * new_component)109 void CanonicalizePathURLPath(const char* source,
110 const Component& component,
111 CanonOutput* output,
112 Component* new_component) {
113 DoCanonicalizePathComponent<char, unsigned char>(source, component, '\0',
114 output, new_component);
115 }
116
CanonicalizePathURLPath(const char16_t * source,const Component & component,CanonOutput * output,Component * new_component)117 void CanonicalizePathURLPath(const char16_t* source,
118 const Component& component,
119 CanonOutput* output,
120 Component* new_component) {
121 DoCanonicalizePathComponent<char16_t, char16_t>(source, component, '\0',
122 output, new_component);
123 }
124
ReplacePathURL(const char * base,const Parsed & base_parsed,const Replacements<char> & replacements,CanonOutput * output,Parsed * new_parsed)125 bool ReplacePathURL(const char* base,
126 const Parsed& base_parsed,
127 const Replacements<char>& replacements,
128 CanonOutput* output,
129 Parsed* new_parsed) {
130 URLComponentSource<char> source(base);
131 Parsed parsed(base_parsed);
132 SetupOverrideComponents(base, replacements, &source, &parsed);
133 return DoCanonicalizePathURL<char, unsigned char>(
134 source, parsed, output, new_parsed);
135 }
136
ReplacePathURL(const char * base,const Parsed & base_parsed,const Replacements<char16_t> & replacements,CanonOutput * output,Parsed * new_parsed)137 bool ReplacePathURL(const char* base,
138 const Parsed& base_parsed,
139 const Replacements<char16_t>& replacements,
140 CanonOutput* output,
141 Parsed* new_parsed) {
142 RawCanonOutput<1024> utf8;
143 URLComponentSource<char> source(base);
144 Parsed parsed(base_parsed);
145 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
146 return DoCanonicalizePathURL<char, unsigned char>(
147 source, parsed, output, new_parsed);
148 }
149
150 } // namespace url
151