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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
The TestProtocols tests the basic protocols architecture and can
|
1999-06-02 02:02:40 +04:00
|
|
|
be used to test individual protocols as well. If this grows too
|
1999-05-21 10:29:13 +04:00
|
|
|
big then we should split it to individual protocols.
|
|
|
|
|
|
|
|
-Gagan Saksena 04/29/99
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "nsIByteBufferInputStream.h"
|
|
|
|
#include "nsCRT.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIChannel.h"
|
1999-05-21 10:29:13 +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
|
|
|
|
|
|
|
#ifdef XP_PC
|
|
|
|
#define XPCOM_DLL "xpcom32.dll"
|
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "nsMacRepository.h"
|
|
|
|
#else
|
|
|
|
#define XPCOM_DLL "libxpcom.so"
|
|
|
|
#endif
|
|
|
|
#endif
|
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;
|
|
|
|
static int gKeepRunning = 1;
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
|
1999-06-03 03:02:53 +04:00
|
|
|
class InputTestConsumer : public nsIHTTPEventSink
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InputTestConsumer();
|
|
|
|
virtual ~InputTestConsumer();
|
|
|
|
|
|
|
|
// 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-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,
|
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength);
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
|
|
|
nsIString* 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,
|
|
|
|
nsIString* aMsg) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
InputTestConsumer::InputTestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer::~InputTestConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-03 03:02:53 +04:00
|
|
|
//NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
|
|
|
|
NS_IMPL_ISUPPORTS(InputTestConsumer,nsIHTTPEventSink::GetIID());
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnStartBinding(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ InputTestConsumer::OnStartBinding +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnDataAvailable(nsISupports* context,
|
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
|
|
|
do {
|
|
|
|
nsresult rv = aIStream->Read(buf, 1024, &amt);
|
|
|
|
buf[amt] = '\0';
|
|
|
|
printf(buf);
|
|
|
|
} while (amt != 0);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnStopBinding(nsISupports* context,
|
1999-06-08 01:33:30 +04:00
|
|
|
nsresult aStatus,
|
|
|
|
nsIString* aMsg)
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
gKeepRunning = 0;
|
|
|
|
printf("\n+++ InputTestConsumer::OnStopBinding (status = %x) +++\n", aStatus);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-03 03:02:53 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnAwaitingInput(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ InputTestConsumer::OnAwaitingInput +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnHeadersAvailable(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ InputTestConsumer::OnHeadersAvailable +++\n");
|
1999-06-08 01:33:30 +04:00
|
|
|
nsCOMPtr<nsIHTTPChannel> pHTTPCon(do_QueryInterface(context));
|
1999-06-03 03:02:53 +04:00
|
|
|
if (pHTTPCon)
|
|
|
|
{
|
1999-06-08 01:33:30 +04:00
|
|
|
char* type;
|
|
|
|
//optimize later TODO allow atoms here...! intead of just the header strings
|
|
|
|
pHTTPCon->GetResponseHeader("Content-type", &type);
|
|
|
|
if (type) {
|
|
|
|
printf("\nRecieving ... %s\n", type);
|
|
|
|
nsCRT::free(type);
|
|
|
|
}
|
1999-06-03 03:02:53 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputTestConsumer::OnProgress(nsISupports* context, PRUint32 i_Progress, PRUint32 i_ProgressMax)
|
|
|
|
{
|
|
|
|
printf("\n+++ InputTestConsumer::OnProgress +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-06-08 01:33:30 +04:00
|
|
|
InputTestConsumer::OnRedirect(nsISupports* context, nsIURI* i_NewLocation)
|
1999-06-03 03:02:53 +04:00
|
|
|
{
|
|
|
|
printf("\n+++ InputTestConsumer::OnRedirect +++\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsEventSinkGetter : public nsIEventSinkGetter {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD GetEventSink(const char* verb, const nsIID& eventSinkIID,
|
|
|
|
nsISupports* *result) {
|
|
|
|
if (nsCRT::strcmp(verb, "load") == 0) { // makeshift verb for now
|
|
|
|
if (eventSinkIID.Equals(nsIHTTPEventSink::GetIID())) {
|
|
|
|
*result = new InputTestConsumer();
|
|
|
|
if (*result)
|
|
|
|
return NS_OK;
|
|
|
|
else
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsEventSinkGetter, nsIEventSinkGetter::GetIID());
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv= -1;
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <url> \n", argv[0]);
|
|
|
|
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
|
|
|
*/
|
|
|
|
// XXX why do I have to do this?!
|
1999-06-02 02:02:40 +04:00
|
|
|
|
1999-05-21 10:29:13 +04:00
|
|
|
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
|
1999-06-02 02:02:40 +04:00
|
|
|
"components");
|
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
|
|
|
|
|
|
|
//Create the nsINetService...
|
1999-06-08 01:33:30 +04:00
|
|
|
NS_WITH_SERVICE(nsIIOService, pService, kIOServiceCID, &rv);
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// The story of how to retrieve a document.
|
1999-06-02 02:02:40 +04:00
|
|
|
// Now available in 2 different flavours.
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
if (pService)
|
|
|
|
{
|
1999-06-02 02:02:40 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
nsCOMPtr<nsIURI> pURL;
|
1999-05-21 10:29:13 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
if (NS_OK == pService->NewURI(argv[1], nsnull, getter_AddRefs(pURL)))
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
if (pURL)
|
|
|
|
{
|
1999-06-08 01:33:30 +04:00
|
|
|
nsCOMPtr<nsIChannel> pConnection;
|
1999-05-21 10:29:13 +04:00
|
|
|
/* Flavour Two */
|
1999-06-08 01:33:30 +04:00
|
|
|
nsEventSinkGetter* pMyConsumer = new nsEventSinkGetter();
|
1999-06-03 03:02:53 +04:00
|
|
|
if (!pMyConsumer)
|
|
|
|
{
|
|
|
|
NS_ERROR("Failed to create a new consumer!");
|
|
|
|
return -1;
|
|
|
|
}
|
1999-05-21 10:29:13 +04:00
|
|
|
// Async reading thru the calls of the event sink interface
|
1999-06-08 01:33:30 +04:00
|
|
|
if (NS_OK == pService->NewChannelFromURI("load", pURL, pMyConsumer,
|
|
|
|
getter_AddRefs(pConnection)))
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
if (pConnection)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
You may optionally add/set other headers on this
|
|
|
|
request object. This is done by QI for the specific
|
|
|
|
protocolConnection.
|
|
|
|
*/
|
1999-06-08 01:33:30 +04:00
|
|
|
nsCOMPtr<nsIHTTPChannel> pHTTPCon(do_QueryInterface(pConnection));
|
1999-05-21 10:29:13 +04:00
|
|
|
|
|
|
|
if (pHTTPCon)
|
|
|
|
{
|
|
|
|
// Setting a sample user agent string.
|
1999-06-03 03:02:53 +04:00
|
|
|
if (NS_OK == pHTTPCon->SetRequestHeader("User-Agent", "Mozilla/5.0 [en] (Win98; U)"))
|
1999-05-21 10:29:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// But calling the open is required!
|
1999-06-08 01:33:30 +04:00
|
|
|
// pConnection->Open();
|
1999-05-21 10:29:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
1999-06-02 02:02:40 +04:00
|
|
|
#ifdef WIN32
|
1999-05-21 10:29:13 +04:00
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
} else {
|
|
|
|
gKeepRunning = FALSE;
|
|
|
|
}
|
1999-06-08 00:06:41 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
|
|
|
PLEvent *gEvent;
|
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
|
|
|
PL_HandleEvent(gEvent);
|
|
|
|
#endif /* XP_UNIX */
|
|
|
|
#endif /* !WIN32 */
|
1999-05-21 10:29:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|