1999-05-21 10:29:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-06-25 01:41:19 +04:00
|
|
|
The TestProtocols tests the basic protocols architecture and can
|
|
|
|
be used to test individual protocols as well. If this grows too
|
|
|
|
big then we should split it to individual protocols.
|
|
|
|
|
|
|
|
-Gagan Saksena 04/29/99
|
1999-05-21 10:29:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-06-02 02:02:40 +04:00
|
|
|
#ifdef WIN32
|
1999-05-21 10:29:13 +04:00
|
|
|
#include <windows.h>
|
1999-06-02 02:02:40 +04:00
|
|
|
#endif
|
1999-05-21 10:29:13 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIIOService.h"
|
1999-05-21 10:29:13 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInputStream.h"
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBufferInputStream.h"
|
1999-05-21 10:29:13 +04:00
|
|
|
#include "nsCRT.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIChannel.h"
|
1999-06-08 13:46:10 +04:00
|
|
|
#include "nsIURL.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIHTTPChannel.h"
|
1999-06-04 01:56:56 +04:00
|
|
|
#include "nsIHttpEventSink.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIEventSinkGetter.h"
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-07-01 00:00:19 +04:00
|
|
|
#if 1
|
1999-06-25 01:41:19 +04:00
|
|
|
// this test app handles cookies.
|
|
|
|
#include "nsICookieService.h"
|
|
|
|
static NS_DEFINE_CID(nsCookieServiceCID, NS_COOKIESERVICE_CID);
|
|
|
|
#endif // 0
|
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-06-08 01:33:30 +04:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
static PRTime gElapsedTime;
|
1999-06-23 23:27:44 +04:00
|
|
|
static int gKeepRunning = 0;
|
1999-06-25 11:52:12 +04:00
|
|
|
static PRBool gVerbose = PR_FALSE;
|
1999-05-21 10:29:13 +04:00
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
class URLLoadInfo : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
URLLoadInfo(const char* aUrl);
|
|
|
|
virtual ~URLLoadInfo();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
const char* Name() { return mURLString.GetBuffer(); }
|
|
|
|
PRInt32 mBytesRead;
|
|
|
|
PRTime mTotalTime;
|
|
|
|
PRTime mConnectTime;
|
|
|
|
nsString mURLString;
|
|
|
|
};
|
|
|
|
|
|
|
|
URLLoadInfo::URLLoadInfo(const char *aUrl) : mURLString(aUrl, eOneByte)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
|
|
|
mBytesRead = 0;
|
|
|
|
mConnectTime = mTotalTime = PR_Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
URLLoadInfo::~URLLoadInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-01 00:20:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS(URLLoadInfo,nsCOMTypeInfo<nsCOMTypeInfo><nsISupports>::GetIID());
|
1999-06-27 15:02:40 +04:00
|
|
|
|
|
|
|
|
1999-06-16 13:40:40 +04:00
|
|
|
class TestHTTPEventSink : public nsIHTTPEventSink
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
1999-06-16 13:40:40 +04:00
|
|
|
TestHTTPEventSink();
|
|
|
|
virtual ~TestHTTPEventSink();
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
// nsIHTTPEventSink interface...
|
|
|
|
NS_IMETHOD OnAwaitingInput(nsISupports* i_Context);
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
NS_IMETHOD OnHeadersAvailable(nsISupports* i_Context);
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
NS_IMETHOD OnProgress(nsISupports* i_Context,
|
|
|
|
PRUint32 i_Progress,
|
|
|
|
PRUint32 i_ProgressMax);
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
// OnRedirect gets fired only if you have set FollowRedirects on the handler!
|
|
|
|
NS_IMETHOD OnRedirect(nsISupports* i_Context,
|
|
|
|
nsIURI* i_NewLocation);
|
1999-06-16 13:40:40 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
TestHTTPEventSink::TestHTTPEventSink()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestHTTPEventSink::~TestHTTPEventSink()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-01 00:20:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TestHTTPEventSink,nsCOMTypeInfo<nsIHTTPEventSink>::GetIID());
|
1999-06-16 13:40:40 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestHTTPEventSink::OnAwaitingInput(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ TestHTTPEventSink::OnAwaitingInput +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestHTTPEventSink::OnHeadersAvailable(nsISupports* context)
|
|
|
|
{
|
1999-06-27 15:02:40 +04:00
|
|
|
if (gVerbose) {
|
|
|
|
printf("\n+++ TestHTTPEventSink::OnHeadersAvailable +++\n");
|
|
|
|
nsCOMPtr<nsIHTTPChannel> pHTTPCon(do_QueryInterface(context));
|
|
|
|
if (pHTTPCon) {
|
|
|
|
char* type;
|
|
|
|
//optimize later TODO allow atoms here...! intead of just the header strings
|
1999-06-30 10:05:10 +04:00
|
|
|
pHTTPCon->GetContentType(&type);
|
1999-06-27 15:02:40 +04:00
|
|
|
if (type) {
|
|
|
|
printf("\nRecieving ... %s\n", type);
|
|
|
|
nsCRT::free(type);
|
|
|
|
}
|
|
|
|
}
|
1999-06-16 13:40:40 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestHTTPEventSink::OnProgress(nsISupports* context, PRUint32 i_Progress, PRUint32 i_ProgressMax)
|
|
|
|
{
|
|
|
|
printf("\n+++ TestHTTPEventSink::OnProgress +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestHTTPEventSink::OnRedirect(nsISupports* context, nsIURI* i_NewLocation)
|
|
|
|
{
|
|
|
|
printf("\n+++ TestHTTPEventSink::OnRedirect +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class InputTestConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InputTestConsumer();
|
|
|
|
virtual ~InputTestConsumer();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
// IStreamListener interface...
|
|
|
|
NS_IMETHOD OnStartBinding(nsISupports* context);
|
|
|
|
|
|
|
|
NS_IMETHOD OnDataAvailable(nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-05-21 10:29:13 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength);
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg);
|
1999-06-08 01:33:30 +04:00
|
|
|
|
|
|
|
NS_IMETHOD OnStartRequest(nsISupports* context) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg) {
|
1999-06-08 01:33:30 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
InputTestConsumer::InputTestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer::~InputTestConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-01 00:20:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS(InputTestConsumer,nsCOMTypeInfo<nsIStreamListener>::GetIID());
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnStartBinding(nsISupports* context)
|
|
|
|
{
|
1999-06-27 15:02:40 +04:00
|
|
|
URLLoadInfo* info = (URLLoadInfo*)context;
|
|
|
|
if (info) {
|
|
|
|
info->mConnectTime = PR_Now() - info->mConnectTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gVerbose) {
|
|
|
|
printf("\nStarted loading: %s\n", info ? info->Name() : "UNKNOWN URL");
|
|
|
|
}
|
|
|
|
/*
|
1999-06-25 11:52:12 +04:00
|
|
|
nsCOMPtr<nsIURI> pURI(do_QueryInterface(context));
|
|
|
|
char* location = nsnull;
|
|
|
|
|
|
|
|
if (pURI) {
|
|
|
|
pURI->GetSpec(&location);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nStarted loading: %s\n", location ? location : "UNKNOWN URL");
|
|
|
|
if (location) {
|
|
|
|
nsCRT::free(location);
|
|
|
|
}
|
1999-06-27 15:02:40 +04:00
|
|
|
*/
|
1999-05-21 10:29:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnDataAvailable(nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-05-21 10:29:13 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
1999-06-25 10:28:58 +04:00
|
|
|
nsresult rv;
|
1999-06-27 15:02:40 +04:00
|
|
|
URLLoadInfo* info = (URLLoadInfo*)context;
|
1999-06-25 10:28:58 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
rv = aIStream->Read(buf, 1024, &amt);
|
1999-06-25 01:41:19 +04:00
|
|
|
if (rv == NS_BASE_STREAM_EOF) break;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-25 11:52:12 +04:00
|
|
|
if (gVerbose) {
|
|
|
|
buf[amt] = '\0';
|
|
|
|
puts(buf);
|
|
|
|
}
|
1999-06-27 15:02:40 +04:00
|
|
|
if (info) {
|
|
|
|
info->mBytesRead += amt;
|
|
|
|
}
|
1999-06-25 10:28:58 +04:00
|
|
|
} while (amt);
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnStopBinding(nsISupports* context,
|
1999-06-08 01:33:30 +04:00
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg)
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
1999-06-27 15:02:40 +04:00
|
|
|
URLLoadInfo* info = (URLLoadInfo*)context;
|
|
|
|
|
|
|
|
if (info) {
|
1999-06-29 10:54:05 +04:00
|
|
|
double connectTime;
|
|
|
|
double readTime;
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
info->mTotalTime = PR_Now() - info->mTotalTime;
|
1999-06-29 10:54:05 +04:00
|
|
|
|
|
|
|
connectTime = (info->mConnectTime/1000.0)/1000.0;
|
|
|
|
readTime = ((info->mTotalTime-info->mConnectTime)/1000.0)/1000.0;
|
1999-06-27 15:02:40 +04:00
|
|
|
|
|
|
|
printf("\nFinished loading: %s Status Code: %x\n", info->Name(), aStatus);
|
|
|
|
printf("\tRead: %d bytes.\n", info->mBytesRead);
|
1999-06-29 10:54:05 +04:00
|
|
|
printf("\tTime to connect: %f seconds\n", connectTime);
|
|
|
|
printf("\tTime to read: %f seconds.\n", readTime);
|
|
|
|
if (readTime > 0.0) {
|
|
|
|
printf("\tThroughput: %f bps.\n", (info->mBytesRead*8)/readTime);
|
|
|
|
} else {
|
|
|
|
printf("\tThroughput: REAL FAST!!\n");
|
|
|
|
}
|
1999-06-27 15:02:40 +04:00
|
|
|
} else {
|
|
|
|
printf("\nFinished loading: UNKNOWN URL. Status Code: %x\n", aStatus);
|
|
|
|
}
|
|
|
|
/*
|
1999-06-25 11:52:12 +04:00
|
|
|
nsCOMPtr<nsIURI> pURI(do_QueryInterface(context));
|
|
|
|
char* location = nsnull;
|
|
|
|
|
|
|
|
if (pURI) {
|
|
|
|
pURI->GetSpec(&location);
|
|
|
|
}
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\nFinished loading: %s Status Code: %x\n", location ? location : "UNKNOWN URL", aStatus);
|
1999-06-25 11:52:12 +04:00
|
|
|
|
|
|
|
if (location) {
|
|
|
|
nsCRT::free(location);
|
|
|
|
}
|
1999-06-27 15:02:40 +04:00
|
|
|
*/
|
1999-06-23 23:27:44 +04:00
|
|
|
gKeepRunning -= 1;
|
1999-05-21 10:29:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsEventSinkGetter : public nsIEventSinkGetter {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
1999-06-16 13:40:40 +04:00
|
|
|
nsEventSinkGetter() {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
NS_IMETHOD GetEventSink(const char* verb, const nsIID& eventSinkIID,
|
|
|
|
nsISupports* *result) {
|
1999-06-11 13:18:25 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
if (nsCRT::strcmp(verb, "load") == 0) { // makeshift verb for now
|
1999-07-01 00:20:17 +04:00
|
|
|
if (eventSinkIID.Equals(nsCOMTypeInfo<nsIHTTPEventSink>::GetIID())) {
|
1999-06-16 13:40:40 +04:00
|
|
|
TestHTTPEventSink *sink;
|
1999-06-11 13:18:25 +04:00
|
|
|
|
1999-06-16 13:40:40 +04:00
|
|
|
sink = new TestHTTPEventSink();
|
1999-06-17 07:38:49 +04:00
|
|
|
if (sink == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(sink);
|
|
|
|
rv = sink->QueryInterface(eventSinkIID, (void**)result);
|
|
|
|
NS_RELEASE(sink);
|
1999-06-08 01:33:30 +04:00
|
|
|
}
|
|
|
|
}
|
1999-06-11 13:18:25 +04:00
|
|
|
return rv;
|
1999-06-08 01:33:30 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
1999-07-01 00:20:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsEventSinkGetter, nsCOMTypeInfo<nsIEventSinkGetter>::GetIID());
|
1999-06-08 01:33:30 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-06-17 11:46:00 +04:00
|
|
|
|
|
|
|
nsresult StartLoadingURL(const char* aUrlString)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIIOService, pService, kIOServiceCID, &rv);
|
|
|
|
if (pService) {
|
|
|
|
nsCOMPtr<nsIURI> pURL;
|
|
|
|
|
|
|
|
rv = pService->NewURI(aUrlString, nsnull, getter_AddRefs(pURL));
|
|
|
|
if (NS_FAILED(rv)) {
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("ERROR: NewURI failed for %s\n", aUrlString);
|
1999-06-17 11:46:00 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIChannel> pChannel;
|
|
|
|
nsEventSinkGetter* pMySink;
|
|
|
|
|
|
|
|
pMySink = new nsEventSinkGetter();
|
|
|
|
NS_IF_ADDREF(pMySink);
|
|
|
|
if (!pMySink) {
|
|
|
|
NS_ERROR("Failed to create a new consumer!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async reading thru the calls of the event sink interface
|
|
|
|
rv = pService->NewChannelFromURI("load", pURL, pMySink,
|
|
|
|
getter_AddRefs(pChannel));
|
|
|
|
if (NS_FAILED(rv)) {
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("ERROR: NewChannelFromURI failed for %s\n", aUrlString);
|
1999-06-17 11:46:00 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
NS_RELEASE(pMySink);
|
|
|
|
|
|
|
|
/*
|
|
|
|
You may optionally add/set other headers on this
|
|
|
|
request object. This is done by QI for the specific
|
|
|
|
protocolConnection.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIHTTPChannel> pHTTPCon(do_QueryInterface(pChannel));
|
|
|
|
|
|
|
|
if (pHTTPCon) {
|
|
|
|
// Setting a sample user agent string.
|
|
|
|
rv = pHTTPCon->SetRequestHeader("User-Agent", "Mozilla/5.0 [en] (Win98; U)");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer* listener;
|
|
|
|
|
|
|
|
listener = new InputTestConsumer;
|
|
|
|
NS_IF_ADDREF(listener);
|
|
|
|
if (!listener) {
|
|
|
|
NS_ERROR("Failed to create a new stream listener!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;;
|
|
|
|
}
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
URLLoadInfo* info;
|
|
|
|
info = new URLLoadInfo(aUrlString);
|
|
|
|
NS_IF_ADDREF(info);
|
|
|
|
if (!info) {
|
|
|
|
NS_ERROR("Failed to create a load info!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;;
|
|
|
|
}
|
|
|
|
|
1999-06-17 11:46:00 +04:00
|
|
|
|
|
|
|
rv = pChannel->AsyncRead(0, // staring position
|
|
|
|
-1, // number of bytes to read
|
1999-06-27 15:02:40 +04:00
|
|
|
info, // ISupports context
|
1999-06-17 11:46:00 +04:00
|
|
|
listener); // IStreamListener consumer
|
1999-06-27 15:02:40 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
gKeepRunning += 1;
|
|
|
|
}
|
1999-06-17 11:46:00 +04:00
|
|
|
NS_RELEASE(listener);
|
1999-06-27 15:02:40 +04:00
|
|
|
NS_RELEASE(info);
|
1999-06-17 11:46:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
|
|
|
|
nsresult LoadURLsFromFile(char *aFileName)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRInt32 len, offset;
|
|
|
|
PRFileDesc* fd;
|
|
|
|
char buffer[1024];
|
|
|
|
nsString fileBuffer(eOneByte);
|
|
|
|
nsAutoString urlString(eOneByte);
|
|
|
|
|
|
|
|
fd = PR_Open(aFileName, PR_RDONLY, 777);
|
|
|
|
if (!fd) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep reading the file until EOF (or an error) is reached...
|
|
|
|
do {
|
|
|
|
len = PR_Read(fd, buffer, sizeof(buffer));
|
|
|
|
if (len>0) {
|
|
|
|
fileBuffer.Append(buffer, len);
|
|
|
|
// Treat each line as a URL...
|
|
|
|
while ((offset = fileBuffer.Find('\n')) != -1) {
|
|
|
|
fileBuffer.Left(urlString, offset);
|
|
|
|
fileBuffer.Cut(0, offset+1);
|
|
|
|
|
|
|
|
urlString.StripChars("\r");
|
|
|
|
if (urlString.Length()) {
|
|
|
|
printf("\t%s\n", urlString.GetBuffer());
|
|
|
|
rv = StartLoadingURL(urlString.GetBuffer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (len>0);
|
|
|
|
|
|
|
|
// If anything is left in the fileBuffer, treat it as a URL...
|
|
|
|
fileBuffer.StripChars("\r");
|
|
|
|
if (fileBuffer.Length()) {
|
|
|
|
printf("\t%s\n", fileBuffer.GetBuffer());
|
|
|
|
StartLoadingURL(fileBuffer.GetBuffer());
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-22 22:24:02 +04:00
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-06-17 11:46:00 +04:00
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv= -1;
|
|
|
|
if (argc < 2) {
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("usage: %s [-verbose] [-file <name>] <url> <url> ... \n", argv[0]);
|
1999-05-21 10:29:13 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-06-02 02:02:40 +04:00
|
|
|
The following code only deals with XPCOM registration stuff. and setting
|
|
|
|
up the event queues. Copied from TestSocketIO.cpp
|
1999-05-21 10:29:13 +04:00
|
|
|
*/
|
1999-06-02 02:02:40 +04:00
|
|
|
|
1999-06-22 22:24:02 +04:00
|
|
|
rv = NS_AutoregisterComponents();
|
1999-05-21 10:29:13 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-08 00:06:41 +04:00
|
|
|
eventQService->GetThreadEventQueue(PR_CurrentThread(), &gEventQ);
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-07-01 00:00:19 +04:00
|
|
|
#if 1
|
1999-06-25 01:41:19 +04:00
|
|
|
// fire up an instance of the cookie manager.
|
|
|
|
// I'm doing this using the serviceManager for convenience's sake.
|
|
|
|
// Presumably an application will init it's own cookie service a
|
|
|
|
// different way (this way works too though).
|
|
|
|
NS_WITH_SERVICE(nsICookieService, cookieService, nsCookieServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#endif // 0
|
|
|
|
|
1999-06-17 11:46:00 +04:00
|
|
|
int i;
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\nTrying to load:\n");
|
1999-06-17 11:46:00 +04:00
|
|
|
for (i=1; i<argc; i++) {
|
1999-06-27 15:02:40 +04:00
|
|
|
// Turn on verbose printing...
|
1999-06-25 11:52:12 +04:00
|
|
|
if (PL_strcasecmp(argv[i], "-verbose") == 0) {
|
|
|
|
gVerbose = PR_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
// Turn on netlib tracing...
|
|
|
|
if (PL_strcasecmp(argv[i], "-file") == 0) {
|
|
|
|
LoadURLsFromFile(argv[++i]);
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-25 11:52:12 +04:00
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\t%s\n", argv[i]);
|
1999-06-17 11:46:00 +04:00
|
|
|
rv = StartLoadingURL(argv[i]);
|
1999-05-21 10:29:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
1999-06-17 11:46:00 +04:00
|
|
|
while ( gKeepRunning ) {
|
1999-06-02 02:02:40 +04:00
|
|
|
#ifdef WIN32
|
1999-06-17 11:46:00 +04:00
|
|
|
MSG msg;
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-06-17 11:46:00 +04:00
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
} else {
|
1999-06-23 23:27:44 +04:00
|
|
|
gKeepRunning = 0;
|
1999-05-21 10:29:13 +04:00
|
|
|
}
|
1999-06-08 00:06:41 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
|
|
|
PLEvent *gEvent;
|
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
1999-06-08 16:26:12 +04:00
|
|
|
rv = gEventQ->HandleEvent(gEvent);
|
1999-06-08 00:06:41 +04:00
|
|
|
#endif /* XP_UNIX */
|
|
|
|
#endif /* !WIN32 */
|
1999-06-17 11:46:00 +04:00
|
|
|
}
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|