Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / url / url_canon_fileurl.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
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 "base/strings/string_util.h"
8 #include "url/url_canon.h"
9 #include "url/url_canon_internal.h"
10 #include "url/url_file.h"
11 #include "url/url_parse_internal.h"
12
13 namespace url {
14
15 namespace {
16
17 #ifdef WIN32
18
19 // Given a pointer into the spec, this copies and canonicalizes the drive
20 // letter and colon to the output, if one is found. If there is not a drive
21 // spec, it won't do anything. The index of the next character in the input
22 // spec is returned (after the colon when a drive spec is found, the begin
23 // offset if one is not).
24 template<typename CHAR>
25 int FileDoDriveSpec(const CHAR* spec, int begin, int end,
26                     CanonOutput* output) {
27   // The path could be one of several things: /foo/bar, c:/foo/bar, /c:/foo,
28   // (with backslashes instead of slashes as well).
29   int num_slashes = CountConsecutiveSlashes(spec, begin, end);
30   int after_slashes = begin + num_slashes;
31
32   if (!DoesBeginWindowsDriveSpec(spec, after_slashes, end))
33     return begin;  // Haven't consumed any characters
34
35   // A drive spec is the start of a path, so we need to add a slash for the
36   // authority terminator (typically the third slash).
37   output->push_back('/');
38
39   // DoesBeginWindowsDriveSpec will ensure that the drive letter is valid
40   // and that it is followed by a colon/pipe.
41
42   // Normalize Windows drive letters to uppercase
43   if (base::IsAsciiLower(spec[after_slashes]))
44     output->push_back(static_cast<char>(spec[after_slashes] - 'a' + 'A'));
45   else
46     output->push_back(static_cast<char>(spec[after_slashes]));
47
48   // Normalize the character following it to a colon rather than pipe.
49   output->push_back(':');
50   return after_slashes + 2;
51 }
52
53 #endif  // WIN32
54
55 template<typename CHAR, typename UCHAR>
56 bool DoFileCanonicalizePath(const CHAR* spec,
57                             const Component& path,
58                             CanonOutput* output,
59                             Component* out_path) {
60   // Copies and normalizes the "c:" at the beginning, if present.
61   out_path->begin = output->length();
62   int after_drive;
63 #ifdef WIN32
64   after_drive = FileDoDriveSpec(spec, path.begin, path.end(), output);
65 #else
66   after_drive = path.begin;
67 #endif
68
69   // Copies the rest of the path, starting from the slash following the
70   // drive colon (if any, Windows only), or the first slash of the path.
71   bool success = true;
72   if (after_drive < path.end()) {
73     // Use the regular path canonicalizer to canonicalize the rest of the
74     // path. Give it a fake output component to write into. DoCanonicalizeFile
75     // will compute the full path component.
76     Component sub_path = MakeRange(after_drive, path.end());
77     Component fake_output_path;
78     success = CanonicalizePath(spec, sub_path, output, &fake_output_path);
79   } else {
80     // No input path, canonicalize to a slash.
81     output->push_back('/');
82   }
83
84   out_path->len = output->length() - out_path->begin;
85   return success;
86 }
87
88 template<typename CHAR, typename UCHAR>
89 bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
90                            const Parsed& parsed,
91                            CharsetConverter* query_converter,
92                            CanonOutput* output,
93                            Parsed* new_parsed) {
94   // Things we don't set in file: URLs.
95   new_parsed->username = Component();
96   new_parsed->password = Component();
97   new_parsed->port = Component();
98
99   // Scheme (known, so we don't bother running it through the more
100   // complicated scheme canonicalizer).
101   new_parsed->scheme.begin = output->length();
102   output->Append("file://", 7);
103   new_parsed->scheme.len = 4;
104
105   // Append the host. For many file URLs, this will be empty. For UNC, this
106   // will be present.
107   // TODO(brettw) This doesn't do any checking for host name validity. We
108   // should probably handle validity checking of UNC hosts differently than
109   // for regular IP hosts.
110   bool success = CanonicalizeHost(source.host, parsed.host,
111                                   output, &new_parsed->host);
112   success &= DoFileCanonicalizePath<CHAR, UCHAR>(source.path, parsed.path,
113                                     output, &new_parsed->path);
114   CanonicalizeQuery(source.query, parsed.query, query_converter,
115                     output, &new_parsed->query);
116
117   // Ignore failure for refs since the URL can probably still be loaded.
118   CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
119
120   return success;
121 }
122
123 } // namespace
124
125 bool CanonicalizeFileURL(const char* spec,
126                          int spec_len,
127                          const Parsed& parsed,
128                          CharsetConverter* query_converter,
129                          CanonOutput* output,
130                          Parsed* new_parsed) {
131   return DoCanonicalizeFileURL<char, unsigned char>(
132       URLComponentSource<char>(spec), parsed, query_converter,
133       output, new_parsed);
134 }
135
136 bool CanonicalizeFileURL(const base::char16* spec,
137                          int spec_len,
138                          const Parsed& parsed,
139                          CharsetConverter* query_converter,
140                          CanonOutput* output,
141                          Parsed* new_parsed) {
142   return DoCanonicalizeFileURL<base::char16, base::char16>(
143       URLComponentSource<base::char16>(spec), parsed, query_converter,
144       output, new_parsed);
145 }
146
147 bool FileCanonicalizePath(const char* spec,
148                           const Component& path,
149                           CanonOutput* output,
150                           Component* out_path) {
151   return DoFileCanonicalizePath<char, unsigned char>(spec, path,
152                                                      output, out_path);
153 }
154
155 bool FileCanonicalizePath(const base::char16* spec,
156                           const Component& path,
157                           CanonOutput* output,
158                           Component* out_path) {
159   return DoFileCanonicalizePath<base::char16, base::char16>(spec, path,
160                                                             output, out_path);
161 }
162
163 bool ReplaceFileURL(const char* base,
164                     const Parsed& base_parsed,
165                     const Replacements<char>& replacements,
166                     CharsetConverter* query_converter,
167                     CanonOutput* output,
168                     Parsed* new_parsed) {
169   URLComponentSource<char> source(base);
170   Parsed parsed(base_parsed);
171   SetupOverrideComponents(base, replacements, &source, &parsed);
172   return DoCanonicalizeFileURL<char, unsigned char>(
173       source, parsed, query_converter, output, new_parsed);
174 }
175
176 bool ReplaceFileURL(const char* base,
177                     const Parsed& base_parsed,
178                     const Replacements<base::char16>& replacements,
179                     CharsetConverter* query_converter,
180                     CanonOutput* output,
181                     Parsed* new_parsed) {
182   RawCanonOutput<1024> utf8;
183   URLComponentSource<char> source(base);
184   Parsed parsed(base_parsed);
185   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
186   return DoCanonicalizeFileURL<char, unsigned char>(
187       source, parsed, query_converter, output, new_parsed);
188 }
189
190 }  // namespace url