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 // 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>
DoCanonicalizePathComponent(const CHAR * source,const Component & component,char separator,CanonOutput * output,Component * new_component)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 characters from the
30 // C0 control percent-encode set, but leave all other characters alone.
31 // This helps readability of JavaScript.
32 // https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state
33 // https://url.spec.whatwg.org/#c0-control-percent-encode-set
34 new_component->begin = output->length();
35 size_t end = static_cast<size_t>(component.end());
36 for (size_t i = static_cast<size_t>(component.begin); i < end; i++) {
37 UCHAR uch = static_cast<UCHAR>(source[i]);
38 if (uch < 0x20 || uch > 0x7E)
39 AppendUTF8EscapedChar(source, &i, end, output);
40 else
41 output->push_back(static_cast<char>(uch));
42 }
43 new_component->len = output->length() - new_component->begin;
44 } else {
45 // Empty part.
46 new_component->reset();
47 }
48 }
49
50 template <typename CHAR, typename UCHAR>
DoCanonicalizePathURL(const URLComponentSource<CHAR> & source,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)51 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
52 const Parsed& parsed,
53 CanonOutput* output,
54 Parsed* new_parsed) {
55 // Scheme: this will append the colon.
56 bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
57 output, &new_parsed->scheme);
58
59 // We assume there's no authority for path URLs. Note that hosts should never
60 // have -1 length.
61 new_parsed->username.reset();
62 new_parsed->password.reset();
63 new_parsed->host.reset();
64 new_parsed->port.reset();
65
66 // Canonicalize path via the weaker path URL rules.
67 //
68 // Note: parsing the path part should never cause a failure, see
69 // https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state
70 DoCanonicalizePathComponent<CHAR, UCHAR>(source.path, parsed.path, '\0',
71 output, &new_parsed->path);
72
73 // Similar to mailto:, always use the default UTF-8 charset converter for
74 // query.
75 CanonicalizeQuery(source.query, parsed.query, nullptr, output,
76 &new_parsed->query);
77
78 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
79
80 return success;
81 }
82
83 } // namespace
84
CanonicalizePathURL(const char * spec,int spec_len,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)85 bool CanonicalizePathURL(const char* spec,
86 int spec_len,
87 const Parsed& parsed,
88 CanonOutput* output,
89 Parsed* new_parsed) {
90 return DoCanonicalizePathURL<char, unsigned char>(
91 URLComponentSource<char>(spec), parsed, output, new_parsed);
92 }
93
CanonicalizePathURL(const char16_t * spec,int spec_len,const Parsed & parsed,CanonOutput * output,Parsed * new_parsed)94 bool CanonicalizePathURL(const char16_t* spec,
95 int spec_len,
96 const Parsed& parsed,
97 CanonOutput* output,
98 Parsed* new_parsed) {
99 return DoCanonicalizePathURL<char16_t, char16_t>(
100 URLComponentSource<char16_t>(spec), parsed, output, new_parsed);
101 }
102
CanonicalizePathURLPath(const char * source,const Component & component,CanonOutput * output,Component * new_component)103 void CanonicalizePathURLPath(const char* source,
104 const Component& component,
105 CanonOutput* output,
106 Component* new_component) {
107 DoCanonicalizePathComponent<char, unsigned char>(source, component, '\0',
108 output, new_component);
109 }
110
CanonicalizePathURLPath(const char16_t * source,const Component & component,CanonOutput * output,Component * new_component)111 void CanonicalizePathURLPath(const char16_t* source,
112 const Component& component,
113 CanonOutput* output,
114 Component* new_component) {
115 DoCanonicalizePathComponent<char16_t, char16_t>(source, component, '\0',
116 output, new_component);
117 }
118
ReplacePathURL(const char * base,const Parsed & base_parsed,const Replacements<char> & replacements,CanonOutput * output,Parsed * new_parsed)119 bool ReplacePathURL(const char* base,
120 const Parsed& base_parsed,
121 const Replacements<char>& replacements,
122 CanonOutput* output,
123 Parsed* new_parsed) {
124 URLComponentSource<char> source(base);
125 Parsed parsed(base_parsed);
126 SetupOverrideComponents(base, replacements, &source, &parsed);
127 return DoCanonicalizePathURL<char, unsigned char>(
128 source, parsed, output, new_parsed);
129 }
130
ReplacePathURL(const char * base,const Parsed & base_parsed,const Replacements<char16_t> & replacements,CanonOutput * output,Parsed * new_parsed)131 bool ReplacePathURL(const char* base,
132 const Parsed& base_parsed,
133 const Replacements<char16_t>& replacements,
134 CanonOutput* output,
135 Parsed* new_parsed) {
136 RawCanonOutput<1024> utf8;
137 URLComponentSource<char> source(base);
138 Parsed parsed(base_parsed);
139 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
140 return DoCanonicalizePathURL<char, unsigned char>(
141 source, parsed, output, new_parsed);
142 }
143
144 } // namespace url
145