2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-11-01 23:48:12 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
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.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* 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
|
|
|
*
|
2002-02-14 06:10:19 +03:00
|
|
|
* Contributor(s): Bradley Baetz <bbaetz@student.usyd.edu.au>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* 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
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* 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
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** 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"
|
2002-03-21 01:50:33 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prio.h" // for read/write flags, permissions, etc.
|
|
|
|
|
2002-03-21 01:50:33 +03:00
|
|
|
#include "nsIURI.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsIInputStream.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
#include "nsIOutputStream.h"
|
1999-11-01 23:48:12 +03:00
|
|
|
#include "nsIStreamListener.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsIRequestObserverProxy.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIStreamListenerProxy.h" // XXX for nsIAsyncStreamListener
|
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"
|
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"
|
2002-02-14 06:10:19 +03:00
|
|
|
#include "nsIResumableEntityID.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"
|
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"
|
2001-01-24 01:10:34 +03:00
|
|
|
|
2000-09-09 03:31:26 +04: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-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
|
|
|
{
|
2002-03-06 10:48:55 +03:00
|
|
|
return NS_NewURI(result, NS_ConvertUCS2toUTF8(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
|
2003-06-19 03:07:09 +04: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 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) {
|
|
|
|
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 07:58:50 +04:00
|
|
|
}
|
1999-11-01 23:48:12 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-02-03 02:04:47 +03:00
|
|
|
// Use this function with CAUTION. And do not use it on
|
|
|
|
// the UI thread. It creates a stream that blocks when
|
|
|
|
// you Read() from it and blocking the UI thread is
|
|
|
|
// illegal. If you don't want to implement a full
|
|
|
|
// blown asyncrhonous consumer (via nsIStreamListener)
|
|
|
|
// look 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,
|
|
|
|
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)) {
|
|
|
|
nsIInputStream *stream;
|
|
|
|
rv = channel->Open(&stream);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
*result = stream;
|
|
|
|
}
|
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
|
|
|
|
rv = baseURI->Resolve(NS_ConvertUCS2toUTF8(spec), resultBuf);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
CopyUTF8toUTF16(resultBuf, result);
|
|
|
|
}
|
|
|
|
return rv;
|
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,
|
|
|
|
const nsACString &contentType = NS_LITERAL_CSTRING(""),
|
2003-01-18 05:15:14 +03:00
|
|
|
const nsACString &contentCharset = NS_LITERAL_CSTRING(""))
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
static NS_DEFINE_CID(kInputStreamChannelCID, NS_INPUTSTREAMCHANNEL_CID);
|
|
|
|
nsCOMPtr<nsIInputStreamChannel> channel =
|
|
|
|
do_CreateInstance(kInputStreamChannelCID, &rv);
|
2003-06-19 03:07:09 +04: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 23:48:12 +03:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
PRInt32 streamPos = -1,
|
|
|
|
PRInt32 streamLen = -1,
|
|
|
|
PRUint32 segsize = 0,
|
|
|
|
PRUint32 segcount = 0,
|
|
|
|
PRBool closeWhenDone = PR_FALSE)
|
2000-04-13 13:20:50 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
static NS_DEFINE_CID(kInputStreamPumpCID, NS_INPUTSTREAMPUMP_CID);
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump =
|
|
|
|
do_CreateInstance(kInputStreamPumpCID, &rv);
|
2003-06-19 03:07:09 +04: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 13:20:50 +04:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// NOTE: you can optimize the copy by specifying whether or not your streams
|
|
|
|
// are buffered (i.e., do they implement ReadSegments/WriteSegments). the
|
|
|
|
// default assumption of FALSE for both streams is OK, but the copy is much
|
|
|
|
// more efficient if one of the streams is buffered.
|
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,
|
|
|
|
PRBool sourceBuffered = PR_FALSE,
|
|
|
|
PRBool sinkBuffered = PR_FALSE,
|
|
|
|
PRUint32 chunkSize = 0)
|
1999-11-30 07:50:42 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
static NS_DEFINE_CID(kAsyncStreamCopierCID, NS_ASYNCSTREAMCOPIER_CID);
|
|
|
|
nsCOMPtr<nsIAsyncStreamCopier> copier =
|
|
|
|
do_CreateInstance(kAsyncStreamCopierCID, &rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = copier->Init(source, sink, sourceBuffered, sinkBuffered, chunkSize);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
NS_ADDREF(*result = copier);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
static NS_DEFINE_CID(kLoadGroupCID, NS_LOADGROUP_CID);
|
2003-06-19 03:07:09 +04: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 07:50:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-15 23:27:05 +04:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewDownloader(nsIDownloader **result,
|
|
|
|
nsIURI *uri,
|
|
|
|
nsIDownloadObserver *observer,
|
|
|
|
nsISupports *context = nsnull,
|
|
|
|
PRBool synchronous = PR_FALSE,
|
|
|
|
nsILoadGroup *loadGroup = nsnull,
|
|
|
|
nsIInterfaceRequestor *callbacks = nsnull,
|
|
|
|
PRUint32 loadFlags = nsIRequest::LOAD_NORMAL)
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kDownloaderCID, NS_DOWNLOADER_CID);
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIDownloader> downloader =
|
|
|
|
do_CreateInstance(kDownloaderCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = downloader->Init(uri, observer, context, synchronous, loadGroup,
|
|
|
|
callbacks, loadFlags);
|
|
|
|
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
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewStreamLoader(nsIStreamLoader **aResult,
|
|
|
|
nsIChannel *aChannel,
|
2002-09-27 08:22:13 +04:00
|
|
|
nsIStreamLoaderObserver *aObserver,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsISupports *aContext)
|
1999-11-01 23:48:12 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-02-03 02:04:47 +03:00
|
|
|
static NS_DEFINE_CID(kStreamLoaderCID, NS_STREAMLOADER_CID);
|
2003-06-19 03:07:09 +04: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 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);
|
|
|
|
rv = NS_NewStreamLoader(result, channel, observer, context);
|
|
|
|
}
|
|
|
|
return rv;
|
2002-09-27 08:22:13 +04:00
|
|
|
}
|
|
|
|
|
2002-08-28 03:47:25 +04:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **aResult,
|
|
|
|
nsIChannel *aChannel,
|
2002-08-28 03:47:25 +04:00
|
|
|
nsIUnicharStreamLoaderObserver *aObserver,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsISupports *aContext = nsnull,
|
|
|
|
PRUint32 aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE)
|
2002-08-28 03:47:25 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kUnicharStreamLoaderCID, NS_UNICHARSTREAMLOADER_CID);
|
2003-06-19 03:07:09 +04: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-28 03:47:25 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-01-24 01:10:34 +03:00
|
|
|
inline nsresult
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_NewRequestObserverProxy(nsIRequestObserver **aResult,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIRequestObserver *aObserver,
|
|
|
|
nsIEventQueue *aEventQ = nsnull)
|
2001-01-24 01:10:34 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2001-04-10 10:01:08 +04:00
|
|
|
static NS_DEFINE_CID(kRequestObserverProxyCID, NS_REQUESTOBSERVERPROXY_CID);
|
2003-06-19 03:07:09 +04: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-24 01:10:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
NS_NewSimpleStreamListener(nsIStreamListener **aResult,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIOutputStream *aSink,
|
|
|
|
nsIRequestObserver *aObserver = nsnull)
|
2001-01-24 01:10:34 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kSimpleStreamListenerCID, NS_SIMPLESTREAMLISTENER_CID);
|
2003-06-19 03:07:09 +04: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-24 01:10:34 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// Deprecated, prefer NS_NewStreamListenerProxy
|
1999-11-30 07:50:42 +03:00
|
|
|
inline nsresult
|
2000-03-29 07:58:50 +04:00
|
|
|
NS_NewAsyncStreamListener(nsIStreamListener **result,
|
2003-06-19 03:07:09 +04:00
|
|
|
nsIStreamListener *receiver,
|
|
|
|
nsIEventQueue *eventQueue)
|
1999-11-30 07:50:42 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kAsyncStreamListenerCID, NS_ASYNCSTREAMLISTENER_CID);
|
2003-06-19 03:07:09 +04: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 07:50:42 +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) {
|
|
|
|
PRBool allow;
|
|
|
|
rv = ioService->AllowPort(port, scheme, &allow);
|
|
|
|
if (NS_SUCCEEDED(rv) && !allow)
|
|
|
|
rv = NS_ERROR_PORT_ACCESS_NOT_ALLOWED;
|
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
|
|
|
}
|
|
|
|
|
2001-09-13 06:21:05 +04:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewProxyInfo(const char *type,
|
|
|
|
const char *host,
|
|
|
|
PRInt32 port,
|
|
|
|
nsIProxyInfo **result)
|
2001-09-13 06:21:05 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kPPSServiceCID, NS_PROTOCOLPROXYSERVICE_CID);
|
2003-06-19 03:07:09 +04:00
|
|
|
nsCOMPtr<nsIProtocolProxyService> pps = do_GetService(kPPSServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = pps->NewProxyInfo(type, host, port, result);
|
|
|
|
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
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_GetURLSpecFromFile(nsIFile *aFile,
|
|
|
|
nsACString &aUrl,
|
|
|
|
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))
|
|
|
|
rv = fileHandler->GetURLSpecFromFile(aFile, aUrl);
|
|
|
|
return rv;
|
2001-12-19 03:55:42 +03:00
|
|
|
}
|
|
|
|
|
2002-02-14 06:10:19 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewResumableEntityID(nsIResumableEntityID **aRes,
|
|
|
|
PRUint32 size,
|
|
|
|
PRTime lastModified)
|
2002-02-14 06:10:19 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIResumableEntityID> ent =
|
|
|
|
do_CreateInstance(NS_RESUMABLEENTITYID_CONTRACTID,&rv);
|
2003-06-19 03:07:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ent->SetSize(size);
|
|
|
|
ent->SetLastModified(lastModified);
|
|
|
|
NS_ADDREF(*aRes = ent);
|
|
|
|
}
|
|
|
|
return rv;
|
2002-02-14 06:10:19 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
static NS_DEFINE_CID(kPPSServiceCID, NS_PROTOCOLPROXYSERVICE_CID);
|
2003-06-19 03:07:09 +04: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 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
|
|
|
|
nsACString::const_iterator begin, it, end;
|
|
|
|
it = rawContentType.BeginReading(begin);
|
2002-04-23 03:55:30 +04:00
|
|
|
rawContentType.EndReading(end);
|
2002-03-21 01:50:33 +03:00
|
|
|
if (FindCharInReadable(';', it, end)) {
|
|
|
|
contentType = Substring(begin, it);
|
|
|
|
// now look for "charset=FOO" and extract "FOO"
|
|
|
|
begin = ++it;
|
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("charset="), begin, it = end)) {
|
|
|
|
contentCharset = Substring(it, end);
|
|
|
|
contentCharset.StripWhitespace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
contentType = rawContentType;
|
|
|
|
ToLowerCase(contentType);
|
|
|
|
contentType.StripWhitespace();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewLocalFileInputStream(nsIInputStream **aResult,
|
|
|
|
nsIFile *aFile,
|
|
|
|
PRInt32 aIOFlags = -1,
|
|
|
|
PRInt32 aPerm = -1,
|
|
|
|
PRInt32 aBehaviorFlags = 0)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kLocalFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID);
|
2003-06-19 03:07:09 +04: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 05:15:14 +03:00
|
|
|
}
|
2002-02-19 10:07:23 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewLocalFileOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIFile *aFile,
|
|
|
|
PRInt32 aIOFlags = -1,
|
|
|
|
PRInt32 aPerm = -1,
|
|
|
|
PRInt32 aBehaviorFlags = 0)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kLocalFileOutputStreamCID, NS_LOCALFILEOUTPUTSTREAM_CID);
|
2003-06-19 03:07:09 +04: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 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
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
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_BackgroundInputStream(nsIInputStream **aResult,
|
|
|
|
nsIInputStream *aStream,
|
|
|
|
PRUint32 aSegmentSize = 0,
|
|
|
|
PRUint32 aSegmentCount = 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;
|
|
|
|
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 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
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_BackgroundOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIOutputStream *aStream,
|
|
|
|
PRUint32 aSegmentSize = 0,
|
|
|
|
PRUint32 aSegmentCount = 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;
|
|
|
|
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 16:57:09 +03:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewBufferedInputStream(nsIInputStream **aResult,
|
|
|
|
nsIInputStream *aStr,
|
|
|
|
PRUint32 aBufferSize)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID);
|
2003-06-19 03:07:09 +04: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 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsresult
|
2003-06-19 03:07:09 +04:00
|
|
|
NS_NewBufferedOutputStream(nsIOutputStream **aResult,
|
|
|
|
nsIOutputStream *aStr,
|
|
|
|
PRUint32 aBufferSize)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
static NS_DEFINE_CID(kBufferedOutputStreamCID, NS_BUFFEREDOUTPUTSTREAM_CID);
|
2003-06-19 03:07:09 +04: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 05:15:14 +03:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
PRBool 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
|
|
|
{
|
|
|
|
if (isFile) {
|
2003-06-19 03:07:09 +04:00
|
|
|
nsresult rv;
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
nsCOMPtr<nsIInputStream> fileStream;
|
|
|
|
|
|
|
|
rv = NS_NewNativeLocalFile(data, PR_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)
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_NewCStringInputStream(result, data);
|
|
|
|
}
|
|
|
|
|
2003-06-19 03:07:09 +04:00
|
|
|
#endif // !nsNetUtil_h__
|