2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2004-10-25 17:15:25 +04:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2001-09-29 00:14:13 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-11-01 23:48:12 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04: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-11-01 23:48:12 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04: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-11-01 23:48:12 +03:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Bradley Baetz <bbaetz@student.usyd.edu.au>
|
2004-12-16 05:46:12 +03:00
|
|
|
* Malcolm Smith <malsmith@cs.rmit.edu.au>
|
2010-04-12 19:44:28 +04:00
|
|
|
* Taras Glek <tglek@mozilla.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04: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-19 02:01:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04: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-19 02:01:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-11-01 23:48:12 +03:00
|
|
|
|
|
|
|
#ifndef nsNetUtil_h__
|
|
|
|
#define nsNetUtil_h__
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsNetError.h"
|
|
|
|
#include "nsNetCID.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsStringGlue.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prio.h" // for read/write flags, permissions, etc.
|
|
|
|
|
2008-10-08 17:16:27 +04:00
|
|
|
#include "nsCRT.h"
|
2002-03-21 01:50:33 +03:00
|
|
|
#include "nsIURI.h"
|
2010-10-16 01:55:53 +04:00
|
|
|
#include "nsIStandardURL.h"
|
2010-11-19 23:58:59 +03:00
|
|
|
#include "nsIURLParser.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsIInputStream.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
#include "nsIOutputStream.h"
|
2004-07-20 02:00:03 +04:00
|
|
|
#include "nsISafeOutputStream.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsIStreamListener.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsIRequestObserverProxy.h"
|
2002-05-03 11:49:43 +04:00
|
|
|
#include "nsISimpleStreamListener.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsILoadGroup.h"
|
1999-11-18 10:36:41 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIChannel.h"
|
2009-02-19 23:25:18 +03:00
|
|
|
#include "nsChannelProperties.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIInputStreamChannel.h"
|
2001-02-21 23:38:08 +03:00
|
|
|
#include "nsITransport.h"
|
2003-02-25 16:57:09 +03:00
|
|
|
#include "nsIStreamTransportService.h"
|
2002-09-27 08:22:13 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2000-09-15 23:27:05 +04:00
|
|
|
#include "nsIDownloader.h"
|
2000-02-03 02:04:47 +03:00
|
|
|
#include "nsIStreamLoader.h"
|
2002-08-28 03:47:25 +04:00
|
|
|
#include "nsIUnicharStreamLoader.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
#include "nsIPipe.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIProtocolHandler.h"
|
2002-09-13 23:32:45 +04:00
|
|
|
#include "nsIFileProtocolHandler.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIStringStream.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
2008-08-28 05:15:32 +04:00
|
|
|
#include "nsIFileURL.h"
|
2001-09-13 06:21:05 +04:00
|
|
|
#include "nsIProtocolProxyService.h"
|
2002-02-19 10:07:23 +03:00
|
|
|
#include "nsIProxyInfo.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIBufferedStreams.h"
|
|
|
|
#include "nsIInputStreamPump.h"
|
|
|
|
#include "nsIAsyncStreamCopier.h"
|
2003-07-12 01:31:54 +04:00
|
|
|
#include "nsIPersistentProperties2.h"
|
2003-10-08 08:27:13 +04:00
|
|
|
#include "nsISyncStreamListener.h"
|
2004-11-15 23:16:52 +03:00
|
|
|
#include "nsInterfaceRequestorAgg.h"
|
2005-07-26 00:27:04 +04:00
|
|
|
#include "nsINetUtil.h"
|
2010-03-03 10:51:09 +03:00
|
|
|
#include "nsIURIWithPrincipal.h"
|
2006-08-27 01:42:54 +04:00
|
|
|
#include "nsIAuthPrompt.h"
|
|
|
|
#include "nsIAuthPrompt2.h"
|
|
|
|
#include "nsIAuthPromptAdapterFactory.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2006-05-02 22:54:19 +04:00
|
|
|
#include "nsINestedURI.h"
|
|
|
|
#include "nsIMutable.h"
|
2007-11-23 23:20:41 +03:00
|
|
|
#include "nsIPropertyBag2.h"
|
2010-04-11 00:08:21 +04:00
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2008-01-31 11:16:54 +03:00
|
|
|
#include "nsIIDNService.h"
|
2009-01-22 01:50:28 +03:00
|
|
|
#include "nsIChannelEventSink.h"
|
2010-04-11 00:08:21 +04:00
|
|
|
#include "nsIChannelPolicy.h"
|
2010-11-23 15:01:30 +03:00
|
|
|
#include "nsISocketProviderService.h"
|
|
|
|
#include "nsISocketProvider.h"
|
2011-09-10 02:41:04 +04:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
2010-04-12 19:44:28 +04:00
|
|
|
#include "mozilla/Services.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
|
2010-11-24 04:19:21 +03:00
|
|
|
#include "nsIRedirectChannelRegistrar.h"
|
|
|
|
|
2010-04-12 19:44:28 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
|
|
|
inline already_AddRefed<nsIIOService>
|
|
|
|
do_GetIOService(nsresult* error = 0)
|
|
|
|
{
|
|
|
|
already_AddRefed<nsIIOService> ret = mozilla::services::GetIOService();
|
|
|
|
if (error)
|
|
|
|
*error = ret.get() ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline already_AddRefed<nsINetUtil>
|
|
|
|
do_GetNetUtil(nsresult *error = 0)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIOService> io = mozilla::services::GetIOService();
|
|
|
|
already_AddRefed<nsINetUtil> ret = nsnull;
|
|
|
|
if (io)
|
|
|
|
CallQueryInterface(io, &ret.mRawPtr);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = ret.get() ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2000-09-09 03:31:26 +04:00
|
|
|
// Helper, to simplify getting the I/O service.
|
2005-11-12 21:17:19 +03:00
|
|
|
inline const nsGetServiceByContractIDWithError
|
2000-09-09 03:31:26 +04:00
|
|
|
do_GetIOService(nsresult* error = 0)
|
|
|
|
{
|
2005-11-12 21:17:19 +03:00
|
|
|
return nsGetServiceByContractIDWithError(NS_IOSERVICE_CONTRACTID, error);
|
2000-09-09 03:31:26 +04:00
|
|
|
}
|
|
|
|
|
2010-04-12 19:44:28 +04:00
|
|
|
// An alias to do_GetIOService
|
|
|
|
inline const nsGetServiceByContractIDWithError
|
|
|
|
do_GetNetUtil(nsresult* error = 0)
|
|
|
|
{
|
|
|
|
return do_GetIOService(error);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
// private little helper function... don't call this directly!
|
|
|
|
inline nsresult
|
|
|
|
net_EnsureIOService(nsIIOService **ios, nsCOMPtr<nsIIOService> &grip)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!*ios) {
|
|
|
|
grip = do_GetIOService(&rv);
|
|
|
|
*ios = grip;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-11-01 23:48:12 +03:00
|
|
|
inline nsresult
|
2002-03-06 10:48:55 +03:00
|
|
|
NS_NewURI(nsIURI **result,
|
|
|
|
const nsACString &spec,
|
|
|
|
const char *charset = nsnull,
|
|
|
|
nsIURI *baseURI = nsnull,
|
|
|
|
nsIIOService *ioService = nsnull) // pass in nsIIOService to optimize callers
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewURI(spec, charset, baseURI, result);
|
|
|
|
return rv;
|
1999-11-01 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2000-03-29 07:58:50 +04:00
|
|
|
NS_NewURI(nsIURI* *result,
|
2002-03-06 10:48:55 +03:00
|
|
|
const nsAString& spec,
|
|
|
|
const char *charset = nsnull,
|
2000-03-29 07:58:50 +04:00
|
|
|
nsIURI* baseURI = nsnull,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
2005-11-08 22:23:00 +03:00
|
|
|
return NS_NewURI(result, NS_ConvertUTF16toUTF8(spec), charset, baseURI, ioService);
|
1999-11-01 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
inline nsresult
|
|
|
|
NS_NewURI(nsIURI* *result,
|
|
|
|
const char *spec,
|
|
|
|
nsIURI* baseURI = nsnull,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
|
|
|
{
|
|
|
|
return NS_NewURI(result, nsDependentCString(spec), nsnull, baseURI, ioService);
|
|
|
|
}
|
2001-06-22 02:02:47 +04:00
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewFileURI(nsIURI* *result,
|
|
|
|
nsIFile* spec,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewFileURI(spec, result);
|
|
|
|
return rv;
|
2001-06-22 02:02:47 +04:00
|
|
|
}
|
|
|
|
|
1999-11-01 23:48:12 +03:00
|
|
|
inline nsresult
|
2010-04-11 00:08:21 +04:00
|
|
|
NS_NewChannel(nsIChannel **result,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIURI *uri,
|
|
|
|
nsIIOService *ioService = nsnull, // pass in nsIIOService to optimize callers
|
|
|
|
nsILoadGroup *loadGroup = nsnull,
|
|
|
|
nsIInterfaceRequestor *callbacks = nsnull,
|
2010-04-11 00:08:21 +04:00
|
|
|
PRUint32 loadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIChannelPolicy *channelPolicy = nsnull)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
2009-03-23 21:53:14 +03:00
|
|
|
nsCOMPtr<nsIChannel> chan;
|
|
|
|
rv = ioService->NewChannelFromURI(uri, getter_AddRefs(chan));
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (loadGroup)
|
|
|
|
rv |= chan->SetLoadGroup(loadGroup);
|
|
|
|
if (callbacks)
|
|
|
|
rv |= chan->SetNotificationCallbacks(callbacks);
|
|
|
|
if (loadFlags != nsIRequest::LOAD_NORMAL)
|
|
|
|
rv |= chan->SetLoadFlags(loadFlags);
|
2010-04-11 00:08:21 +04:00
|
|
|
if (channelPolicy) {
|
|
|
|
nsCOMPtr<nsIWritablePropertyBag2> props = do_QueryInterface(chan, &rv);
|
|
|
|
if (props) {
|
|
|
|
props->SetPropertyAsInterface(NS_CHANNEL_PROP_CHANNEL_POLICY,
|
|
|
|
channelPolicy);
|
|
|
|
}
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2009-03-23 21:53:14 +03:00
|
|
|
chan.forget(result);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
2000-03-29 07:58:50 +04:00
|
|
|
}
|
1999-11-01 23:48:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-05-08 04:26:40 +04:00
|
|
|
// Use this function with CAUTION. It creates a stream that blocks when you
|
|
|
|
// Read() from it and blocking the UI thread is a bad idea. If you don't want
|
2004-05-08 10:45:58 +04:00
|
|
|
// to implement a full blown asynchronous consumer (via nsIStreamListener) look
|
2004-05-08 04:26:40 +04:00
|
|
|
// at nsIStreamLoader instead.
|
1999-11-01 23:48:12 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_OpenURI(nsIInputStream **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIIOService *ioService = nsnull, // pass in nsIIOService to optimize callers
|
|
|
|
nsILoadGroup *loadGroup = nsnull,
|
|
|
|
nsIInterfaceRequestor *callbacks = nsnull,
|
2007-02-04 21:03:13 +03:00
|
|
|
PRUint32 loadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIChannel **channelOut = nsnull)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-03-29 07:58:50 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService,
|
2003-06-19 03:07:09 +04:00
|
|
|
loadGroup, callbacks, loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsIInputStream *stream;
|
|
|
|
rv = channel->Open(&stream);
|
2007-02-04 21:03:13 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-06-19 03:07:09 +04:00
|
|
|
*result = stream;
|
2007-02-04 21:03:13 +03:00
|
|
|
if (channelOut) {
|
|
|
|
*channelOut = nsnull;
|
|
|
|
channel.swap(*channelOut);
|
|
|
|
}
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
1999-11-01 23:48:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_OpenURI(nsIStreamListener *listener,
|
|
|
|
nsISupports *context,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIIOService *ioService = nsnull, // pass in nsIIOService to optimize callers
|
|
|
|
nsILoadGroup *loadGroup = nsnull,
|
|
|
|
nsIInterfaceRequestor *callbacks = nsnull,
|
|
|
|
PRUint32 loadFlags = nsIRequest::LOAD_NORMAL)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-03-29 07:58:50 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService,
|
2003-06-19 03:07:09 +04:00
|
|
|
loadGroup, callbacks, loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = channel->AsyncOpen(listener, context);
|
1999-11-01 23:48:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_MakeAbsoluteURI(nsACString &result,
|
2002-03-06 10:48:55 +03:00
|
|
|
const nsACString &spec,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2002-06-14 21:44:20 +04:00
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_OK;
|
2002-06-14 21:44:20 +04:00
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
else if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(result);
|
|
|
|
else
|
|
|
|
rv = baseURI->Resolve(spec, result);
|
|
|
|
return rv;
|
1999-11-01 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_MakeAbsoluteURI(char **result,
|
|
|
|
const char *spec,
|
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString resultBuf;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_MakeAbsoluteURI(resultBuf, nsDependentCString(spec), baseURI);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = ToNewCString(resultBuf);
|
|
|
|
if (!*result)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
return rv;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_MakeAbsoluteURI(nsAString &result,
|
2002-03-06 10:48:55 +03:00
|
|
|
const nsAString &spec,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
2002-03-06 10:48:55 +03:00
|
|
|
{
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2002-06-14 21:44:20 +04:00
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_OK;
|
2002-06-14 21:44:20 +04:00
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
else {
|
|
|
|
nsCAutoString resultBuf;
|
|
|
|
if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(resultBuf);
|
|
|
|
else
|
2005-11-08 22:23:00 +03:00
|
|
|
rv = baseURI->Resolve(NS_ConvertUTF16toUTF8(spec), resultBuf);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
CopyUTF8toUTF16(resultBuf, result);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-01 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
2008-01-31 11:16:54 +03:00
|
|
|
/**
|
|
|
|
* This function is a helper function to get a scheme's default port.
|
|
|
|
*/
|
|
|
|
inline PRInt32
|
|
|
|
NS_GetDefaultPort(const char *scheme,
|
|
|
|
nsIIOService* ioService = nsnull)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
net_EnsureIOService(&ioService, grip);
|
|
|
|
if (!ioService)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = ioService->GetProtocolHandler(scheme, getter_AddRefs(handler));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
PRInt32 port;
|
|
|
|
rv = handler->GetDefaultPort(&port);
|
|
|
|
return NS_SUCCEEDED(rv) ? port : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is a helper function to apply the ToAscii conversion
|
|
|
|
* to a string
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2008-01-31 11:16:54 +03:00
|
|
|
NS_StringToACE(const nsACString &idn, nsACString &result)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIIDNService> idnSrv = do_GetService(NS_IDNSERVICE_CONTRACTID);
|
|
|
|
if (!idnSrv)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-01-31 11:16:54 +03:00
|
|
|
nsresult rv = idnSrv->ConvertUTF8toACE(idn, result);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-01-31 11:16:54 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-01-31 11:16:54 +03:00
|
|
|
}
|
|
|
|
|
2006-08-27 01:42:54 +04:00
|
|
|
/**
|
|
|
|
* This function is a helper function to get a protocol's default port if the
|
|
|
|
* URI does not specify a port explicitly. Returns -1 if this protocol has no
|
|
|
|
* concept of ports or if there was an error getting the port.
|
|
|
|
*/
|
|
|
|
inline PRInt32
|
|
|
|
NS_GetRealPort(nsIURI* aURI,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
|
|
|
{
|
|
|
|
PRInt32 port;
|
|
|
|
nsresult rv = aURI->GetPort(&port);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (port != -1)
|
|
|
|
return port; // explicitly specified
|
|
|
|
|
|
|
|
// Otherwise, we have to get the default port from the protocol handler
|
|
|
|
|
|
|
|
// Need the scheme first
|
|
|
|
nsCAutoString scheme;
|
|
|
|
rv = aURI->GetScheme(scheme);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
2008-01-31 11:16:54 +03:00
|
|
|
return NS_GetDefaultPort(scheme.get());
|
2006-08-27 01:42:54 +04:00
|
|
|
}
|
|
|
|
|
1999-11-01 23:48:12 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewInputStreamChannel(nsIChannel **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIInputStream *stream,
|
2004-10-25 17:15:25 +04:00
|
|
|
const nsACString &contentType,
|
|
|
|
const nsACString *contentCharset)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIInputStreamChannel> isc =
|
|
|
|
do_CreateInstance(NS_INPUTSTREAMCHANNEL_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv |= isc->SetURI(uri);
|
|
|
|
rv |= isc->SetContentStream(stream);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(isc, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!contentType.IsEmpty())
|
|
|
|
rv |= chan->SetContentType(contentType);
|
|
|
|
if (contentCharset && !contentCharset->IsEmpty())
|
|
|
|
rv |= chan->SetContentCharset(*contentCharset);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
*result = nsnull;
|
|
|
|
chan.swap(*result);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
1999-11-01 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
2004-10-25 17:15:25 +04:00
|
|
|
inline nsresult
|
|
|
|
NS_NewInputStreamChannel(nsIChannel **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIInputStream *stream,
|
|
|
|
const nsACString &contentType = EmptyCString())
|
|
|
|
{
|
|
|
|
return NS_NewInputStreamChannel(result, uri, stream, contentType, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewInputStreamChannel(nsIChannel **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIInputStream *stream,
|
|
|
|
const nsACString &contentType,
|
|
|
|
const nsACString &contentCharset)
|
|
|
|
{
|
|
|
|
return NS_NewInputStreamChannel(result, uri, stream, contentType,
|
|
|
|
&contentCharset);
|
|
|
|
}
|
|
|
|
|
2000-04-13 13:20:50 +04:00
|
|
|
inline nsresult
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_NewInputStreamPump(nsIInputStreamPump **result,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIInputStream *stream,
|
2011-04-01 04:15:27 +04:00
|
|
|
PRInt64 streamPos = PRInt64(-1),
|
|
|
|
PRInt64 streamLen = PRInt64(-1),
|
2003-06-19 03:07:09 +04:00
|
|
|
PRUint32 segsize = 0,
|
|
|
|
PRUint32 segcount = 0,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool closeWhenDone = false)
|
2000-04-13 13:20:50 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIInputStreamPump> pump =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = pump->Init(stream, streamPos, streamLen,
|
|
|
|
segsize, segcount, closeWhenDone);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
pump.swap(*result);
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2000-04-13 13:20:50 +04:00
|
|
|
}
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// NOTE: you will need to specify whether or not your streams are buffered
|
|
|
|
// (i.e., do they implement ReadSegments/WriteSegments). the default
|
|
|
|
// assumption of TRUE for both streams might not be right for you!
|
1999-11-30 07:50:42 +03:00
|
|
|
inline nsresult
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIEventTarget *target,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool sourceBuffered = true,
|
|
|
|
bool sinkBuffered = true,
|
2009-03-30 23:10:13 +04:00
|
|
|
PRUint32 chunkSize = 0,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool closeSource = true,
|
|
|
|
bool closeSink = true)
|
1999-11-30 07:50:42 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIAsyncStreamCopier> copier =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_ASYNCSTREAMCOPIER_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2009-03-30 23:10:13 +04:00
|
|
|
rv = copier->Init(source, sink, target, sourceBuffered, sinkBuffered,
|
|
|
|
chunkSize, closeSource, closeSink);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
copier.swap(*result);
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
1999-11-30 07:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewLoadGroup(nsILoadGroup **result,
|
|
|
|
nsIRequestObserver *obs)
|
1999-11-30 07:50:42 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> group =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_LOADGROUP_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = group->SetGroupObserver(obs);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
group.swap(*result);
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
1999-11-30 07:50:42 +03:00
|
|
|
}
|
|
|
|
|
2000-09-15 23:27:05 +04:00
|
|
|
inline nsresult
|
2003-06-24 02:58:28 +04:00
|
|
|
NS_NewDownloader(nsIStreamListener **result,
|
|
|
|
nsIDownloadObserver *observer,
|
|
|
|
nsIFile *downloadLocation = nsnull)
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIDownloader> downloader =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_DOWNLOADER_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-06-24 02:58:28 +04:00
|
|
|
rv = downloader->Init(observer, downloadLocation);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = downloader);
|
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-11-01 23:48:12 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewStreamLoader(nsIStreamLoader **result,
|
2006-10-25 01:47:40 +04:00
|
|
|
nsIStreamLoaderObserver *observer)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-10-25 01:47:40 +04:00
|
|
|
rv = loader->Init(observer);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
loader.swap(*result);
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
1999-11-01 23:48:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-09-27 08:22:13 +04:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewStreamLoader(nsIStreamLoader **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIStreamLoaderObserver *observer,
|
|
|
|
nsISupports *context = nsnull,
|
|
|
|
nsILoadGroup *loadGroup = nsnull,
|
|
|
|
nsIInterfaceRequestor *callbacks = nsnull,
|
|
|
|
PRUint32 loadFlags = nsIRequest::LOAD_NORMAL,
|
|
|
|
nsIURI *referrer = nsnull)
|
2002-09-27 08:22:13 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
nsnull,
|
|
|
|
loadGroup,
|
2003-06-19 03:07:09 +04:00
|
|
|
callbacks,
|
|
|
|
loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel)
|
|
|
|
httpChannel->SetReferrer(referrer);
|
2006-10-25 01:47:40 +04:00
|
|
|
rv = NS_NewStreamLoader(result, observer);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = channel->AsyncOpen(*result, context);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2002-09-27 08:22:13 +04:00
|
|
|
}
|
|
|
|
|
2002-08-28 03:47:25 +04:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **result,
|
2011-05-19 05:22:59 +04:00
|
|
|
nsIUnicharStreamLoaderObserver *observer)
|
2002-08-28 03:47:25 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIUnicharStreamLoader> loader =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_UNICHARSTREAMLOADER_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-05-19 05:22:59 +04:00
|
|
|
rv = loader->Init(observer);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
loader.swap(*result);
|
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
2002-08-28 03:47:25 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-10-08 08:27:13 +04:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewSyncStreamListener(nsIStreamListener **result,
|
|
|
|
nsIInputStream **stream)
|
2003-10-08 08:27:13 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISyncStreamListener> listener =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_SYNCSTREAMLISTENER_CONTRACTID, &rv);
|
2003-10-08 08:27:13 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = listener->GetInputStream(stream);
|
2003-10-08 08:27:13 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = listener); // cannot use nsCOMPtr::swap
|
2003-10-08 08:27:13 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
|
|
|
|
* AsyncOpen method.
|
2003-10-08 08:37:38 +04:00
|
|
|
*
|
|
|
|
* NOTE: Reading from the returned nsIInputStream may spin the current
|
|
|
|
* thread's event queue, which could result in any event being processed.
|
2003-10-08 08:27:13 +04:00
|
|
|
*/
|
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ImplementChannelOpen(nsIChannel *channel,
|
|
|
|
nsIInputStream **result)
|
2003-10-08 08:27:13 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = NS_NewSyncStreamListener(getter_AddRefs(listener),
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = channel->AsyncOpen(listener, nsnull);
|
2003-10-08 08:27:13 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 n;
|
|
|
|
// block until the initial response is received or an error occurs.
|
|
|
|
rv = stream->Available(&n);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
stream.swap(*result);
|
|
|
|
}
|
2003-10-08 08:27:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-01-24 01:10:34 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewRequestObserverProxy(nsIRequestObserver **result,
|
|
|
|
nsIRequestObserver *observer,
|
2006-05-10 21:30:15 +04:00
|
|
|
nsIEventTarget *target = nsnull)
|
2001-01-24 01:10:34 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIRequestObserverProxy> proxy =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_REQUESTOBSERVERPROXY_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = proxy->Init(observer, target);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = proxy); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2001-01-24 01:10:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewSimpleStreamListener(nsIStreamListener **result,
|
|
|
|
nsIOutputStream *sink,
|
|
|
|
nsIRequestObserver *observer = nsnull)
|
2001-01-24 01:10:34 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsISimpleStreamListener> listener =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_SIMPLESTREAMLISTENER_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = listener->Init(sink, observer);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = listener); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2001-01-24 01:10:34 +03:00
|
|
|
}
|
|
|
|
|
2001-06-06 04:10:09 +04:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_CheckPortSafety(PRInt32 port,
|
|
|
|
const char *scheme,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2001-06-06 04:10:09 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allow;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = ioService->AllowPort(port, scheme, &allow);
|
2006-06-24 05:25:17 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !allow) {
|
|
|
|
NS_WARNING("port blocked");
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_ERROR_PORT_ACCESS_NOT_ALLOWED;
|
2006-06-24 05:25:17 +04:00
|
|
|
}
|
2001-06-06 04:10:09 +04:00
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
return rv;
|
2001-06-06 04:10:09 +04:00
|
|
|
}
|
|
|
|
|
2006-06-24 05:25:17 +04:00
|
|
|
// Determine if this URI is using a safe port.
|
|
|
|
inline nsresult
|
|
|
|
NS_CheckPortSafety(nsIURI *uri) {
|
|
|
|
PRInt32 port;
|
|
|
|
nsresult rv = uri->GetPort(&port);
|
|
|
|
if (NS_FAILED(rv) || port == -1) // port undefined or default-valued
|
|
|
|
return NS_OK;
|
|
|
|
nsCAutoString scheme;
|
|
|
|
uri->GetScheme(scheme);
|
|
|
|
return NS_CheckPortSafety(port, scheme.get());
|
|
|
|
}
|
|
|
|
|
2001-09-13 06:21:05 +04:00
|
|
|
inline nsresult
|
2004-07-02 03:31:47 +04:00
|
|
|
NS_NewProxyInfo(const nsACString &type,
|
|
|
|
const nsACString &host,
|
|
|
|
PRInt32 port,
|
2004-12-16 05:46:12 +03:00
|
|
|
PRUint32 flags,
|
2004-07-02 03:31:47 +04:00
|
|
|
nsIProxyInfo **result)
|
2001-09-13 06:21:05 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps =
|
|
|
|
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-03-25 06:41:33 +03:00
|
|
|
rv = pps->NewProxyInfo(type, host, port, flags, PR_UINT32_MAX, nsnull,
|
|
|
|
result);
|
2003-06-19 03:07:09 +04:00
|
|
|
return rv;
|
2001-09-13 06:21:05 +04:00
|
|
|
}
|
|
|
|
|
2001-12-19 03:55:42 +03:00
|
|
|
inline nsresult
|
2002-09-13 23:32:45 +04:00
|
|
|
NS_GetFileProtocolHandler(nsIFileProtocolHandler **result,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIIOService *ioService = nsnull)
|
2001-12-19 03:55:42 +03:00
|
|
|
{
|
2002-09-13 23:32:45 +04:00
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = ioService->GetProtocolHandler("file", getter_AddRefs(handler));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = CallQueryInterface(handler, result);
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
2003-06-19 03:07:09 +04:00
|
|
|
return rv;
|
2002-09-13 23:32:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_GetFileFromURLSpec(const nsACString &inURL,
|
|
|
|
nsIFile **result,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2002-09-13 23:32:45 +04:00
|
|
|
{
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2002-09-13 23:32:45 +04:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetFileFromURLSpec(inURL, result);
|
|
|
|
return rv;
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_GetURLSpecFromFile(nsIFile *file,
|
|
|
|
nsACString &url,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIIOService *ioService = nsnull)
|
2001-12-19 03:55:42 +03:00
|
|
|
{
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2002-09-13 23:32:45 +04:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
2003-06-19 03:07:09 +04:00
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = fileHandler->GetURLSpecFromFile(file, url);
|
2003-06-19 03:07:09 +04:00
|
|
|
return rv;
|
2007-11-23 23:20:41 +03:00
|
|
|
}
|
|
|
|
|
2009-10-06 17:43:59 +04:00
|
|
|
/**
|
|
|
|
* Converts the nsIFile to the corresponding URL string.
|
|
|
|
* Should only be called on files which are not directories,
|
|
|
|
* is otherwise identical to NS_GetURLSpecFromFile, but is
|
|
|
|
* usually more efficient.
|
|
|
|
* Warning: this restriction may not be enforced at runtime!
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetURLSpecFromActualFile(nsIFile *file,
|
|
|
|
nsACString &url,
|
|
|
|
nsIIOService *ioService = nsnull)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromActualFile(file, url);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the nsIFile to the corresponding URL string.
|
|
|
|
* Should only be called on files which are directories,
|
|
|
|
* is otherwise identical to NS_GetURLSpecFromFile, but is
|
|
|
|
* usually more efficient.
|
|
|
|
* Warning: this restriction may not be enforced at runtime!
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetURLSpecFromDir(nsIFile *file,
|
|
|
|
nsACString &url,
|
|
|
|
nsIIOService *ioService = nsnull)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromDir(file, url);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-11-23 23:20:41 +03:00
|
|
|
/**
|
|
|
|
* Obtains the referrer for a given channel. This first tries to obtain the
|
|
|
|
* referrer from the property docshell.internalReferrer, and if that doesn't
|
|
|
|
* work and the channel is an nsIHTTPChannel, we check it's referrer property.
|
|
|
|
*
|
|
|
|
* @returns NS_ERROR_NOT_AVAILABLE if no referrer is available.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetReferrerFromChannel(nsIChannel *channel,
|
|
|
|
nsIURI **referrer)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
*referrer = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props(do_QueryInterface(channel));
|
|
|
|
if (props) {
|
|
|
|
// We have to check for a property on a property bag because the
|
|
|
|
// referrer may be empty for security reasons (for example, when loading
|
|
|
|
// an http page with an https referrer).
|
|
|
|
rv = props->GetPropertyAsInterface(NS_LITERAL_STRING("docshell.internalReferrer"),
|
|
|
|
NS_GET_IID(nsIURI),
|
|
|
|
reinterpret_cast<void **>(referrer));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
*referrer = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if that didn't work, we can still try to get the referrer from the
|
|
|
|
// nsIHttpChannel (if we can QI to it)
|
|
|
|
if (!(*referrer)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> chan(do_QueryInterface(channel));
|
|
|
|
if (chan) {
|
|
|
|
rv = chan->GetReferrer(referrer);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
*referrer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2007-11-23 23:20:47 +03:00
|
|
|
}
|
|
|
|
|
2005-11-08 22:23:00 +03:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
2002-02-19 10:07:23 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_ExamineForProxy(const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
PRInt32 port,
|
|
|
|
nsIProxyInfo **proxyInfo)
|
2002-02-19 10:07:23 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps =
|
|
|
|
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCAutoString spec(scheme);
|
|
|
|
spec.Append("://");
|
|
|
|
spec.Append(host);
|
|
|
|
spec.Append(':');
|
|
|
|
spec.AppendInt(port);
|
|
|
|
// XXXXX - Under no circumstances whatsoever should any code which
|
|
|
|
// wants a uri do this. I do this here because I do not, in fact,
|
|
|
|
// actually want a uri (the dummy uris created here may not be
|
|
|
|
// syntactically valid for the specific protocol), and all we need
|
|
|
|
// is something which has a valid scheme, hostname, and a string
|
|
|
|
// to pass to PAC if needed - bbaetz
|
2005-11-12 21:17:19 +03:00
|
|
|
nsCOMPtr<nsIURI> uri =
|
|
|
|
do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = uri->SetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-03-25 06:41:33 +03:00
|
|
|
rv = pps->Resolve(uri, 0, proxyInfo);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2002-02-19 10:07:23 +03:00
|
|
|
}
|
2005-11-08 22:23:00 +03:00
|
|
|
#endif
|
2002-02-19 10:07:23 +03:00
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
inline nsresult
|
|
|
|
NS_ParseContentType(const nsACString &rawContentType,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCString &contentType,
|
|
|
|
nsCString &contentCharset)
|
2002-03-21 01:50:33 +03:00
|
|
|
{
|
|
|
|
// contentCharset is left untouched if not present in rawContentType
|
2005-07-26 00:27:04 +04:00
|
|
|
nsresult rv;
|
2010-04-12 19:44:28 +04:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2005-07-26 00:27:04 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCString charset;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hadCharset;
|
2005-07-26 00:27:04 +04:00
|
|
|
rv = util->ParseContentType(rawContentType, charset, &hadCharset,
|
|
|
|
contentType);
|
|
|
|
if (NS_SUCCEEDED(rv) && hadCharset)
|
|
|
|
contentCharset = charset;
|
|
|
|
return rv;
|
2002-03-21 01:50:33 +03:00
|
|
|
}
|
|
|
|
|
2007-12-04 00:34:44 +03:00
|
|
|
inline nsresult
|
|
|
|
NS_ExtractCharsetFromContentType(const nsACString &rawContentType,
|
|
|
|
nsCString &contentCharset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *hadCharset,
|
2007-12-04 00:34:44 +03:00
|
|
|
PRInt32 *charsetStart,
|
|
|
|
PRInt32 *charsetEnd)
|
|
|
|
{
|
|
|
|
// contentCharset is left untouched if not present in rawContentType
|
|
|
|
nsresult rv;
|
2010-04-12 19:44:28 +04:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2007-12-04 00:34:44 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return util->ExtractCharsetFromContentType(rawContentType,
|
|
|
|
contentCharset,
|
|
|
|
charsetStart,
|
|
|
|
charsetEnd,
|
|
|
|
hadCharset);
|
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewLocalFileInputStream(nsIInputStream **result,
|
|
|
|
nsIFile *file,
|
|
|
|
PRInt32 ioFlags = -1,
|
|
|
|
PRInt32 perm = -1,
|
|
|
|
PRInt32 behaviorFlags = 0)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIFileInputStream> in =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = in->Init(file, ioFlags, perm, behaviorFlags);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = in); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
2002-02-19 10:07:23 +03:00
|
|
|
|
2010-10-14 03:25:33 +04:00
|
|
|
inline nsresult
|
|
|
|
NS_NewPartialLocalFileInputStream(nsIInputStream **result,
|
|
|
|
nsIFile *file,
|
|
|
|
PRUint64 offset,
|
|
|
|
PRUint64 length,
|
|
|
|
PRInt32 ioFlags = -1,
|
|
|
|
PRInt32 perm = -1,
|
|
|
|
PRInt32 behaviorFlags = 0)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPartialFileInputStream> in =
|
|
|
|
do_CreateInstance(NS_PARTIALLOCALFILEINPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(file, offset, length, ioFlags, perm, behaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = CallQueryInterface(in, result);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewLocalFileOutputStream(nsIOutputStream **result,
|
|
|
|
nsIFile *file,
|
|
|
|
PRInt32 ioFlags = -1,
|
|
|
|
PRInt32 perm = -1,
|
|
|
|
PRInt32 behaviorFlags = 0)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_LOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = out->Init(file, ioFlags, perm, behaviorFlags);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
2004-07-20 02:00:03 +04:00
|
|
|
// returns a file output stream which can be QI'ed to nsISafeOutputStream.
|
2004-07-07 00:35:40 +04:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewSafeLocalFileOutputStream(nsIOutputStream **result,
|
|
|
|
nsIFile *file,
|
|
|
|
PRInt32 ioFlags = -1,
|
|
|
|
PRInt32 perm = -1,
|
|
|
|
PRInt32 behaviorFlags = 0)
|
2004-07-07 00:35:40 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_SAFELOCALFILEOUTPUTSTREAM_CONTRACTID, &rv);
|
2004-07-07 00:35:40 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = out->Init(file, ioFlags, perm, behaviorFlags);
|
2004-07-07 00:35:40 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap
|
2004-07-07 00:35:40 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
// returns the input end of a pipe. the output end of the pipe
|
|
|
|
// is attached to the original stream. data from the original
|
|
|
|
// stream is read into the pipe on a background thread.
|
2003-02-25 16:57:09 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_BackgroundInputStream(nsIInputStream **result,
|
|
|
|
nsIInputStream *stream,
|
|
|
|
PRUint32 segmentSize = 0,
|
|
|
|
PRUint32 segmentCount = 0)
|
2003-02-25 16:57:09 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
2011-04-01 04:15:27 +04:00
|
|
|
rv = sts->CreateInputTransport(stream, PRInt64(-1), PRInt64(-1),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, getter_AddRefs(inTransport));
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenInputStream(nsITransport::OPEN_BLOCKING,
|
2005-11-12 21:17:19 +03:00
|
|
|
segmentSize, segmentCount,
|
|
|
|
result);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-02-25 16:57:09 +03:00
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
// returns the output end of a pipe. the input end of the pipe
|
|
|
|
// is attached to the original stream. data written to the pipe
|
|
|
|
// is copied to the original stream on a background thread.
|
2003-02-25 16:57:09 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_BackgroundOutputStream(nsIOutputStream **result,
|
|
|
|
nsIOutputStream *stream,
|
|
|
|
PRUint32 segmentSize = 0,
|
|
|
|
PRUint32 segmentCount = 0)
|
2003-02-25 16:57:09 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
2011-04-01 04:15:27 +04:00
|
|
|
rv = sts->CreateOutputTransport(stream, PRInt64(-1), PRInt64(-1),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, getter_AddRefs(inTransport));
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING,
|
2005-11-12 21:17:19 +03:00
|
|
|
segmentSize, segmentCount,
|
|
|
|
result);
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-02-25 16:57:09 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewBufferedInputStream(nsIInputStream **result,
|
|
|
|
nsIInputStream *str,
|
|
|
|
PRUint32 bufferSize)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIBufferedInputStream> in =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_BUFFEREDINPUTSTREAM_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = in->Init(str, bufferSize);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = in); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
2004-07-20 02:00:03 +04:00
|
|
|
// note: the resulting stream can be QI'ed to nsISafeOutputStream iff the
|
|
|
|
// provided stream supports it.
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewBufferedOutputStream(nsIOutputStream **result,
|
|
|
|
nsIOutputStream *str,
|
|
|
|
PRUint32 bufferSize)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIBufferedOutputStream> out =
|
2005-11-12 21:17:19 +03:00
|
|
|
do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2005-11-12 21:17:19 +03:00
|
|
|
rv = out->Init(str, bufferSize);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap
|
2003-06-19 03:07:09 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
2008-01-04 20:55:02 +03:00
|
|
|
/**
|
|
|
|
* Attempts to buffer a given output stream. If this fails, it returns the
|
|
|
|
* passed-in output stream.
|
|
|
|
*
|
|
|
|
* @param aOutputStream
|
|
|
|
* The output stream we want to buffer. This cannot be null.
|
|
|
|
* @param aBufferSize
|
|
|
|
* The size of the buffer for the buffered output stream.
|
|
|
|
* @returns an nsIOutputStream that is buffered with the specified buffer size,
|
|
|
|
* or is aOutputStream if creating the new buffered stream failed.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIOutputStream>
|
|
|
|
NS_BufferOutputStream(nsIOutputStream *aOutputStream,
|
|
|
|
PRUint32 aBufferSize)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aOutputStream, "No output stream given!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> bos;
|
|
|
|
nsresult rv = NS_NewBufferedOutputStream(getter_AddRefs(bos), aOutputStream,
|
|
|
|
aBufferSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return bos.forget();
|
|
|
|
|
|
|
|
NS_ADDREF(aOutputStream);
|
|
|
|
return aOutputStream;
|
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
// returns an input stream compatible with nsIUploadChannel::SetUploadStream()
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewPostDataStream(nsIInputStream **result,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isFile,
|
2003-01-18 05:15:14 +03:00
|
|
|
const nsACString &data,
|
2003-06-19 03:07:09 +04:00
|
|
|
PRUint32 encodeFlags,
|
|
|
|
nsIIOService *unused = nsnull)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
2005-11-08 22:23:00 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
if (isFile) {
|
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewNativeLocalFile(data, false, getter_AddRefs(file));
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileStream), file);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// wrap the file stream with a buffered input stream
|
|
|
|
rv = NS_NewBufferedInputStream(result, fileStream, 8192);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
// otherwise, create a string stream for the data (copies)
|
2005-11-08 22:23:00 +03:00
|
|
|
nsCOMPtr<nsIStringInputStream> stream
|
|
|
|
(do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = stream->SetData(data.BeginReading(), data.Length());
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ADDREF(*result = stream);
|
|
|
|
return NS_OK;
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
2010-05-31 02:30:28 +04:00
|
|
|
inline nsresult
|
2011-06-08 22:11:41 +04:00
|
|
|
NS_ReadInputStreamToBuffer(nsIInputStream *aInputStream,
|
|
|
|
void** aDest,
|
2010-05-31 02:30:28 +04:00
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-06-08 22:11:41 +04:00
|
|
|
if (!*aDest) {
|
|
|
|
*aDest = malloc(aCount);
|
|
|
|
if (!*aDest)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * p = reinterpret_cast<char*>(*aDest);
|
2010-05-31 02:30:28 +04:00
|
|
|
PRUint32 bytesRead;
|
|
|
|
PRUint32 totalRead = 0;
|
|
|
|
while (1) {
|
|
|
|
rv = aInputStream->Read(p + totalRead, aCount - totalRead, &bytesRead);
|
|
|
|
if (!NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
|
|
|
totalRead += bytesRead;
|
|
|
|
if (totalRead == aCount)
|
|
|
|
break;
|
|
|
|
// if Read reads 0 bytes, we've hit EOF
|
|
|
|
if (bytesRead == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-08 22:11:41 +04:00
|
|
|
inline nsresult
|
|
|
|
NS_ReadInputStreamToString(nsIInputStream *aInputStream,
|
|
|
|
nsACString &aDest,
|
|
|
|
PRUint32 aCount)
|
|
|
|
{
|
|
|
|
aDest.SetLength(aCount);
|
|
|
|
if (aDest.Length() != aCount)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
void* dest = aDest.BeginWriting();
|
|
|
|
return NS_ReadInputStreamToBuffer(aInputStream, &dest, aCount);
|
|
|
|
}
|
|
|
|
|
2003-07-12 01:31:54 +04:00
|
|
|
inline nsresult
|
|
|
|
NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties **result,
|
2005-11-12 21:17:19 +03:00
|
|
|
nsIURI *uri,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2003-07-12 01:31:54 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
nsresult rv = NS_OpenURI(getter_AddRefs(in), uri, ioService);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIPersistentProperties> properties =
|
|
|
|
do_CreateInstance(NS_PERSISTENTPROPERTIES_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = properties->Load(in);
|
2005-11-12 21:17:19 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
properties.swap(*result);
|
|
|
|
}
|
2003-07-12 01:31:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_LoadPersistentPropertiesFromURISpec(nsIPersistentProperties **result,
|
|
|
|
const nsACString &spec,
|
|
|
|
const char *charset = nsnull,
|
|
|
|
nsIURI *baseURI = nsnull,
|
|
|
|
nsIIOService *ioService = nsnull)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv =
|
|
|
|
NS_NewURI(getter_AddRefs(uri), spec, charset, baseURI, ioService);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = NS_LoadPersistentPropertiesFromURI(result, uri, ioService);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-11-15 23:16:52 +03:00
|
|
|
/**
|
|
|
|
* NS_QueryNotificationCallbacks implements the canonical algorithm for
|
|
|
|
* querying interfaces from a channel's notification callbacks. It first
|
|
|
|
* searches the channel's notificationCallbacks attribute, and if the interface
|
|
|
|
* is not found there, then it inspects the notificationCallbacks attribute of
|
|
|
|
* the channel's loadGroup.
|
|
|
|
*/
|
|
|
|
inline void
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(nsIChannel *channel,
|
|
|
|
const nsIID &iid,
|
|
|
|
void **result)
|
2004-11-15 23:16:52 +03:00
|
|
|
{
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_PRECONDITION(channel, "null channel");
|
|
|
|
*result = nsnull;
|
2004-11-15 23:16:52 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
2005-11-12 21:17:19 +03:00
|
|
|
channel->GetNotificationCallbacks(getter_AddRefs(cbs));
|
2004-11-15 23:16:52 +03:00
|
|
|
if (cbs)
|
2005-11-12 21:17:19 +03:00
|
|
|
cbs->GetInterface(iid, result);
|
|
|
|
if (!*result) {
|
2004-11-15 23:16:52 +03:00
|
|
|
// try load group's notification callbacks...
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2005-11-12 21:17:19 +03:00
|
|
|
channel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2004-11-15 23:16:52 +03:00
|
|
|
if (loadGroup) {
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
if (cbs)
|
2005-11-12 21:17:19 +03:00
|
|
|
cbs->GetInterface(iid, result);
|
2004-11-15 23:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(nsIChannel *channel,
|
|
|
|
nsCOMPtr<T> &result)
|
2004-11-15 23:16:52 +03:00
|
|
|
{
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(channel, NS_GET_TEMPLATE_IID(T),
|
|
|
|
getter_AddRefs(result));
|
2004-11-15 23:16:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternate form of NS_QueryNotificationCallbacks designed for use by
|
|
|
|
* nsIChannel implementations.
|
|
|
|
*/
|
|
|
|
inline void
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
const nsIID &iid,
|
|
|
|
void **result)
|
2004-11-15 23:16:52 +03:00
|
|
|
{
|
2005-11-12 21:17:19 +03:00
|
|
|
*result = nsnull;
|
2004-11-15 23:16:52 +03:00
|
|
|
|
2005-11-12 21:17:19 +03:00
|
|
|
if (callbacks)
|
|
|
|
callbacks->GetInterface(iid, result);
|
|
|
|
if (!*result) {
|
2004-11-15 23:16:52 +03:00
|
|
|
// try load group's notification callbacks...
|
2005-11-12 21:17:19 +03:00
|
|
|
if (loadGroup) {
|
2004-11-15 23:16:52 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
2005-11-12 21:17:19 +03:00
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
2004-11-15 23:16:52 +03:00
|
|
|
if (cbs)
|
2005-11-12 21:17:19 +03:00
|
|
|
cbs->GetInterface(iid, result);
|
2004-11-15 23:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 01:42:54 +04:00
|
|
|
/**
|
|
|
|
* Wraps an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2. This
|
|
|
|
* method is provided mainly for use by other methods in this file.
|
|
|
|
*
|
|
|
|
* *aAuthPrompt2 should be set to null before calling this function.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_WrapAuthPrompt(nsIAuthPrompt *aAuthPrompt, nsIAuthPrompt2** aAuthPrompt2)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAuthPromptAdapterFactory> factory =
|
|
|
|
do_GetService(NS_AUTHPROMPT_ADAPTER_FACTORY_CONTRACTID);
|
|
|
|
if (!factory)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_WARNING("Using deprecated nsIAuthPrompt");
|
|
|
|
factory->CreateAdapter(aAuthPrompt, aAuthPrompt2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an auth prompt from an interface requestor. This takes care of wrapping
|
|
|
|
* an nsIAuthPrompt so that it can be used as an nsIAuthPrompt2.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_QueryAuthPrompt2(nsIInterfaceRequestor *aCallbacks,
|
|
|
|
nsIAuthPrompt2 **aAuthPrompt)
|
|
|
|
{
|
|
|
|
CallGetInterface(aCallbacks, aAuthPrompt);
|
|
|
|
if (*aAuthPrompt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Maybe only nsIAuthPrompt is provided and we have to wrap it.
|
|
|
|
nsCOMPtr<nsIAuthPrompt> prompt(do_GetInterface(aCallbacks));
|
|
|
|
if (!prompt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_WrapAuthPrompt(prompt, aAuthPrompt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an nsIAuthPrompt2 from a channel. Use this instead of
|
|
|
|
* NS_QueryNotificationCallbacks for better backwards compatibility.
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_QueryAuthPrompt2(nsIChannel *aChannel,
|
|
|
|
nsIAuthPrompt2 **aAuthPrompt)
|
|
|
|
{
|
|
|
|
*aAuthPrompt = nsnull;
|
|
|
|
|
|
|
|
// We want to use any auth prompt we can find on the channel's callbacks,
|
|
|
|
// and if that fails use the loadgroup's prompt (if any)
|
|
|
|
// Therefore, we can't just use NS_QueryNotificationCallbacks, because
|
|
|
|
// that would prefer a loadgroup's nsIAuthPrompt2 over a channel's
|
|
|
|
// nsIAuthPrompt.
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
aChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (callbacks) {
|
|
|
|
NS_QueryAuthPrompt2(callbacks, aAuthPrompt);
|
|
|
|
if (*aAuthPrompt)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> group;
|
|
|
|
aChannel->GetLoadGroup(getter_AddRefs(group));
|
|
|
|
if (!group)
|
|
|
|
return;
|
|
|
|
|
|
|
|
group->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (!callbacks)
|
|
|
|
return;
|
|
|
|
NS_QueryAuthPrompt2(callbacks, aAuthPrompt);
|
|
|
|
}
|
|
|
|
|
2004-11-15 23:16:52 +03:00
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
nsCOMPtr<T> &result)
|
2004-11-15 23:16:52 +03:00
|
|
|
{
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_QueryNotificationCallbacks(callbacks, loadGroup,
|
2005-11-11 17:36:26 +03:00
|
|
|
NS_GET_TEMPLATE_IID(T),
|
2005-11-12 21:17:19 +03:00
|
|
|
getter_AddRefs(result));
|
2004-11-15 23:16:52 +03:00
|
|
|
}
|
|
|
|
|
2006-04-24 06:54:26 +04:00
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(const nsCOMPtr<nsIInterfaceRequestor> &aCallbacks,
|
|
|
|
const nsCOMPtr<nsILoadGroup> &aLoadGroup,
|
|
|
|
nsCOMPtr<T> &aResult)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(aCallbacks.get(), aLoadGroup.get(), aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* template helper */
|
|
|
|
template <class T> inline void
|
|
|
|
NS_QueryNotificationCallbacks(const nsCOMPtr<nsIChannel> &aChannel,
|
|
|
|
nsCOMPtr<T> &aResult)
|
|
|
|
{
|
|
|
|
NS_QueryNotificationCallbacks(aChannel.get(), aResult);
|
|
|
|
}
|
|
|
|
|
2004-11-15 23:16:52 +03:00
|
|
|
/**
|
|
|
|
* This function returns a nsIInterfaceRequestor instance that returns the
|
|
|
|
* same result as NS_QueryNotificationCallbacks when queried. It is useful
|
|
|
|
* as the value for nsISocketTransport::securityCallbacks.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
2005-11-12 21:17:19 +03:00
|
|
|
NS_NewNotificationCallbacksAggregation(nsIInterfaceRequestor *callbacks,
|
|
|
|
nsILoadGroup *loadGroup,
|
|
|
|
nsIInterfaceRequestor **result)
|
2004-11-15 23:16:52 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
2005-11-12 21:17:19 +03:00
|
|
|
if (loadGroup)
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
return NS_NewInterfaceRequestorAggregation(callbacks, cbs, result);
|
2004-11-15 23:16:52 +03:00
|
|
|
}
|
|
|
|
|
2006-03-03 19:27:35 +03:00
|
|
|
/**
|
|
|
|
* Helper function for testing online/offline state of the browser.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2006-03-03 19:27:35 +03:00
|
|
|
NS_IsOffline()
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool offline = true;
|
2006-03-03 19:27:35 +03:00
|
|
|
nsCOMPtr<nsIIOService> ios = do_GetIOService();
|
|
|
|
if (ios)
|
|
|
|
ios->GetOffline(&offline);
|
|
|
|
return offline;
|
|
|
|
}
|
|
|
|
|
2006-05-02 22:54:19 +04:00
|
|
|
/**
|
|
|
|
* Helper functions for implementing nsINestedURI::innermostURI.
|
|
|
|
*
|
|
|
|
* Note that NS_DoImplGetInnermostURI is "private" -- call
|
|
|
|
* NS_ImplGetInnermostURI instead.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_DoImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nestedURI, "Must have a nested URI!");
|
|
|
|
NS_PRECONDITION(!*result, "Must have null *result");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> inner;
|
|
|
|
nsresult rv = nestedURI->GetInnerURI(getter_AddRefs(inner));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-12 08:56:45 +03:00
|
|
|
// We may need to loop here until we reach the innermost
|
|
|
|
// URI.
|
2006-05-02 22:54:19 +04:00
|
|
|
nsCOMPtr<nsINestedURI> nestedInner(do_QueryInterface(inner));
|
2007-03-12 08:56:45 +03:00
|
|
|
while (nestedInner) {
|
|
|
|
rv = nestedInner->GetInnerURI(getter_AddRefs(inner));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nestedInner = do_QueryInterface(inner);
|
2006-05-02 22:54:19 +04:00
|
|
|
}
|
|
|
|
|
2007-03-12 08:56:45 +03:00
|
|
|
// Found the innermost one if we reach here.
|
|
|
|
inner.swap(*result);
|
|
|
|
|
|
|
|
return rv;
|
2006-05-02 22:54:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_ImplGetInnermostURI(nsINestedURI* nestedURI, nsIURI** result)
|
|
|
|
{
|
|
|
|
// Make it safe to use swap()
|
|
|
|
*result = nsnull;
|
|
|
|
|
|
|
|
return NS_DoImplGetInnermostURI(nestedURI, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that ensures that |result| is a URI that's safe to
|
|
|
|
* return. If |uri| is immutable, just returns it, otherwise returns
|
|
|
|
* a clone. |uri| must not be null.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_EnsureSafeToReturn(nsIURI* uri, nsIURI** result)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(uri, "Must have a URI");
|
|
|
|
|
|
|
|
// Assume mutable until told otherwise
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isMutable = true;
|
2006-05-02 22:54:19 +04:00
|
|
|
nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(uri));
|
|
|
|
if (mutableObj) {
|
|
|
|
nsresult rv = mutableObj->GetMutable(&isMutable);
|
|
|
|
isMutable = NS_FAILED(rv) || isMutable;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isMutable) {
|
|
|
|
NS_ADDREF(*result = uri);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-05 02:00:00 +04:00
|
|
|
nsresult rv = uri->Clone(result);
|
|
|
|
if (NS_SUCCEEDED(rv) && !*result) {
|
|
|
|
NS_ERROR("nsIURI.clone contract was violated");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2006-05-02 22:54:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function that tries to set the argument URI to be immutable
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
NS_TryToSetImmutable(nsIURI* uri)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(uri));
|
|
|
|
if (mutableObj) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mutableObj->SetMutable(false);
|
2006-05-02 22:54:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 21:57:14 +04:00
|
|
|
/**
|
|
|
|
* Helper function for calling ToImmutableURI. If all else fails, returns
|
|
|
|
* the input URI. The optional second arg indicates whether we had to fall
|
|
|
|
* back to the input URI. Passing in a null URI is ok.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIURI>
|
|
|
|
NS_TryToMakeImmutable(nsIURI* uri,
|
|
|
|
nsresult* outRv = nsnull)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2010-04-12 19:44:28 +04:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
|
|
|
|
2006-05-31 21:57:14 +04:00
|
|
|
nsIURI* result = nsnull;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-04-12 19:44:28 +04:00
|
|
|
NS_ASSERTION(util, "do_GetNetUtil lied");
|
2006-05-31 21:57:14 +04:00
|
|
|
rv = util->ToImmutableURI(uri, &result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_IF_ADDREF(result = uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outRv) {
|
|
|
|
*outRv = rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-05-02 22:54:19 +04:00
|
|
|
/**
|
|
|
|
* Helper function for testing whether the given URI, or any of its
|
|
|
|
* inner URIs, has all the given protocol flags.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_URIChainHasFlags(nsIURI *uri,
|
|
|
|
PRUint32 flags,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *result)
|
2006-05-02 22:54:19 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2010-04-12 19:44:28 +04:00
|
|
|
nsCOMPtr<nsINetUtil> util = do_GetNetUtil(&rv);
|
2006-05-02 22:54:19 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return util->URIChainHasFlags(uri, flags, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for getting the innermost URI for a given URI. The return
|
|
|
|
* value could be just the object passed in if it's not a nested URI.
|
|
|
|
*/
|
|
|
|
inline already_AddRefed<nsIURI>
|
|
|
|
NS_GetInnermostURI(nsIURI *uri)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(uri, "Must have URI");
|
|
|
|
|
|
|
|
nsCOMPtr<nsINestedURI> nestedURI(do_QueryInterface(uri));
|
|
|
|
if (!nestedURI) {
|
|
|
|
NS_ADDREF(uri);
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = nestedURI->GetInnermostURI(&uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
|
2007-10-24 01:56:41 +04:00
|
|
|
/**
|
|
|
|
* Get the "final" URI for a channel. This is either the same as GetURI or
|
|
|
|
* GetOriginalURI, depending on whether this channel has
|
|
|
|
* nsIChanel::LOAD_REPLACE set. For channels without that flag set, the final
|
|
|
|
* URI is the original URI, while for ones with the flag the final URI is the
|
|
|
|
* channel URI.
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri)
|
|
|
|
{
|
|
|
|
*uri = nsnull;
|
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
nsresult rv = channel->GetLoadFlags(&loadFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (loadFlags & nsIChannel::LOAD_REPLACE) {
|
|
|
|
return channel->GetURI(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return channel->GetOriginalURI(uri);
|
|
|
|
}
|
|
|
|
|
2008-10-08 17:16:27 +04:00
|
|
|
// NS_SecurityHashURI must return the same hash value for any two URIs that
|
|
|
|
// compare equal according to NS_SecurityCompareURIs. Unfortunately, in the
|
|
|
|
// case of files, it's not clear we can do anything better than returning
|
|
|
|
// the schemeHash, so hashing files degenerates to storing them in a list.
|
|
|
|
inline PRUint32
|
|
|
|
NS_SecurityHashURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> baseURI = NS_GetInnermostURI(aURI);
|
|
|
|
|
|
|
|
nsCAutoString scheme;
|
|
|
|
PRUint32 schemeHash = 0;
|
|
|
|
if (NS_SUCCEEDED(baseURI->GetScheme(scheme)))
|
|
|
|
schemeHash = nsCRT::HashCode(scheme.get());
|
|
|
|
|
|
|
|
// TODO figure out how to hash file:// URIs
|
|
|
|
if (scheme.EqualsLiteral("file"))
|
|
|
|
return schemeHash; // sad face
|
|
|
|
|
2011-09-01 04:12:50 +04:00
|
|
|
if (scheme.EqualsLiteral("imap") ||
|
|
|
|
scheme.EqualsLiteral("mailbox") ||
|
|
|
|
scheme.EqualsLiteral("news"))
|
2008-10-08 17:16:27 +04:00
|
|
|
{
|
|
|
|
nsCAutoString spec;
|
|
|
|
PRUint32 specHash = baseURI->GetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(specHash))
|
|
|
|
specHash = nsCRT::HashCode(spec.get());
|
|
|
|
return specHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString host;
|
|
|
|
PRUint32 hostHash = 0;
|
2010-10-16 01:55:53 +04:00
|
|
|
if (NS_SUCCEEDED(baseURI->GetAsciiHost(host)))
|
2008-10-08 17:16:27 +04:00
|
|
|
hostHash = nsCRT::HashCode(host.get());
|
|
|
|
|
|
|
|
// XOR to combine hash values
|
2008-11-03 19:00:54 +03:00
|
|
|
return schemeHash ^ hostHash ^ NS_GetRealPort(baseURI);
|
2008-10-08 17:16:27 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2008-08-28 05:15:32 +04:00
|
|
|
NS_SecurityCompareURIs(nsIURI* aSourceURI,
|
|
|
|
nsIURI* aTargetURI,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aStrictFileOriginPolicy)
|
2008-08-28 05:15:32 +04:00
|
|
|
{
|
|
|
|
// Note that this is not an Equals() test on purpose -- for URIs that don't
|
|
|
|
// support host/port, we want equality to basically be object identity, for
|
|
|
|
// security purposes. Otherwise, for example, two javascript: URIs that
|
|
|
|
// are otherwise unrelated could end up "same origin", which would be
|
|
|
|
// unfortunate.
|
|
|
|
if (aSourceURI && aSourceURI == aTargetURI)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aTargetURI || !aSourceURI)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If either URI is a nested URI, get the base URI
|
|
|
|
nsCOMPtr<nsIURI> sourceBaseURI = NS_GetInnermostURI(aSourceURI);
|
|
|
|
nsCOMPtr<nsIURI> targetBaseURI = NS_GetInnermostURI(aTargetURI);
|
|
|
|
|
2010-03-03 10:51:09 +03:00
|
|
|
// If either uri is an nsIURIWithPrincipal
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(sourceBaseURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
uriPrinc->GetPrincipalUri(getter_AddRefs(sourceBaseURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
uriPrinc = do_QueryInterface(targetBaseURI);
|
|
|
|
if (uriPrinc) {
|
|
|
|
uriPrinc->GetPrincipalUri(getter_AddRefs(targetBaseURI));
|
|
|
|
}
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
if (!sourceBaseURI || !targetBaseURI)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
|
|
|
|
// Compare schemes
|
|
|
|
nsCAutoString targetScheme;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool sameScheme = false;
|
2008-08-28 05:15:32 +04:00
|
|
|
if (NS_FAILED( targetBaseURI->GetScheme(targetScheme) ) ||
|
|
|
|
NS_FAILED( sourceBaseURI->SchemeIs(targetScheme.get(), &sameScheme) ) ||
|
|
|
|
!sameScheme)
|
|
|
|
{
|
|
|
|
// Not same-origin if schemes differ
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// special handling for file: URIs
|
|
|
|
if (targetScheme.EqualsLiteral("file"))
|
|
|
|
{
|
|
|
|
// in traditional unsafe behavior all files are the same origin
|
|
|
|
if (!aStrictFileOriginPolicy)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-28 05:15:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFileURL> sourceFileURL(do_QueryInterface(sourceBaseURI));
|
|
|
|
nsCOMPtr<nsIFileURL> targetFileURL(do_QueryInterface(targetBaseURI));
|
|
|
|
|
|
|
|
if (!sourceFileURL || !targetFileURL)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> sourceFile, targetFile;
|
|
|
|
|
|
|
|
sourceFileURL->GetFile(getter_AddRefs(sourceFile));
|
|
|
|
targetFileURL->GetFile(getter_AddRefs(targetFile));
|
|
|
|
|
|
|
|
if (!sourceFile || !targetFile)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
|
|
|
|
// Otherwise they had better match
|
2011-09-29 10:19:26 +04:00
|
|
|
bool filesAreEqual = false;
|
2008-08-28 05:15:32 +04:00
|
|
|
nsresult rv = sourceFile->Equals(targetFile, &filesAreEqual);
|
|
|
|
return NS_SUCCEEDED(rv) && filesAreEqual;
|
|
|
|
}
|
|
|
|
|
2011-09-01 04:12:50 +04:00
|
|
|
// Special handling for mailnews schemes
|
|
|
|
if (targetScheme.EqualsLiteral("imap") ||
|
|
|
|
targetScheme.EqualsLiteral("mailbox") ||
|
|
|
|
targetScheme.EqualsLiteral("news"))
|
2008-08-28 05:15:32 +04:00
|
|
|
{
|
2011-09-01 04:12:50 +04:00
|
|
|
// Each message is a distinct trust domain; use the
|
|
|
|
// whole spec for comparison
|
2008-08-28 05:15:32 +04:00
|
|
|
nsCAutoString targetSpec;
|
|
|
|
nsCAutoString sourceSpec;
|
|
|
|
return ( NS_SUCCEEDED( targetBaseURI->GetSpec(targetSpec) ) &&
|
|
|
|
NS_SUCCEEDED( sourceBaseURI->GetSpec(sourceSpec) ) &&
|
|
|
|
targetSpec.Equals(sourceSpec) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare hosts
|
|
|
|
nsCAutoString targetHost;
|
|
|
|
nsCAutoString sourceHost;
|
|
|
|
if (NS_FAILED( targetBaseURI->GetAsciiHost(targetHost) ) ||
|
|
|
|
NS_FAILED( sourceBaseURI->GetAsciiHost(sourceHost) ))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
2010-10-16 01:55:53 +04:00
|
|
|
nsCOMPtr<nsIStandardURL> targetURL(do_QueryInterface(targetBaseURI));
|
|
|
|
nsCOMPtr<nsIStandardURL> sourceURL(do_QueryInterface(sourceBaseURI));
|
|
|
|
if (!targetURL || !sourceURL)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-10-16 01:55:53 +04:00
|
|
|
}
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
if (!targetHost.Equals(sourceHost, nsCaseInsensitiveCStringComparator() ))
|
|
|
|
#else
|
|
|
|
if (!targetHost.Equals(sourceHost, CaseInsensitiveCompare))
|
|
|
|
#endif
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
2008-11-03 19:00:54 +03:00
|
|
|
return NS_GetRealPort(targetBaseURI) == NS_GetRealPort(sourceBaseURI);
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2009-01-22 01:50:28 +03:00
|
|
|
NS_IsInternalSameURIRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
|
|
|
PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
if (!(aFlags & nsIChannelEventSink::REDIRECT_INTERNAL)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-01-22 01:50:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldURI, newURI;
|
|
|
|
aOldChannel->GetURI(getter_AddRefs(oldURI));
|
|
|
|
aNewChannel->GetURI(getter_AddRefs(newURI));
|
|
|
|
|
|
|
|
if (!oldURI || !newURI) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-01-22 01:50:28 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool res;
|
2009-01-22 01:50:28 +03:00
|
|
|
return NS_SUCCEEDED(oldURI->Equals(newURI, &res)) && res;
|
|
|
|
}
|
|
|
|
|
2010-11-24 01:56:06 +03:00
|
|
|
inline nsresult
|
|
|
|
NS_LinkRedirectChannels(PRUint32 channelId,
|
|
|
|
nsIParentChannel *parentChannel,
|
|
|
|
nsIChannel** _result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRedirectChannelRegistrar> registrar =
|
|
|
|
do_GetService("@mozilla.org/redirectchannelregistrar;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return registrar->LinkChannels(channelId,
|
|
|
|
parentChannel,
|
|
|
|
_result);
|
|
|
|
}
|
|
|
|
|
2010-11-19 23:58:59 +03:00
|
|
|
/**
|
|
|
|
* Helper function to create a random URL string that's properly formed
|
|
|
|
* but guaranteed to be invalid.
|
|
|
|
*/
|
|
|
|
#define NS_FAKE_SCHEME "http://"
|
|
|
|
#define NS_FAKE_TLD ".invalid"
|
|
|
|
inline nsresult
|
|
|
|
NS_MakeRandomInvalidURLString(nsCString& result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsID idee;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&idee);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
idee.ToProvidedString(chars);
|
|
|
|
|
|
|
|
result.AssignLiteral(NS_FAKE_SCHEME);
|
|
|
|
// Strip off the '{' and '}' at the beginning and end of the UUID
|
|
|
|
result.Append(chars + 1, NSID_LENGTH - 3);
|
|
|
|
result.AppendLiteral(NS_FAKE_TLD);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#undef NS_FAKE_SCHEME
|
|
|
|
#undef NS_FAKE_TLD
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to determine whether urlString is Java-compatible --
|
|
|
|
* whether it can be passed to the Java URL(String) constructor without the
|
|
|
|
* latter throwing a MalformedURLException, or without Java otherwise
|
2011-01-11 19:00:36 +03:00
|
|
|
* mishandling it. This function (in effect) implements a scheme whitelist
|
|
|
|
* for Java.
|
2010-11-19 23:58:59 +03:00
|
|
|
*/
|
|
|
|
inline nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_CheckIsJavaCompatibleURLString(nsCString& urlString, bool *result)
|
2010-11-19 23:58:59 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*result = false; // Default to "no"
|
2010-11-19 23:58:59 +03:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIURLParser> urlParser =
|
|
|
|
do_GetService(NS_STDURLPARSER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv) || !urlParser)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool compatible = true;
|
2010-11-19 23:58:59 +03:00
|
|
|
PRUint32 schemePos = 0;
|
|
|
|
PRInt32 schemeLen = 0;
|
|
|
|
urlParser->ParseURL(urlString.get(), -1, &schemePos, &schemeLen,
|
|
|
|
nsnull, nsnull, nsnull, nsnull);
|
|
|
|
if (schemeLen != -1) {
|
|
|
|
nsCString scheme;
|
|
|
|
scheme.Assign(urlString.get() + schemePos, schemeLen);
|
|
|
|
// By default Java only understands a small number of URL schemes, and of
|
2011-01-11 19:00:36 +03:00
|
|
|
// these only some can legitimately represent a browser page's "origin"
|
|
|
|
// (and be something we can legitimately expect Java to handle ... or not
|
|
|
|
// to mishandle).
|
|
|
|
//
|
|
|
|
// Besides those listed below, the OJI plugin understands the "jar",
|
|
|
|
// "mailto", "netdoc", "javascript" and "rmi" schemes, and Java Plugin2
|
|
|
|
// also understands the "about" scheme. We actually pass "about" URLs
|
|
|
|
// to Java ("about:blank" when processing a javascript: URL (one that
|
|
|
|
// calls Java) from the location bar of a blank page, and (in FF4 and up)
|
|
|
|
// "about:home" when processing a javascript: URL from the home page).
|
|
|
|
// And Java doesn't appear to mishandle them (for example it doesn't allow
|
|
|
|
// connections to "about" URLs). But it doesn't make any sense to do
|
|
|
|
// same-origin checks on "about" URLs, so we don't include them in our
|
|
|
|
// scheme whitelist.
|
|
|
|
//
|
|
|
|
// The OJI plugin doesn't understand "chrome" URLs (only Java Plugin2
|
|
|
|
// does) -- so we mustn't pass them to the OJI plugin. But we do need to
|
|
|
|
// pass "chrome" URLs to Java Plugin2: Java Plugin2 grants additional
|
|
|
|
// privileges to chrome "origins", and some extensions take advantage of
|
|
|
|
// this. For more information see bug 620773.
|
|
|
|
//
|
|
|
|
// As of FF4, we no longer support the OJI plugin.
|
2010-11-19 23:58:59 +03:00
|
|
|
if (PL_strcasecmp(scheme.get(), "http") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "https") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "file") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "ftp") &&
|
2011-01-11 19:00:36 +03:00
|
|
|
PL_strcasecmp(scheme.get(), "gopher") &&
|
|
|
|
PL_strcasecmp(scheme.get(), "chrome"))
|
2011-10-17 18:59:28 +04:00
|
|
|
compatible = false;
|
2010-11-19 23:58:59 +03:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
compatible = false;
|
2010-11-19 23:58:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*result = compatible;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:41:04 +04:00
|
|
|
/** Given the first (disposition) token from a Content-Disposition header,
|
|
|
|
* tell whether it indicates the content is inline or attachment
|
|
|
|
* @param aDispToken the disposition token from the content-disposition header
|
|
|
|
*/
|
|
|
|
inline PRUint32
|
|
|
|
NS_GetContentDispositionFromToken(const nsAString& aDispToken)
|
|
|
|
{
|
|
|
|
// RFC 2183, section 2.8 says that an unknown disposition
|
|
|
|
// value should be treated as "attachment"
|
|
|
|
// If all of these tests eval to false, then we have a content-disposition of
|
|
|
|
// "attachment" or unknown
|
|
|
|
if (aDispToken.IsEmpty() ||
|
|
|
|
aDispToken.LowerCaseEqualsLiteral("inline") ||
|
|
|
|
// Broken sites just send
|
|
|
|
// Content-Disposition: filename="file"
|
|
|
|
// without a disposition token... screen those out.
|
|
|
|
StringHead(aDispToken, 8).LowerCaseEqualsLiteral("filename") ||
|
|
|
|
// Also in use is Content-Disposition: name="file"
|
|
|
|
StringHead(aDispToken, 4).LowerCaseEqualsLiteral("name"))
|
|
|
|
return nsIChannel::DISPOSITION_INLINE;
|
|
|
|
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Determine the disposition (inline/attachment) of the content based on the
|
|
|
|
* Content-Disposition header
|
|
|
|
* @param aHeader the content-disposition header (full value)
|
|
|
|
* @param aChan the channel the header came from
|
|
|
|
*/
|
|
|
|
inline PRUint32
|
|
|
|
NS_GetContentDispositionFromHeader(const nsACString& aHeader, nsIChannel *aChan = nsnull)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
|
|
|
|
|
|
|
nsCAutoString fallbackCharset;
|
|
|
|
if (aChan) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aChan->GetURI(getter_AddRefs(uri));
|
|
|
|
if (uri)
|
|
|
|
uri->GetOriginCharset(fallbackCharset);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString dispToken;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = mimehdrpar->GetParameter(aHeader, "", fallbackCharset, true, nsnull,
|
2011-09-10 02:41:04 +04:00
|
|
|
dispToken);
|
2011-09-13 07:14:16 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// special case (see bug 272541): empty disposition type handled as "inline"
|
2011-09-14 02:15:28 +04:00
|
|
|
if (rv == NS_ERROR_FIRST_HEADER_FIELD_COMPONENT_EMPTY)
|
|
|
|
return nsIChannel::DISPOSITION_INLINE;
|
|
|
|
return nsIChannel::DISPOSITION_ATTACHMENT;
|
2011-09-13 07:14:16 +04:00
|
|
|
}
|
2011-09-10 02:41:04 +04:00
|
|
|
|
|
|
|
return NS_GetContentDispositionFromToken(dispToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Extracts the filename out of a content-disposition header
|
|
|
|
* @param aFilename [out] The filename. Can be empty on error.
|
|
|
|
* @param aDisposition Value of a Content-Disposition header
|
|
|
|
* @param aURI Optional. Will be used to get a fallback charset for the
|
|
|
|
* filename, if it is QI'able to nsIURL
|
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_GetFilenameFromDisposition(nsAString& aFilename,
|
|
|
|
const nsACString& aDisposition,
|
|
|
|
nsIURI* aURI = nsnull)
|
|
|
|
{
|
|
|
|
aFilename.Truncate();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar =
|
|
|
|
do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
|
|
|
|
nsCAutoString fallbackCharset;
|
|
|
|
if (url)
|
|
|
|
url->GetOriginCharset(fallbackCharset);
|
|
|
|
// Get the value of 'filename' parameter
|
|
|
|
rv = mimehdrpar->GetParameter(aDisposition, "filename",
|
2011-10-17 18:59:28 +04:00
|
|
|
fallbackCharset, true, nsnull,
|
2011-09-10 02:41:04 +04:00
|
|
|
aFilename);
|
|
|
|
if (NS_FAILED(rv) || aFilename.IsEmpty()) {
|
|
|
|
// Try 'name' parameter, instead.
|
|
|
|
rv = mimehdrpar->GetParameter(aDisposition, "name", fallbackCharset,
|
2011-10-17 18:59:28 +04:00
|
|
|
true, nsnull, aFilename);
|
2011-09-10 02:41:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aFilename.Truncate();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFilename.IsEmpty())
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-23 15:01:30 +03:00
|
|
|
/**
|
|
|
|
* Make sure Personal Security Manager is initialized
|
|
|
|
*/
|
|
|
|
inline void
|
|
|
|
net_EnsurePSMInit()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISocketProviderService> spserv =
|
|
|
|
do_GetService(NS_SOCKETPROVIDERSERVICE_CONTRACTID);
|
|
|
|
if (spserv) {
|
|
|
|
nsCOMPtr<nsISocketProvider> provider;
|
|
|
|
spserv->GetSocketProvider("ssl", getter_AddRefs(provider));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 01:00:41 +04:00
|
|
|
/**
|
|
|
|
* Test whether a URI is "about:blank". |uri| must not be null
|
|
|
|
*/
|
|
|
|
inline bool
|
|
|
|
NS_IsAboutBlank(nsIURI *uri)
|
|
|
|
{
|
|
|
|
// GetSpec can be expensive for some URIs, so check the scheme first.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isAbout = false;
|
2011-09-21 01:00:41 +04:00
|
|
|
if (NS_FAILED(uri->SchemeIs("about", &isAbout)) || !isAbout) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString str;
|
|
|
|
uri->GetSpec(str);
|
|
|
|
return str.EqualsLiteral("about:blank");
|
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
#endif // !nsNetUtil_h__
|