2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-11-01 20:48:12 +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-11-01 20:48:12 +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-11-01 20:48:12 +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
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* Contributor(s):
|
|
|
|
* Bradley Baetz <bbaetz@student.usyd.edu.au>
|
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-11-01 20:48:12 +00:00
|
|
|
|
|
|
|
#ifndef nsNetUtil_h__
|
|
|
|
#define nsNetUtil_h__
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsNetError.h"
|
|
|
|
#include "nsNetCID.h"
|
2002-03-20 22:50:33 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prio.h" // for read/write flags, permissions, etc.
|
|
|
|
|
2002-03-20 22:50:33 +00:00
|
|
|
#include "nsIURI.h"
|
1999-11-01 20:48:12 +00:00
|
|
|
#include "nsIInputStream.h"
|
2001-01-23 22:10:34 +00:00
|
|
|
#include "nsIOutputStream.h"
|
1999-11-01 20:48:12 +00:00
|
|
|
#include "nsIStreamListener.h"
|
2001-04-10 06:01:08 +00:00
|
|
|
#include "nsIRequestObserverProxy.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsIStreamListenerProxy.h" // XXX for nsIAsyncStreamListener
|
2002-05-03 07:49:43 +00:00
|
|
|
#include "nsISimpleStreamListener.h"
|
1999-11-01 20:48:12 +00:00
|
|
|
#include "nsILoadGroup.h"
|
1999-11-18 07:36:41 +00:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-05 21:28:38 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
1999-11-01 20:48:12 +00:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIChannel.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsIInputStreamChannel.h"
|
2001-02-21 20:38:08 +00:00
|
|
|
#include "nsITransport.h"
|
2003-02-25 13:57:09 +00:00
|
|
|
#include "nsIStreamTransportService.h"
|
2002-09-27 04:22:13 +00:00
|
|
|
#include "nsIHttpChannel.h"
|
2000-09-15 19:27:05 +00:00
|
|
|
#include "nsIDownloader.h"
|
2002-02-14 03:10:19 +00:00
|
|
|
#include "nsIResumableEntityID.h"
|
2000-02-02 23:04:47 +00:00
|
|
|
#include "nsIStreamLoader.h"
|
2002-08-27 23:47:25 +00:00
|
|
|
#include "nsIUnicharStreamLoader.h"
|
2001-01-23 22:10:34 +00:00
|
|
|
#include "nsIPipe.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
#include "nsIProtocolHandler.h"
|
2002-09-13 19:32:45 +00:00
|
|
|
#include "nsIFileProtocolHandler.h"
|
2001-05-11 21:04:09 +00:00
|
|
|
#include "nsIStringStream.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIFileStreams.h"
|
2001-09-13 02:21:05 +00:00
|
|
|
#include "nsIProtocolProxyService.h"
|
2002-02-19 07:07:23 +00:00
|
|
|
#include "nsIProxyInfo.h"
|
2003-01-18 02:15:14 +00:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIBufferedStreams.h"
|
|
|
|
#include "nsIInputStreamPump.h"
|
|
|
|
#include "nsIAsyncStreamCopier.h"
|
2003-07-11 21:31:54 +00:00
|
|
|
#include "nsIPersistentProperties2.h"
|
2003-10-08 04:27:13 +00:00
|
|
|
#include "nsISyncStreamListener.h"
|
2001-01-23 22:10:34 +00:00
|
|
|
|
2000-09-08 23:31:26 +00:00
|
|
|
// Helper, to simplify getting the I/O service.
|
|
|
|
inline const nsGetServiceByCID
|
|
|
|
do_GetIOService(nsresult* error = 0)
|
|
|
|
{
|
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
|
|
|
return nsGetServiceByCID(kIOServiceCID, 0, error);
|
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00: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 20:48:12 +00:00
|
|
|
inline nsresult
|
2002-03-06 07:48:55 +00: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 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewURI(spec, charset, baseURI, result);
|
|
|
|
return rv;
|
1999-11-01 20:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NewURI(nsIURI* *result,
|
2002-03-06 07:48:55 +00:00
|
|
|
const nsAString& spec,
|
|
|
|
const char *charset = nsnull,
|
2000-03-29 03:58:50 +00:00
|
|
|
nsIURI* baseURI = nsnull,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
1999-11-01 20:48:12 +00:00
|
|
|
{
|
2002-03-06 07:48:55 +00:00
|
|
|
return NS_NewURI(result, NS_ConvertUCS2toUTF8(spec), charset, baseURI, ioService);
|
1999-11-01 20:48:12 +00:00
|
|
|
}
|
|
|
|
|
2002-03-06 07:48:55 +00: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-21 22:02:47 +00:00
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewFileURI(nsIURI* *result,
|
|
|
|
nsIFile* spec,
|
|
|
|
nsIIOService* ioService = nsnull) // pass in nsIIOService to optimize callers
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService)
|
|
|
|
rv = ioService->NewFileURI(spec, result);
|
|
|
|
return rv;
|
2001-06-21 22:02:47 +00:00
|
|
|
}
|
|
|
|
|
1999-11-01 20:48:12 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewChannel(nsIChannel **result,
|
|
|
|
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 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
|
|
|
nsIChannel *chan;
|
|
|
|
rv = ioService->NewChannelFromURI(uri, &chan);
|
|
|
|
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);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
*result = chan;
|
|
|
|
else
|
|
|
|
NS_RELEASE(chan);
|
|
|
|
}
|
2000-03-29 03:58:50 +00:00
|
|
|
}
|
1999-11-01 20:48:12 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-05-08 00:26:40 +00: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
|
|
|
|
// to implement a full blown asyncrhonous consumer (via nsIStreamListener) look
|
|
|
|
// at nsIStreamLoader instead.
|
1999-11-01 20:48:12 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_OpenURI(nsIInputStream **result,
|
|
|
|
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 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-03-29 03:58:50 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService,
|
2003-06-18 23:07:09 +00:00
|
|
|
loadGroup, callbacks, loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsIInputStream *stream;
|
|
|
|
rv = channel->Open(&stream);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
*result = stream;
|
|
|
|
}
|
1999-11-01 20:48:12 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00: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 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-03-29 03:58:50 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel), uri, ioService,
|
2003-06-18 23:07:09 +00:00
|
|
|
loadGroup, callbacks, loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = channel->AsyncOpen(listener, context);
|
1999-11-01 20:48:12 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_MakeAbsoluteURI(nsACString &result,
|
2002-03-06 07:48:55 +00:00
|
|
|
const nsACString &spec,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
1999-11-01 20:48:12 +00:00
|
|
|
{
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2002-06-14 17:44:20 +00:00
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
2003-06-18 23:07:09 +00:00
|
|
|
rv = NS_OK;
|
2002-06-14 17:44:20 +00:00
|
|
|
}
|
2003-06-18 23:07:09 +00:00
|
|
|
else if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(result);
|
|
|
|
else
|
|
|
|
rv = baseURI->Resolve(spec, result);
|
|
|
|
return rv;
|
1999-11-01 20:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_MakeAbsoluteURI(char **result,
|
|
|
|
const char *spec,
|
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
1999-11-01 20:48:12 +00:00
|
|
|
{
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCAutoString resultBuf;
|
2003-06-18 23:07:09 +00: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 07:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_MakeAbsoluteURI(nsAString &result,
|
2002-03-06 07:48:55 +00:00
|
|
|
const nsAString &spec,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIURI *baseURI,
|
|
|
|
nsIIOService *unused = nsnull)
|
2002-03-06 07:48:55 +00:00
|
|
|
{
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2002-06-14 17:44:20 +00:00
|
|
|
if (!baseURI) {
|
|
|
|
NS_WARNING("It doesn't make sense to not supply a base URI");
|
|
|
|
result = spec;
|
2003-06-18 23:07:09 +00:00
|
|
|
rv = NS_OK;
|
2002-06-14 17:44:20 +00:00
|
|
|
}
|
2003-06-18 23:07:09 +00:00
|
|
|
else {
|
|
|
|
nsCAutoString resultBuf;
|
|
|
|
if (spec.IsEmpty())
|
|
|
|
rv = baseURI->GetSpec(resultBuf);
|
|
|
|
else
|
|
|
|
rv = baseURI->Resolve(NS_ConvertUCS2toUTF8(spec), resultBuf);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
CopyUTF8toUTF16(resultBuf, result);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-01 20:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewInputStreamChannel(nsIChannel **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIInputStream *stream,
|
2004-01-29 22:04:45 +00:00
|
|
|
const nsACString &contentType = EmptyCString(),
|
|
|
|
const nsACString &contentCharset = EmptyCString())
|
1999-11-01 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_DEFINE_CID(kInputStreamChannelCID, NS_INPUTSTREAMCHANNEL_CID);
|
|
|
|
nsCOMPtr<nsIInputStreamChannel> channel =
|
|
|
|
do_CreateInstance(kInputStreamChannelCID, &rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv |= channel->SetURI(uri);
|
|
|
|
rv |= channel->SetContentStream(stream);
|
|
|
|
rv |= channel->SetContentType(contentType);
|
|
|
|
rv |= channel->SetContentCharset(contentCharset);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = channel);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-01 20:48:12 +00:00
|
|
|
}
|
|
|
|
|
2000-04-13 09:20:50 +00:00
|
|
|
inline nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_NewInputStreamPump(nsIInputStreamPump **result,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIInputStream *stream,
|
|
|
|
PRInt32 streamPos = -1,
|
|
|
|
PRInt32 streamLen = -1,
|
|
|
|
PRUint32 segsize = 0,
|
|
|
|
PRUint32 segcount = 0,
|
|
|
|
PRBool closeWhenDone = PR_FALSE)
|
2000-04-13 09:20:50 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_DEFINE_CID(kInputStreamPumpCID, NS_INPUTSTREAMPUMP_CID);
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump =
|
|
|
|
do_CreateInstance(kInputStreamPumpCID, &rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = pump->Init(stream, streamPos, streamLen,
|
|
|
|
segsize, segcount, closeWhenDone);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = pump);
|
|
|
|
}
|
|
|
|
return rv;
|
2000-04-13 09:20:50 +00:00
|
|
|
}
|
|
|
|
|
2003-10-06 01:46:31 +00: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 04:50:42 +00:00
|
|
|
inline nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_NewAsyncStreamCopier(nsIAsyncStreamCopier **result,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIInputStream *source,
|
|
|
|
nsIOutputStream *sink,
|
2003-10-06 01:46:31 +00:00
|
|
|
nsIEventTarget *target,
|
|
|
|
PRBool sourceBuffered = PR_TRUE,
|
|
|
|
PRBool sinkBuffered = PR_TRUE,
|
2003-06-18 23:07:09 +00:00
|
|
|
PRUint32 chunkSize = 0)
|
1999-11-30 04:50:42 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
static NS_DEFINE_CID(kAsyncStreamCopierCID, NS_ASYNCSTREAMCOPIER_CID);
|
|
|
|
nsCOMPtr<nsIAsyncStreamCopier> copier =
|
|
|
|
do_CreateInstance(kAsyncStreamCopierCID, &rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-10-06 01:46:31 +00:00
|
|
|
rv = copier->Init(source, sink, target, sourceBuffered, sinkBuffered, chunkSize);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = copier);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-30 04:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewLoadGroup(nsILoadGroup **result,
|
|
|
|
nsIRequestObserver *obs)
|
1999-11-30 04:50:42 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kLoadGroupCID, NS_LOADGROUP_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsILoadGroup> group =
|
|
|
|
do_CreateInstance(kLoadGroupCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = group->SetGroupObserver(obs);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = group);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-30 04:50:42 +00:00
|
|
|
}
|
|
|
|
|
2000-09-15 19:27:05 +00:00
|
|
|
inline nsresult
|
2003-06-23 22:58:28 +00:00
|
|
|
NS_NewDownloader(nsIStreamListener **result,
|
|
|
|
nsIDownloadObserver *observer,
|
|
|
|
nsIFile *downloadLocation = nsnull)
|
2000-09-15 19:27:05 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kDownloaderCID, NS_DOWNLOADER_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIDownloader> downloader =
|
|
|
|
do_CreateInstance(kDownloaderCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2003-06-23 22:58:28 +00:00
|
|
|
rv = downloader->Init(observer, downloadLocation);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = downloader);
|
|
|
|
}
|
2000-09-15 19:27:05 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-11-01 20:48:12 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewStreamLoader(nsIStreamLoader **aResult,
|
|
|
|
nsIChannel *aChannel,
|
2002-09-27 04:22:13 +00:00
|
|
|
nsIStreamLoaderObserver *aObserver,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsISupports *aContext)
|
1999-11-01 20:48:12 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-02-02 23:04:47 +00:00
|
|
|
static NS_DEFINE_CID(kStreamLoaderCID, NS_STREAMLOADER_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIStreamLoader> loader =
|
|
|
|
do_CreateInstance(kStreamLoaderCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = loader->Init(aChannel, aObserver, aContext);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = loader);
|
|
|
|
}
|
1999-11-01 20:48:12 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-09-27 04:22:13 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00: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 04:22:13 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
nsnull,
|
|
|
|
loadGroup,
|
2003-06-18 23:07:09 +00:00
|
|
|
callbacks,
|
|
|
|
loadFlags);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(channel));
|
|
|
|
if (httpChannel)
|
|
|
|
httpChannel->SetReferrer(referrer);
|
|
|
|
rv = NS_NewStreamLoader(result, channel, observer, context);
|
|
|
|
}
|
|
|
|
return rv;
|
2002-09-27 04:22:13 +00:00
|
|
|
}
|
|
|
|
|
2002-08-27 23:47:25 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **aResult,
|
|
|
|
nsIChannel *aChannel,
|
2002-08-27 23:47:25 +00:00
|
|
|
nsIUnicharStreamLoaderObserver *aObserver,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsISupports *aContext = nsnull,
|
|
|
|
PRUint32 aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE)
|
2002-08-27 23:47:25 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kUnicharStreamLoaderCID, NS_UNICHARSTREAMLOADER_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIUnicharStreamLoader> loader =
|
|
|
|
do_CreateInstance(kUnicharStreamLoaderCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = loader->Init(aChannel, aObserver, aContext, aSegmentSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = loader);
|
|
|
|
}
|
2002-08-27 23:47:25 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-10-08 04:27:13 +00:00
|
|
|
inline nsresult
|
|
|
|
NS_NewSyncStreamListener(nsIStreamListener **aResult,
|
|
|
|
nsIInputStream **aStream)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kSyncStreamListenerCID, NS_SYNCSTREAMLISTENER_CID);
|
|
|
|
nsCOMPtr<nsISyncStreamListener> listener =
|
|
|
|
do_CreateInstance(kSyncStreamListenerCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = listener->GetInputStream(aStream);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = listener);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement the nsIChannel::Open(nsIInputStream**) method using the channel's
|
|
|
|
* AsyncOpen method.
|
2003-10-08 04:37:38 +00: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 04:27:13 +00:00
|
|
|
*/
|
|
|
|
inline nsresult
|
|
|
|
NS_ImplementChannelOpen(nsIChannel *aChannel,
|
|
|
|
nsIInputStream **aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = NS_NewSyncStreamListener(getter_AddRefs(listener),
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = aChannel->AsyncOpen(listener, nsnull);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 n;
|
|
|
|
// block until the initial response is received or an error occurs.
|
|
|
|
rv = stream->Available(&n);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-01-23 22:10:34 +00:00
|
|
|
inline nsresult
|
2001-04-10 06:01:08 +00:00
|
|
|
NS_NewRequestObserverProxy(nsIRequestObserver **aResult,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIRequestObserver *aObserver,
|
|
|
|
nsIEventQueue *aEventQ = nsnull)
|
2001-01-23 22:10:34 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-04-10 06:01:08 +00:00
|
|
|
static NS_DEFINE_CID(kRequestObserverProxyCID, NS_REQUESTOBSERVERPROXY_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIRequestObserverProxy> proxy =
|
|
|
|
do_CreateInstance(kRequestObserverProxyCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = proxy->Init(aObserver, aEventQ);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = proxy);
|
|
|
|
}
|
|
|
|
return rv;
|
2001-01-23 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewSimpleStreamListener(nsIStreamListener **aResult,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIOutputStream *aSink,
|
|
|
|
nsIRequestObserver *aObserver = nsnull)
|
2001-01-23 22:10:34 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kSimpleStreamListenerCID, NS_SIMPLESTREAMLISTENER_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsISimpleStreamListener> listener =
|
|
|
|
do_CreateInstance(kSimpleStreamListenerCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = listener->Init(aSink, aObserver);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = listener);
|
|
|
|
}
|
|
|
|
return rv;
|
2001-01-23 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
1999-11-30 04:50:42 +00:00
|
|
|
inline nsresult
|
2000-03-29 03:58:50 +00:00
|
|
|
NS_NewAsyncStreamListener(nsIStreamListener **result,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIStreamListener *receiver,
|
|
|
|
nsIEventQueue *eventQueue)
|
1999-11-30 04:50:42 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kAsyncStreamListenerCID, NS_ASYNCSTREAMLISTENER_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIAsyncStreamListener> lsnr =
|
|
|
|
do_CreateInstance(kAsyncStreamListenerCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = lsnr->Init(receiver, eventQueue);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = lsnr);
|
|
|
|
}
|
|
|
|
return rv;
|
1999-11-30 04:50:42 +00:00
|
|
|
}
|
|
|
|
|
2001-06-06 00:10:09 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_CheckPortSafety(PRInt32 port,
|
|
|
|
const char *scheme,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2001-06-06 00:10:09 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIIOService> grip;
|
|
|
|
rv = net_EnsureIOService(&ioService, grip);
|
|
|
|
if (ioService) {
|
|
|
|
PRBool allow;
|
|
|
|
rv = ioService->AllowPort(port, scheme, &allow);
|
|
|
|
if (NS_SUCCEEDED(rv) && !allow)
|
|
|
|
rv = NS_ERROR_PORT_ACCESS_NOT_ALLOWED;
|
2001-06-06 00:10:09 +00:00
|
|
|
}
|
2003-06-18 23:07:09 +00:00
|
|
|
return rv;
|
2001-06-06 00:10:09 +00:00
|
|
|
}
|
|
|
|
|
2001-09-13 02:21:05 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewProxyInfo(const char *type,
|
|
|
|
const char *host,
|
|
|
|
PRInt32 port,
|
|
|
|
nsIProxyInfo **result)
|
2001-09-13 02:21:05 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kPPSServiceCID, NS_PROTOCOLPROXYSERVICE_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps = do_GetService(kPPSServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = pps->NewProxyInfo(type, host, port, result);
|
|
|
|
return rv;
|
2001-09-13 02:21:05 +00:00
|
|
|
}
|
|
|
|
|
2001-12-19 00:55:42 +00:00
|
|
|
inline nsresult
|
2002-09-13 19:32:45 +00:00
|
|
|
NS_GetFileProtocolHandler(nsIFileProtocolHandler **result,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsIIOService *ioService = nsnull)
|
2001-12-19 00:55:42 +00:00
|
|
|
{
|
2002-09-13 19:32:45 +00:00
|
|
|
nsresult rv;
|
2003-06-18 23:07:09 +00: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 00:55:42 +00:00
|
|
|
}
|
2003-06-18 23:07:09 +00:00
|
|
|
return rv;
|
2002-09-13 19:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_GetFileFromURLSpec(const nsACString &inURL,
|
|
|
|
nsIFile **result,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2002-09-13 19:32:45 +00:00
|
|
|
{
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2002-09-13 19:32:45 +00:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
2003-06-18 23:07:09 +00:00
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetFileFromURLSpec(inURL, result);
|
|
|
|
return rv;
|
2001-12-19 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_GetURLSpecFromFile(nsIFile *aFile,
|
|
|
|
nsACString &aUrl,
|
|
|
|
nsIIOService *ioService = nsnull)
|
2001-12-19 00:55:42 +00:00
|
|
|
{
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2002-09-13 19:32:45 +00:00
|
|
|
nsCOMPtr<nsIFileProtocolHandler> fileHandler;
|
2003-06-18 23:07:09 +00:00
|
|
|
rv = NS_GetFileProtocolHandler(getter_AddRefs(fileHandler), ioService);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = fileHandler->GetURLSpecFromFile(aFile, aUrl);
|
|
|
|
return rv;
|
2001-12-19 00:55:42 +00:00
|
|
|
}
|
|
|
|
|
2002-02-14 03:10:19 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewResumableEntityID(nsIResumableEntityID **aRes,
|
|
|
|
PRUint32 size,
|
2004-04-14 17:37:39 +00:00
|
|
|
const nsACString &lastModified,
|
|
|
|
const nsACString &entityTag)
|
2002-02-14 03:10:19 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIResumableEntityID> ent =
|
|
|
|
do_CreateInstance(NS_RESUMABLEENTITYID_CONTRACTID,&rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ent->SetSize(size);
|
|
|
|
ent->SetLastModified(lastModified);
|
2004-04-14 17:37:39 +00:00
|
|
|
ent->SetEntityTag(entityTag);
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_ADDREF(*aRes = ent);
|
|
|
|
}
|
|
|
|
return rv;
|
2002-02-14 03:10:19 +00:00
|
|
|
}
|
|
|
|
|
2002-02-19 07:07:23 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_ExamineForProxy(const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
PRInt32 port,
|
|
|
|
nsIProxyInfo **proxyInfo)
|
2002-02-19 07:07:23 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kPPSServiceCID, NS_PROTOCOLPROXYSERVICE_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps = do_GetService(kPPSServiceCID, &rv);
|
|
|
|
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
|
|
|
|
static NS_DEFINE_CID(kStandardURLCID, NS_STANDARDURL_CID);
|
|
|
|
nsCOMPtr<nsIURI> uri = do_CreateInstance(kStandardURLCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = uri->SetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = pps->ExamineForProxy(uri, proxyInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2002-02-19 07:07:23 +00:00
|
|
|
}
|
|
|
|
|
2002-03-20 22:50:33 +00:00
|
|
|
inline nsresult
|
|
|
|
NS_ParseContentType(const nsACString &rawContentType,
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCString &contentType,
|
|
|
|
nsCString &contentCharset)
|
2002-03-20 22:50:33 +00:00
|
|
|
{
|
|
|
|
// contentCharset is left untouched if not present in rawContentType
|
|
|
|
nsACString::const_iterator begin, it, end;
|
|
|
|
it = rawContentType.BeginReading(begin);
|
2002-04-22 23:55:30 +00:00
|
|
|
rawContentType.EndReading(end);
|
2002-03-20 22:50:33 +00:00
|
|
|
if (FindCharInReadable(';', it, end)) {
|
|
|
|
contentType = Substring(begin, it);
|
|
|
|
// now look for "charset=FOO" and extract "FOO"
|
|
|
|
begin = ++it;
|
2003-09-25 15:37:56 +00:00
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("charset="), begin, it = end,
|
|
|
|
nsCaseInsensitiveCStringComparator())) {
|
2002-03-20 22:50:33 +00:00
|
|
|
contentCharset = Substring(it, end);
|
|
|
|
contentCharset.StripWhitespace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
contentType = rawContentType;
|
|
|
|
ToLowerCase(contentType);
|
|
|
|
contentType.StripWhitespace();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewLocalFileInputStream(nsIInputStream **aResult,
|
|
|
|
nsIFile *aFile,
|
|
|
|
PRInt32 aIOFlags = -1,
|
|
|
|
PRInt32 aPerm = -1,
|
|
|
|
PRInt32 aBehaviorFlags = 0)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kLocalFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIFileInputStream> in =
|
|
|
|
do_CreateInstance(kLocalFileInputStreamCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(aFile, aIOFlags, aPerm, aBehaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = in);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
2002-02-19 07:07:23 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewLocalFileOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIFile *aFile,
|
|
|
|
PRInt32 aIOFlags = -1,
|
|
|
|
PRInt32 aPerm = -1,
|
|
|
|
PRInt32 aBehaviorFlags = 0)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kLocalFileOutputStreamCID, NS_LOCALFILEOUTPUTSTREAM_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIFileOutputStream> out =
|
|
|
|
do_CreateInstance(kLocalFileOutputStreamCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(aFile, aIOFlags, aPerm, aBehaviorFlags);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = out);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00: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 13:57:09 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_BackgroundInputStream(nsIInputStream **aResult,
|
|
|
|
nsIInputStream *aStream,
|
|
|
|
PRUint32 aSegmentSize = 0,
|
|
|
|
PRUint32 aSegmentCount = 0)
|
2003-02-25 13:57:09 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
|
|
|
rv = sts->CreateInputTransport(aStream, -1, -1, PR_TRUE,
|
|
|
|
getter_AddRefs(inTransport));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenInputStream(nsITransport::OPEN_BLOCKING,
|
|
|
|
aSegmentSize, aSegmentCount,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-02-25 13:57:09 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00: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 13:57:09 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_BackgroundOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIOutputStream *aStream,
|
|
|
|
PRUint32 aSegmentSize = 0,
|
|
|
|
PRUint32 aSegmentCount = 0)
|
2003-02-25 13:57:09 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIStreamTransportService> sts =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv);
|
2003-06-18 23:07:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITransport> inTransport;
|
|
|
|
rv = sts->CreateOutputTransport(aStream, -1, -1, PR_TRUE,
|
|
|
|
getter_AddRefs(inTransport));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = inTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING,
|
|
|
|
aSegmentSize, aSegmentCount,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-02-25 13:57:09 +00:00
|
|
|
}
|
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewBufferedInputStream(nsIInputStream **aResult,
|
|
|
|
nsIInputStream *aStr,
|
|
|
|
PRUint32 aBufferSize)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIBufferedInputStream> in =
|
|
|
|
do_CreateInstance(kBufferedInputStreamCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = in->Init(aStr, aBufferSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = in);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewBufferedOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIOutputStream *aStr,
|
|
|
|
PRUint32 aBufferSize)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kBufferedOutputStreamCID, NS_BUFFEREDOUTPUTSTREAM_CID);
|
2003-06-18 23:07:09 +00:00
|
|
|
nsCOMPtr<nsIBufferedOutputStream> out =
|
|
|
|
do_CreateInstance(kBufferedOutputStreamCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = out->Init(aStr, aBufferSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*aResult = out);
|
|
|
|
}
|
|
|
|
return rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00:00
|
|
|
// returns an input stream compatible with nsIUploadChannel::SetUploadStream()
|
2003-01-18 02:15:14 +00:00
|
|
|
inline nsresult
|
2003-06-18 23:07:09 +00:00
|
|
|
NS_NewPostDataStream(nsIInputStream **result,
|
|
|
|
PRBool isFile,
|
2003-01-18 02:15:14 +00:00
|
|
|
const nsACString &data,
|
2003-06-18 23:07:09 +00:00
|
|
|
PRUint32 encodeFlags,
|
|
|
|
nsIIOService *unused = nsnull)
|
2003-01-18 02:15:14 +00:00
|
|
|
{
|
|
|
|
if (isFile) {
|
2003-06-18 23:07:09 +00:00
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
|
|
|
|
|
|
|
rv = NS_NewNativeLocalFile(data, PR_FALSE, getter_AddRefs(file));
|
2003-06-18 23:07:09 +00: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 02:15:14 +00:00
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00:00
|
|
|
// otherwise, create a string stream for the data (copies)
|
2003-01-18 02:15:14 +00:00
|
|
|
return NS_NewCStringInputStream(result, data);
|
|
|
|
}
|
|
|
|
|
2003-07-11 21:31:54 +00:00
|
|
|
inline nsresult
|
|
|
|
NS_LoadPersistentPropertiesFromURI(nsIPersistentProperties **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIIOService *ioService = nsnull)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-06-18 23:07:09 +00:00
|
|
|
#endif // !nsNetUtil_h__
|