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