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.
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:".
9 #include "url/url_canon.h"
10 #include "url/url_canon_internal.h"
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,
24 Component* new_component) {
25 if (component.is_valid()) {
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);
41 output->push_back(static_cast<char>(uch));
43 new_component->len = output->length() - new_component->begin;
46 new_component->reset();
50 template <typename CHAR, typename UCHAR>
51 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
55 // Scheme: this will append the colon.
56 bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
57 output, &new_parsed->scheme);
59 // We assume there's no authority for path URLs. Note that hosts should never
61 new_parsed->username.reset();
62 new_parsed->password.reset();
63 new_parsed->host.reset();
64 new_parsed->port.reset();
66 // Canonicalize path via the weaker path URL rules.
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);
73 // Similar to mailto:, always use the default UTF-8 charset converter for
75 CanonicalizeQuery(source.query, parsed.query, nullptr, output,
78 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
85 bool CanonicalizePathURL(const char* spec,
90 return DoCanonicalizePathURL<char, unsigned char>(
91 URLComponentSource<char>(spec), parsed, output, new_parsed);
94 bool CanonicalizePathURL(const char16_t* spec,
99 return DoCanonicalizePathURL<char16_t, char16_t>(
100 URLComponentSource<char16_t>(spec), parsed, output, new_parsed);
103 void CanonicalizePathURLPath(const char* source,
104 const Component& component,
106 Component* new_component) {
107 DoCanonicalizePathComponent<char, unsigned char>(source, component, '\0',
108 output, new_component);
111 void CanonicalizePathURLPath(const char16_t* source,
112 const Component& component,
114 Component* new_component) {
115 DoCanonicalizePathComponent<char16_t, char16_t>(source, component, '\0',
116 output, new_component);
119 bool ReplacePathURL(const char* base,
120 const Parsed& base_parsed,
121 const Replacements<char>& replacements,
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);
131 bool ReplacePathURL(const char* base,
132 const Parsed& base_parsed,
133 const Replacements<char16_t>& replacements,
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);