michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim:set ts=4 sw=4 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Unix-specific local file uri parsing */ michael@0: #include "nsURLHelper.h" michael@0: #include "nsEscape.h" michael@0: #include "nsIFile.h" michael@0: #include "nsNativeCharsetUtils.h" michael@0: michael@0: nsresult michael@0: net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result) michael@0: { michael@0: nsresult rv; michael@0: nsAutoCString nativePath, ePath; michael@0: nsAutoString path; michael@0: michael@0: rv = aFile->GetNativePath(nativePath); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: // Convert to unicode and back to check correct conversion to native charset michael@0: NS_CopyNativeToUnicode(nativePath, path); michael@0: NS_CopyUnicodeToNative(path, ePath); michael@0: michael@0: // Use UTF8 version if conversion was successful michael@0: if (nativePath == ePath) michael@0: CopyUTF16toUTF8(path, ePath); michael@0: else michael@0: ePath = nativePath; michael@0: michael@0: nsAutoCString escPath; michael@0: NS_NAMED_LITERAL_CSTRING(prefix, "file://"); michael@0: michael@0: // Escape the path with the directory mask michael@0: if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath)) michael@0: escPath.Insert(prefix, 0); michael@0: else michael@0: escPath.Assign(prefix + ePath); michael@0: michael@0: // esc_Directory does not escape the semicolons, so if a filename michael@0: // contains semicolons we need to manually escape them. michael@0: // This replacement should be removed in bug #473280 michael@0: escPath.ReplaceSubstring(";", "%3b"); michael@0: result = escPath; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) michael@0: { michael@0: // NOTE: See also the implementation in nsURLHelperOSX.cpp, michael@0: // which is based on this. michael@0: michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr localFile; michael@0: rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsAutoCString directory, fileBaseName, fileExtension, path; michael@0: michael@0: rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: if (!directory.IsEmpty()) michael@0: NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); michael@0: if (!fileBaseName.IsEmpty()) michael@0: NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path); michael@0: if (!fileExtension.IsEmpty()) { michael@0: path += '.'; michael@0: NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path); michael@0: } michael@0: michael@0: NS_UnescapeURL(path); michael@0: if (path.Length() != strlen(path.get())) michael@0: return NS_ERROR_FILE_INVALID_PATH; michael@0: michael@0: if (IsUTF8(path)) { michael@0: // speed up the start-up where UTF-8 is the native charset michael@0: // (e.g. on recent Linux distributions) michael@0: if (NS_IsNativeUTF8()) michael@0: rv = localFile->InitWithNativePath(path); michael@0: else michael@0: rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path)); michael@0: // XXX In rare cases, a valid UTF-8 string can be valid as a native michael@0: // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x). michael@0: // However, the chance is very low that a meaningful word in a legacy michael@0: // encoding is valid as UTF-8. michael@0: } michael@0: else michael@0: // if path is not in UTF-8, assume it is encoded in the native charset michael@0: rv = localFile->InitWithNativePath(path); michael@0: michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: NS_ADDREF(*result = localFile); michael@0: return NS_OK; michael@0: }