2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
// vim:ts=4 sw=2 sts=2 et cin:
|
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/. */
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsIFile.h"
|
1999-06-11 05:37:24 +04:00
|
|
|
#include "nsFileProtocolHandler.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsFileChannel.h"
|
2002-10-06 06:20:35 +04:00
|
|
|
#include "nsStandardURL.h"
|
2002-09-13 23:32:45 +04:00
|
|
|
#include "nsURLHelper.h"
|
2018-01-24 22:33:02 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2004-07-16 00:21:44 +04:00
|
|
|
|
2004-07-16 00:49:15 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2017-04-12 21:43:50 +03:00
|
|
|
#include "FileChannelChild.h"
|
|
|
|
|
2020-09-22 10:26:33 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
|
|
|
|
2004-07-16 00:21:44 +04:00
|
|
|
// URL file handling, copied and modified from
|
|
|
|
// xpfe/components/bookmarks/src/nsBookmarksService.cpp
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <shlobj.h>
|
|
|
|
# include <intshcut.h>
|
|
|
|
# include "nsIFileURL.h"
|
|
|
|
# ifdef CompareString
|
|
|
|
# undef CompareString
|
|
|
|
# endif
|
|
|
|
#endif
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2005-09-23 21:54:53 +04:00
|
|
|
// URL file handling for freedesktop.org
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
# include "nsINIParser.h"
|
|
|
|
# define DESKTOP_ENTRY_SECTION "Desktop Entry"
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 05:37:24 +04:00
|
|
|
|
1999-06-12 11:14:19 +04:00
|
|
|
nsresult nsFileProtocolHandler::Init() { return NS_OK; }
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsFileProtocolHandler, nsIFileProtocolHandler,
|
|
|
|
nsIProtocolHandler, nsISupportsWeakReference)
|
1999-06-11 05:37:24 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-11 05:37:24 +04:00
|
|
|
// nsIProtocolHandler methods:
|
|
|
|
|
2005-04-22 17:29:41 +04:00
|
|
|
#if defined(XP_WIN)
|
2004-07-16 00:21:44 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
|
|
|
|
nsAutoString path;
|
2004-07-16 01:10:06 +04:00
|
|
|
nsresult rv = aFile->GetPath(path);
|
2004-07-16 00:21:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-16 00:21:44 +04:00
|
|
|
if (path.Length() < 4) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (!StringTail(path, 4).LowerCaseEqualsLiteral(".url"))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-16 00:21:44 +04:00
|
|
|
HRESULT result;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-16 00:21:44 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
IUniformResourceLocatorW* urlLink = nullptr;
|
2013-09-19 23:29:27 +04:00
|
|
|
result =
|
|
|
|
::CoCreateInstance(CLSID_InternetShortcut, nullptr, CLSCTX_INPROC_SERVER,
|
2008-08-28 01:44:54 +04:00
|
|
|
IID_IUniformResourceLocatorW, (void**)&urlLink);
|
2004-07-16 00:21:44 +04:00
|
|
|
if (SUCCEEDED(result) && urlLink) {
|
2012-07-30 18:20:58 +04:00
|
|
|
IPersistFile* urlFile = nullptr;
|
2004-07-16 00:21:44 +04:00
|
|
|
result = urlLink->QueryInterface(IID_IPersistFile, (void**)&urlFile);
|
|
|
|
if (SUCCEEDED(result) && urlFile) {
|
|
|
|
result = urlFile->Load(path.get(), STGM_READ);
|
|
|
|
if (SUCCEEDED(result)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
LPWSTR lpTemp = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-07-16 00:21:44 +04:00
|
|
|
// The URL this method will give us back seems to be already
|
|
|
|
// escaped. Hence, do not do escaping of our own.
|
|
|
|
result = urlLink->GetURL(&lpTemp);
|
|
|
|
if (SUCCEEDED(result) && lpTemp) {
|
2008-08-28 02:06:34 +04:00
|
|
|
rv = NS_NewURI(aURI, nsDependentString(lpTemp));
|
2004-07-16 00:21:44 +04:00
|
|
|
// free the string that GetURL alloc'd
|
2005-06-01 19:58:12 +04:00
|
|
|
CoTaskMemFree(lpTemp);
|
2004-07-16 00:21:44 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-07-16 00:21:44 +04:00
|
|
|
urlFile->Release();
|
|
|
|
}
|
|
|
|
urlLink->Release();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-07-16 00:21:44 +04:00
|
|
|
return rv;
|
2005-04-22 17:29:41 +04:00
|
|
|
}
|
|
|
|
|
2011-09-03 04:55:30 +04:00
|
|
|
#elif defined(XP_UNIX)
|
2005-09-23 21:54:53 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
|
|
|
|
// We only support desktop files that end in ".desktop" like the spec says:
|
|
|
|
// http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s02.html
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString leafName;
|
2005-09-23 21:54:53 +04:00
|
|
|
nsresult rv = aFile->GetNativeLeafName(leafName);
|
2021-06-04 15:35:16 +03:00
|
|
|
if (NS_FAILED(rv) || !StringEndsWith(leafName, ".desktop"_ns)) {
|
2016-06-10 00:51:35 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2021-06-04 15:35:16 +03:00
|
|
|
}
|
2016-06-10 00:51:35 +03:00
|
|
|
|
2005-09-23 21:54:53 +04:00
|
|
|
bool isFile = false;
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = aFile->IsFile(&isFile);
|
2005-09-23 21:54:53 +04:00
|
|
|
if (NS_FAILED(rv) || !isFile) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-09-23 21:54:53 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsINIParser parser;
|
2005-09-23 21:54:53 +04:00
|
|
|
rv = parser.Init(aFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString type;
|
2005-09-23 21:54:53 +04:00
|
|
|
parser.GetString(DESKTOP_ENTRY_SECTION, "Type", type);
|
|
|
|
if (!type.EqualsLiteral("Link")) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2018-01-24 22:33:02 +03:00
|
|
|
nsAutoCString url;
|
2005-09-23 21:54:53 +04:00
|
|
|
rv = parser.GetString(DESKTOP_ENTRY_SECTION, "URL", url);
|
|
|
|
if (NS_FAILED(rv) || url.IsEmpty()) return NS_ERROR_NOT_AVAILABLE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-09-23 21:54:53 +04:00
|
|
|
return NS_NewURI(aURI, url);
|
|
|
|
}
|
|
|
|
|
2005-04-22 17:29:41 +04:00
|
|
|
#else // other platforms
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::ReadURLFile(nsIFile* aFile, nsIURI** aURI) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2004-07-16 00:21:44 +04:00
|
|
|
}
|
2005-04-22 17:29:41 +04:00
|
|
|
#endif // ReadURLFile()
|
2004-07-16 00:21:44 +04:00
|
|
|
|
2020-09-22 10:26:33 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::ReadShellLink(nsIFile* aFile, nsIURI** aURI) {
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
nsAutoString path;
|
|
|
|
MOZ_TRY(aFile->GetPath(path));
|
|
|
|
|
|
|
|
if (path.Length() < 4 ||
|
|
|
|
!StringTail(path, 4).LowerCaseEqualsLiteral(".lnk")) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
RefPtr<IPersistFile> persistFile;
|
|
|
|
RefPtr<IShellLinkW> shellLink;
|
|
|
|
WCHAR lpTemp[MAX_PATH];
|
|
|
|
// Get a pointer to the IPersistFile interface.
|
|
|
|
if (FAILED(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_IShellLinkW, getter_AddRefs(shellLink))) ||
|
|
|
|
FAILED(shellLink->QueryInterface(IID_IPersistFile,
|
|
|
|
getter_AddRefs(persistFile))) ||
|
|
|
|
FAILED(persistFile->Load(path.get(), STGM_READ)) ||
|
|
|
|
FAILED(shellLink->Resolve(nullptr, SLR_NO_UI)) ||
|
|
|
|
FAILED(shellLink->GetPath(lpTemp, MAX_PATH, nullptr, SLGP_UNCPRIORITY))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIFile> linkedFile;
|
|
|
|
MOZ_TRY(NS_NewLocalFile(nsDependentString(lpTemp), false,
|
|
|
|
getter_AddRefs(linkedFile)));
|
|
|
|
return NS_NewFileURI(aURI, linkedFile);
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-06-11 05:37:24 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 10:48:55 +03:00
|
|
|
nsFileProtocolHandler::GetScheme(nsACString& result) {
|
2004-06-17 04:13:25 +04:00
|
|
|
result.AssignLiteral("file");
|
1999-06-11 05:37:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileProtocolHandler::GetDefaultPort(int32_t* result) {
|
1999-06-17 07:38:23 +04:00
|
|
|
*result = -1; // no port for file: URLs
|
1999-06-11 05:37:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-08 00:42:57 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileProtocolHandler::GetProtocolFlags(uint32_t* result) {
|
Bug 1328695 - Use protocol flags to determine if a URI is potentially trustworthy r=ckerschb, r=dveditz, r=mcmanus, r=bz
Before this change, the trusted URI schemes, based on a string whitelist, were:
https, file, resource, app, moz-extension and wss.
This change removes "app" from the list (since we don't implement it),
and adds "about" to the list (because we control the delivery of that).
2018-05-31 08:51:42 +03:00
|
|
|
*result = URI_NOAUTH | URI_IS_LOCAL_FILE | URI_IS_LOCAL_RESOURCE |
|
|
|
|
URI_IS_POTENTIALLY_TRUSTWORTHY;
|
2001-08-08 00:42:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-11 05:37:24 +04:00
|
|
|
NS_IMETHODIMP
|
2019-02-19 21:20:37 +03:00
|
|
|
nsFileProtocolHandler::NewChannel(nsIURI* uri, nsILoadInfo* aLoadInfo,
|
|
|
|
nsIChannel** result) {
|
2017-05-30 19:07:59 +03:00
|
|
|
nsresult rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-05 21:43:02 +03:00
|
|
|
RefPtr<nsFileChannel> chan;
|
2017-04-12 21:43:50 +03:00
|
|
|
if (IsNeckoChild()) {
|
|
|
|
chan = new mozilla::net::FileChannelChild(uri);
|
|
|
|
} else {
|
|
|
|
chan = new nsFileChannel(uri);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-30 19:07:59 +03:00
|
|
|
// set the loadInfo on the new channel ; must do this
|
|
|
|
// before calling Init() on it, since it needs the load
|
|
|
|
// info be already set.
|
|
|
|
rv = chan->SetLoadInfo(aLoadInfo);
|
1999-06-11 05:37:24 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:07:59 +03:00
|
|
|
rv = chan->Init();
|
2014-12-12 20:07:36 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2019-03-05 21:43:02 +03:00
|
|
|
chan.forget(result);
|
1999-06-11 05:37:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-06-06 04:10:09 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsFileProtocolHandler::AllowPort(int32_t port, const char* scheme,
|
|
|
|
bool* result) {
|
2001-06-06 04:10:09 +04:00
|
|
|
// don't override anything.
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = false;
|
2001-06-06 04:10:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-02-01 01:17:35 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-01 01:17:35 +03:00
|
|
|
// nsIFileProtocolHandler methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-02-26 07:24:09 +03:00
|
|
|
nsFileProtocolHandler::NewFileURI(nsIFile* aFile, nsIURI** aResult) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-02-26 07:24:09 +03:00
|
|
|
RefPtr<nsIFile> file(aFile);
|
2005-10-14 01:50:25 +04:00
|
|
|
// NOTE: the origin charset is assigned the value of the platform
|
|
|
|
// charset by the SetFile method.
|
2018-02-26 07:24:09 +03:00
|
|
|
return NS_MutateURI(new nsStandardURL::Mutator())
|
2021-08-12 10:40:58 +03:00
|
|
|
.Apply(&nsIFileURLMutator::SetFile, file)
|
2018-02-26 07:24:09 +03:00
|
|
|
.Finalize(aResult);
|
2002-02-01 01:17:35 +03:00
|
|
|
}
|
2002-09-13 23:32:45 +04:00
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::NewFileURIMutator(nsIFile* aFile,
|
|
|
|
nsIURIMutator** aResult) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aFile);
|
|
|
|
nsresult rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-03-20 14:52:43 +03:00
|
|
|
nsCOMPtr<nsIURIMutator> mutator = new nsStandardURL::Mutator();
|
2018-02-26 22:43:46 +03:00
|
|
|
nsCOMPtr<nsIFileURLMutator> fileMutator = do_QueryInterface(mutator, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: the origin charset is assigned the value of the platform
|
|
|
|
// charset by the SetFile method.
|
|
|
|
rv = fileMutator->SetFile(aFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutator.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-09-13 23:32:45 +04:00
|
|
|
NS_IMETHODIMP
|
2003-01-18 05:15:14 +03:00
|
|
|
nsFileProtocolHandler::GetURLSpecFromFile(nsIFile* file, nsACString& result) {
|
2005-02-28 00:13:13 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(file);
|
2003-01-18 05:15:14 +03:00
|
|
|
return net_GetURLSpecFromFile(file, result);
|
2002-09-13 23:32:45 +04:00
|
|
|
}
|
|
|
|
|
2009-10-06 17:43:59 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::GetURLSpecFromActualFile(nsIFile* file,
|
|
|
|
nsACString& result) {
|
|
|
|
NS_ENSURE_ARG_POINTER(file);
|
|
|
|
return net_GetURLSpecFromActualFile(file, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFileProtocolHandler::GetURLSpecFromDir(nsIFile* file, nsACString& result) {
|
|
|
|
NS_ENSURE_ARG_POINTER(file);
|
|
|
|
return net_GetURLSpecFromDir(file, result);
|
|
|
|
}
|
|
|
|
|
2002-09-13 23:32:45 +04:00
|
|
|
NS_IMETHODIMP
|
2003-01-18 05:15:14 +03:00
|
|
|
nsFileProtocolHandler::GetFileFromURLSpec(const nsACString& spec,
|
|
|
|
nsIFile** result) {
|
|
|
|
return net_GetFileFromURLSpec(spec, result);
|
2002-09-13 23:32:45 +04:00
|
|
|
}
|