2003-11-11 07:31:47 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sw=4 et cindent: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2002-01-18 21:08:07 +00:00
|
|
|
/* Unix-specific local file uri parsing */
|
2002-09-13 19:32:45 +00:00
|
|
|
#include "nsURLHelper.h"
|
2001-12-19 00:55:42 +00:00
|
|
|
#include "nsEscape.h"
|
2012-06-06 02:08:30 +00:00
|
|
|
#include "nsIFile.h"
|
2006-03-29 04:53:21 +00:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2017-07-06 12:00:35 +00:00
|
|
|
nsresult
|
2009-10-06 13:43:59 +00:00
|
|
|
net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
|
2001-12-19 00:55:42 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativePath, ePath;
|
2006-03-29 04:53:21 +00:00
|
|
|
nsAutoString path;
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2008-09-07 13:59:48 +00:00
|
|
|
rv = aFile->GetNativePath(nativePath);
|
2002-01-25 23:08:02 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-09-07 13:59:48 +00:00
|
|
|
// Convert to unicode and back to check correct conversion to native charset
|
|
|
|
NS_CopyNativeToUnicode(nativePath, path);
|
|
|
|
NS_CopyUnicodeToNative(path, ePath);
|
|
|
|
|
|
|
|
// Use UTF8 version if conversion was successful
|
|
|
|
if (nativePath == ePath)
|
|
|
|
CopyUTF16toUTF8(path, ePath);
|
|
|
|
else
|
|
|
|
ePath = nativePath;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString escPath;
|
2002-01-25 23:08:02 +00:00
|
|
|
NS_NAMED_LITERAL_CSTRING(prefix, "file://");
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2002-01-25 23:08:02 +00:00
|
|
|
// Escape the path with the directory mask
|
2006-03-29 08:10:50 +00:00
|
|
|
if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
|
2002-01-25 23:08:02 +00:00
|
|
|
escPath.Insert(prefix, 0);
|
|
|
|
else
|
|
|
|
escPath.Assign(prefix + ePath);
|
|
|
|
|
2017-07-06 12:00:35 +00:00
|
|
|
// esc_Directory does not escape the semicolons, so if a filename
|
2002-10-17 03:23:33 +00:00
|
|
|
// contains semicolons we need to manually escape them.
|
2009-02-19 17:23:19 +00:00
|
|
|
// This replacement should be removed in bug #473280
|
2002-10-17 03:23:33 +00:00
|
|
|
escPath.ReplaceSubstring(";", "%3b");
|
2002-03-06 07:48:55 +00:00
|
|
|
result = escPath;
|
|
|
|
return NS_OK;
|
2001-12-19 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:32:45 +00:00
|
|
|
nsresult
|
|
|
|
net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
|
2001-12-19 00:55:42 +00:00
|
|
|
{
|
2003-06-17 13:55:47 +00:00
|
|
|
// NOTE: See also the implementation in nsURLHelperOSX.cpp,
|
|
|
|
// which is based on this.
|
|
|
|
|
2001-12-19 00:55:42 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
|
2003-06-17 13:55:47 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString directory, fileBaseName, fileExtension, path;
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2002-09-13 19:32:45 +00:00
|
|
|
rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
|
2002-03-06 07:48:55 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2017-01-24 19:11:44 +00:00
|
|
|
if (!directory.IsEmpty()) {
|
|
|
|
rv = NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path,
|
|
|
|
mozilla::fallible);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (!fileBaseName.IsEmpty()) {
|
|
|
|
rv = NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path,
|
|
|
|
mozilla::fallible);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2002-03-06 07:48:55 +00:00
|
|
|
if (!fileExtension.IsEmpty()) {
|
2001-12-19 00:55:42 +00:00
|
|
|
path += '.';
|
2017-01-24 19:11:44 +00:00
|
|
|
rv = NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path,
|
|
|
|
mozilla::fallible);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-12-19 00:55:42 +00:00
|
|
|
}
|
2017-07-06 12:00:35 +00:00
|
|
|
|
2002-04-27 05:33:09 +00:00
|
|
|
NS_UnescapeURL(path);
|
2007-07-12 23:04:24 +00:00
|
|
|
if (path.Length() != strlen(path.get()))
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2001-12-19 00:55:42 +00:00
|
|
|
|
2006-03-29 04:53:21 +00:00
|
|
|
if (IsUTF8(path)) {
|
|
|
|
// speed up the start-up where UTF-8 is the native charset
|
|
|
|
// (e.g. on recent Linux distributions)
|
|
|
|
if (NS_IsNativeUTF8())
|
|
|
|
rv = localFile->InitWithNativePath(path);
|
|
|
|
else
|
|
|
|
rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
|
2017-07-06 12:00:35 +00:00
|
|
|
// XXX In rare cases, a valid UTF-8 string can be valid as a native
|
2006-03-29 04:53:21 +00:00
|
|
|
// encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x).
|
|
|
|
// However, the chance is very low that a meaningful word in a legacy
|
|
|
|
// encoding is valid as UTF-8.
|
|
|
|
}
|
2017-07-06 12:00:35 +00:00
|
|
|
else
|
2006-03-29 04:53:21 +00:00
|
|
|
// if path is not in UTF-8, assume it is encoded in the native charset
|
|
|
|
rv = localFile->InitWithNativePath(path);
|
|
|
|
|
2002-08-15 18:38:46 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-03-31 14:03:49 +00:00
|
|
|
localFile.forget(result);
|
2002-08-15 18:38:46 +00:00
|
|
|
return NS_OK;
|
2001-12-19 00:55:42 +00:00
|
|
|
}
|