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.
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 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);
38 output->push_back(static_cast<char>(uch));
40 new_component->len = output->length() - new_component->begin;
43 new_component->reset();
47 template <typename CHAR, typename UCHAR>
48 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
52 // Scheme: this will append the colon.
53 bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
54 output, &new_parsed->scheme);
56 // We assume there's no authority for path URLs. Note that hosts should never
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.
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,
79 bool CanonicalizePathURL(const char* spec,
84 return DoCanonicalizePathURL<char, unsigned char>(
85 URLComponentSource<char>(spec), parsed, output, new_parsed);
88 bool CanonicalizePathURL(const base::char16* spec,
93 return DoCanonicalizePathURL<base::char16, base::char16>(
94 URLComponentSource<base::char16>(spec), parsed, output, new_parsed);
97 bool ReplacePathURL(const char* base,
98 const Parsed& base_parsed,
99 const Replacements<char>& replacements,
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);
109 bool ReplacePathURL(const char* base,
110 const Parsed& base_parsed,
111 const Replacements<base::char16>& replacements,
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);