2003-11-11 10:31:47 +03: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 15:12:37 +04: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 03:55:42 +03:00
|
|
|
|
2002-01-19 00:08:07 +03:00
|
|
|
/* Unix-specific local file uri parsing */
|
2002-09-13 23:32:45 +04:00
|
|
|
#include "nsURLHelper.h"
|
2001-12-19 03:55:42 +03:00
|
|
|
#include "nsEscape.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2006-03-29 08:53:21 +04:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2009-10-06 17:43:59 +04:00
|
|
|
nsresult
|
|
|
|
net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
|
2001-12-19 03:55:42 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString nativePath, ePath;
|
2006-03-29 08:53:21 +04:00
|
|
|
nsAutoString path;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2008-09-07 17:59:48 +04:00
|
|
|
rv = aFile->GetNativePath(nativePath);
|
2002-01-26 02:08:02 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-09-07 17:59:48 +04: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;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString escPath;
|
2002-01-26 02:08:02 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(prefix, "file://");
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2002-01-26 02:08:02 +03:00
|
|
|
// Escape the path with the directory mask
|
2006-03-29 12:10:50 +04:00
|
|
|
if (NS_EscapeURL(ePath.get(), -1, esc_Directory+esc_Forced, escPath))
|
2002-01-26 02:08:02 +03:00
|
|
|
escPath.Insert(prefix, 0);
|
|
|
|
else
|
|
|
|
escPath.Assign(prefix + ePath);
|
|
|
|
|
2002-10-17 07:23:33 +04:00
|
|
|
// esc_Directory does not escape the semicolons, so if a filename
|
|
|
|
// contains semicolons we need to manually escape them.
|
2009-02-19 20:23:19 +03:00
|
|
|
// This replacement should be removed in bug #473280
|
2002-10-17 07:23:33 +04:00
|
|
|
escPath.ReplaceSubstring(";", "%3b");
|
2002-03-06 10:48:55 +03:00
|
|
|
result = escPath;
|
|
|
|
return NS_OK;
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
|
|
|
|
2002-09-13 23:32:45 +04:00
|
|
|
nsresult
|
|
|
|
net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result)
|
2001-12-19 03:55:42 +03:00
|
|
|
{
|
2003-06-17 17:55:47 +04:00
|
|
|
// NOTE: See also the implementation in nsURLHelperOSX.cpp,
|
|
|
|
// which is based on this.
|
|
|
|
|
2001-12-19 03:55:42 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localFile));
|
2003-06-17 17:55:47 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString directory, fileBaseName, fileExtension, path;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2002-09-13 23:32:45 +04:00
|
|
|
rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension);
|
2002-03-06 10:48:55 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2017-01-24 22:11:44 +03: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 10:48:55 +03:00
|
|
|
if (!fileExtension.IsEmpty()) {
|
2001-12-19 03:55:42 +03:00
|
|
|
path += '.';
|
2017-01-24 22:11:44 +03:00
|
|
|
rv = NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path,
|
|
|
|
mozilla::fallible);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
NS_UnescapeURL(path);
|
2007-07-13 03:04:24 +04:00
|
|
|
if (path.Length() != strlen(path.get()))
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2006-03-29 08:53:21 +04: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));
|
|
|
|
// XXX In rare cases, a valid UTF-8 string can be valid as a native
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// if path is not in UTF-8, assume it is encoded in the native charset
|
|
|
|
rv = localFile->InitWithNativePath(path);
|
|
|
|
|
2002-08-15 22:38:46 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-03-31 17:03:49 +03:00
|
|
|
localFile.forget(result);
|
2002-08-15 22:38:46 +04:00
|
|
|
return NS_OK;
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|