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: /* Windows-specific local file uri parsing */ michael@0: #include "nsURLHelper.h" michael@0: #include "nsEscape.h" michael@0: #include "nsIFile.h" michael@0: #include michael@0: michael@0: nsresult michael@0: net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result) michael@0: { michael@0: nsresult rv; michael@0: nsAutoString path; michael@0: michael@0: // construct URL spec from file path michael@0: rv = aFile->GetPath(path); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: // Replace \ with / to convert to an url michael@0: path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu)); michael@0: michael@0: nsAutoCString escPath; michael@0: michael@0: // Windows Desktop paths begin with a drive letter, so need an 'extra' michael@0: // slash at the begining michael@0: // C:\Windows => file:///C:/Windows michael@0: NS_NAMED_LITERAL_CSTRING(prefix, "file:///"); michael@0: michael@0: // Escape the path with the directory mask michael@0: NS_ConvertUTF16toUTF8 ePath(path); 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: 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: nsresult rv; michael@0: michael@0: nsCOMPtr localFile( michael@0: do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv)); michael@0: if (NS_FAILED(rv)) { michael@0: NS_ERROR("Only nsIFile supported right now"); michael@0: return rv; michael@0: } michael@0: michael@0: localFile->SetFollowLinks(true); michael@0: michael@0: const nsACString *specPtr; michael@0: michael@0: nsAutoCString buf; michael@0: if (net_NormalizeFileURL(aURL, buf)) michael@0: specPtr = &buf; michael@0: else michael@0: specPtr = &aURL; michael@0: michael@0: nsAutoCString directory, fileBaseName, fileExtension; michael@0: michael@0: rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsAutoCString path; michael@0: michael@0: if (!directory.IsEmpty()) { michael@0: NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); michael@0: if (path.Length() > 2 && path.CharAt(2) == '|') michael@0: path.SetCharAt(':', 2); michael@0: path.ReplaceChar('/', '\\'); michael@0: } 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: // remove leading '\' michael@0: if (path.CharAt(0) == '\\') michael@0: path.Cut(0, 1); michael@0: michael@0: if (IsUTF8(path)) 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: 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: }