Fix for Geolocation webTCT failures
[platform/framework/web/chromium-efl.git] / url / url_canon_fileurl.cc
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
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
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>
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>
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>
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>
125 bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
126                            const Parsed& parsed,
127                            CharsetConverter* query_converter,
128                            CanonOutput* output,
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();
134
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;
140
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
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
156   // 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.
161   bool success =
162       CanonicalizeHost(source.host, host_range, output, &new_parsed->host);
163   success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
164                                     output, &new_parsed->path);
165
166   CanonicalizeQuery(source.query, parsed.query, query_converter,
167                     output, &new_parsed->query);
168   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
169
170   return success;
171 }
172
173 } // namespace
174
175 int FindWindowsDriveLetter(const char* spec, int begin, int end) {
176   return DoFindWindowsDriveLetter(spec, begin, end);
177 }
178
179 int FindWindowsDriveLetter(const char16_t* spec, int begin, int end) {
180   return DoFindWindowsDriveLetter(spec, begin, end);
181 }
182
183 bool CanonicalizeFileURL(const char* spec,
184                          int spec_len,
185                          const Parsed& parsed,
186                          CharsetConverter* query_converter,
187                          CanonOutput* output,
188                          Parsed* new_parsed) {
189   return DoCanonicalizeFileURL<char, unsigned char>(
190       URLComponentSource<char>(spec), parsed, query_converter,
191       output, new_parsed);
192 }
193
194 bool CanonicalizeFileURL(const char16_t* spec,
195                          int spec_len,
196                          const Parsed& parsed,
197                          CharsetConverter* query_converter,
198                          CanonOutput* output,
199                          Parsed* new_parsed) {
200   return DoCanonicalizeFileURL<char16_t, char16_t>(
201       URLComponentSource<char16_t>(spec), parsed, query_converter, output,
202       new_parsed);
203 }
204
205 bool FileCanonicalizePath(const char* spec,
206                           const Component& path,
207                           CanonOutput* output,
208                           Component* out_path) {
209   return DoFileCanonicalizePath<char, unsigned char>(spec, path,
210                                                      output, out_path);
211 }
212
213 bool FileCanonicalizePath(const char16_t* spec,
214                           const Component& path,
215                           CanonOutput* output,
216                           Component* out_path) {
217   return DoFileCanonicalizePath<char16_t, char16_t>(spec, path, output,
218                                                     out_path);
219 }
220
221 bool ReplaceFileURL(const char* base,
222                     const Parsed& base_parsed,
223                     const Replacements<char>& replacements,
224                     CharsetConverter* query_converter,
225                     CanonOutput* output,
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);
232 }
233
234 bool ReplaceFileURL(const char* base,
235                     const Parsed& base_parsed,
236                     const Replacements<char16_t>& replacements,
237                     CharsetConverter* query_converter,
238                     CanonOutput* output,
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);
246 }
247
248 }  // namespace url