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
|
|
|
|
|
|
|
/* Windows-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"
|
2001-12-19 03:55:42 +03:00
|
|
|
#include <windows.h>
|
|
|
|
|
2002-09-13 23:32:45 +04:00
|
|
|
nsresult
|
2009-10-06 17:43:59 +04:00
|
|
|
net_GetURLSpecFromActualFile(nsIFile *aFile, nsACString &result)
|
2001-12-19 03:55:42 +03:00
|
|
|
{
|
2002-01-26 02:08:02 +03:00
|
|
|
nsresult rv;
|
2006-03-29 08:53:21 +04:00
|
|
|
nsAutoString path;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2006-03-29 08:53:21 +04:00
|
|
|
// construct URL spec from file path
|
|
|
|
rv = aFile->GetPath(path);
|
2002-01-26 02:08:02 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2002-01-26 02:08:02 +03:00
|
|
|
// Replace \ with / to convert to an url
|
2014-01-04 19:02:17 +04:00
|
|
|
path.ReplaceChar(char16_t(0x5Cu), char16_t(0x2Fu));
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString escPath;
|
2008-10-20 21:03:20 +04:00
|
|
|
|
2011-05-07 12:03:16 +04:00
|
|
|
// Windows Desktop paths begin with a drive letter, so need an 'extra'
|
2008-10-20 21:03:20 +04:00
|
|
|
// slash at the begining
|
2011-05-07 12:03:16 +04:00
|
|
|
// C:\Windows => file:///C:/Windows
|
2002-01-29 04:09:37 +03:00
|
|
|
NS_NAMED_LITERAL_CSTRING(prefix, "file:///");
|
2011-05-07 12:03:16 +04:00
|
|
|
|
2002-01-26 02:08:02 +03:00
|
|
|
// Escape the path with the directory mask
|
2006-03-29 08:53:21 +04:00
|
|
|
NS_ConvertUTF16toUTF8 ePath(path);
|
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);
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
// esc_Directory does not escape the semicolons, so if a filename
|
2002-10-17 07:23:33 +04:00
|
|
|
// 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
|
|
|
{
|
|
|
|
nsresult rv;
|
2006-11-06 17:44:28 +03:00
|
|
|
|
2015-03-11 07:08:27 +03:00
|
|
|
if (aURL.Length() > (uint32_t) net_GetURLMaxLength()) {
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
}
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> localFile(
|
2002-08-15 22:38:46 +04:00
|
|
|
do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
2002-01-26 02:08:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-06-06 06:08:30 +04:00
|
|
|
NS_ERROR("Only nsIFile supported right now");
|
2002-01-26 02:08:02 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2004-08-04 21:08:27 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
localFile->SetFollowLinks(true);
|
2006-11-06 17:44:28 +03:00
|
|
|
|
2004-08-04 21:08:27 +04:00
|
|
|
const nsACString *specPtr;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString buf;
|
2004-08-04 21:08:27 +04:00
|
|
|
if (net_NormalizeFileURL(aURL, buf))
|
|
|
|
specPtr = &buf;
|
|
|
|
else
|
|
|
|
specPtr = &aURL;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString directory, fileBaseName, fileExtension;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2004-08-04 21:08:27 +04:00
|
|
|
rv = net_ParseFileURL(*specPtr, directory, fileBaseName, fileExtension);
|
2001-12-19 03:55:42 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2001-12-19 03:55:42 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
if (!directory.IsEmpty()) {
|
|
|
|
NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path);
|
2003-07-09 06:28:07 +04:00
|
|
|
if (path.Length() > 2 && path.CharAt(2) == '|')
|
|
|
|
path.SetCharAt(':', 2);
|
|
|
|
path.ReplaceChar('/', '\\');
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
if (!fileBaseName.IsEmpty())
|
|
|
|
NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path);
|
|
|
|
if (!fileExtension.IsEmpty()) {
|
2001-12-19 03:55:42 +03:00
|
|
|
path += '.';
|
2002-03-06 10:48:55 +03:00
|
|
|
NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path);
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +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
|
|
|
|
|
|
|
// remove leading '\'
|
|
|
|
if (path.CharAt(0) == '\\')
|
|
|
|
path.Cut(0, 1);
|
|
|
|
|
2006-03-29 08:53:21 +04:00
|
|
|
if (IsUTF8(path))
|
|
|
|
rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path));
|
2017-07-06 15:00:35 +03:00
|
|
|
// XXX In rare cases, a valid UTF-8 string can be valid as a native
|
2006-03-29 08:53:21 +04: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 15:00:35 +03:00
|
|
|
else
|
2006-03-29 08:53:21 +04:00
|
|
|
// 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
|
|
|
}
|