Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / base / filename_util.cc
blobc9d5fe4b5f2652bb7ab47c57d3cf80fe7ab95b9e
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"
18 #include "url/gurl.h"
20 namespace net {
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 if (!path.IsAbsolute()) {
32 base::FilePath current_dir;
33 PathService::Get(base::DIR_CURRENT, &current_dir);
34 url_string.append(current_dir.value());
35 url_string.push_back(base::FilePath::kSeparators[0]);
37 url_string.append(path.value());
39 // Now do replacement of some characters. Since we assume the input is a
40 // literal filename, anything the URL parser might consider special should
41 // be escaped here.
43 // must be the first substitution since others will introduce percents as the
44 // escape character
45 ReplaceSubstringsAfterOffset(
46 &url_string, 0, FILE_PATH_LITERAL("%"), FILE_PATH_LITERAL("%25"));
48 // semicolon is supposed to be some kind of separator according to RFC 2396
49 ReplaceSubstringsAfterOffset(
50 &url_string, 0, FILE_PATH_LITERAL(";"), FILE_PATH_LITERAL("%3B"));
52 ReplaceSubstringsAfterOffset(
53 &url_string, 0, FILE_PATH_LITERAL("#"), FILE_PATH_LITERAL("%23"));
55 ReplaceSubstringsAfterOffset(
56 &url_string, 0, FILE_PATH_LITERAL("?"), FILE_PATH_LITERAL("%3F"));
58 #if defined(OS_POSIX)
59 ReplaceSubstringsAfterOffset(
60 &url_string, 0, FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("%5C"));
61 #endif
63 return GURL(url_string);
66 bool FileURLToFilePath(const GURL& url, base::FilePath* file_path) {
67 *file_path = base::FilePath();
68 base::FilePath::StringType& file_path_str =
69 const_cast<base::FilePath::StringType&>(file_path->value());
70 file_path_str.clear();
72 if (!url.is_valid())
73 return false;
75 #if defined(OS_WIN)
76 std::string path;
77 std::string host = url.host();
78 if (host.empty()) {
79 // URL contains no host, the path is the filename. In this case, the path
80 // will probably be preceeded with a slash, as in "/C:/foo.txt", so we
81 // trim out that here.
82 path = url.path();
83 size_t first_non_slash = path.find_first_not_of("/\\");
84 if (first_non_slash != std::string::npos && first_non_slash > 0)
85 path.erase(0, first_non_slash);
86 } else {
87 // URL contains a host: this means it's UNC. We keep the preceeding slash
88 // on the path.
89 path = "\\\\";
90 path.append(host);
91 path.append(url.path());
93 std::replace(path.begin(), path.end(), '/', '\\');
94 #else // defined(OS_WIN)
95 // Firefox seems to ignore the "host" of a file url if there is one. That is,
96 // file://foo/bar.txt maps to /bar.txt.
97 // TODO(dhg): This should probably take into account UNCs which could
98 // include a hostname other than localhost or blank
99 std::string path = url.path();
100 #endif // !defined(OS_WIN)
102 if (path.empty())
103 return false;
105 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
106 path = UnescapeURLComponent(
107 path, UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
109 #if defined(OS_WIN)
110 if (base::IsStringUTF8(path)) {
111 file_path_str.assign(base::UTF8ToWide(path));
112 // We used to try too hard and see if |path| made up entirely of
113 // the 1st 256 characters in the Unicode was a zero-extended UTF-16.
114 // If so, we converted it to 'Latin-1' and checked if the result was UTF-8.
115 // If the check passed, we converted the result to UTF-8.
116 // Otherwise, we treated the result as the native OS encoding.
117 // However, that led to http://crbug.com/4619 and http://crbug.com/14153
118 } else {
119 // Not UTF-8, assume encoding is native codepage and we're done. We know we
120 // are giving the conversion function a nonempty string, and it may fail if
121 // the given string is not in the current encoding and give us an empty
122 // string back. We detect this and report failure.
123 file_path_str = base::SysNativeMBToWide(path);
125 #else // defined(OS_WIN)
126 // Collapse multiple path slashes into a single path slash.
127 std::string new_path;
128 do {
129 new_path = path;
130 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
131 path.swap(new_path);
132 } while (new_path != path);
134 file_path_str.assign(path);
135 #endif // !defined(OS_WIN)
137 return !file_path_str.empty();
140 void GenerateSafeFileName(const std::string& mime_type,
141 bool ignore_extension,
142 base::FilePath* file_path) {
143 // Make sure we get the right file extension
144 EnsureSafeExtension(mime_type, ignore_extension, file_path);
146 #if defined(OS_WIN)
147 // Prepend "_" to the file name if it's a reserved name
148 base::FilePath::StringType leaf_name = file_path->BaseName().value();
149 DCHECK(!leaf_name.empty());
150 if (IsReservedName(leaf_name)) {
151 leaf_name = base::FilePath::StringType(FILE_PATH_LITERAL("_")) + leaf_name;
152 *file_path = file_path->DirName();
153 if (file_path->value() == base::FilePath::kCurrentDirectory) {
154 *file_path = base::FilePath(leaf_name);
155 } else {
156 *file_path = file_path->Append(leaf_name);
159 #endif
162 } // namespace net