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