xref: /aosp_15_r20/external/cronet/url/url_canon_fileurl.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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.
4 
5 // Functions for canonicalizing "file:" URLs.
6 
7 #include <string_view>
8 
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"
14 
15 namespace url {
16 
17 namespace {
18 
IsLocalhost(const char * spec,int begin,int end)19 bool IsLocalhost(const char* spec, int begin, int end) {
20   if (begin > end)
21     return false;
22   return std::string_view(&spec[begin], end - begin) == "localhost";
23 }
24 
IsLocalhost(const char16_t * spec,int begin,int end)25 bool IsLocalhost(const char16_t* spec, int begin, int end) {
26   if (begin > end)
27     return false;
28   return std::u16string_view(&spec[begin], end - begin) == u"localhost";
29 }
30 
31 template <typename CHAR>
DoFindWindowsDriveLetter(const CHAR * spec,int begin,int end)32 int 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 
57 #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).
64 template <typename CHAR>
FileDoDriveSpec(const CHAR * spec,int begin,int end,CanonOutput * output)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)
68     return begin;
69 
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).
72 
73   output->push_back('/');
74 
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'));
78   else
79     output->push_back(static_cast<char>(spec[drive_letter_pos]));
80 
81   // Normalize the character following it to a colon rather than pipe.
82   output->push_back(':');
83   return drive_letter_pos + 2;
84 }
85 
86 #endif  // WIN32
87 
88 template<typename CHAR, typename UCHAR>
DoFileCanonicalizePath(const CHAR * spec,const Component & path,CanonOutput * output,Component * out_path)89 bool DoFileCanonicalizePath(const CHAR* spec,
90                             const Component& path,
91                             CanonOutput* output,
92                             Component* out_path) {
93   // 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()) {
106     // 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).
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('/');
118   }
119 
120   out_path->len = output->length() - out_path->begin;
121   return success;
122 }
123 
124 template<typename CHAR, typename UCHAR>
DoCanonicalizeFileURL(const URLComponentSource<CHAR> & source,const Parsed & parsed,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)125 bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
126                            const Parsed& parsed,
127                            CharsetConverter* query_converter,
128                            CanonOutput* output,
129                            Parsed* new_parsed) {
130   DCHECK(!parsed.has_opaque_path);
131 
132   // Things we don't set in file: URLs.
133   new_parsed->username = Component();
134   new_parsed->password = Component();
135   new_parsed->port = Component();
136 
137   // Scheme (known, so we don't bother running it through the more
138   // complicated scheme canonicalizer).
139   new_parsed->scheme.begin = output->length();
140   output->Append("file://");
141   new_parsed->scheme.len = 4;
142 
143   // 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 
158   // 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.
163   bool success =
164       CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
165   success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
166                                     output, &new_parsed->path);
167 
168   CanonicalizeQuery(source.query, parsed.query, query_converter,
169                     output, &new_parsed->query);
170   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
171 
172   return success;
173 }
174 
175 } // namespace
176 
FindWindowsDriveLetter(const char * spec,int begin,int end)177 int FindWindowsDriveLetter(const char* spec, int begin, int end) {
178   return DoFindWindowsDriveLetter(spec, begin, end);
179 }
180 
FindWindowsDriveLetter(const char16_t * spec,int begin,int end)181 int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
182   return DoFindWindowsDriveLetter(spec, begin, end);
183 }
184 
CanonicalizeFileURL(const char * spec,int spec_len,const Parsed & parsed,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)185 bool CanonicalizeFileURL(const char* spec,
186                          int spec_len,
187                          const Parsed& parsed,
188                          CharsetConverter* query_converter,
189                          CanonOutput* output,
190                          Parsed* new_parsed) {
191   return DoCanonicalizeFileURL<char, unsigned char>(
192       URLComponentSource<char>(spec), parsed, query_converter,
193       output, new_parsed);
194 }
195 
CanonicalizeFileURL(const char16_t * spec,int spec_len,const Parsed & parsed,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)196 bool CanonicalizeFileURL(const char16_t* spec,
197                          int spec_len,
198                          const Parsed& parsed,
199                          CharsetConverter* query_converter,
200                          CanonOutput* output,
201                          Parsed* new_parsed) {
202   return DoCanonicalizeFileURL<char16_t, char16_t>(
203       URLComponentSource<char16_t>(spec), parsed, query_converter, output,
204       new_parsed);
205 }
206 
FileCanonicalizePath(const char * spec,const Component & path,CanonOutput * output,Component * out_path)207 bool FileCanonicalizePath(const char* spec,
208                           const Component& path,
209                           CanonOutput* output,
210                           Component* out_path) {
211   return DoFileCanonicalizePath<char, unsigned char>(spec, path,
212                                                      output, out_path);
213 }
214 
FileCanonicalizePath(const char16_t * spec,const Component & path,CanonOutput * output,Component * out_path)215 bool FileCanonicalizePath(const char16_t* spec,
216                           const Component& path,
217                           CanonOutput* output,
218                           Component* out_path) {
219   return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
220                                                     out_path);
221 }
222 
ReplaceFileURL(const char * base,const Parsed & base_parsed,const Replacements<char> & replacements,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)223 bool ReplaceFileURL(const char* base,
224                     const Parsed& base_parsed,
225                     const Replacements<char>& replacements,
226                     CharsetConverter* query_converter,
227                     CanonOutput* output,
228                     Parsed* new_parsed) {
229   URLComponentSource<char> source(base);
230   Parsed parsed(base_parsed);
231   SetupOverrideComponents(base, replacements, &source, &parsed);
232   return DoCanonicalizeFileURL<char, unsigned char>(
233       source, parsed, query_converter, output, new_parsed);
234 }
235 
ReplaceFileURL(const char * base,const Parsed & base_parsed,const Replacements<char16_t> & replacements,CharsetConverter * query_converter,CanonOutput * output,Parsed * new_parsed)236 bool ReplaceFileURL(const char* base,
237                     const Parsed& base_parsed,
238                     const Replacements<char16_t>& replacements,
239                     CharsetConverter* query_converter,
240                     CanonOutput* output,
241                     Parsed* new_parsed) {
242   RawCanonOutput<1024> utf8;
243   URLComponentSource<char> source(base);
244   Parsed parsed(base_parsed);
245   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
246   return DoCanonicalizeFileURL<char, unsigned char>(
247       source, parsed, query_converter, output, new_parsed);
248 }
249 
250 }  // namespace url
251