[go: nahoru, domu]

blob: 802fe427c12870d1fa24acec08ad4a732225794c [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) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52130 // Things we don't set in file: URLs.
vitalybuka@chromium.org0318f922014-04-22 00:09:23131 new_parsed->username = Component();
132 new_parsed->password = Component();
133 new_parsed->port = Component();
brettw@chromium.orge7bba5f2013-04-10 20:10:52134
135 // Scheme (known, so we don't bother running it through the more
136 // complicated scheme canonicalizer).
137 new_parsed->scheme.begin = output->length();
David Benjamincc4d2b22023-10-02 23:12:04138 output->Append("file://");
brettw@chromium.orge7bba5f2013-04-10 20:10:52139 new_parsed->scheme.len = 4;
140
Timothy Gu825e85c2021-09-21 05:28:48141 // 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
144 //
145 // Note: we do this on every platform per URL Standard, not just Windows.
146 //
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) {
153 host_range.reset();
154 }
155
brettw@chromium.orge7bba5f2013-04-10 20:10:52156 // Append the host. For many file URLs, this will be empty. For UNC, this
157 // will be present.
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.
Timothy Gu825e85c2021-09-21 05:28:48161 bool success =
162 CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
brettw@chromium.orge7bba5f2013-04-10 20:10:52163 success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
164 output, &new_parsed->path);
Timothy Gu825e85c2021-09-21 05:28:48165
brettw@chromium.orge7bba5f2013-04-10 20:10:52166 CanonicalizeQuery(source.query, parsed.query, query_converter,
167 output, &new_parsed->query);
brettw@chromium.orge7bba5f2013-04-10 20:10:52168 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
169
170 return success;
171}
172
173} // namespace
174
Timothy Gu825e85c2021-09-21 05:28:48175int FindWindowsDriveLetter(const char* spec, int begin, int end) {
176 return DoFindWindowsDriveLetter(spec, begin, end);
177}
178
179int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
180 return DoFindWindowsDriveLetter(spec, begin, end);
181}
182
brettw@chromium.orge7bba5f2013-04-10 20:10:52183bool CanonicalizeFileURL(const char* spec,
184 int spec_len,
vitalybuka@chromium.org0318f922014-04-22 00:09:23185 const Parsed& parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52186 CharsetConverter* query_converter,
187 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23188 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52189 return DoCanonicalizeFileURL<char, unsigned char>(
190 URLComponentSource<char>(spec), parsed, query_converter,
191 output, new_parsed);
192}
193
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12194bool CanonicalizeFileURL(const char16_t* spec,
brettw@chromium.orge7bba5f2013-04-10 20:10:52195 int spec_len,
vitalybuka@chromium.org0318f922014-04-22 00:09:23196 const Parsed& parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52197 CharsetConverter* query_converter,
198 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23199 Parsed* new_parsed) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12200 return DoCanonicalizeFileURL<char16_t, char16_t>(
201 URLComponentSource<char16_t>(spec), parsed, query_converter, output,
202 new_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52203}
204
205bool FileCanonicalizePath(const char* spec,
vitalybuka@chromium.org0318f922014-04-22 00:09:23206 const Component& path,
brettw@chromium.orge7bba5f2013-04-10 20:10:52207 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23208 Component* out_path) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52209 return DoFileCanonicalizePath<char, unsigned char>(spec, path,
210 output, out_path);
211}
212
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12213bool FileCanonicalizePath(const char16_t* spec,
vitalybuka@chromium.org0318f922014-04-22 00:09:23214 const Component& path,
brettw@chromium.orge7bba5f2013-04-10 20:10:52215 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23216 Component* out_path) {
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12217 return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
218 out_path);
brettw@chromium.orge7bba5f2013-04-10 20:10:52219}
220
221bool ReplaceFileURL(const char* base,
vitalybuka@chromium.org0318f922014-04-22 00:09:23222 const Parsed& base_parsed,
brettw@chromium.orge7bba5f2013-04-10 20:10:52223 const Replacements<char>& replacements,
224 CharsetConverter* query_converter,
225 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23226 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52227 URLComponentSource<char> source(base);
vitalybuka@chromium.org0318f922014-04-22 00:09:23228 Parsed parsed(base_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52229 SetupOverrideComponents(base, replacements, &source, &parsed);
230 return DoCanonicalizeFileURL<char, unsigned char>(
231 source, parsed, query_converter, output, new_parsed);
232}
233
234bool ReplaceFileURL(const char* base,
vitalybuka@chromium.org0318f922014-04-22 00:09:23235 const Parsed& base_parsed,
Jan Wilken Dörrie5aad5c22021-03-08 21:44:12236 const Replacements<char16_t>& replacements,
brettw@chromium.orge7bba5f2013-04-10 20:10:52237 CharsetConverter* query_converter,
238 CanonOutput* output,
vitalybuka@chromium.org0318f922014-04-22 00:09:23239 Parsed* new_parsed) {
brettw@chromium.orge7bba5f2013-04-10 20:10:52240 RawCanonOutput<1024> utf8;
241 URLComponentSource<char> source(base);
vitalybuka@chromium.org0318f922014-04-22 00:09:23242 Parsed parsed(base_parsed);
brettw@chromium.orge7bba5f2013-04-10 20:10:52243 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
244 return DoCanonicalizeFileURL<char, unsigned char>(
245 source, parsed, query_converter, output, new_parsed);
246}
247
vitalybuka@chromium.org0318f922014-04-22 00:09:23248} // namespace url