2000-07-25 09:45:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
1999-05-21 10:29:13 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03: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-05-21 10:29:13 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03: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-05-21 10:29:13 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-05-21 10:29:13 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-05-21 10:29:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
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 "nsIStreamListener.h"
|
|
|
|
#include "nsIInputStream.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.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"
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIHttpEventSink.h"
|
1999-11-18 10:36:41 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
1999-07-30 08:17:23 +04:00
|
|
|
#include "nsIDNSService.h"
|
1999-06-03 03:02:53 +04:00
|
|
|
|
1999-08-05 12:29:36 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2000-03-29 07:58:50 +04:00
|
|
|
#include "nsNetUtil.h"
|
1999-08-05 12:29:36 +04:00
|
|
|
|
1999-06-25 01:41:19 +04:00
|
|
|
// this test app handles cookies.
|
|
|
|
#include "nsICookieService.h"
|
|
|
|
static NS_DEFINE_CID(nsCookieServiceCID, NS_COOKIESERVICE_CID);
|
|
|
|
|
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
|
|
|
|
1999-07-13 06:37:08 +04:00
|
|
|
//static PRTime gElapsedTime; // enable when we time it...
|
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;
|
2001-02-21 23:38:08 +03:00
|
|
|
static PRBool gAskUserForInput = PR_FALSE;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// HeaderVisitor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class HeaderVisitor : public nsIHttpHeaderVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIHTTPHEADERVISITOR
|
|
|
|
|
|
|
|
HeaderVisitor() { NS_INIT_ISUPPORTS(); }
|
|
|
|
virtual ~HeaderVisitor() {}
|
|
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS1(HeaderVisitor, nsIHttpHeaderVisitor)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HeaderVisitor::VisitHeader(const char *header, const char *value)
|
|
|
|
{
|
|
|
|
printf("%s: %s\n", header, value);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// URLLoadInfo
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
class URLLoadInfo : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
URLLoadInfo(const char* aUrl);
|
|
|
|
virtual ~URLLoadInfo();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char* Name() { return mURLString.get(); }
|
1999-06-27 15:02:40 +04:00
|
|
|
PRInt32 mBytesRead;
|
|
|
|
PRTime mTotalTime;
|
|
|
|
PRTime mConnectTime;
|
1999-08-29 21:42:34 +04:00
|
|
|
nsCString mURLString;
|
1999-06-27 15:02:40 +04:00
|
|
|
};
|
|
|
|
|
1999-08-29 21:42:34 +04:00
|
|
|
URLLoadInfo::URLLoadInfo(const char *aUrl) : mURLString(aUrl)
|
1999-06-27 15:02:40 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
|
|
|
mBytesRead = 0;
|
|
|
|
mConnectTime = mTotalTime = PR_Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
URLLoadInfo::~URLLoadInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-21 05:48:11 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(URLLoadInfo)
|
1999-06-27 15:02:40 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// TestHttpEventSink
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-27 15:02:40 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
class TestHttpEventSink : public nsIHttpEventSink
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-05-12 01:04:09 +04:00
|
|
|
NS_DECL_NSIHTTPEVENTSINK
|
1999-05-21 10:29:13 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
TestHttpEventSink();
|
|
|
|
virtual ~TestHttpEventSink();
|
1999-06-16 13:40:40 +04:00
|
|
|
};
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
TestHttpEventSink::TestHttpEventSink()
|
1999-06-16 13:40:40 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
TestHttpEventSink::~TestHttpEventSink()
|
1999-06-16 13:40:40 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(TestHttpEventSink, nsIHttpEventSink);
|
1999-06-16 13:40:40 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
#if 0
|
1999-06-16 13:40:40 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
TestHTTPEventSink::OnHeadersAvailable(nsISupports* context)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-05-12 01:04:09 +04:00
|
|
|
#endif
|
1999-06-16 13:40:40 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-05-12 01:04:09 +04:00
|
|
|
TestHttpEventSink::OnRedirect(nsIHttpChannel *channel, nsIChannel *newChannel)
|
1999-06-16 13:40:40 +04:00
|
|
|
{
|
|
|
|
printf("\n+++ TestHTTPEventSink::OnRedirect +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// InputTestConsumer
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-16 13:40:40 +04:00
|
|
|
|
|
|
|
class InputTestConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InputTestConsumer();
|
|
|
|
virtual ~InputTestConsumer();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
1999-09-16 05:16:22 +04:00
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-05-21 10:29:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
InputTestConsumer::InputTestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer::~InputTestConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-08-21 05:48:11 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(InputTestConsumer, nsIStreamListener)
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
1999-06-27 15:02:40 +04:00
|
|
|
URLLoadInfo* info = (URLLoadInfo*)context;
|
2001-05-12 01:04:09 +04:00
|
|
|
if (info)
|
1999-06-27 15:02:40 +04:00
|
|
|
info->mConnectTime = PR_Now() - info->mConnectTime;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
if (gVerbose)
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\nStarted loading: %s\n", info ? info->Name() : "UNKNOWN URL");
|
1999-06-25 11:52:12 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsXPIDLCString value;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
if (channel) {
|
|
|
|
|
|
|
|
printf("Channel Info:\n");
|
|
|
|
channel->GetContentType(getter_Copies(value));
|
|
|
|
printf("\tContent-Type: %s\n", (const char*)value);
|
|
|
|
|
|
|
|
PRInt32 length = -1;
|
|
|
|
channel->GetContentLength(&length);
|
|
|
|
printf("\tContent-Length: %d\n", length);
|
1999-06-25 11:52:12 +04:00
|
|
|
}
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(request));
|
|
|
|
if (httpChannel) {
|
|
|
|
HeaderVisitor *visitor = new HeaderVisitor();
|
|
|
|
if (!visitor)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(visitor);
|
|
|
|
|
|
|
|
httpChannel->GetCharset(getter_Copies(value));
|
|
|
|
printf("\tCharset: %s\n", (const char*)value);
|
|
|
|
|
|
|
|
printf("\nHTTP request headers:\n");
|
|
|
|
httpChannel->VisitRequestHeaders(visitor);
|
|
|
|
|
|
|
|
printf("\nHTTP response headers:\n");
|
|
|
|
httpChannel->VisitResponseHeaders(visitor);
|
|
|
|
|
|
|
|
NS_RELEASE(visitor);
|
1999-06-25 11:52:12 +04:00
|
|
|
}
|
1999-05-21 10:29:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
InputTestConsumer::OnDataAvailable(nsIRequest *request,
|
1999-07-07 12:08:40 +04:00
|
|
|
nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-05-21 10:29:13 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
1999-10-06 12:26:01 +04:00
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
char buf[1025];
|
1999-10-05 09:02:56 +04:00
|
|
|
PRUint32 amt, size;
|
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
|
|
|
|
1999-10-05 09:02:56 +04:00
|
|
|
while (aLength) {
|
1999-10-05 10:28:17 +04:00
|
|
|
size = PR_MIN(aLength, sizeof(buf));
|
1999-10-05 09:02:56 +04:00
|
|
|
|
|
|
|
rv = aIStream->Read(buf, size, &amt);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ASSERTION((NS_BASE_STREAM_WOULD_BLOCK != rv),
|
|
|
|
"The stream should never block.");
|
|
|
|
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-05-21 10:29:13 +04:00
|
|
|
|
1999-10-05 09:02:56 +04:00
|
|
|
aLength -= amt;
|
|
|
|
}
|
1999-05-21 10:29:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
PR_STATIC_CALLBACK(void) DecrementDestroyHandler(PLEvent *self)
|
|
|
|
{
|
|
|
|
PR_DELETE(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PR_STATIC_CALLBACK(void*) DecrementEventHandler(PLEvent *self)
|
|
|
|
{
|
|
|
|
gKeepRunning--;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FireDecrement()
|
|
|
|
{
|
|
|
|
PLEvent *event = PR_NEW(PLEvent);
|
|
|
|
PL_InitEvent(event,
|
|
|
|
nsnull,
|
|
|
|
DecrementEventHandler,
|
|
|
|
DecrementDestroyHandler);
|
|
|
|
|
|
|
|
gEventQ->PostEvent(event);
|
|
|
|
}
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
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-07-16 02:57:50 +04:00
|
|
|
PRUint32 httpStatus;
|
|
|
|
PRBool bHTTPURL = PR_FALSE;
|
1999-06-29 10:54:05 +04:00
|
|
|
|
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
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> pHTTPCon(do_QueryInterface(request));
|
1999-07-16 02:57:50 +04:00
|
|
|
if (pHTTPCon) {
|
|
|
|
pHTTPCon->GetResponseStatus(&httpStatus);
|
|
|
|
bHTTPURL = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\nFinished loading: %s Status Code: %x\n", info->Name(), aStatus);
|
1999-07-16 02:57:50 +04:00
|
|
|
if (bHTTPURL)
|
|
|
|
printf("\tHTTP Status: %u\n", httpStatus);
|
1999-07-30 08:17:23 +04:00
|
|
|
if (NS_ERROR_UNKNOWN_HOST == aStatus) {
|
1999-07-30 06:35:12 +04:00
|
|
|
printf("\tDNS lookup failed.\n");
|
|
|
|
}
|
1999-06-27 15:02:40 +04:00
|
|
|
printf("\tRead: %d bytes.\n", info->mBytesRead);
|
1999-07-10 15:26:59 +04:00
|
|
|
printf("\tTime to connect: %.3f seconds\n", connectTime);
|
|
|
|
printf("\tTime to read: %.3f seconds.\n", readTime);
|
1999-06-29 10:54:05 +04:00
|
|
|
if (readTime > 0.0) {
|
1999-07-10 15:26:59 +04:00
|
|
|
printf("\tThroughput: %.0f bps.\n", (info->mBytesRead*8)/readTime);
|
1999-06-29 10:54:05 +04:00
|
|
|
} 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
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
FireDecrement();
|
1999-05-21 10:29:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NotificationCallbacks
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-06-08 01:33:30 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
class NotificationCallbacks : public nsIInterfaceRequestor {
|
1999-06-08 01:33:30 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
NotificationCallbacks() {
|
1999-06-16 13:40:40 +04:00
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
1999-11-18 10:36:41 +03:00
|
|
|
NS_IMETHOD GetInterface(const nsIID& eventSinkIID, void* *result) {
|
1999-06-11 13:18:25 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
if (eventSinkIID.Equals(NS_GET_IID(nsIHttpEventSink))) {
|
|
|
|
TestHttpEventSink *sink;
|
1999-06-11 13:18:25 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
sink = new TestHttpEventSink();
|
1999-11-17 11:14:52 +03:00
|
|
|
if (sink == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(sink);
|
|
|
|
rv = sink->QueryInterface(eventSinkIID, 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(NotificationCallbacks, nsIInterfaceRequestor)
|
1999-06-08 01:33:30 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// helpers...
|
|
|
|
//-----------------------------------------------------------------------------
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-06-17 11:46:00 +04:00
|
|
|
nsresult StartLoadingURL(const char* aUrlString)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIIOService> pService(do_GetService(kIOServiceCID, &rv));
|
1999-06-17 11:46:00 +04:00
|
|
|
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;
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
NotificationCallbacks* callbacks = new NotificationCallbacks();
|
1999-11-17 11:14:52 +03:00
|
|
|
if (!callbacks) {
|
1999-06-17 11:46:00 +04:00
|
|
|
NS_ERROR("Failed to create a new consumer!");
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;;
|
|
|
|
}
|
1999-11-17 11:14:52 +03:00
|
|
|
NS_ADDREF(callbacks);
|
1999-06-17 11:46:00 +04:00
|
|
|
|
|
|
|
// Async reading thru the calls of the event sink interface
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = NS_OpenURI(getter_AddRefs(pChannel), pURL, pService,
|
|
|
|
nsnull, // loadGroup
|
|
|
|
callbacks); // notificationCallbacks
|
1999-11-17 11:14:52 +03:00
|
|
|
NS_RELEASE(callbacks);
|
1999-06-17 11:46:00 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2000-03-29 07:58:50 +04:00
|
|
|
printf("ERROR: NS_OpenURI failed for %s\n", aUrlString);
|
1999-06-17 11:46:00 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
You may optionally add/set other headers on this
|
|
|
|
request object. This is done by QI for the specific
|
|
|
|
protocolConnection.
|
|
|
|
*/
|
2001-05-12 01:04:09 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> pHTTPCon(do_QueryInterface(pChannel));
|
1999-06-17 11:46:00 +04:00
|
|
|
|
|
|
|
if (pHTTPCon) {
|
1999-10-02 03:32:12 +04:00
|
|
|
// Setting a sample header.
|
2001-05-12 01:04:09 +04:00
|
|
|
rv = pHTTPCon->SetRequestHeader("sample-header", "Sample-Value");
|
1999-06-17 11:46:00 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-10-02 03:32:12 +04:00
|
|
|
}
|
1999-06-17 11:46:00 +04:00
|
|
|
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!");
|
1999-10-06 12:26:01 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-06-27 15:02:40 +04:00
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = pChannel->AsyncOpen(listener, // IStreamListener consumer
|
|
|
|
info);
|
2000-03-29 07:58:50 +04:00
|
|
|
|
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];
|
1999-08-29 21:42:34 +04:00
|
|
|
nsCString fileBuffer;
|
|
|
|
nsCAutoString urlString;
|
1999-06-27 15:02:40 +04:00
|
|
|
|
|
|
|
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...
|
1999-08-29 21:42:34 +04:00
|
|
|
while ((offset = fileBuffer.FindChar('\n')) != -1) {
|
1999-06-27 15:02:40 +04:00
|
|
|
fileBuffer.Left(urlString, offset);
|
|
|
|
fileBuffer.Cut(0, offset+1);
|
|
|
|
|
|
|
|
urlString.StripChars("\r");
|
|
|
|
if (urlString.Length()) {
|
2001-03-02 12:26:57 +03:00
|
|
|
printf("\t%s\n", urlString.get());
|
|
|
|
rv = StartLoadingURL(urlString.get());
|
1999-06-27 15:02:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (len>0);
|
|
|
|
|
|
|
|
// If anything is left in the fileBuffer, treat it as a URL...
|
|
|
|
fileBuffer.StripChars("\r");
|
|
|
|
if (fileBuffer.Length()) {
|
2001-03-02 12:26:57 +03:00
|
|
|
printf("\t%s\n", fileBuffer.get());
|
|
|
|
StartLoadingURL(fileBuffer.get());
|
1999-06-27 15:02:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PR_Close(fd);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
|
|
|
|
nsresult LoadURLFromConsole()
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
printf("Enter URL (\"q\" to start): ");
|
|
|
|
scanf("%s", buffer);
|
|
|
|
if (buffer[0]=='q')
|
|
|
|
gAskUserForInput = PR_FALSE;
|
|
|
|
else
|
|
|
|
StartLoadingURL(buffer);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
1999-07-13 06:37:08 +04:00
|
|
|
nsresult rv= (nsresult)-1;
|
1999-05-21 10:29:13 +04:00
|
|
|
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
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
rv = NS_InitXPCOM(nsnull, nsnull);
|
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...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
1999-05-21 10:29:13 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 03:32:43 +03:00
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-07-13 06:37:08 +04:00
|
|
|
#if 0 // Jud sez
|
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).
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsICookieService> cookieService =
|
|
|
|
do_GetService(nsCookieServiceCID, &rv);
|
1999-06-25 01:41:19 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-07-03 08:14:44 +04:00
|
|
|
#endif // NECKO
|
1999-06-25 01:41:19 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
|
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;
|
|
|
|
}
|
2001-02-21 23:38:08 +03:00
|
|
|
|
|
|
|
if (PL_strcasecmp(argv[i], "-console") == 0) {
|
|
|
|
gAskUserForInput = PR_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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;
|
2001-01-24 01:10:34 +03:00
|
|
|
rv = gEventQ->WaitForEvent(&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
|
|
|
}
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_ShutdownXPCOM(nsnull);
|
1999-05-21 10:29:13 +04:00
|
|
|
return rv;
|
|
|
|
}
|