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.
5 // Functions for canonicalizing "file:" URLs.
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"
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;
32 if (!DoesBeginWindowsDriveSpec(spec, after_slashes, end))
33 return begin; // Haven't consumed any characters
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('/');
39 // DoesBeginWindowsDriveSpec will ensure that the drive letter is valid
40 // and that it is followed by a colon/pipe.
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'));
46 output->push_back(static_cast<char>(spec[after_slashes]));
48 // Normalize the character following it to a colon rather than pipe.
49 output->push_back(':');
50 return after_slashes + 2;
55 template<typename CHAR, typename UCHAR>
56 bool DoFileCanonicalizePath(const CHAR* spec,
57 const Component& path,
59 Component* out_path) {
60 // Copies and normalizes the "c:" at the beginning, if present.
61 out_path->begin = output->length();
64 after_drive = FileDoDriveSpec(spec, path.begin, path.end(), output);
66 after_drive = path.begin;
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.
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);
80 // No input path, canonicalize to a slash.
81 output->push_back('/');
84 out_path->len = output->length() - out_path->begin;
88 template<typename CHAR, typename UCHAR>
89 bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source,
91 CharsetConverter* query_converter,
94 // Things we don't set in file: URLs.
95 new_parsed->username = Component();
96 new_parsed->password = Component();
97 new_parsed->port = Component();
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;
105 // Append the host. For many file URLs, this will be empty. For UNC, this
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);
117 // Ignore failure for refs since the URL can probably still be loaded.
118 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref);
125 bool CanonicalizeFileURL(const char* spec,
127 const Parsed& parsed,
128 CharsetConverter* query_converter,
130 Parsed* new_parsed) {
131 return DoCanonicalizeFileURL<char, unsigned char>(
132 URLComponentSource<char>(spec), parsed, query_converter,
136 bool CanonicalizeFileURL(const base::char16* spec,
138 const Parsed& parsed,
139 CharsetConverter* query_converter,
141 Parsed* new_parsed) {
142 return DoCanonicalizeFileURL<base::char16, base::char16>(
143 URLComponentSource<base::char16>(spec), parsed, query_converter,
147 bool FileCanonicalizePath(const char* spec,
148 const Component& path,
150 Component* out_path) {
151 return DoFileCanonicalizePath<char, unsigned char>(spec, path,
155 bool FileCanonicalizePath(const base::char16* spec,
156 const Component& path,
158 Component* out_path) {
159 return DoFileCanonicalizePath<base::char16, base::char16>(spec, path,
163 bool ReplaceFileURL(const char* base,
164 const Parsed& base_parsed,
165 const Replacements<char>& replacements,
166 CharsetConverter* query_converter,
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);
176 bool ReplaceFileURL(const char* base,
177 const Parsed& base_parsed,
178 const Replacements<base::char16>& replacements,
179 CharsetConverter* query_converter,
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);