зеркало из https://github.com/mozilla/pjs.git
Checking in new test harness for URL retrieval
This commit is contained in:
Родитель
1ab657043a
Коммит
33ad92e1a3
|
@ -0,0 +1,96 @@
|
|||
#!gmake
|
||||
#
|
||||
# The contents of this file are subject to the Netscape Public License
|
||||
# Version 1.0 (the "NPL"); you may not use this file except in
|
||||
# compliance with the NPL. You may obtain a copy of the NPL at
|
||||
# http://www.mozilla.org/NPL/
|
||||
#
|
||||
# Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
# for the specific language governing rights and limitations under the
|
||||
# NPL.
|
||||
#
|
||||
# The Initial Developer of this code under the NPL is Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
# Reserved.
|
||||
|
||||
DEPTH = ../../../..
|
||||
topsrcdir = @top_srcdir@
|
||||
srcdir = @srcdir@
|
||||
VPATH = @srcdir@
|
||||
|
||||
include $(DEPTH)/config/autoconf.mk
|
||||
|
||||
CPPSRCS = geturl.cpp \
|
||||
nsURLFetcher.cpp \
|
||||
$(NULL)
|
||||
|
||||
LOCAL_INCLUDES =
|
||||
|
||||
REQUIRES =
|
||||
|
||||
TOOLKIT_GFX_LIB := -lgfx$(MOZ_TOOLKIT)
|
||||
TOOLKIT_WIDGET_LIB := -lwidget$(MOZ_TOOLKIT)
|
||||
|
||||
BASE_LIBS = \
|
||||
-lpref \
|
||||
$(ZLIB_LIBS) \
|
||||
-lreg \
|
||||
-l$(MOZ_LIB_JS_PREFIX)js \
|
||||
-lxpcom \
|
||||
-lsecfree \
|
||||
-l$(MOZ_LIB_UTIL_PREFIX)util \
|
||||
$(NULL)
|
||||
|
||||
GECKO_LIBS = \
|
||||
-lraptorwebwidget \
|
||||
$(TOOLKIT_WIDGET_LIB) \
|
||||
-lraptorgfx \
|
||||
$(TOOLKIT_GFX_LIB) \
|
||||
-lgfxps \
|
||||
-lraptorhtmlpars \
|
||||
-lexpat \
|
||||
-lxmltok \
|
||||
-lraptorview \
|
||||
-ljsdom \
|
||||
-lraptorplugin \
|
||||
$(NULL)
|
||||
|
||||
IMGLIB_LIBS = \
|
||||
-limg \
|
||||
$(JPEG_LIBS) \
|
||||
$(PNG_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
NETLIB_LIBS = \
|
||||
-labouturl \
|
||||
-lfileurl \
|
||||
-lftpurl \
|
||||
-lgophurl \
|
||||
-lhttpurl \
|
||||
-lremoturl \
|
||||
-lsockstuburl \
|
||||
-lmimetype \
|
||||
-lnetcache \
|
||||
-lnetcnvts \
|
||||
-lnetlib \
|
||||
-lnetwork \
|
||||
-lxp \
|
||||
-ljsurl \
|
||||
-l$(MOZ_LIB_DBM_PREFIX)dbm \
|
||||
-lpwcac \
|
||||
$(NULL)
|
||||
|
||||
LIBS = \
|
||||
$(GECKO_LIBS) \
|
||||
$(NETLIB_LIBS) \
|
||||
$(IMGLIB_LIBS) \
|
||||
$(BASE_LIBS) \
|
||||
$(NSPR_LIBS) \
|
||||
$(TK_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
PROGRAM = geturl
|
||||
|
||||
include $(topsrcdir)/config/rules.mk
|
|
@ -0,0 +1,270 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
/*
|
||||
This program simply goes through the network service to grab a URL
|
||||
and write it to standard out...
|
||||
|
||||
The program takes a single parameter: url to fetch
|
||||
*/
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsIEventQueueService.h"
|
||||
#include "nsINetService.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsIGenericFactory.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsFileStream.h"
|
||||
#include "nsFileSpec.h"
|
||||
#include "nsMimeTypes.h"
|
||||
#include "nsIPref.h"
|
||||
#include "nsICharsetConverterManager.h"
|
||||
#include "prprf.h"
|
||||
#include "nsIAllocator.h" // for the CID
|
||||
#include "nsURLFetcher.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include "windows.h"
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// THIS IS THE STUFF TO GET THE TEST HARNESS OFF THE GROUND
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
#if defined(XP_PC)
|
||||
#define NETLIB_DLL "netlib.dll"
|
||||
#define XPCOM_DLL "xpcom32.dll"
|
||||
#define MIME_DLL "mime.dll"
|
||||
#define PREF_DLL "xppref32.dll"
|
||||
#define UNICHAR_DLL "uconv.dll"
|
||||
#elif defined(XP_UNIX)
|
||||
#define NETLIB_DLL "libnetlib"MOZ_DLL_SUFFIX
|
||||
#define XPCOM_DLL "libxpcom"MOZ_DLL_SUFFIX
|
||||
#define MIME_DLL "libmime.dll"MOZ_DLL_SUFFIX
|
||||
#define PREF_DLL "libxppref32"MOZ_DLL_SUFFIX
|
||||
#define UNICHAR_DLL "libunicharutil"MOZ_DLL_SUFFIX
|
||||
#elif defined(XP_MAC)
|
||||
#define NETLIB_DLL "NETLIB_DLL"
|
||||
#define XPCOM_DLL "XPCOM_DLL"
|
||||
#define MIME_DLL "MIME_DLL"
|
||||
#define PREF_DLL "XPPREF32_DLL"
|
||||
#define UNICHAR_DLL "UNICHARUTIL_DLL"
|
||||
#endif
|
||||
|
||||
// {588595CB-2012-11d3-8EF0-00A024A7D144}
|
||||
#define CONV_CID { 0x1e3f79f1, 0x6b6b, 0x11d2, { 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } };
|
||||
#define CONV_IID { 0x1e3f79f0, 0x6b6b, 0x11d2, { 0x8a, 0x86, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36 } }
|
||||
|
||||
// CIDs
|
||||
|
||||
// I18N
|
||||
static NS_DEFINE_CID(charsetCID, CONV_CID);
|
||||
NS_DEFINE_IID(kConvMeIID, CONV_IID);
|
||||
|
||||
// prefs
|
||||
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
|
||||
|
||||
// xpcom
|
||||
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
||||
static NS_DEFINE_CID(kGenericFactoryCID, NS_GENERICFACTORY_CID);
|
||||
|
||||
// netlib definitions....
|
||||
static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID);
|
||||
|
||||
nsICharsetConverterManager *ccMan = nsnull;
|
||||
|
||||
static nsresult
|
||||
SetupRegistry(void)
|
||||
{
|
||||
// i18n
|
||||
nsComponentManager::RegisterComponent(charsetCID, NULL, NULL, UNICHAR_DLL, PR_FALSE, PR_FALSE);
|
||||
nsresult res = nsServiceManager::GetService(charsetCID, kConvMeIID, (nsISupports **)&ccMan);
|
||||
if (NS_FAILED(res))
|
||||
{
|
||||
printf("ERROR at GetService() code=0x%x.\n",res);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
// netlib
|
||||
nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);
|
||||
|
||||
// xpcom
|
||||
static NS_DEFINE_CID(kAllocatorCID, NS_ALLOCATOR_CID);
|
||||
static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
||||
nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
|
||||
nsComponentManager::RegisterComponent(kEventQueueCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
|
||||
nsComponentManager::RegisterComponent(kGenericFactoryCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
|
||||
nsComponentManager::RegisterComponent(kAllocatorCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);
|
||||
|
||||
// prefs
|
||||
nsComponentManager::RegisterComponent(kPrefCID, NULL, NULL, PREF_DLL, PR_FALSE, PR_FALSE);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// END OF STUFF TO GET THE TEST HARNESS OFF THE GROUND
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// THIS IS THE CLASS THAT WOULD BE IMPLEMENTED BY THE CONSUMER OF THE HTML OUPUT
|
||||
// FROM LIBMIME. THIS EXAMPLE SIMPLY WRITES THE OUTPUT TO STDOUT()
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
class ConsoleOutputStreamImpl : public nsIOutputStream
|
||||
{
|
||||
public:
|
||||
ConsoleOutputStreamImpl(void) { NS_INIT_REFCNT(); }
|
||||
virtual ~ConsoleOutputStreamImpl(void) {}
|
||||
|
||||
// nsISupports interface
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
// nsIBaseStream interface
|
||||
NS_IMETHOD Close(void)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// nsIOutputStream interface
|
||||
NS_IMETHOD Write(const char* aBuf, PRUint32 aCount, PRUint32 *aWriteCount) {
|
||||
PR_Write(PR_GetSpecialFD(PR_StandardOutput), aBuf, aCount);
|
||||
*aWriteCount = aCount;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHOD Flush(void) {
|
||||
PR_Sync(PR_GetSpecialFD(PR_StandardOutput));
|
||||
return NS_OK;
|
||||
}
|
||||
};
|
||||
NS_IMPL_ISUPPORTS(ConsoleOutputStreamImpl, nsIOutputStream::GetIID());
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
// END OF CONSUMER STREAM
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Utility to create a nsIURI object...
|
||||
nsresult
|
||||
nsMsgNewURL(nsIURI** aInstancePtrResult, const nsString& aSpec)
|
||||
{
|
||||
if (nsnull == aInstancePtrResult)
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
||||
nsINetService *inet = nsnull;
|
||||
nsresult rv = nsServiceManager::GetService(kNetServiceCID, nsINetService::GetIID(),
|
||||
(nsISupports **)&inet);
|
||||
if (rv != NS_OK)
|
||||
return rv;
|
||||
|
||||
rv = inet->CreateURL(aInstancePtrResult, aSpec, nsnull, nsnull, nsnull);
|
||||
nsServiceManager::ReleaseService(kNetServiceCID, inet);
|
||||
return rv;
|
||||
}
|
||||
|
||||
void
|
||||
FlushEvents(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
MSG msg;
|
||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
nsresult
|
||||
MyCallback(nsIURI* aURL, nsresult aStatus, PRInt32 totalSize, const PRUnichar* aMsg, void *tagData)
|
||||
{
|
||||
printf("Done with URL Request. Exit Code = [%d] - Received [%d] bytes\n", aStatus, totalSize);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
nsresult rv;
|
||||
nsIURI *aURL;
|
||||
|
||||
// Do some sanity checking...
|
||||
if (argc < 3)
|
||||
{
|
||||
fprintf(stderr, "usage: %s <url> <output_file>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup the registry...
|
||||
SetupRegistry();
|
||||
|
||||
// Get an event queue and get it started...
|
||||
NS_WITH_SERVICE(nsIEventQueueService, theEventQueueService, kEventQueueServiceCID, &rv);
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
rv = theEventQueueService->CreateThreadEventQueue();
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create thread event queue");
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
// Create an nsIURI object needed for the URL operation IO...
|
||||
if (NS_FAILED(nsMsgNewURL(&aURL, nsString(argv[1]))))
|
||||
{
|
||||
printf("Unable to create URL\n");
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
// Create a fetcher for the URL attachment...
|
||||
nsURLFetcher *sFetcher = new nsURLFetcher();
|
||||
if (!sFetcher)
|
||||
{
|
||||
printf("Failed to create an attachment stream listener...\n");
|
||||
return rv;
|
||||
}
|
||||
|
||||
nsFileSpec fSpec(argv[2]);
|
||||
nsOutputFileStream dstFile(fSpec, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE );
|
||||
if (!dstFile.is_open() )
|
||||
{
|
||||
printf("Failed to open the output file!\n");
|
||||
return rv;
|
||||
}
|
||||
|
||||
rv = sFetcher->FireURLRequest(aURL, &dstFile, MyCallback, sFetcher);
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
printf("Failed to fire the URL request\n");
|
||||
return rv;
|
||||
}
|
||||
|
||||
PRBool running = PR_TRUE;
|
||||
while (running)
|
||||
{
|
||||
sFetcher->StillRunning(&running);
|
||||
FlushEvents();
|
||||
}
|
||||
|
||||
NS_RELEASE(sFetcher);
|
||||
|
||||
// Cleanup stuff necessary...
|
||||
NS_RELEASE(ccMan);
|
||||
return NS_OK;
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
#!gmake
|
||||
#
|
||||
# The contents of this file are subject to the Netscape Public License
|
||||
# Version 1.0 (the "NPL"); you may not use this file except in
|
||||
# compliance with the NPL. You may obtain a copy of the NPL at
|
||||
# http://www.mozilla.org/NPL/
|
||||
#
|
||||
# Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
# for the specific language governing rights and limitations under the
|
||||
# NPL.
|
||||
#
|
||||
# The Initial Developer of this code under the NPL is Netscape
|
||||
# Communications Corporation. Portions created by Netscape are
|
||||
# Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
# Reserved.
|
||||
|
||||
|
||||
DEPTH = ..\..\..\..
|
||||
MODULE=geturl
|
||||
|
||||
MAKE_OBJ_TYPE=EXE
|
||||
|
||||
EXENAME=$(MODULE)
|
||||
PDBFILE=$(MODULE)
|
||||
MAPFILE=$(MODULE).map
|
||||
|
||||
PROGRAM= .\$(OBJDIR)\$(EXENAME).exe
|
||||
|
||||
OBJS=\
|
||||
.\$(OBJDIR)\geturl.obj \
|
||||
.\$(OBJDIR)\nsURLFetcher.obj \
|
||||
$(NULL)
|
||||
|
||||
LLIBS= \
|
||||
$(DIST)\lib\rdf.lib \
|
||||
$(DIST)\lib\xpcom.lib \
|
||||
$(DIST)\lib\netlib.lib \
|
||||
$(DIST)\lib\plc3.lib \
|
||||
$(DIST)\lib\uconv.lib \
|
||||
$(LIBNSPR)
|
||||
|
||||
include <$(DEPTH)\config\rules.mak>
|
||||
|
||||
install:: $(PROGRAM)
|
||||
$(MAKE_INSTALL) $(PROGRAM) $(DIST)\bin
|
||||
|
|
@ -0,0 +1,267 @@
|
|||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
#include "nsCOMPtr.h"
|
||||
#include "stdio.h"
|
||||
#include "nscore.h"
|
||||
#include "nsIFactory.h"
|
||||
#include "nsISupports.h"
|
||||
#include "comi18n.h"
|
||||
#include "prmem.h"
|
||||
#include "plstr.h"
|
||||
#include "nsRepository.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsString.h"
|
||||
#include "nsINetService.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsURLFetcher.h"
|
||||
|
||||
// netlib definitions....
|
||||
static NS_DEFINE_CID(kNetServiceCID, NS_NETSERVICE_CID);
|
||||
|
||||
/*
|
||||
* This function will be used by the factory to generate an
|
||||
* mime object class object....
|
||||
*/
|
||||
nsresult NS_NewURLFetcher(nsURLFetcher ** aInstancePtrResult)
|
||||
{
|
||||
//nsresult result = NS_OK;
|
||||
NS_PRECONDITION(nsnull != aInstancePtrResult, "nsnull ptr");
|
||||
if (nsnull != aInstancePtrResult)
|
||||
{
|
||||
nsURLFetcher *obj = new nsURLFetcher();
|
||||
if (obj)
|
||||
return obj->QueryInterface(nsIStreamListener::GetIID(), (void**) aInstancePtrResult);
|
||||
else
|
||||
return NS_ERROR_OUT_OF_MEMORY; // we couldn't allocate the object
|
||||
}
|
||||
else
|
||||
return NS_ERROR_NULL_POINTER; // aInstancePtrResult was NULL....
|
||||
}
|
||||
|
||||
// The following macros actually implement addref, release and
|
||||
// query interface for our component.
|
||||
NS_IMPL_ISUPPORTS(nsURLFetcher, nsIStreamListener::GetIID());
|
||||
|
||||
/*
|
||||
* Inherited methods for nsMimeConverter
|
||||
*/
|
||||
nsURLFetcher::nsURLFetcher()
|
||||
{
|
||||
/* the following macro is used to initialize the ref counting data */
|
||||
NS_INIT_REFCNT();
|
||||
|
||||
// Init member variables...
|
||||
mOutStream = nsnull;
|
||||
mTotalWritten = 0;
|
||||
mURL = nsnull;
|
||||
mStillRunning = PR_TRUE;
|
||||
mCallback = nsnull;
|
||||
mNetService = nsnull;
|
||||
}
|
||||
|
||||
nsURLFetcher::~nsURLFetcher()
|
||||
{
|
||||
mStillRunning = PR_FALSE;
|
||||
if (mNetService)
|
||||
{
|
||||
nsServiceManager::ReleaseService(kNetServiceCID, mNetService);
|
||||
}
|
||||
|
||||
NS_RELEASE(mURL);
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsURLFetcher::StillRunning(PRBool *running)
|
||||
{
|
||||
*running = mStillRunning;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
// Methods for nsIStreamListener...
|
||||
//
|
||||
// Return information regarding the current URL load.<BR>
|
||||
// The info structure that is passed in is filled out and returned
|
||||
// to the caller.
|
||||
//
|
||||
//This method is currently not called.
|
||||
//
|
||||
nsresult
|
||||
nsURLFetcher::GetBindInfo(nsIURI* aURL, nsStreamBindingInfo* aInfo)
|
||||
{
|
||||
#ifdef NS_DEBUG_richie
|
||||
printf("nsURLFetcher::GetBindInfo()\n");
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the client that data is available in the input stream. This
|
||||
* method is called whenver data is written into the input stream by the
|
||||
* networking library...<BR><BR>
|
||||
*
|
||||
* @param pIStream The input stream containing the data. This stream can
|
||||
* be either a blocking or non-blocking stream.
|
||||
* @param length The amount of data that was just pushed into the stream.
|
||||
* @return The return value is currently ignored.
|
||||
*/
|
||||
nsresult
|
||||
nsURLFetcher::OnDataAvailable(nsIURI* aURL, nsIInputStream *aIStream,
|
||||
PRUint32 aLength)
|
||||
{
|
||||
nsresult rc;
|
||||
PRUint32 readLen = aLength;
|
||||
|
||||
if (!mOutStream)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
char *buf = (char *)PR_Malloc(aLength);
|
||||
if (!buf)
|
||||
return NS_ERROR_OUT_OF_MEMORY; /* we couldn't allocate the object */
|
||||
|
||||
// read the data from the input stram...
|
||||
aIStream->Read(buf, aLength, &readLen);
|
||||
|
||||
// write to the output file...
|
||||
mTotalWritten += mOutStream->write(buf, readLen);
|
||||
|
||||
PR_FREEIF(buf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
// Methods for nsIStreamObserver
|
||||
/**
|
||||
* Notify the observer that the URL has started to load. This method is
|
||||
* called only once, at the beginning of a URL load.<BR><BR>
|
||||
*
|
||||
* @return The return value is currently ignored. In the future it may be
|
||||
* used to cancel the URL load..
|
||||
*/
|
||||
nsresult
|
||||
nsURLFetcher::OnStartBinding(nsIURI* aURL, const char *aContentType)
|
||||
{
|
||||
#ifdef NS_DEBUG_richie
|
||||
printf("nsURLFetcher::OnStartBinding() for Content-Type: %s\n", aContentType);
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the observer that progress as occurred for the URL load.<BR>
|
||||
*/
|
||||
nsresult
|
||||
nsURLFetcher::OnProgress(nsIURI* aURL, PRUint32 aProgress, PRUint32 aProgressMax)
|
||||
{
|
||||
#ifdef NS_DEBUG_richie
|
||||
printf("nsURLFetcher::OnProgress() - %d bytes\n", aProgress);
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the observer with a status message for the URL load.<BR>
|
||||
*/
|
||||
nsresult
|
||||
nsURLFetcher::OnStatus(nsIURI* aURL, const PRUnichar* aMsg)
|
||||
{
|
||||
#ifdef NS_DEBUG_richie
|
||||
nsString tmp(aMsg);
|
||||
char *msg = tmp.ToNewCString();
|
||||
printf("nsURLFetcher::OnStatus(): %s\n", msg);
|
||||
PR_FREEIF(msg);
|
||||
#endif
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the observer that the URL has finished loading. This method is
|
||||
* called once when the networking library has finished processing the
|
||||
* URL transaction initiatied via the nsINetService::Open(...) call.<BR><BR>
|
||||
*
|
||||
* This method is called regardless of whether the URL loaded successfully.<BR><BR>
|
||||
*
|
||||
* @param status Status code for the URL load.
|
||||
* @param msg A text string describing the error.
|
||||
* @return The return value is currently ignored.
|
||||
*/
|
||||
nsresult
|
||||
nsURLFetcher::OnStopBinding(nsIURI* aURL, nsresult aStatus, const PRUnichar* aMsg)
|
||||
{
|
||||
#ifdef NS_DEBUG_richie
|
||||
printf("nsURLFetcher::OnStopBinding()\n");
|
||||
#endif
|
||||
|
||||
//
|
||||
// Now complete the stream!
|
||||
//
|
||||
mStillRunning = PR_FALSE;
|
||||
|
||||
// First close the output stream...
|
||||
if (mOutStream)
|
||||
mOutStream->close();
|
||||
|
||||
// Now if there is a callback, we need to call it...
|
||||
if (mCallback)
|
||||
mCallback (mURL, aStatus, mTotalWritten, aMsg, mTagData);
|
||||
|
||||
// Time to return...
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
nsURLFetcher::FireURLRequest(nsIURI *aURL, nsOutputFileStream *fOut,
|
||||
nsAttachSaveCompletionCallback cb, void *tagData)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
if ( (!aURL) || (!fOut) )
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
if (!fOut->is_open())
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
rv = nsServiceManager::GetService(kNetServiceCID, nsINetService::GetIID(),
|
||||
(nsISupports **)&mNetService);
|
||||
if ((rv != NS_OK) || (!mNetService))
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
if (NS_FAILED(mNetService->OpenStream(aURL, this)))
|
||||
{
|
||||
nsServiceManager::ReleaseService(kNetServiceCID, mNetService);
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
mURL = aURL;
|
||||
mOutStream = fOut;
|
||||
mCallback = cb;
|
||||
mTagData = tagData;
|
||||
NS_ADDREF(mURL);
|
||||
NS_ADDREF(this);
|
||||
return NS_OK;
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
#ifndef nsURLFetcher_h_
|
||||
#define nsURLFetcher_h_
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsFileStream.h"
|
||||
|
||||
//
|
||||
// Callback declarations for URL completion
|
||||
//
|
||||
// For completion of send/message creation operations...
|
||||
typedef nsresult (*nsAttachSaveCompletionCallback) (nsIURI* aURL, nsresult aStatus,
|
||||
PRInt32 totalSize, const PRUnichar* aMsg,
|
||||
void *tagData);
|
||||
|
||||
class nsURLFetcher : public nsIStreamListener {
|
||||
public:
|
||||
nsURLFetcher();
|
||||
virtual ~nsURLFetcher();
|
||||
|
||||
/* this macro defines QueryInterface, AddRef and Release for this class */
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
//
|
||||
// This is the output stream where the stream converter will write processed data after
|
||||
// conversion.
|
||||
//
|
||||
NS_IMETHOD StillRunning(PRBool *running);
|
||||
|
||||
NS_IMETHOD FireURLRequest(nsIURI *aURL, nsOutputFileStream *fOut,
|
||||
nsAttachSaveCompletionCallback cb, void *tagData);
|
||||
|
||||
// Methods for nsIStreamListener...
|
||||
/**
|
||||
* Return information regarding the current URL load.<BR>
|
||||
* The info structure that is passed in is filled out and returned
|
||||
* to the caller.
|
||||
*
|
||||
* This method is currently not called.
|
||||
*/
|
||||
NS_IMETHOD GetBindInfo(nsIURI* aURL, nsStreamBindingInfo* aInfo);
|
||||
|
||||
/**
|
||||
* Notify the client that data is available in the input stream. This
|
||||
* method is called whenver data is written into the input stream by the
|
||||
* networking library...<BR><BR>
|
||||
*
|
||||
* @param pIStream The input stream containing the data. This stream can
|
||||
* be either a blocking or non-blocking stream.
|
||||
* @param length The amount of data that was just pushed into the stream.
|
||||
* @return The return value is currently ignored.
|
||||
*/
|
||||
NS_IMETHOD OnDataAvailable(nsIURI* aURL, nsIInputStream *aIStream,
|
||||
PRUint32 aLength);
|
||||
|
||||
|
||||
// Methods for nsIStreamObserver
|
||||
/**
|
||||
* Notify the observer that the URL has started to load. This method is
|
||||
* called only once, at the beginning of a URL load.<BR><BR>
|
||||
*
|
||||
* @return The return value is currently ignored. In the future it may be
|
||||
* used to cancel the URL load..
|
||||
*/
|
||||
NS_IMETHOD OnStartBinding(nsIURI* aURL, const char *aContentType);
|
||||
|
||||
/**
|
||||
* Notify the observer that progress as occurred for the URL load.<BR>
|
||||
*/
|
||||
NS_IMETHOD OnProgress(nsIURI* aURL, PRUint32 aProgress, PRUint32 aProgressMax);
|
||||
|
||||
/**
|
||||
* Notify the observer with a status message for the URL load.<BR>
|
||||
*/
|
||||
NS_IMETHOD OnStatus(nsIURI* aURL, const PRUnichar* aMsg);
|
||||
|
||||
/**
|
||||
* Notify the observer that the URL has finished loading. This method is
|
||||
* called once when the networking library has finished processing the
|
||||
* URL transaction initiatied via the nsINetService::Open(...) call.<BR><BR>
|
||||
*
|
||||
* This method is called regardless of whether the URL loaded successfully.<BR><BR>
|
||||
*
|
||||
* @param status Status code for the URL load.
|
||||
* @param msg A text string describing the error.
|
||||
* @return The return value is currently ignored.
|
||||
*/
|
||||
NS_IMETHOD OnStopBinding(nsIURI* aURL, nsresult aStatus, const PRUnichar* aMsg);
|
||||
|
||||
|
||||
private:
|
||||
nsINetService *mNetService; // Net service for the URL operation
|
||||
nsOutputFileStream *mOutStream; // the output file stream
|
||||
PRBool mStillRunning; // Are we still running?
|
||||
PRInt32 mTotalWritten; // Size counter variable
|
||||
nsIURI *mURL; // URL being processed
|
||||
void *mTagData; // Tag data for callback...
|
||||
nsAttachSaveCompletionCallback mCallback; // Callback to call once the file is saved...
|
||||
};
|
||||
|
||||
/* this function will be used by the factory to generate an class access object....*/
|
||||
extern nsresult NS_NewURLFetcher(nsURLFetcher **aInstancePtrResult);
|
||||
|
||||
#endif /* nsURLFetcher_h_ */
|
Загрузка…
Ссылка в новой задаче