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 "file:" URLs.
9 #include "base/strings/string_util.h"
10 #include "url/url_canon.h"
11 #include "url/url_canon_internal.h"
12 #include "url/url_file.h"
13 #include "url/url_parse_internal.h"
19 bool IsLocalhost(const char* spec, int begin, int end) {
22 return std::string_view(&spec[begin], end - begin) == "localhost";
25 bool IsLocalhost(const char16_t* spec, int begin, int end) {
28 return std::u16string_view(&spec[begin], end - begin) == u"localhost";
31 template <typename CHAR>
32 int DoFindWindowsDriveLetter(const CHAR* spec, int begin, int end) {
36 // First guess the beginning of the drive letter.
37 // If there is something that looks like a drive letter in the spec between
38 // begin and end, store its position in drive_letter_pos.
39 int drive_letter_pos =
40 DoesContainWindowsDriveSpecUntil(spec, begin, end, end);
41 if (drive_letter_pos < begin)
44 // Check if the path up to the drive letter candidate can be canonicalized as
46 Component sub_path = MakeRange(begin, drive_letter_pos);
47 RawCanonOutput<1024> output;
48 Component output_path;
49 bool success = CanonicalizePath(spec, sub_path, &output, &output_path);
50 if (!success || output_path.len != 1 || output.at(output_path.begin) != '/') {
54 return drive_letter_pos;
59 // Given a pointer into the spec, this copies and canonicalizes the drive
60 // letter and colon to the output, if one is found. If there is not a drive
61 // spec, it won't do anything. The index of the next character in the input
62 // spec is returned (after the colon when a drive spec is found, the begin
63 // offset if one is not).
64 template <typename CHAR>
65 int FileDoDriveSpec(const CHAR* spec, int begin, int end, CanonOutput* output) {
66 int drive_letter_pos = FindWindowsDriveLetter(spec, begin, end);
67 if (drive_letter_pos < begin)
70 // By now, a valid drive letter is confirmed at position drive_letter_pos,
71 // followed by a valid drive letter separator (a colon or a pipe).
73 output->push_back('/');
75 // Normalize Windows drive letters to uppercase.
76 if (base::IsAsciiLower(spec[drive_letter_pos]))
77 output->push_back(static_cast<char>(spec[drive_letter_pos] - 'a' + 'A'));
79 output->push_back(static_cast<char>(spec[drive_letter_pos]));
81 // Normalize the character following it to a colon rather than pipe.
82 output->push_back(':');
83 return drive_letter_pos + 2;
88 template<typename CHAR, typename UCHAR>
89 bool DoFileCanonicalizePath(const CHAR* spec,
90 const Component& path,
92 Component* out_path) {
93 // Copies and normalizes the "c:" at the beginning, if present.
94 out_path->begin = output->length();
97 after_drive = FileDoDriveSpec(spec, path.begin, path.end(), output);
99 after_drive = path.begin;
102 // Copies the rest of the path, starting from the slash following the
103 // drive colon (if any, Windows only), or the first slash of the path.
105 if (after_drive < path.end()) {
106 // Use the regular path canonicalizer to canonicalize the rest of the path
109 // Give it a fake output component to write into, since we will be
110 // calculating the out_path ourselves (consisting of both the drive and the
111 // path we canonicalize here).
112 Component sub_path = MakeRange(after_drive, path.end());
113 Component fake_output_path;
114 success = CanonicalizePath(spec, sub_path, output, &fake_output_path);
115 } else if (after_drive == path.begin) {
116 // No input path and no drive spec, canonicalize to a slash.
117 output->push_back('/');
120 out_path->len = output->length() - out_path->begin;
124 template<typename CHAR, typename UCHAR>
125 bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
126 const Parsed& parsed,
127 CharsetConverter* query_converter,
129 Parsed* new_parsed) {
130 // Things we don't set in file: URLs.
131 new_parsed->username = Component();
132 new_parsed->password = Component();
133 new_parsed->port = Component();
135 // Scheme (known, so we don't bother running it through the more
136 // complicated scheme canonicalizer).
137 new_parsed->scheme.begin = output->length();
138 output->Append("file://");
139 new_parsed->scheme.len = 4;
141 // If the host is localhost, and the path starts with a Windows drive letter,
142 // remove the host component. This does the following transformation:
143 // file://localhost/C:/hello.txt -> file:///C:/hello.txt
145 // Note: we do this on every platform per URL Standard, not just Windows.
147 // TODO(https://crbug.com/688961): According to the latest URL spec, this
148 // transformation should be done regardless of the path.
149 Component host_range = parsed.host;
150 if (IsLocalhost(source.host, host_range.begin, host_range.end()) &&
151 FindWindowsDriveLetter(source.path, parsed.path.begin,
152 parsed.path.end()) >= parsed.path.begin) {
156 // Append the host. For many file URLs, this will be empty. For UNC, this
158 // TODO(brettw) This doesn't do any checking for host name validity. We
159 // should probably handle validity checking of UNC hosts differently than
160 // for regular IP hosts.
162 CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
163 success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
164 output, &new_parsed->path);
166 CanonicalizeQuery(source.query, parsed.query, query_converter,
167 output, &new_parsed->query);
168 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
175 int FindWindowsDriveLetter(const char* spec, int begin, int end) {
176 return DoFindWindowsDriveLetter(spec, begin, end);
179 int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
180 return DoFindWindowsDriveLetter(spec, begin, end);
183 bool CanonicalizeFileURL(const char* spec,
185 const Parsed& parsed,
186 CharsetConverter* query_converter,
188 Parsed* new_parsed) {
189 return DoCanonicalizeFileURL<char, unsigned char>(
190 URLComponentSource<char>(spec), parsed, query_converter,
194 bool CanonicalizeFileURL(const char16_t* spec,
196 const Parsed& parsed,
197 CharsetConverter* query_converter,
199 Parsed* new_parsed) {
200 return DoCanonicalizeFileURL<char16_t, char16_t>(
201 URLComponentSource<char16_t>(spec), parsed, query_converter, output,
205 bool FileCanonicalizePath(const char* spec,
206 const Component& path,
208 Component* out_path) {
209 return DoFileCanonicalizePath<char, unsigned char>(spec, path,
213 bool FileCanonicalizePath(const char16_t* spec,
214 const Component& path,
216 Component* out_path) {
217 return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
221 bool ReplaceFileURL(const char* base,
222 const Parsed& base_parsed,
223 const Replacements<char>& replacements,
224 CharsetConverter* query_converter,
226 Parsed* new_parsed) {
227 URLComponentSource<char> source(base);
228 Parsed parsed(base_parsed);
229 SetupOverrideComponents(base, replacements, &source, &parsed);
230 return DoCanonicalizeFileURL<char, unsigned char>(
231 source, parsed, query_converter, output, new_parsed);
234 bool ReplaceFileURL(const char* base,
235 const Parsed& base_parsed,
236 const Replacements<char16_t>& replacements,
237 CharsetConverter* query_converter,
239 Parsed* new_parsed) {
240 RawCanonOutput<1024> utf8;
241 URLComponentSource<char> source(base);
242 Parsed parsed(base_parsed);
243 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
244 return DoCanonicalizeFileURL<char, unsigned char>(
245 source, parsed, query_converter, output, new_parsed);