2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2006-05-02 18:54:19 +00:00
|
|
|
/* vim:set ts=4 sw=4 cindent et: */
|
2001-09-28 20:14:13 +00:00
|
|
|
/* ***** 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):
|
2007-06-21 20:18:25 +00:00
|
|
|
* Prasad Sunkari <prasad@medhas.org>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* 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 "nsIURI.h"
|
1999-06-12 08:07:05 +00:00
|
|
|
#include "nsIStreamListener.h"
|
1999-06-08 19:04:13 +00:00
|
|
|
#include "prprf.h"
|
2006-02-22 14:07:21 +00:00
|
|
|
#include "prlog.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"
|
2005-02-25 20:46:35 +00:00
|
|
|
#include "nsIPrefBranch2.h"
|
2001-10-08 22:27:14 +00:00
|
|
|
#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"
|
2009-08-23 22:48:07 +00:00
|
|
|
#include "nsSimpleNestedURI.h"
|
2006-05-31 17:57:14 +00:00
|
|
|
#include "nsNetUtil.h"
|
2007-03-12 05:56:45 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2008-08-26 23:09:02 +00:00
|
|
|
#include "nsIPermissionManager.h"
|
2009-01-22 04:15:34 +00:00
|
|
|
#include "nsTArray.h"
|
2009-11-18 23:21:13 +00:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIUploadChannel2.h"
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2009-11-18 15:20:45 +00:00
|
|
|
#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
|
2007-03-20 01:58:19 +00:00
|
|
|
#include "nsNativeConnectionHelper.h"
|
|
|
|
#endif
|
|
|
|
|
2009-01-13 05:52:00 +00:00
|
|
|
#define PORT_PREF_PREFIX "network.security.ports."
|
|
|
|
#define PORT_PREF(x) PORT_PREF_PREFIX x
|
|
|
|
#define AUTODIAL_PREF "network.autodial-helper.enabled"
|
|
|
|
#define MANAGE_OFFLINE_STATUS_PREF "network.manage-offline-status"
|
2001-10-08 22:27:14 +00:00
|
|
|
|
2007-03-12 05:56:45 +00:00
|
|
|
#define MAX_RECURSION_COUNT 50
|
|
|
|
|
2006-02-22 14:07:21 +00:00
|
|
|
nsIOService* gIOService = nsnull;
|
2009-11-18 23:21:13 +00:00
|
|
|
static PRBool gHasWarnedUploadChannel2;
|
2006-02-22 14:07:21 +00:00
|
|
|
|
2006-07-31 14:52:49 +00:00
|
|
|
// A general port blacklist. Connections to these ports will not be allowed unless
|
2001-06-06 00:10:09 +00:00
|
|
|
// 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
|
2006-07-06 19:40:38 +00:00
|
|
|
465, // smtp+ssl
|
2001-06-06 00:10:09 +00:00
|
|
|
512, // print / exec
|
|
|
|
513, // login
|
|
|
|
514, // shell
|
|
|
|
515, // printer
|
|
|
|
526, // tempo
|
|
|
|
530, // courier
|
|
|
|
531, // Chat
|
|
|
|
532, // netnews
|
|
|
|
540, // uucp
|
|
|
|
556, // remotefs
|
2006-07-06 19:40:38 +00:00
|
|
|
563, // nntp+ssl
|
2001-06-06 00:10:09 +00:00
|
|
|
587, //
|
|
|
|
601, //
|
2006-07-06 19:40:38 +00:00
|
|
|
636, // ldap+ssl
|
|
|
|
993, // imap+ssl
|
|
|
|
995, // pop3+ssl
|
2001-06-06 00:10:09 +00:00
|
|
|
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()
|
2006-02-22 14:07:21 +00:00
|
|
|
: mOffline(PR_FALSE)
|
|
|
|
, mOfflineForProfileChange(PR_FALSE)
|
2009-06-09 16:03:59 +00:00
|
|
|
, mManageOfflineStatus(PR_TRUE)
|
2009-02-22 19:09:09 +00:00
|
|
|
, mSettingOffline(PR_FALSE)
|
|
|
|
, mSetOfflineValue(PR_FALSE)
|
2009-03-18 14:52:31 +00:00
|
|
|
, mShutdown(PR_FALSE)
|
2006-02-22 14:07:21 +00:00
|
|
|
, mChannelEventSinks(NS_CHANNEL_EVENT_SINK_CATEGORY)
|
2006-03-03 21:06:53 +00:00
|
|
|
, mContentSniffers(NS_CONTENT_SNIFFER_CATEGORY)
|
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
|
|
|
|
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
|
|
|
|
2005-03-25 03:41:33 +00:00
|
|
|
// TODO(darin): Load the Socket and DNS services lazily.
|
|
|
|
|
2007-09-05 13:04:54 +00:00
|
|
|
mSocketTransportService = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
2007-06-11 00:01:48 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_WARNING("failed to get socket transport service");
|
2007-06-11 00:01:48 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2001-05-11 21:04:09 +00:00
|
|
|
|
2007-09-05 13:04:54 +00:00
|
|
|
mDNSService = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
|
2007-06-11 00:01:48 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_WARNING("failed to get DNS service");
|
2007-06-11 00:01:48 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2000-07-25 05:45:56 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
// XXX hack until xpidl supports error info directly (bug 13423)
|
2007-09-05 13:04:54 +00:00
|
|
|
nsCOMPtr<nsIErrorService> errorService = do_GetService(NS_ERRORSERVICE_CONTRACTID);
|
2003-01-18 02:15:14 +00:00
|
|
|
if (errorService) {
|
|
|
|
errorService->RegisterErrorStringBundle(NS_ERROR_MODULE_NETWORK, NECKO_MSGS_URL);
|
|
|
|
}
|
|
|
|
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++)
|
2009-02-23 01:05:28 +00:00
|
|
|
mRestrictedPortList.AppendElement(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
|
2005-02-25 20:46:35 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch2> 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);
|
2009-01-13 05:52:00 +00:00
|
|
|
prefBranch->AddObserver(MANAGE_OFFLINE_STATUS_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);
|
2006-04-04 21:26:17 +00:00
|
|
|
observerService->AddObserver(this, NS_NETWORK_LINK_TOPIC, 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");
|
2006-04-04 21:26:17 +00:00
|
|
|
|
2006-02-22 14:07:21 +00:00
|
|
|
gIOService = this;
|
2001-11-07 04:26:19 +00:00
|
|
|
|
2006-04-04 21:26:17 +00:00
|
|
|
// go into managed mode if we can
|
|
|
|
mNetworkLinkService = do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID);
|
2009-01-13 05:52:00 +00:00
|
|
|
if (!mNetworkLinkService)
|
|
|
|
mManageOfflineStatus = PR_FALSE;
|
|
|
|
|
|
|
|
if (mManageOfflineStatus)
|
2006-04-04 21:26:17 +00:00
|
|
|
TrackNetworkLinkStatusForOffline();
|
|
|
|
|
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()
|
|
|
|
{
|
2006-02-22 14:07:21 +00:00
|
|
|
gIOService = nsnull;
|
2001-02-21 20:38:08 +00:00
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2006-03-01 18:45:14 +00:00
|
|
|
nsIOService*
|
|
|
|
nsIOService::GetInstance() {
|
|
|
|
if (!gIOService) {
|
|
|
|
gIOService = new nsIOService();
|
|
|
|
if (!gIOService)
|
|
|
|
return nsnull;
|
|
|
|
NS_ADDREF(gIOService);
|
|
|
|
|
|
|
|
nsresult rv = gIOService->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(gIOService);
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return gIOService;
|
|
|
|
}
|
|
|
|
NS_ADDREF(gIOService);
|
|
|
|
return gIOService;
|
|
|
|
}
|
|
|
|
|
2008-10-20 04:12:25 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS5(nsIOService,
|
2008-02-21 20:39:20 +00:00
|
|
|
nsIIOService,
|
|
|
|
nsIIOService2,
|
|
|
|
nsINetUtil,
|
|
|
|
nsIObserver,
|
2008-10-20 04:12:25 +00:00
|
|
|
nsISupportsWeakReference)
|
2008-02-21 20:39:20 +00:00
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-02-22 14:07:21 +00:00
|
|
|
nsresult
|
|
|
|
nsIOService::OnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
|
|
|
|
PRUint32 flags)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChannelEventSink> sink =
|
|
|
|
do_GetService(NS_GLOBAL_CHANNELEVENTSINK_CONTRACTID);
|
|
|
|
if (sink) {
|
|
|
|
nsresult rv = sink->OnChannelRedirect(oldChan, newChan, flags);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, our category
|
|
|
|
const nsCOMArray<nsIChannelEventSink>& entries =
|
|
|
|
mChannelEventSinks.GetEntries();
|
|
|
|
PRInt32 len = entries.Count();
|
|
|
|
for (PRInt32 i = 0; i < len; ++i) {
|
|
|
|
nsresult rv = entries[i]->OnChannelRedirect(oldChan, newChan, flags);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2005-11-25 08:16:51 +00:00
|
|
|
// Don't cache handlers that don't support weak reference as
|
2000-08-22 06:16:50 +00:00
|
|
|
// 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);
|
2004-08-24 21:50:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
2000-08-22 06:16:50 +00:00
|
|
|
|
2002-03-26 15:58:26 +00:00
|
|
|
PRBool externalProtocol = PR_FALSE;
|
2002-10-09 00:54:43 +00:00
|
|
|
PRBool listedProtocol = PR_TRUE;
|
2005-02-25 20:46:35 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch2> 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);
|
2004-08-24 21:50:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CacheProtocolHandler(scheme, *result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-05-21 00:28:52 +00:00
|
|
|
#ifdef MOZ_X11
|
2007-11-09 06:13:26 +00:00
|
|
|
// check to see whether 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.
|
2004-08-24 21:50:15 +00:00
|
|
|
|
2007-11-09 06:13:26 +00:00
|
|
|
// XXX should this be generalized into something that searches a
|
|
|
|
// category? (see bug 234714)
|
2004-08-24 21:50:15 +00:00
|
|
|
|
2007-11-09 06:13:26 +00:00
|
|
|
rv = CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"moz-gnomevfs",
|
|
|
|
result);
|
2004-05-21 00:28:52 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2007-11-09 06:13:26 +00:00
|
|
|
nsCAutoString spec(scheme);
|
|
|
|
spec.Append(':');
|
|
|
|
|
|
|
|
nsIURI *uri;
|
|
|
|
rv = (*result)->NewURI(spec, nsnull, nsnull, &uri);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_RELEASE(uri);
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-07 05:54:35 +00:00
|
|
|
|
2007-11-09 06:13:26 +00:00
|
|
|
NS_RELEASE(*result);
|
|
|
|
}
|
2004-08-24 21:50:15 +00:00
|
|
|
#endif
|
2007-11-09 06:13:26 +00:00
|
|
|
}
|
1999-06-07 21:33:30 +00:00
|
|
|
|
2004-08-24 21:50:15 +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.
|
2000-08-22 06:16:50 +00:00
|
|
|
|
2004-08-24 21:50:15 +00:00
|
|
|
rv = CallGetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX"default",
|
|
|
|
result);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_UNKNOWN_PROTOCOL;
|
|
|
|
|
|
|
|
return rv;
|
1999-06-07 21:33:30 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-03-12 05:56:45 +00:00
|
|
|
class AutoIncrement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoIncrement(PRUint32 *var) : mVar(var)
|
|
|
|
{
|
|
|
|
++*var;
|
|
|
|
}
|
|
|
|
~AutoIncrement()
|
|
|
|
{
|
|
|
|
--*mVar;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
PRUint32 *mVar;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2007-03-12 05:56:45 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "wrong thread");
|
|
|
|
|
|
|
|
static PRUint32 recursionCount = 0;
|
|
|
|
if (recursionCount >= MAX_RECURSION_COUNT)
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
AutoIncrement inc(&recursionCount);
|
2001-08-17 04:01:38 +00:00
|
|
|
|
2007-03-12 05:56:45 +00:00
|
|
|
nsCAutoString scheme;
|
|
|
|
nsresult 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);
|
2005-03-25 03:41:33 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-06-08 18:28:10 +00:00
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
2005-03-25 03:41:33 +00:00
|
|
|
rv = GetProtocolHandler(scheme.get(), getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-09-13 02:21:05 +00:00
|
|
|
|
2005-03-25 03:41:33 +00:00
|
|
|
PRUint32 protoFlags;
|
|
|
|
rv = handler->GetProtocolFlags(&protoFlags);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-09-13 02:21:05 +00:00
|
|
|
|
2005-03-25 03:41:33 +00:00
|
|
|
// Talk to the PPS if the protocol handler allows proxying. Otherwise,
|
|
|
|
// skip this step. This allows us to lazily load the PPS at startup.
|
|
|
|
if (protoFlags & nsIProtocolHandler::ALLOWS_PROXY) {
|
|
|
|
nsCOMPtr<nsIProxyInfo> pi;
|
|
|
|
if (!mProxyService) {
|
|
|
|
mProxyService = do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID);
|
|
|
|
if (!mProxyService)
|
|
|
|
NS_WARNING("failed to get protocol proxy service");
|
|
|
|
}
|
|
|
|
if (mProxyService) {
|
2008-11-05 04:35:39 +00:00
|
|
|
rv = mProxyService->Resolve(aURI, 0, getter_AddRefs(pi));
|
2005-03-25 03:41:33 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
pi = nsnull;
|
|
|
|
}
|
|
|
|
if (pi) {
|
|
|
|
nsCAutoString type;
|
|
|
|
if (NS_SUCCEEDED(pi->GetType(type)) && type.EqualsLiteral("http")) {
|
|
|
|
// we are going to proxy this channel using an http proxy
|
|
|
|
rv = GetProtocolHandler("http", getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIProxiedProtocolHandler> pph = do_QueryInterface(handler);
|
|
|
|
if (pph)
|
|
|
|
return pph->NewProxiedChannel(aURI, pi, result);
|
|
|
|
}
|
|
|
|
}
|
2001-09-13 02:21:05 +00:00
|
|
|
|
2009-11-18 23:21:13 +00:00
|
|
|
rv = handler->NewChannel(aURI, result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Some extensions override the http protocol handler and provide their own
|
|
|
|
// implementation. The channels returned from that implementation doesn't
|
|
|
|
// seem to always implement the nsIUploadChannel2 interface, presumably
|
|
|
|
// because it's a new interface.
|
|
|
|
// Eventually we should remove this and simply require that http channels
|
|
|
|
// implement the new interface.
|
|
|
|
// See bug 529041
|
|
|
|
if (!gHasWarnedUploadChannel2 && scheme.EqualsLiteral("http")) {
|
|
|
|
nsCOMPtr<nsIUploadChannel2> uploadChannel2 = do_QueryInterface(*result);
|
|
|
|
if (!uploadChannel2) {
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (consoleService) {
|
|
|
|
consoleService->LogStringMessage(NS_LITERAL_STRING(
|
|
|
|
"Http channel implementation doesn't support "
|
|
|
|
"nsIUploadChannel2. An extension has supplied a "
|
|
|
|
"non-functional http protocol handler. This will break "
|
|
|
|
"behavior and in future releases not work at all.").get());
|
|
|
|
}
|
|
|
|
gHasWarnedUploadChannel2 = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
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
|
|
|
}
|
|
|
|
|
2008-11-13 15:38:02 +00:00
|
|
|
PRBool
|
|
|
|
nsIOService::IsLinkUp()
|
|
|
|
{
|
|
|
|
if (!mNetworkLinkService) {
|
|
|
|
// We cannot decide, assume the link is up
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool isLinkUp;
|
|
|
|
nsresult rv;
|
|
|
|
rv = mNetworkLinkService->GetIsLinkUp(&isLinkUp);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isLinkUp;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-03-18 14:52:31 +00:00
|
|
|
// When someone wants to go online (!offline) after we got XPCOM shutdown
|
|
|
|
// throw ERROR_NOT_AVAILABLE to prevent return to online state.
|
|
|
|
if (mShutdown && !offline)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2009-02-22 19:09:09 +00:00
|
|
|
// SetOffline() may re-enter while it's shutting down services.
|
|
|
|
// If that happens, save the most recent value and it will be
|
|
|
|
// processed when the first SetOffline() call is done bringing
|
|
|
|
// down the service.
|
|
|
|
mSetOfflineValue = offline;
|
|
|
|
if (mSettingOffline) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mSettingOffline = PR_TRUE;
|
|
|
|
|
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
|
|
|
|
2009-02-22 19:09:09 +00:00
|
|
|
while (mSetOfflineValue != mOffline) {
|
|
|
|
offline = mSetOfflineValue;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (offline && !mOffline) {
|
|
|
|
NS_NAMED_LITERAL_STRING(offlineString, NS_IOSERVICE_OFFLINE);
|
|
|
|
mOffline = PR_TRUE; // indicate we're trying to shutdown
|
|
|
|
|
|
|
|
// don't care if notification fails
|
|
|
|
// this allows users to attempt a little cleanup before dns and socket transport are shut down.
|
|
|
|
if (observerService)
|
|
|
|
observerService->NotifyObservers(static_cast<nsIIOService *>(this),
|
|
|
|
NS_IOSERVICE_GOING_OFFLINE_TOPIC,
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't care if notification fails
|
|
|
|
if (observerService)
|
|
|
|
observerService->NotifyObservers(static_cast<nsIIOService *>(this),
|
|
|
|
NS_IOSERVICE_OFFLINE_STATUS_TOPIC,
|
|
|
|
offlineString.get());
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2009-02-22 19:09:09 +00:00
|
|
|
else if (!offline && mOffline) {
|
|
|
|
// go online
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
mOffline = PR_FALSE; // indicate success only AFTER we've
|
|
|
|
// brought up the services
|
|
|
|
|
|
|
|
// trigger a PAC reload when we come back online
|
|
|
|
if (mProxyService)
|
|
|
|
mProxyService->ReloadPAC();
|
|
|
|
|
|
|
|
// don't care if notification fails
|
|
|
|
if (observerService)
|
|
|
|
observerService->NotifyObservers(static_cast<nsIIOService *>(this),
|
|
|
|
NS_IOSERVICE_OFFLINE_STATUS_TOPIC,
|
|
|
|
NS_LITERAL_STRING(NS_IOSERVICE_ONLINE).get());
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2000-03-27 22:17:48 +00:00
|
|
|
}
|
2009-02-22 19:09:09 +00:00
|
|
|
|
|
|
|
mSettingOffline = PR_FALSE;
|
|
|
|
|
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:
|
2009-02-23 01:05:28 +00:00
|
|
|
PRInt32 badPortListCnt = mRestrictedPortList.Length();
|
2001-06-06 00:10:09 +00:00
|
|
|
for (int i=0; i<badPortListCnt; i++)
|
|
|
|
{
|
2009-02-23 01:05:28 +00:00
|
|
|
if (port == 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);
|
|
|
|
}
|
|
|
|
}
|
2009-01-13 05:52:00 +00:00
|
|
|
|
|
|
|
if (!pref || strcmp(pref, MANAGE_OFFLINE_STATUS_PREF) == 0) {
|
|
|
|
PRBool manage;
|
|
|
|
if (NS_SUCCEEDED(prefs->GetBoolPref(MANAGE_OFFLINE_STATUS_PREF,
|
|
|
|
&manage)))
|
|
|
|
SetManageOfflineStatus(manage);
|
|
|
|
}
|
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) {
|
2009-01-22 04:15:34 +00:00
|
|
|
nsTArray<nsCString> portListArray;
|
|
|
|
ParseString(portList, ',', portListArray);
|
|
|
|
PRUint32 index;
|
|
|
|
for (index=0; index < portListArray.Length(); index++) {
|
|
|
|
portListArray[index].StripWhitespace();
|
2002-06-13 20:51:03 +00:00
|
|
|
PRInt32 aErrorCode, portBegin, portEnd;
|
|
|
|
|
2009-01-22 04:15:34 +00:00
|
|
|
if (PR_sscanf(portListArray[index].get(), "%d-%d", &portBegin, &portEnd) == 2) {
|
2002-06-13 20:51:03 +00:00
|
|
|
if ((portBegin < 65536) && (portEnd < 65536)) {
|
|
|
|
PRInt32 curPort;
|
|
|
|
if (remove) {
|
|
|
|
for (curPort=portBegin; curPort <= portEnd; curPort++)
|
2009-02-23 01:05:28 +00:00
|
|
|
mRestrictedPortList.RemoveElement(curPort);
|
2002-06-13 20:51:03 +00:00
|
|
|
} else {
|
|
|
|
for (curPort=portBegin; curPort <= portEnd; curPort++)
|
2009-02-23 01:05:28 +00:00
|
|
|
mRestrictedPortList.AppendElement(curPort);
|
2002-06-13 20:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-01-22 04:15:34 +00:00
|
|
|
PRInt32 port = portListArray[index].ToInteger(&aErrorCode);
|
2002-06-13 20:51:03 +00:00
|
|
|
if (NS_SUCCEEDED(aErrorCode) && port < 65536) {
|
|
|
|
if (remove)
|
2009-02-23 01:05:28 +00:00
|
|
|
mRestrictedPortList.RemoveElement(port);
|
2002-06-13 20:51:03 +00:00
|
|
|
else
|
2009-02-23 01:05:28 +00:00
|
|
|
mRestrictedPortList.AppendElement(port);
|
2002-06-13 20:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-08 22:27:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-25 20:46:35 +00:00
|
|
|
nsIOService::GetPrefBranch(nsIPrefBranch2 **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)
|
2006-02-03 14:18:39 +00:00
|
|
|
PrefsChanged(prefBranch, NS_ConvertUTF16toUTF8(data).get());
|
2001-10-08 22:27:14 +00:00
|
|
|
}
|
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) {
|
|
|
|
mOfflineForProfileChange = PR_FALSE;
|
2006-04-04 21:26:17 +00:00
|
|
|
if (!mManageOfflineStatus ||
|
|
|
|
NS_FAILED(TrackNetworkLinkStatusForOffline())) {
|
|
|
|
SetOffline(PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
2001-11-07 04:26:19 +00:00
|
|
|
}
|
2002-05-30 23:11:49 +00:00
|
|
|
else if (!strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
|
2009-03-18 14:52:31 +00:00
|
|
|
// Remember we passed XPCOM shutdown notification to prevent any
|
|
|
|
// changes of the offline status from now. We must not allow going
|
|
|
|
// online after this point.
|
|
|
|
mShutdown = PR_TRUE;
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
SetOffline(PR_TRUE);
|
|
|
|
|
2002-11-06 13:03:49 +00:00
|
|
|
// Break circular reference.
|
2005-03-25 03:41:33 +00:00
|
|
|
mProxyService = nsnull;
|
2002-05-30 23:11:49 +00:00
|
|
|
}
|
2006-04-04 21:26:17 +00:00
|
|
|
else if (!strcmp(topic, NS_NETWORK_LINK_TOPIC)) {
|
|
|
|
if (!mOfflineForProfileChange && mManageOfflineStatus) {
|
|
|
|
TrackNetworkLinkStatusForOffline();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-08 22:27:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-07-25 20:27:04 +00:00
|
|
|
|
|
|
|
// nsINetUtil interface
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::ParseContentType(const nsACString &aTypeHeader,
|
|
|
|
nsACString &aCharset,
|
|
|
|
PRBool *aHadCharset,
|
|
|
|
nsACString &aContentType)
|
|
|
|
{
|
2006-04-04 21:26:17 +00:00
|
|
|
net_ParseContentType(aTypeHeader, aContentType, aCharset, aHadCharset);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-02 18:54:19 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::ProtocolHasFlags(nsIURI *uri,
|
|
|
|
PRUint32 flags,
|
|
|
|
PRBool *result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(uri);
|
|
|
|
|
|
|
|
*result = PR_FALSE;
|
|
|
|
nsCAutoString scheme;
|
|
|
|
nsresult rv = uri->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 protocolFlags;
|
|
|
|
rv = GetProtocolFlags(scheme.get(), &protocolFlags);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = (protocolFlags & flags) == flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::URIChainHasFlags(nsIURI *uri,
|
|
|
|
PRUint32 flags,
|
|
|
|
PRBool *result)
|
|
|
|
{
|
|
|
|
nsresult rv = ProtocolHasFlags(uri, flags, result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (*result) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dig deeper into the chain. Note that this is not a do/while loop to
|
|
|
|
// avoid the extra addref/release on |uri| in the common (non-nested) case.
|
|
|
|
nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(uri);
|
|
|
|
while (nestedURI) {
|
|
|
|
nsCOMPtr<nsIURI> innerURI;
|
|
|
|
rv = nestedURI->GetInnerURI(getter_AddRefs(innerURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ProtocolHasFlags(innerURI, flags, result);
|
|
|
|
|
|
|
|
if (*result) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nestedURI = do_QueryInterface(innerURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:57:14 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::ToImmutableURI(nsIURI* uri, nsIURI** result)
|
|
|
|
{
|
|
|
|
if (!uri) {
|
|
|
|
*result = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_EnsureSafeToReturn(uri, result);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_TryToSetImmutable(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-08-23 22:48:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::NewSimpleNestedURI(nsIURI* aURI, nsIURI** aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aURI);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> safeURI;
|
|
|
|
nsresult rv = NS_EnsureSafeToReturn(aURI, getter_AddRefs(safeURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aResult = new nsSimpleNestedURI(safeURI));
|
|
|
|
return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2006-04-04 21:26:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::SetManageOfflineStatus(PRBool aManage) {
|
|
|
|
PRBool wasManaged = mManageOfflineStatus;
|
|
|
|
mManageOfflineStatus = aManage;
|
|
|
|
if (mManageOfflineStatus && !wasManaged)
|
|
|
|
return TrackNetworkLinkStatusForOffline();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::GetManageOfflineStatus(PRBool* aManage) {
|
|
|
|
*aManage = mManageOfflineStatus;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIOService::TrackNetworkLinkStatusForOffline()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mManageOfflineStatus,
|
|
|
|
"Don't call this unless we're managing the offline status");
|
|
|
|
if (!mNetworkLinkService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-03-18 14:52:31 +00:00
|
|
|
|
|
|
|
if (mShutdown)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2006-04-04 21:26:17 +00:00
|
|
|
|
|
|
|
// check to make sure this won't collide with Autodial
|
|
|
|
if (mSocketTransportService) {
|
|
|
|
PRBool autodialEnabled = PR_FALSE;
|
|
|
|
mSocketTransportService->GetAutodialEnabled(&autodialEnabled);
|
2007-03-20 01:58:19 +00:00
|
|
|
// If autodialing-on-link-down is enabled, check if the OS auto dial
|
|
|
|
// option is set to always autodial. If so, then we are
|
2006-04-04 21:26:17 +00:00
|
|
|
// always up for the purposes of offline management.
|
2007-03-20 01:58:19 +00:00
|
|
|
if (autodialEnabled) {
|
2009-11-18 15:20:45 +00:00
|
|
|
#if defined(XP_WIN) || defined(MOZ_ENABLE_LIBCONIC)
|
|
|
|
// On Windows and Maemo (libconic) we should first check with the OS
|
|
|
|
// to see if autodial is enabled. If it is enabled then we are
|
|
|
|
// allowed to manage the offline state.
|
2007-03-20 01:58:19 +00:00
|
|
|
if(nsNativeConnectionHelper::IsAutodialEnabled())
|
|
|
|
return SetOffline(PR_FALSE);
|
|
|
|
#else
|
2006-04-04 21:26:17 +00:00
|
|
|
return SetOffline(PR_FALSE);
|
2007-03-20 01:58:19 +00:00
|
|
|
#endif
|
|
|
|
}
|
2006-04-04 21:26:17 +00:00
|
|
|
}
|
2009-11-18 15:20:45 +00:00
|
|
|
|
2006-04-04 21:26:17 +00:00
|
|
|
PRBool isUp;
|
|
|
|
nsresult rv = mNetworkLinkService->GetIsLinkUp(&isUp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return SetOffline(!isUp);
|
2005-07-25 20:27:04 +00:00
|
|
|
}
|
2006-05-02 16:27:23 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::EscapeString(const nsACString& aString,
|
|
|
|
PRUint32 aEscapeType,
|
|
|
|
nsACString& aResult)
|
|
|
|
{
|
2007-04-23 14:19:04 +00:00
|
|
|
NS_ENSURE_ARG_RANGE(aEscapeType, 0, 4);
|
2006-05-02 16:27:23 +00:00
|
|
|
|
|
|
|
nsCAutoString stringCopy(aString);
|
|
|
|
nsCString result;
|
|
|
|
|
|
|
|
if (!NS_Escape(stringCopy, result, (nsEscapeMask) aEscapeType))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
aResult.Assign(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-04-23 14:19:04 +00:00
|
|
|
|
2007-06-21 20:18:25 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::EscapeURL(const nsACString &aStr,
|
|
|
|
PRUint32 aFlags, nsACString &aResult)
|
2007-04-23 14:19:04 +00:00
|
|
|
{
|
2007-06-21 20:18:25 +00:00
|
|
|
aResult.Truncate();
|
2008-09-15 13:34:46 +00:00
|
|
|
NS_EscapeURL(aStr.BeginReading(), aStr.Length(),
|
|
|
|
aFlags | esc_AlwaysCopy, aResult);
|
2007-06-21 20:18:25 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-04-23 14:19:04 +00:00
|
|
|
|
2007-06-21 20:18:25 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::UnescapeString(const nsACString &aStr,
|
|
|
|
PRUint32 aFlags, nsACString &aResult)
|
|
|
|
{
|
|
|
|
aResult.Truncate();
|
2008-09-15 13:34:46 +00:00
|
|
|
NS_UnescapeURL(aStr.BeginReading(), aStr.Length(),
|
|
|
|
aFlags | esc_AlwaysCopy, aResult);
|
2007-04-23 14:19:04 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-06-21 20:18:25 +00:00
|
|
|
|
2007-12-03 21:34:44 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIOService::ExtractCharsetFromContentType(const nsACString &aTypeHeader,
|
|
|
|
nsACString &aCharset,
|
|
|
|
PRInt32 *aCharsetStart,
|
|
|
|
PRInt32 *aCharsetEnd,
|
|
|
|
PRBool *aHadCharset)
|
|
|
|
{
|
|
|
|
nsCAutoString ignored;
|
|
|
|
net_ParseContentType(aTypeHeader, ignored, aCharset, aHadCharset,
|
|
|
|
aCharsetStart, aCharsetEnd);
|
2008-02-29 04:24:32 +00:00
|
|
|
if (*aHadCharset && *aCharsetStart == *aCharsetEnd) {
|
2007-12-03 21:34:44 +00:00
|
|
|
*aHadCharset = PR_FALSE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|