2004-02-28 00:05:58 +00:00
|
|
|
/* vim:set ts=4 sw=4 cindent et: */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-06-07 21:33:30 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-06-07 21:33:30 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-06-07 21:33:30 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-28 20:14:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 22:01:16 +00:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-28 20:14:13 +00:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-28 20:14:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-06-07 21:33:30 +00:00
|
|
|
|
|
|
|
#include "nsIOService.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2002-01-31 22:17:35 +00:00
|
|
|
#include "nsIFileProtocolHandler.h"
|
1999-06-07 21:33:30 +00:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIURI.h"
|
1999-06-12 08:07:05 +00:00
|
|
|
#include "nsIStreamListener.h"
|
1999-06-08 19:04:13 +00:00
|
|
|
#include "prprf.h"
|
1999-06-24 22:58:42 +00:00
|
|
|
#include "nsLoadGroup.h"
|
1999-07-03 04:05:14 +00:00
|
|
|
#include "nsInputStreamChannel.h"
|
1999-07-10 05:00:23 +00:00
|
|
|
#include "nsXPIDLString.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2000-07-25 05:45:56 +00:00
|
|
|
#include "nsIErrorService.h"
|
2001-01-17 19:51:28 +00:00
|
|
|
#include "netCore.h"
|
|
|
|
#include "nsIObserverService.h"
|
2001-10-08 22:27:14 +00:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranchInternal.h"
|
|
|
|
#include "nsIPrefLocalizedString.h"
|
2001-06-21 22:02:47 +00:00
|
|
|
#include "nsICategoryManager.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-06-21 22:02:47 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2001-09-13 02:21:05 +00:00
|
|
|
#include "nsIProxiedProtocolHandler.h"
|
|
|
|
#include "nsIProxyInfo.h"
|
2001-08-17 02:03:34 +00:00
|
|
|
#include "nsITimelineService.h"
|
2001-09-08 12:47:05 +00:00
|
|
|
#include "nsEscape.h"
|
2001-11-16 02:09:13 +00:00
|
|
|
#include "nsNetCID.h"
|
2002-07-02 18:15:19 +00:00
|
|
|
#include "nsIRecyclingAllocator.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsCRT.h"
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2001-10-08 22:27:14 +00:00
|
|
|
#define PORT_PREF_PREFIX "network.security.ports."
|
|
|
|
#define PORT_PREF(x) PORT_PREF_PREFIX x
|
2002-06-28 23:03:52 +00:00
|
|
|
#define AUTODIAL_PREF "network.autodial-helper.enabled"
|
2001-10-08 22:27:14 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
|
2000-01-05 03:44:42 +00:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
|
2000-07-25 05:45:56 +00:00
|
|
|
static NS_DEFINE_CID(kErrorServiceCID, NS_ERRORSERVICE_CID);
|
2001-05-11 21:04:09 +00:00
|
|
|
static NS_DEFINE_CID(kProtocolProxyServiceCID, NS_PROTOCOLPROXYSERVICE_CID);
|
2001-06-21 22:02:47 +00:00
|
|
|
|
2001-06-06 00:10:09 +00:00
|
|
|
// A general port blacklist. Connections to these ports will not be avoided unless
|
|
|
|
// the protocol overrides.
|
|
|
|
//
|
|
|
|
// TODO: I am sure that there are more ports to be added.
|
|
|
|
// This cut is based on the classic mozilla codebase
|
|
|
|
|
2001-08-16 04:53:16 +00:00
|
|
|
PRInt16 gBadPortList[] = {
|
2001-06-06 00:10:09 +00:00
|
|
|
1, // tcpmux
|
|
|
|
7, // echo
|
|
|
|
9, // discard
|
|
|
|
11, // systat
|
|
|
|
13, // daytime
|
|
|
|
15, // netstat
|
|
|
|
17, // qotd
|
|
|
|
19, // chargen
|
|
|
|
20, // ftp-data
|
|
|
|
21, // ftp-cntl
|
|
|
|
22, // ssh
|
|
|
|
23, // telnet
|
|
|
|
25, // smtp
|
|
|
|
37, // time
|
|
|
|
42, // name
|
|
|
|
43, // nicname
|
|
|
|
53, // domain
|
|
|
|
77, // priv-rjs
|
|
|
|
79, // finger
|
|
|
|
87, // ttylink
|
|
|
|
95, // supdup
|
|
|
|
101, // hostriame
|
|
|
|
102, // iso-tsap
|
|
|
|
103, // gppitnp
|
|
|
|
104, // acr-nema
|
|
|
|
109, // pop2
|
|
|
|
110, // pop3
|
|
|
|
111, // sunrpc
|
|
|
|
113, // auth
|
|
|
|
115, // sftp
|
|
|
|
117, // uucp-path
|
|
|
|
119, // nntp
|
|
|
|
123, // NTP
|
|
|
|
135, // loc-srv / epmap
|
|
|
|
139, // netbios
|
|
|
|
143, // imap2
|
|
|
|
179, // BGP
|
|
|
|
389, // ldap
|
|
|
|
512, // print / exec
|
|
|
|
513, // login
|
|
|
|
514, // shell
|
|
|
|
515, // printer
|
|
|
|
526, // tempo
|
|
|
|
530, // courier
|
|
|
|
531, // Chat
|
|
|
|
532, // netnews
|
|
|
|
540, // uucp
|
|
|
|
556, // remotefs
|
|
|
|
587, //
|
|
|
|
601, //
|
|
|
|
2049, // nfs
|
|
|
|
4045, // lockd
|
|
|
|
6000, // x11
|
|
|
|
0, // This MUST be zero so that we can populating the array
|
|
|
|
};
|
1999-08-29 16:42:27 +00:00
|
|
|
|
2001-11-07 04:26:19 +00:00
|
|
|
static const char kProfileChangeNetTeardownTopic[] = "profile-change-net-teardown";
|
|
|
|
static const char kProfileChangeNetRestoreTopic[] = "profile-change-net-restore";
|
|
|
|
|
2002-07-02 18:15:19 +00:00
|
|
|
// Necko buffer cache
|
|
|
|
nsIMemory* nsIOService::gBufferCache = nsnull;
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsIOService::nsIOService()
|
2001-11-07 04:26:19 +00:00
|
|
|
: mOffline(PR_FALSE),
|
|
|
|
mOfflineForProfileChange(PR_FALSE)
|
1999-06-07 21:33:30 +00:00
|
|
|
{
|
2002-07-02 18:15:19 +00:00
|
|
|
// Get the allocator ready
|
|
|
|
if (!gBufferCache)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIRecyclingAllocator> recyclingAllocator =
|
|
|
|
do_CreateInstance(NS_RECYCLINGALLOCATOR_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
rv = recyclingAllocator->Init(NS_NECKO_BUFFER_CACHE_COUNT,
|
|
|
|
NS_NECKO_15_MINS, "necko");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMemory> eyeMemory = do_QueryInterface(recyclingAllocator);
|
|
|
|
gBufferCache = eyeMemory.get();
|
|
|
|
NS_IF_ADDREF(gBufferCache);
|
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIOService::Init()
|
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult rv;
|
2001-02-21 20:38:08 +00:00
|
|
|
|
|
|
|
// Hold onto the eventQueue service. We do not want any eventqueues to go away
|
|
|
|
// when we shutdown until we process all remaining transports
|
|
|
|
|
2003-01-19 10:55:23 +00:00
|
|
|
mEventQueueService = do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("failed to get event queue service");
|
2001-02-21 20:38:08 +00:00
|
|
|
|
2000-01-19 07:58:40 +00:00
|
|
|
// We need to get references to these services so that we can shut them
|
|
|
|
// down later. If we wait until the nsIOService is being shut down,
|
|
|
|
// GetService will fail at that point.
|
2000-06-13 03:00:53 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mSocketTransportService = do_GetService(kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("failed to get socket transport service");
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mDNSService = do_GetService(kDNSServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("failed to get DNS service");
|
2000-07-25 05:45:56 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
mProxyService = do_GetService(kProtocolProxyServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("failed to get protocol proxy service");
|
|
|
|
|
|
|
|
// XXX hack until xpidl supports error info directly (bug 13423)
|
|
|
|
nsCOMPtr<nsIErrorService> errorService = do_GetService(kErrorServiceCID);
|
|
|
|
if (errorService) {
|
|
|
|
errorService->RegisterErrorStringBundle(NS_ERROR_MODULE_NETWORK, NECKO_MSGS_URL);
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_RESOLVING, "ResolvingHost");
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_CONNECTED_TO, "ConnectedTo");
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_SENDING_TO, "SendingTo");
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_RECEIVING_FROM, "ReceivingFrom");
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_CONNECTING_TO, "ConnectingTo");
|
|
|
|
errorService->RegisterErrorStringBundleKey(nsISocketTransport::STATUS_WAITING_FOR, "WaitingFor");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_WARNING("failed to get error service");
|
2001-06-06 00:10:09 +00:00
|
|
|
|
|
|
|
// setup our bad port list stuff
|
2003-01-18 02:15:14 +00:00
|
|
|
for(int i=0; gBadPortList[i]; i++)
|
2002-03-06 07:48:55 +00:00
|
|
|
mRestrictedPortList.AppendElement(NS_REINTERPRET_CAST(void *, gBadPortList[i]));
|
2001-06-06 00:10:09 +00:00
|
|
|
|
2001-10-08 22:27:14 +00:00
|
|
|
// Further modifications to the port list come from prefs
|
2004-01-28 23:45:17 +00:00
|
|
|
nsCOMPtr<nsIPrefBranchInternal> prefBranch;
|
2001-10-08 22:27:14 +00:00
|
|
|
GetPrefBranch(getter_AddRefs(prefBranch));
|
|
|
|
if (prefBranch) {
|
2004-01-28 23:45:17 +00:00
|
|
|
prefBranch->AddObserver(PORT_PREF_PREFIX, this, PR_TRUE);
|
|
|
|
prefBranch->AddObserver(AUTODIAL_PREF, this, PR_TRUE);
|
2001-10-08 22:27:14 +00:00
|
|
|
PrefsChanged(prefBranch);
|
2001-06-06 00:10:09 +00:00
|
|
|
}
|
2001-11-07 04:26:19 +00:00
|
|
|
|
|
|
|
// Register for profile change notifications
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(this, kProfileChangeNetTeardownTopic, PR_TRUE);
|
|
|
|
observerService->AddObserver(this, kProfileChangeNetRestoreTopic, PR_TRUE);
|
2002-05-30 23:11:49 +00:00
|
|
|
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_TRUE);
|
2001-11-07 04:26:19 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
else
|
|
|
|
NS_WARNING("failed to get observer service");
|
2001-11-07 04:26:19 +00:00
|
|
|
|
2001-06-06 00:10:09 +00:00
|
|
|
return NS_OK;
|
2000-06-13 03:00:53 +00:00
|
|
|
}
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
|
|
|
|
nsIOService::~nsIOService()
|
|
|
|
{
|
2001-02-21 20:38:08 +00:00
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2001-10-08 22:27:14 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsIOService,
|
|
|
|
nsIIOService,
|
|
|
|
nsIObserver,
|
2003-09-07 23:12:15 +00:00
|
|
|
nsISupportsWeakReference)
|
1999-06-07 21:33:30 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2002-09-13 19:32:45 +00:00
|
|
|
nsresult
|
2000-08-22 06:16:50 +00:00
|
|
|
nsIOService::CacheProtocolHandler(const char *scheme, nsIProtocolHandler *handler)
|
|
|
|
{
|
|
|
|
for (unsigned int i=0; i<NS_N(gScheme); i++)
|
|
|
|
{
|
|
|
|
if (!nsCRT::strcasecmp(scheme, gScheme[i]))
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
NS_ASSERTION(!mWeakHandler[i], "Protocol handler already cached");
|
|
|
|
// Make sure the handler supports weak references.
|
|
|
|
nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(handler, &rv);
|
|
|
|
if (!factoryPtr)
|
|
|
|
{
|
|
|
|
// Dont cache handlers that dont support weak reference as
|
|
|
|
// there is real danger of a circular reference.
|
2000-10-28 22:17:53 +00:00
|
|
|
#ifdef DEBUG_dp
|
|
|
|
printf("DEBUG: %s protcol handler doesn't support weak ref. Not cached.\n", scheme);
|
|
|
|
#endif /* DEBUG_dp */
|
2000-08-22 06:16:50 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-07-25 19:06:59 +00:00
|
|
|
mWeakHandler[i] = do_GetWeakReference(handler);
|
2000-08-22 06:16:50 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-09-13 19:32:45 +00:00
|
|
|
nsresult
|
2001-08-17 04:46:35 +00:00
|
|
|
nsIOService::GetCachedProtocolHandler(const char *scheme, nsIProtocolHandler **result, PRUint32 start, PRUint32 end)
|
2000-08-22 06:16:50 +00:00
|
|
|
{
|
2001-10-30 22:20:06 +00:00
|
|
|
PRUint32 len = end - start - 1;
|
2000-08-22 06:16:50 +00:00
|
|
|
for (unsigned int i=0; i<NS_N(gScheme); i++)
|
|
|
|
{
|
2001-10-30 22:20:06 +00:00
|
|
|
if (!mWeakHandler[i])
|
|
|
|
continue;
|
|
|
|
|
2001-08-17 04:01:38 +00:00
|
|
|
// handle unterminated strings
|
|
|
|
// start is inclusive, end is exclusive, len = end - start - 1
|
2001-10-30 22:20:06 +00:00
|
|
|
if (end ? (!nsCRT::strncasecmp(scheme + start, gScheme[i], len)
|
|
|
|
&& gScheme[i][len] == '\0')
|
|
|
|
: (!nsCRT::strcasecmp(scheme, gScheme[i])))
|
2001-08-17 04:01:38 +00:00
|
|
|
{
|
2002-11-06 13:03:49 +00:00
|
|
|
return CallQueryReferent(mWeakHandler[i].get(), result);
|
2001-08-17 04:01:38 +00:00
|
|
|
}
|
2000-08-22 06:16:50 +00:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::GetProtocolHandler(const char* scheme, nsIProtocolHandler* *result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2000-05-03 21:47:56 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(scheme);
|
1999-06-07 21:33:30 +00:00
|
|
|
// XXX we may want to speed this up by introducing our own protocol
|
|
|
|
// scheme -> protocol handler mapping, avoiding the string manipulation
|
|
|
|
// and service manager stuff
|
|
|
|
|
2000-08-22 06:16:50 +00:00
|
|
|
rv = GetCachedProtocolHandler(scheme, result);
|
|
|
|
if (NS_SUCCEEDED(rv)) return NS_OK;
|
|
|
|
|
2002-03-26 15:58:26 +00:00
|
|
|
PRBool externalProtocol = PR_FALSE;
|
2002-10-09 00:54:43 +00:00
|
|
|
PRBool listedProtocol = PR_TRUE;
|
2004-01-28 23:45:17 +00:00
|
|
|
nsCOMPtr<nsIPrefBranchInternal> prefBranch;
|
2002-03-26 15:58:26 +00:00
|
|
|
GetPrefBranch(getter_AddRefs(prefBranch));
|
|
|
|
if (prefBranch) {
|
|
|
|
nsCAutoString externalProtocolPref("network.protocol-handler.external.");
|
|
|
|
externalProtocolPref += scheme;
|
|
|
|
rv = prefBranch->GetBoolPref(externalProtocolPref.get(), &externalProtocol);
|
2002-10-09 00:54:43 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-03-26 15:58:26 +00:00
|
|
|
externalProtocol = PR_FALSE;
|
2002-10-09 00:54:43 +00:00
|
|
|
listedProtocol = PR_FALSE;
|
|
|
|
}
|
2002-03-26 15:58:26 +00:00
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2002-03-26 15:58:26 +00:00
|
|
|
if (!externalProtocol) {
|
|
|
|
nsCAutoString contractID(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX);
|
|
|
|
contractID += scheme;
|
|
|
|
ToLowerCase(contractID);
|
|
|
|
|
|
|
|
rv = CallGetService(contractID.get(), result);
|
2002-10-09 00:54:43 +00:00
|
|
|
|
|
|
|
// If the pref for this protocol was explicitly set to false,
|
|
|
|
// stop here and do not invoke the default handler.
|
|
|
|
if (NS_FAILED(rv) && listedProtocol)
|
|
|
|
return NS_ERROR_UNKNOWN_PROTOCOL;
|
2002-03-26 15:58:26 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 00:54:43 +00:00
|
|
|
if (externalProtocol || NS_FAILED(rv)) {
|
2004-02-28 00:05:58 +00:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
|
|
|
|
// check to see if GnomeVFS can handle this URI scheme. if it can create
|
|
|
|
// a nsIURI for the "scheme:", then we assume it has support for the
|
|
|
|
// requested protocol. otherwise, we failover to using the default
|
|
|
|
// protocol handler.
|
|
|
|
|
|
|
|
// XXX should this be generalized into something that searches a category?
|
|
|
|
// (see bug 234714)
|
|
|
|
|
|
|
|
rv = CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"moz-gnomevfs",
|
|
|
|
result);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCAutoString spec(scheme);
|
|
|
|
spec.Append(':');
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = (*result)->NewURI(spec, nsnull, nsnull, getter_AddRefs(uri));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
|
|
|
NS_RELEASE(*result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-02-07 05:54:35 +00:00
|
|
|
// okay we don't have a protocol handler to handle this url type, so use the default protocol handler.
|
|
|
|
// this will cause urls to get dispatched out to the OS ('cause we can't do anything with them) when
|
|
|
|
// we try to read from a channel created by the default protocol handler.
|
|
|
|
|
2001-10-16 03:53:44 +00:00
|
|
|
rv = CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"default",
|
|
|
|
result);
|
2001-02-07 05:54:35 +00:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_UNKNOWN_PROTOCOL;
|
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2000-08-22 06:16:50 +00:00
|
|
|
CacheProtocolHandler(scheme, *result);
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-06 07:48:55 +00:00
|
|
|
nsIOService::ExtractScheme(const nsACString &inURI, nsACString &scheme)
|
1999-07-10 11:26:43 +00:00
|
|
|
{
|
2002-09-13 19:32:45 +00:00
|
|
|
return net_ExtractURLScheme(inURI, nsnull, nsnull, &scheme);
|
2001-06-21 22:02:47 +00:00
|
|
|
}
|
|
|
|
|
2001-08-07 20:42:57 +00:00
|
|
|
NS_IMETHODIMP
|
2001-09-13 02:21:05 +00:00
|
|
|
nsIOService::GetProtocolFlags(const char* scheme, PRUint32 *flags)
|
2001-08-07 20:42:57 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
nsresult rv = GetProtocolHandler(scheme, getter_AddRefs(handler));
|
2002-03-06 07:48:55 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
rv = handler->GetProtocolFlags(flags);
|
2001-08-07 20:42:57 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-09-26 10:11:36 +00:00
|
|
|
nsresult
|
2002-03-06 07:48:55 +00:00
|
|
|
nsIOService::NewURI(const nsACString &aSpec, const char *aCharset, nsIURI *aBaseURI, nsIURI **result)
|
1999-06-07 21:33:30 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString scheme;
|
2001-08-17 04:01:38 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = ExtractScheme(aSpec, scheme);
|
2002-08-29 21:31:55 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-07-27 08:45:20 +00:00
|
|
|
// then aSpec is relative
|
2001-08-17 04:01:38 +00:00
|
|
|
if (!aBaseURI)
|
1999-07-27 08:45:20 +00:00
|
|
|
return NS_ERROR_MALFORMED_URI;
|
2002-03-06 07:48:55 +00:00
|
|
|
|
|
|
|
rv = aBaseURI->GetScheme(scheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-08-17 04:01:38 +00:00
|
|
|
}
|
2002-03-06 07:48:55 +00:00
|
|
|
|
|
|
|
// now get the handler for this scheme
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = GetProtocolHandler(scheme.get(), getter_AddRefs(handler));
|
1999-07-10 11:26:43 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
return handler->NewURI(aSpec, aCharset, aBaseURI, result);
|
1999-06-07 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
2001-06-21 22:02:47 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-01-31 22:17:35 +00:00
|
|
|
nsIOService::NewFileURI(nsIFile *file, nsIURI **result)
|
2001-06-21 22:02:47 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-01-31 22:17:35 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(file);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
|
|
|
|
rv = GetProtocolHandler("file", getter_AddRefs(handler));
|
2001-06-21 22:02:47 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-01-31 22:17:35 +00:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler( do_QueryInterface(handler, &rv) );
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return fileHandler->NewFileURI(file, result);
|
2001-06-21 22:02:47 +00:00
|
|
|
}
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
NS_IMETHODIMP
|
2000-03-29 03:58:50 +00:00
|
|
|
nsIOService::NewChannelFromURI(nsIURI *aURI, nsIChannel **result)
|
1999-06-07 21:33:30 +00:00
|
|
|
{
|
1999-06-08 18:28:10 +00:00
|
|
|
nsresult rv;
|
2000-05-03 21:47:56 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2001-08-17 02:03:34 +00:00
|
|
|
NS_TIMELINE_MARK_URI("nsIOService::NewChannelFromURI(%s)", aURI);
|
1999-06-08 18:28:10 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString scheme;
|
|
|
|
rv = aURI->GetScheme(scheme);
|
1999-06-08 18:28:10 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
nsCOMPtr<nsIProxyInfo> pi;
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mProxyService) {
|
|
|
|
rv = mProxyService->ExamineForProxy(aURI, getter_AddRefs(pi));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
pi = 0;
|
|
|
|
}
|
1999-06-08 18:28:10 +00:00
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
|
2002-05-30 23:11:49 +00:00
|
|
|
if (pi && !strcmp(pi->Type(),"http")) {
|
2001-09-13 02:21:05 +00:00
|
|
|
// we are going to proxy this channel using an http proxy
|
|
|
|
rv = GetProtocolHandler("http", getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
} else {
|
|
|
|
rv = GetProtocolHandler(scheme.get(), getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-05-11 21:04:09 +00:00
|
|
|
}
|
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
nsCOMPtr<nsIProxiedProtocolHandler> pph = do_QueryInterface(handler);
|
|
|
|
|
|
|
|
if (pph)
|
|
|
|
rv = pph->NewProxiedChannel(aURI, pi, result);
|
|
|
|
else
|
|
|
|
rv = handler->NewChannel(aURI, result);
|
|
|
|
|
1999-06-08 18:28:10 +00:00
|
|
|
return rv;
|
1999-06-07 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-06 07:48:55 +00:00
|
|
|
nsIOService::NewChannel(const nsACString &aSpec, const char *aCharset, nsIURI *aBaseURI, nsIChannel **result)
|
1999-06-07 21:33:30 +00:00
|
|
|
{
|
1999-06-08 18:28:10 +00:00
|
|
|
nsresult rv;
|
1999-09-26 10:11:36 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = NewURI(aSpec, aCharset, aBaseURI, getter_AddRefs(uri));
|
1999-06-08 18:28:10 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-17 08:14:52 +00:00
|
|
|
|
2001-05-11 21:04:09 +00:00
|
|
|
return NewChannelFromURI(uri, result);
|
1999-06-07 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
2000-01-05 02:44:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::GetOffline(PRBool *offline)
|
|
|
|
{
|
|
|
|
*offline = mOffline;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::SetOffline(PRBool offline)
|
|
|
|
{
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
2001-01-17 19:51:28 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult rv;
|
2000-01-05 03:44:42 +00:00
|
|
|
if (offline) {
|
2002-03-08 14:51:00 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(offlineString, "offline");
|
2003-07-09 02:28:07 +00:00
|
|
|
mOffline = PR_TRUE; // indicate we're trying to shutdown
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2002-03-08 14:51:00 +00:00
|
|
|
// don't care if notification fails
|
|
|
|
// this allows users to attempt a little cleanup before dns and socket transport are shut down.
|
2003-01-18 02:15:14 +00:00
|
|
|
if (observerService)
|
|
|
|
observerService->NotifyObservers(NS_STATIC_CAST(nsIIOService *, this),
|
|
|
|
"network:offline-about-to-go-offline",
|
|
|
|
offlineString.get());
|
|
|
|
|
|
|
|
// be sure to try and shutdown both (even if the first fails)...
|
|
|
|
// shutdown dns service first, because it has callbacks for socket transport
|
|
|
|
if (mDNSService) {
|
|
|
|
rv = mDNSService->Shutdown();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "DNS service shutdown failed");
|
|
|
|
}
|
|
|
|
if (mSocketTransportService) {
|
|
|
|
rv = mSocketTransportService->Shutdown();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "socket transport service shutdown failed");
|
|
|
|
}
|
2001-01-17 19:51:28 +00:00
|
|
|
|
2001-11-07 04:26:19 +00:00
|
|
|
// don't care if notification fails
|
|
|
|
if (observerService)
|
2003-01-18 02:15:14 +00:00
|
|
|
observerService->NotifyObservers(NS_STATIC_CAST(nsIIOService *, this),
|
|
|
|
"network:offline-status-changed",
|
|
|
|
offlineString.get());
|
2000-01-05 03:44:42 +00:00
|
|
|
}
|
2000-03-27 22:17:48 +00:00
|
|
|
else if (!offline && mOffline) {
|
|
|
|
// go online
|
2003-01-18 02:15:14 +00:00
|
|
|
if (mDNSService) {
|
|
|
|
rv = mDNSService->Init();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "DNS service init failed");
|
|
|
|
}
|
|
|
|
if (mSocketTransportService) {
|
|
|
|
rv = mSocketTransportService->Init();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "socket transport service init failed");
|
|
|
|
}
|
2001-01-17 19:51:28 +00:00
|
|
|
mOffline = PR_FALSE; // indicate success only AFTER we've
|
|
|
|
// brought up the services
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2001-01-17 19:51:28 +00:00
|
|
|
// don't care if notification fails
|
2001-01-17 23:42:42 +00:00
|
|
|
if (observerService)
|
2003-01-18 02:15:14 +00:00
|
|
|
observerService->NotifyObservers(NS_STATIC_CAST(nsIIOService *, this),
|
|
|
|
"network:offline-status-changed",
|
|
|
|
NS_LITERAL_STRING("online").get());
|
2000-03-27 22:17:48 +00:00
|
|
|
}
|
2000-01-05 02:44:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-22 07:03:33 +00:00
|
|
|
|
2001-06-06 00:10:09 +00:00
|
|
|
NS_IMETHODIMP
|
2001-08-16 04:53:16 +00:00
|
|
|
nsIOService::AllowPort(PRInt32 inPort, const char *scheme, PRBool *_retval)
|
2001-06-06 00:10:09 +00:00
|
|
|
{
|
2001-08-16 04:53:16 +00:00
|
|
|
PRInt16 port = inPort;
|
2001-06-06 00:10:09 +00:00
|
|
|
if (port == -1) {
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first check to see if the port is in our blacklist:
|
|
|
|
PRInt32 badPortListCnt = mRestrictedPortList.Count();
|
|
|
|
for (int i=0; i<badPortListCnt; i++)
|
|
|
|
{
|
2001-08-14 07:59:59 +00:00
|
|
|
if (port == (PRInt32) NS_PTR_TO_INT32(mRestrictedPortList[i]))
|
2001-06-06 00:10:09 +00:00
|
|
|
{
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
// check to see if the protocol wants to override
|
|
|
|
if (!scheme)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
nsresult rv = GetProtocolHandler(scheme, getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// let the protocol handler decide
|
|
|
|
return handler->AllowPort(port, scheme, _retval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-01-27 08:57:14 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2001-10-08 22:27:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsIOService::PrefsChanged(nsIPrefBranch *prefs, const char *pref)
|
|
|
|
{
|
|
|
|
if (!prefs) return;
|
|
|
|
|
|
|
|
// Look for extra ports to block
|
2003-06-25 01:13:30 +00:00
|
|
|
if (!pref || strcmp(pref, PORT_PREF("banned")) == 0)
|
2001-10-08 22:27:14 +00:00
|
|
|
ParsePortList(prefs, PORT_PREF("banned"), PR_FALSE);
|
|
|
|
|
|
|
|
// ...as well as previous blocks to remove.
|
2003-06-25 01:13:30 +00:00
|
|
|
if (!pref || strcmp(pref, PORT_PREF("banned.override")) == 0)
|
2001-10-08 22:27:14 +00:00
|
|
|
ParsePortList(prefs, PORT_PREF("banned.override"), PR_TRUE);
|
2002-06-28 23:03:52 +00:00
|
|
|
|
2003-06-25 01:13:30 +00:00
|
|
|
if (!pref || strcmp(pref, AUTODIAL_PREF) == 0) {
|
2002-06-28 23:03:52 +00:00
|
|
|
PRBool enableAutodial = PR_FALSE;
|
|
|
|
nsresult rv = prefs->GetBoolPref(AUTODIAL_PREF, &enableAutodial);
|
|
|
|
// If pref not found, default to disabled.
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (mSocketTransportService)
|
|
|
|
mSocketTransportService->SetAutodialEnabled(enableAutodial);
|
|
|
|
}
|
|
|
|
}
|
2001-10-08 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIOService::ParsePortList(nsIPrefBranch *prefBranch, const char *pref, PRBool remove)
|
|
|
|
{
|
|
|
|
nsXPIDLCString portList;
|
|
|
|
|
|
|
|
// Get a pref string and chop it up into a list of ports.
|
|
|
|
prefBranch->GetCharPref(pref, getter_Copies(portList));
|
|
|
|
if (portList) {
|
2002-02-05 08:39:30 +00:00
|
|
|
nsCStringArray portListArray;
|
|
|
|
portListArray.ParseString(portList.get(), ",");
|
|
|
|
PRInt32 index;
|
|
|
|
for (index=0; index < portListArray.Count(); index++) {
|
|
|
|
portListArray[index]->StripWhitespace();
|
2002-06-13 20:51:03 +00:00
|
|
|
PRInt32 aErrorCode, portBegin, portEnd;
|
|
|
|
|
|
|
|
if (PR_sscanf(portListArray[index]->get(), "%d-%d", &portBegin, &portEnd) == 2) {
|
|
|
|
if ((portBegin < 65536) && (portEnd < 65536)) {
|
|
|
|
PRInt32 curPort;
|
|
|
|
if (remove) {
|
|
|
|
for (curPort=portBegin; curPort <= portEnd; curPort++)
|
|
|
|
mRestrictedPortList.RemoveElement((void*)curPort);
|
|
|
|
} else {
|
|
|
|
for (curPort=portBegin; curPort <= portEnd; curPort++)
|
|
|
|
mRestrictedPortList.AppendElement((void*)curPort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PRInt32 port = portListArray[index]->ToInteger(&aErrorCode);
|
|
|
|
if (NS_SUCCEEDED(aErrorCode) && port < 65536) {
|
|
|
|
if (remove)
|
|
|
|
mRestrictedPortList.RemoveElement((void*)port);
|
|
|
|
else
|
|
|
|
mRestrictedPortList.AppendElement((void*)port);
|
|
|
|
}
|
|
|
|
}
|
2001-10-08 22:27:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-01-28 23:45:17 +00:00
|
|
|
nsIOService::GetPrefBranch(nsIPrefBranchInternal **result)
|
2001-10-08 22:27:14 +00:00
|
|
|
{
|
|
|
|
*result = nsnull;
|
2004-01-28 23:45:17 +00:00
|
|
|
CallGetService(NS_PREFSERVICE_CONTRACTID, result);
|
2001-10-08 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIObserver interface
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::Observe(nsISupports *subject,
|
2001-10-19 20:52:59 +00:00
|
|
|
const char *topic,
|
2001-10-08 22:27:14 +00:00
|
|
|
const PRUnichar *data)
|
|
|
|
{
|
2002-05-30 23:11:49 +00:00
|
|
|
if (!strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
|
2001-10-08 22:27:14 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(subject);
|
|
|
|
if (prefBranch)
|
|
|
|
PrefsChanged(prefBranch, NS_ConvertUCS2toUTF8(data).get());
|
|
|
|
}
|
2002-05-30 23:11:49 +00:00
|
|
|
else if (!strcmp(topic, kProfileChangeNetTeardownTopic)) {
|
2001-11-07 04:26:19 +00:00
|
|
|
if (!mOffline) {
|
|
|
|
SetOffline(PR_TRUE);
|
|
|
|
mOfflineForProfileChange = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2002-05-30 23:11:49 +00:00
|
|
|
else if (!strcmp(topic, kProfileChangeNetRestoreTopic)) {
|
2001-11-07 04:26:19 +00:00
|
|
|
if (mOfflineForProfileChange) {
|
|
|
|
SetOffline(PR_FALSE);
|
|
|
|
mOfflineForProfileChange = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2002-05-30 23:11:49 +00:00
|
|
|
else if (!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
2003-01-18 02:15:14 +00:00
|
|
|
SetOffline(PR_TRUE);
|
|
|
|
|
2002-11-06 13:03:49 +00:00
|
|
|
// Break circular reference.
|
2003-01-18 02:15:14 +00:00
|
|
|
mProxyService = 0;
|
2002-05-30 23:11:49 +00:00
|
|
|
}
|
2001-10-08 22:27:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|