1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 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/. */
7 /* Unix-specific local file uri parsing */
8 #include "nsURLHelper.h"
11 #include "nsNativeCharsetUtils.h"
12 #include "mozilla/Utf8.h"
14 using mozilla::IsUtf8
;
16 nsresult
net_GetURLSpecFromActualFile(nsIFile
* aFile
, nsACString
& result
) {
18 nsAutoCString nativePath
, ePath
;
21 rv
= aFile
->GetNativePath(nativePath
);
22 if (NS_FAILED(rv
)) return rv
;
24 // Convert to unicode and back to check correct conversion to native charset
25 NS_CopyNativeToUnicode(nativePath
, path
);
26 NS_CopyUnicodeToNative(path
, ePath
);
28 // Use UTF8 version if conversion was successful
29 if (nativePath
== ePath
) {
30 CopyUTF16toUTF8(path
, ePath
);
35 nsAutoCString escPath
;
36 constexpr auto prefix
= "file://"_ns
;
38 // Escape the path with the directory mask
39 if (NS_EscapeURL(ePath
.get(), -1, esc_Directory
+ esc_Forced
, escPath
)) {
40 escPath
.Insert(prefix
, 0);
42 escPath
.Assign(prefix
+ ePath
);
45 // esc_Directory does not escape the semicolons, so if a filename
46 // contains semicolons we need to manually escape them.
47 // This replacement should be removed in bug #473280
48 escPath
.ReplaceSubstring(";", "%3b");
53 nsresult
net_GetFileFromURLSpec(const nsACString
& aURL
, nsIFile
** result
) {
54 // NOTE: See also the implementation in nsURLHelperOSX.cpp,
55 // which is based on this.
59 nsCOMPtr
<nsIFile
> localFile
;
60 rv
= NS_NewNativeLocalFile(""_ns
, true, getter_AddRefs(localFile
));
61 if (NS_FAILED(rv
)) return rv
;
63 nsAutoCString directory
, fileBaseName
, fileExtension
, path
;
65 rv
= net_ParseFileURL(aURL
, directory
, fileBaseName
, fileExtension
);
66 if (NS_FAILED(rv
)) return rv
;
68 if (!directory
.IsEmpty()) {
69 rv
= NS_EscapeURL(directory
, esc_Directory
| esc_AlwaysCopy
, path
,
71 if (NS_FAILED(rv
)) return rv
;
73 if (!fileBaseName
.IsEmpty()) {
74 rv
= NS_EscapeURL(fileBaseName
, esc_FileBaseName
| esc_AlwaysCopy
, path
,
76 if (NS_FAILED(rv
)) return rv
;
78 if (!fileExtension
.IsEmpty()) {
80 rv
= NS_EscapeURL(fileExtension
, esc_FileExtension
| esc_AlwaysCopy
, path
,
82 if (NS_FAILED(rv
)) return rv
;
86 if (path
.Length() != strlen(path
.get())) return NS_ERROR_FILE_INVALID_PATH
;
89 // speed up the start-up where UTF-8 is the native charset
90 // (e.g. on recent Linux distributions)
91 if (NS_IsNativeUTF8()) {
92 rv
= localFile
->InitWithNativePath(path
);
94 rv
= localFile
->InitWithPath(NS_ConvertUTF8toUTF16(path
));
96 // XXX In rare cases, a valid UTF-8 string can be valid as a native
97 // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
98 // However, the chance is very low that a meaningful word in a legacy
99 // encoding is valid as UTF-8.
101 // if path is not in UTF-8, assume it is encoded in the native charset
102 rv
= localFile
->InitWithNativePath(path
);
105 if (NS_FAILED(rv
)) return rv
;
107 localFile
.forget(result
);