[go: nahoru, domu]

blob: a5af5fbefcf6048f25f8940391e170d685870da5 [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2013 The Chromium Authors
tfarina@chromium.org51bcc5d2013-04-24 01:41:372// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
brettw@chromium.orge7bba5f2013-04-10 20:10:524
5// Functions for canonicalizing "file:" URLs.
6
David Benjaminbf75caf2023-09-29 04:39:597#include <string_view>
8
zhongyi23960342016-04-12 23:13:209#include "base/strings/string_util.h"
tfarina@chromium.org318076b2013-04-18 21:19:4510#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"
brettw@chromium.orge7bba5f2013-04-10 20:10:5214
vitalybuka@chromium.org0318f922014-04-22 00:09:2315namespace url {
brettw@chromium.orge7bba5f2013-04-10 20:10:5216
17namespace {
18
Timothy Gu825e85c2021-09-21 05:28:4819bool IsLocalhost(const char* spec, int begin, int end) {
20 if (begin > end)
21 return false;
David Benjaminbf75caf2023-09-29 04:39:5922 return std::string_view(&spec[begin], end - begin) == "localhost";
Timothy Gu825e85c2021-09-21 05:28:4823}
24
25bool IsLocalhost(const char16_t* spec, int begin, int end) {
26 if (begin > end)
27 return false;
David Benjaminbf75caf2023-09-29 04:39:5928 return std::u16string_view(&spec[begin], end - begin) == u"localhost";
Timothy Gu825e85c2021-09-21 05:28:4829}
30
31template <typename CHAR>
32int DoFindWindowsDriveLetter(const CHAR* spec, int begin, int end) {
33 if (begin > end)
34 return -1;
35
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)
42 return -1;
43
44 // Check if the path up to the drive letter candidate can be canonicalized as
45 // "/".
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) != '/') {
51 return -1;
52 }
53
54 return drive_letter_pos;
55}
56
brettw@chromium.orge7bba5f2013-04-10 20:10:5257#ifdef WIN32
58
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).
Timothy Gu825e85c2021-09-21 05:28:4864template <typename CHAR>
65int FileDoDriveSpec(const CHAR* spec, int begin, int end, CanonOutput* output) {
66 int drive_letter_pos = FindWindowsDriveLetter(spec, begin, end);
Vaclav Brozek14aed3f2021-05-06 04:40:2667 if (drive_letter_pos < begin)
Timothy Gu825e85c2021-09-21 05:28:4868 return begin;
brettw@chromium.orge7bba5f2013-04-10 20:10:5269
Timothy Gu825e85c2021-09-21 05:28:4870 // 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).
brettw@chromium.orge7bba5f2013-04-10 20:10:5272
Timothy Gu825e85c2021-09-21 05:28:4873 output->push_back('/');
brettw@chromium.orge7bba5f2013-04-10 20:10:5274
Vaclav Brozek14aed3f2021-05-06 04:40:2675 // 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'));
brettw@chromium.orge7bba5f2013-04-10 20:10:5278 else
Vaclav Brozek14aed3f2021-05-06 04:40:2679 output->push_back(static_cast<char>(spec[drive_letter_pos]));
brettw@chromium.orge7bba5f2013-04-10 20:10:5280
81 // Normalize the character following it to a colon rather than pipe.
82 output->push_back(':');
Vaclav Brozek14aed3f2021-05-06 04:40:2683 return drive_letter_pos + 2;
brettw@chromium.orge7bba5f2013-04-10 20:10:5284}
85
86#endif // WIN32
87
88template<typename CHAR, typename UCHAR>
89bool DoFileCanonicalizePath(const CHAR* spec,
vitalybuka@chromium.org0318f922014-04-22 00:09:2390 const Component& path,
brettw@chromium.orge7bba5f2013-04-10 20:10:5291 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:2392 Component* out_path) {
brettw@chromium.orge7bba5f2013-04-10 20:10:5293 // Copies and normalizes the "c:" at the beginning, if present.
94 out_path->begin = output->length();
95 int after_drive;
96#ifdef WIN32
97 after_drive = FileDoDriveSpec(spec, path.begin, path.end(), output);
98#else
99 after_drive = path.begin;
100#endif
101
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.
104 bool success = true;
105 if (after_drive < path.end()) {
Timothy Gu825e85c2021-09-21 05:28:48106 // Use the regular path canonicalizer to canonicalize the rest of the path
107 // after the drive.
108 //
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).
vitalybuka@chromium.org0318f922014-04-22 00:09:23112 Component sub_path = MakeRange(after_drive, path.end());
113 Component fake_output_path;
brettw@chromium.orge7bba5f2013-04-10 20:10:52114 success = CanonicalizePath(spec, sub_path, output, &fake_output_path);
Vaclav Brozek2dbbfbc2020-05-12 12:51:29115 } else if (after_drive == path.begin) {
116 // No input path and no drive spec, canonicalize to a slash.
brettw@chromium.orge7bba5f2013-04-10 20:10:52117 output->push_back('/');
118 }
119
120 out_path->len = output->length() - out_path->begin;
121 return success;
122}
123
124template<typename CHAR, typename UCHAR>
125bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
vitalybuka@chromium.org0318f922014-04-22 00:09:23126 const Parsed& parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52127 CharsetConverter* query_converter,
128 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23129 Parsed* new_parsed) {
Hayato Ito735a5cbc2023-12-13 02:49:38130 DCHECK(!parsed.has_opaque_path);
131
brettw@chromium.orge7bba5f2013-04-10 20:10:52132 // Things we don't set in file: URLs.
vitalybuka@chromium.org0318f922014-04-22 00:09:23133 new_parsed->username = Component();
134 new_parsed->password = Component();
135 new_parsed->port = Component();
brettw@chromium.orge7bba5f2013-04-10 20:10:52136
137 // Scheme (known, so we don't bother running it through the more
138 // complicated scheme canonicalizer).
139 new_parsed->scheme.begin = output->length();
David Benjamincc4d2b22023-10-02 23:12:04140 output->Append("file://");
brettw@chromium.orge7bba5f2013-04-10 20:10:52141 new_parsed->scheme.len = 4;
142
Timothy Gu825e85c2021-09-21 05:28:48143 // If the host is localhost, and the path starts with a Windows drive letter,
144 // remove the host component. This does the following transformation:
145 // file://localhost/C:/hello.txt -> file:///C:/hello.txt
146 //
147 // Note: we do this on every platform per URL Standard, not just Windows.
148 //
149 // TODO(https://crbug.com/688961): According to the latest URL spec, this
150 // transformation should be done regardless of the path.
151 Component host_range = parsed.host;
152 if (IsLocalhost(source.host, host_range.begin, host_range.end()) &&
153 FindWindowsDriveLetter(source.path, parsed.path.begin,
154 parsed.path.end()) >= parsed.path.begin) {
155 host_range.reset();
156 }
157
brettw@chromium.orge7bba5f2013-04-10 20:10:52158 // Append the host. For many file URLs, this will be empty. For UNC, this
159 // will be present.
160 // TODO(brettw) This doesn't do any checking for host name validity. We
161 // should probably handle validity checking of UNC hosts differently than
162 // for regular IP hosts.
Timothy Gu825e85c2021-09-21 05:28:48163 bool success =
164 CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
brettw@chromium.orge7bba5f2013-04-10 20:10:52165 success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
166 output, &new_parsed->path);
Timothy Gu825e85c2021-09-21 05:28:48167
brettw@chromium.orge7bba5f2013-04-10 20:10:52168 CanonicalizeQuery(source.query, parsed.query, query_converter,
169 output, &new_parsed->query);
brettw@chromium.orge7bba5f2013-04-10 20:10:52170 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
171
172 return success;
173}
174
175} // namespace
176
Timothy Gu825e85c2021-09-21 05:28:48177int FindWindowsDriveLetter(const char* spec, int begin, int end) {
178 return DoFindWindowsDriveLetter(spec, begin, end);
179}
180
181int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
182 return DoFindWindowsDriveLetter(spec, begin, end);
183}
184
brettw@chromium.orge7bba5f2013-04-10 20:10:52185bool CanonicalizeFileURL(const char* spec,
186 int spec_len,
vitalybuka@chromium.org0318f922014-04-22 00:09:23187 const Parsed& parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52188 CharsetConverter* query_converter,
189 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23190 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52191 return DoCanonicalizeFileURL<char, unsigned char>(
192 URLComponentSource<char>(spec), parsed, query_converter,
193 output, new_parsed);
194}
195
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12196bool CanonicalizeFileURL(const char16_t* spec,
brettw@chromium.orge7bba5f2013-04-10 20:10:52197 int spec_len,
vitalybuka@chromium.org0318f922014-04-22 00:09:23198 const Parsed& parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52199 CharsetConverter* query_converter,
200 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23201 Parsed* new_parsed) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12202 return DoCanonicalizeFileURL<char16_t, char16_t>(
203 URLComponentSource<char16_t>(spec), parsed, query_converter, output,
204 new_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52205}
206
207bool FileCanonicalizePath(const char* spec,
vitalybuka@chromium.org0318f922014-04-22 00:09:23208 const Component& path,
brettw@chromium.orge7bba5f2013-04-10 20:10:52209 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23210 Component* out_path) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52211 return DoFileCanonicalizePath<char, unsigned char>(spec, path,
212 output, out_path);
213}
214
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12215bool FileCanonicalizePath(const char16_t* spec,
vitalybuka@chromium.org0318f922014-04-22 00:09:23216 const Component& path,
brettw@chromium.orge7bba5f2013-04-10 20:10:52217 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23218 Component* out_path) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12219 return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
220 out_path);
brettw@chromium.orge7bba5f2013-04-10 20:10:52221}
222
223bool ReplaceFileURL(const char* base,
vitalybuka@chromium.org0318f922014-04-22 00:09:23224 const Parsed& base_parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52225 const Replacements<char>& replacements,
226 CharsetConverter* query_converter,
227 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23228 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52229 URLComponentSource<char> source(base);
vitalybuka@chromium.org0318f922014-04-22 00:09:23230 Parsed parsed(base_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52231 SetupOverrideComponents(base, replacements, &source, &parsed);
232 return DoCanonicalizeFileURL<char, unsigned char>(
233 source, parsed, query_converter, output, new_parsed);
234}
235
236bool ReplaceFileURL(const char* base,
vitalybuka@chromium.org0318f922014-04-22 00:09:23237 const Parsed& base_parsed,
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12238 const Replacements<char16_t>& replacements,
brettw@chromium.orge7bba5f2013-04-10 20:10:52239 CharsetConverter* query_converter,
240 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23241 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52242 RawCanonOutput<1024> utf8;
243 URLComponentSource<char> source(base);
vitalybuka@chromium.org0318f922014-04-22 00:09:23244 Parsed parsed(base_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52245 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
246 return DoCanonicalizeFileURL<char, unsigned char>(
247 source, parsed, query_converter, output, new_parsed);
248}
249
vitalybuka@chromium.org0318f922014-04-22 00:09:23250} // namespace url