|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim:set ts=4 sw=4 et cindent: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* Unix-specific local file uri parsing */ |
|
8 #include "nsURLHelper.h" |
|
9 #include "nsEscape.h" |
|
10 #include "nsIFile.h" |
|
11 #include "nsNativeCharsetUtils.h" |
|
12 |
|
13 nsresult |
|
14 net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result) |
|
15 { |
|
16 nsresult rv; |
|
17 nsAutoCString nativePath, ePath; |
|
18 nsAutoString path; |
|
19 |
|
20 rv = aFile->GetNativePath(nativePath); |
|
21 if (NS_FAILED(rv)) return rv; |
|
22 |
|
23 // Convert to unicode and back to check correct conversion to native charset |
|
24 NS_CopyNativeToUnicode(nativePath, path); |
|
25 NS_CopyUnicodeToNative(path, ePath); |
|
26 |
|
27 // Use UTF8 version if conversion was successful |
|
28 if (nativePath == ePath) |
|
29 CopyUTF16toUTF8(path, ePath); |
|
30 else |
|
31 ePath = nativePath; |
|
32 |
|
33 nsAutoCString escPath; |
|
34 NS_NAMED_LITERAL_CSTRING(prefix, "file://"); |
|
35 |
|
36 // Escape the path with the directory mask |
|
37 if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath)) |
|
38 escPath.Insert(prefix, 0); |
|
39 else |
|
40 escPath.Assign(prefix + ePath); |
|
41 |
|
42 // esc_Directory does not escape the semicolons, so if a filename |
|
43 // contains semicolons we need to manually escape them. |
|
44 // This replacement should be removed in bug #473280 |
|
45 escPath.ReplaceSubstring(";", "%3b"); |
|
46 result = escPath; |
|
47 return NS_OK; |
|
48 } |
|
49 |
|
50 nsresult |
|
51 net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) |
|
52 { |
|
53 // NOTE: See also the implementation in nsURLHelperOSX.cpp, |
|
54 // which is based on this. |
|
55 |
|
56 nsresult rv; |
|
57 |
|
58 nsCOMPtr<nsIFile> localFile; |
|
59 rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile)); |
|
60 if (NS_FAILED(rv)) |
|
61 return rv; |
|
62 |
|
63 nsAutoCString directory, fileBaseName, fileExtension, path; |
|
64 |
|
65 rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension); |
|
66 if (NS_FAILED(rv)) return rv; |
|
67 |
|
68 if (!directory.IsEmpty()) |
|
69 NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); |
|
70 if (!fileBaseName.IsEmpty()) |
|
71 NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path); |
|
72 if (!fileExtension.IsEmpty()) { |
|
73 path += '.'; |
|
74 NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path); |
|
75 } |
|
76 |
|
77 NS_UnescapeURL(path); |
|
78 if (path.Length() != strlen(path.get())) |
|
79 return NS_ERROR_FILE_INVALID_PATH; |
|
80 |
|
81 if (IsUTF8(path)) { |
|
82 // speed up the start-up where UTF-8 is the native charset |
|
83 // (e.g. on recent Linux distributions) |
|
84 if (NS_IsNativeUTF8()) |
|
85 rv = localFile->InitWithNativePath(path); |
|
86 else |
|
87 rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path)); |
|
88 // XXX In rare cases, a valid UTF-8 string can be valid as a native |
|
89 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x). |
|
90 // However, the chance is very low that a meaningful word in a legacy |
|
91 // encoding is valid as UTF-8. |
|
92 } |
|
93 else |
|
94 // if path is not in UTF-8, assume it is encoded in the native charset |
|
95 rv = localFile->InitWithNativePath(path); |
|
96 |
|
97 if (NS_FAILED(rv)) return rv; |
|
98 |
|
99 NS_ADDREF(*result = localFile); |
|
100 return NS_OK; |
|
101 } |