1 // Copyright 2014 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 #include "net/base/filename_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "net/base/escape.h"
15 #include "net/base/filename_util_internal.h"
16 #include "net/base/net_string_util.h"
17 #include "net/http/http_content_disposition.h"
22 // Prefix to prepend to get a file URL.
23 static const base::FilePath::CharType kFileURLPrefix
[] =
24 FILE_PATH_LITERAL("file:///");
26 GURL
FilePathToFileURL(const base::FilePath
& path
) {
27 // Produce a URL like "file:///C:/foo" for a regular file, or
28 // "file://///server/path" for UNC. The URL canonicalizer will fix up the
29 // latter case to be the canonical UNC form: "file://server/path"
30 base::FilePath::StringType
url_string(kFileURLPrefix
);
31 url_string
.append(path
.value());
33 // Now do replacement of some characters. Since we assume the input is a
34 // literal filename, anything the URL parser might consider special should
37 // must be the first substitution since others will introduce percents as the
39 base::ReplaceSubstringsAfterOffset(
40 &url_string
, 0, FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25"));
42 // semicolon is supposed to be some kind of separator according to RFC 2396
43 base::ReplaceSubstringsAfterOffset(
44 &url_string
, 0, FILE_PATH_LITERAL(";"), FILE_PATH_LITERAL("%3B"));
46 base::ReplaceSubstringsAfterOffset(
47 &url_string
, 0, FILE_PATH_LITERAL("#"), FILE_PATH_LITERAL("%23"));
49 base::ReplaceSubstringsAfterOffset(
50 &url_string
, 0, FILE_PATH_LITERAL("?"), FILE_PATH_LITERAL("%3F"));
53 base::ReplaceSubstringsAfterOffset(
54 &url_string
, 0, FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("%5C"));
57 return GURL(url_string
);
60 bool FileURLToFilePath(const GURL
& url
, base::FilePath
* file_path
) {
61 *file_path
= base::FilePath();
62 base::FilePath::StringType
& file_path_str
=
63 const_cast<base::FilePath::StringType
&>(file_path
->value());
64 file_path_str
.clear();
71 std::string host
= url
.host();
73 // URL contains no host, the path is the filename. In this case, the path
74 // will probably be preceeded with a slash, as in "/C:/foo.txt", so we
75 // trim out that here.
77 size_t first_non_slash
= path
.find_first_not_of("/\\");
78 if (first_non_slash
!= std::string::npos
&& first_non_slash
> 0)
79 path
.erase(0, first_non_slash
);
81 // URL contains a host: this means it's UNC. We keep the preceeding slash
85 path
.append(url
.path());
87 std::replace(path
.begin(), path
.end(), '/', '\\');
88 #else // defined(OS_WIN)
89 // Firefox seems to ignore the "host" of a file url if there is one. That is,
90 // file://foo/bar.txt maps to /bar.txt.
91 // TODO(dhg): This should probably take into account UNCs which could
92 // include a hostname other than localhost or blank
93 std::string path
= url
.path();
94 #endif // !defined(OS_WIN)
99 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
100 path
= UnescapeURLComponent(
101 path
, UnescapeRule::SPACES
| UnescapeRule::URL_SPECIAL_CHARS
);
104 if (base::IsStringUTF8(path
)) {
105 file_path_str
.assign(base::UTF8ToWide(path
));
106 // We used to try too hard and see if |path| made up entirely of
107 // the 1st 256 characters in the Unicode was a zero-extended UTF-16.
108 // If so, we converted it to 'Latin-1' and checked if the result was UTF-8.
109 // If the check passed, we converted the result to UTF-8.
110 // Otherwise, we treated the result as the native OS encoding.
111 // However, that led to http://crbug.com/4619 and http://crbug.com/14153
113 // Not UTF-8, assume encoding is native codepage and we're done. We know we
114 // are giving the conversion function a nonempty string, and it may fail if
115 // the given string is not in the current encoding and give us an empty
116 // string back. We detect this and report failure.
117 file_path_str
= base::SysNativeMBToWide(path
);
119 #else // defined(OS_WIN)
120 // Collapse multiple path slashes into a single path slash.
121 std::string new_path
;
124 base::ReplaceSubstringsAfterOffset(&new_path
, 0, "//", "/");
126 } while (new_path
!= path
);
128 file_path_str
.assign(path
);
129 #endif // !defined(OS_WIN)
131 return !file_path_str
.empty();
134 void GenerateSafeFileName(const std::string
& mime_type
,
135 bool ignore_extension
,
136 base::FilePath
* file_path
) {
137 // Make sure we get the right file extension
138 EnsureSafeExtension(mime_type
, ignore_extension
, file_path
);
141 // Prepend "_" to the file name if it's a reserved name
142 base::FilePath::StringType leaf_name
= file_path
->BaseName().value();
143 DCHECK(!leaf_name
.empty());
144 if (IsReservedNameOnWindows(leaf_name
)) {
145 leaf_name
= base::FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name
;
146 *file_path
= file_path
->DirName();
147 if (file_path
->value() == base::FilePath::kCurrentDirectory
) {
148 *file_path
= base::FilePath(leaf_name
);
150 *file_path
= file_path
->Append(leaf_name
);
156 bool IsReservedNameOnWindows(const base::FilePath::StringType
& filename
) {
157 // This list is taken from the MSDN article "Naming a file"
158 // http://msdn2.microsoft.com/en-us/library/aa365247(VS.85).aspx
159 // I also added clock$ because GetSaveFileName seems to consider it as a
160 // reserved name too.
161 static const char* const known_devices
[] = {
162 "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4",
163 "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3",
164 "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "clock$"};
166 std::string filename_lower
=
167 base::StringToLowerASCII(base::WideToUTF8(filename
));
168 #elif defined(OS_POSIX)
169 std::string filename_lower
= base::StringToLowerASCII(filename
);
172 for (size_t i
= 0; i
< arraysize(known_devices
); ++i
) {
174 if (filename_lower
== known_devices
[i
])
176 // Starts with "DEVICE.".
177 if (filename_lower
.find(std::string(known_devices
[i
]) + ".") == 0)
181 static const char* const magic_names
[] = {
182 // These file names are used by the "Customize folder" feature of the
188 for (size_t i
= 0; i
< arraysize(magic_names
); ++i
) {
189 if (filename_lower
== magic_names
[i
])