Backed out 2 changesets (bug 1900622) for causing Bug 1908553 and ktlint failure...
[gecko.git] / netwerk / base / nsURLHelperUnix.cpp
blobabb8b0cfeb548e6b667ed27a1f757883774ac97a
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"
9 #include "nsEscape.h"
10 #include "nsIFile.h"
11 #include "nsNativeCharsetUtils.h"
12 #include "mozilla/Utf8.h"
14 using mozilla::IsUtf8;
16 nsresult net_GetURLSpecFromActualFile(nsIFile* aFile, nsACString& result) {
17 nsresult rv;
18 nsAutoCString nativePath, ePath;
19 nsAutoString path;
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);
31 } else {
32 ePath = nativePath;
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);
41 } else {
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");
49 result = escPath;
50 return NS_OK;
53 nsresult net_GetFileFromURLSpec(const nsACString& aURL, nsIFile** result) {
54 // NOTE: See also the implementation in nsURLHelperOSX.cpp,
55 // which is based on this.
57 nsresult rv;
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,
70 mozilla::fallible);
71 if (NS_FAILED(rv)) return rv;
73 if (!fileBaseName.IsEmpty()) {
74 rv = NS_EscapeURL(fileBaseName, esc_FileBaseName | esc_AlwaysCopy, path,
75 mozilla::fallible);
76 if (NS_FAILED(rv)) return rv;
78 if (!fileExtension.IsEmpty()) {
79 path += '.';
80 rv = NS_EscapeURL(fileExtension, esc_FileExtension | esc_AlwaysCopy, path,
81 mozilla::fallible);
82 if (NS_FAILED(rv)) return rv;
85 NS_UnescapeURL(path);
86 if (path.Length() != strlen(path.get())) return NS_ERROR_FILE_INVALID_PATH;
88 if (IsUtf8(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);
93 } else {
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.
100 } else {
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);
108 return NS_OK;