1999-06-08 01:33:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1999-04-19 11:17:37 +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-04-19 11:17:37 +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-04-19 11:17:37 +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-04-19 11:17:37 +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-04-19 11:17:37 +04:00
|
|
|
*/
|
1999-04-20 12:01:31 +04:00
|
|
|
#include <stdio.h>
|
1999-04-29 22:52:14 +04:00
|
|
|
|
1999-05-04 22:50:59 +04:00
|
|
|
#ifdef WIN32
|
1999-04-19 11:17:37 +04:00
|
|
|
#include <windows.h>
|
1999-04-29 22:52:14 +04:00
|
|
|
#endif
|
1999-04-19 11:17:37 +04:00
|
|
|
|
1999-08-27 14:33:37 +04:00
|
|
|
#define NSPIPE2
|
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIChannel.h"
|
1999-04-19 11:17:37 +04:00
|
|
|
#include "nsIStreamObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBuffer.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIBufferOutputStream.h"
|
|
|
|
#endif
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBufferInputStream.h"
|
1999-04-22 12:47:06 +04:00
|
|
|
#include "nsCRT.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
1999-06-02 15:27:07 +04:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-05-06 02:41:44 +04:00
|
|
|
static NS_DEFINE_IID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
1999-04-20 12:01:31 +04:00
|
|
|
static PRTime gElapsedTime;
|
1999-04-19 11:17:37 +04:00
|
|
|
static int gKeepRunning = 1;
|
1999-05-06 02:41:44 +04:00
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
class InputTestConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InputTestConsumer();
|
|
|
|
virtual ~InputTestConsumer();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-09-16 05:16:22 +04:00
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-04-19 11:17:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
InputTestConsumer::InputTestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer::~InputTestConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
|
|
|
|
NS_IMPL_ISUPPORTS(InputTestConsumer,kIStreamListenerIID);
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ InputTestConsumer::OnStartRequest +++\n");
|
1999-04-19 11:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnDataAvailable(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-04-22 12:47:06 +04:00
|
|
|
PRUint32 aSourceOffset,
|
1999-04-19 11:17:37 +04:00
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
|
|
|
do {
|
1999-07-13 06:37:08 +04:00
|
|
|
aIStream->Read(buf, 1024, &amt);
|
1999-09-10 02:05:05 +04:00
|
|
|
if (amt == 0) break;
|
1999-04-19 11:17:37 +04:00
|
|
|
buf[amt] = '\0';
|
1999-06-15 03:42:12 +04:00
|
|
|
puts(buf);
|
1999-04-19 11:17:37 +04:00
|
|
|
} while (amt != 0);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMsg)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
|
|
|
gKeepRunning = 0;
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ InputTestConsumer::OnStopRequest (status = %x) +++\n", aStatus);
|
1999-04-19 11:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestWriteObserver : public nsIStreamObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
1999-09-16 05:16:22 +04:00
|
|
|
TestWriteObserver(nsIChannel* aChannel);
|
1999-04-19 11:17:37 +04:00
|
|
|
virtual ~TestWriteObserver();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-09-16 05:16:22 +04:00
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
1999-06-08 01:33:30 +04:00
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
protected:
|
1999-06-08 01:33:30 +04:00
|
|
|
nsIChannel* mTransport;
|
1999-04-19 11:17:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-09-16 05:16:22 +04:00
|
|
|
TestWriteObserver::TestWriteObserver(nsIChannel* aChannel)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
1999-09-16 05:16:22 +04:00
|
|
|
mTransport = aChannel;
|
1999-04-19 11:17:37 +04:00
|
|
|
NS_ADDREF(mTransport);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWriteObserver::~TestWriteObserver()
|
|
|
|
{
|
|
|
|
NS_RELEASE(mTransport);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 03:32:12 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TestWriteObserver,NS_GET_IID(nsIStreamObserver));
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
TestWriteObserver::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ TestWriteObserver::OnStartRequest +++\n");
|
1999-04-19 11:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
TestWriteObserver::OnStopRequest(nsIChannel* channel, nsISupports* context,
|
1999-04-19 11:17:37 +04:00
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("\n+++ TestWriteObserver::OnStopRequest (status = %x) +++\n", aStatus);
|
1999-04-20 12:01:31 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
1999-07-20 12:46:33 +04:00
|
|
|
mTransport->AsyncRead(0, -1, nsnull, new InputTestConsumer);
|
1999-04-20 12:01:31 +04:00
|
|
|
} else {
|
|
|
|
gKeepRunning = 0;
|
|
|
|
}
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
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-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("usage: %s <host> <path>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* hostName = argv[1];
|
|
|
|
char* fileName = argv[2];
|
|
|
|
int port = 80;
|
1999-06-22 22:24:02 +04:00
|
|
|
|
|
|
|
rv = NS_AutoregisterComponents();
|
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// 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-11-30 03:32:43 +03:00
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
// Create the Socket transport service...
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create a stream for the data being written to the server...
|
1999-06-09 00:57:32 +04:00
|
|
|
nsIBufferInputStream* stream;
|
1999-04-19 11:17:37 +04:00
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
|
|
|
nsCOMPtr<nsIBuffer> buf;
|
1999-06-15 09:18:40 +04:00
|
|
|
rv = NS_NewBuffer(&buf, 1024, 4096, nsnull);
|
1999-06-09 00:57:32 +04:00
|
|
|
rv = NS_NewBufferInputStream(&stream, buf);
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
nsCOMPtr<nsIBufferOutputStream> out;
|
|
|
|
rv = NS_NewPipe(&stream, getter_AddRefs(out), nsnull,
|
|
|
|
1024, 4096);
|
|
|
|
#endif
|
1999-04-19 11:17:37 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-22 12:47:06 +04:00
|
|
|
char *buffer = PR_smprintf("GET %s HTML/1.0%s%s", fileName, CRLF, CRLF);
|
1999-07-11 06:43:37 +04:00
|
|
|
#if 0
|
|
|
|
stream->Fill(buffer, strlen(buffer), &bytesWritten);
|
|
|
|
#else
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
buf->Write(buffer, strlen(buffer), &bytesWritten);
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
out->Write(buffer, strlen(buffer), &bytesWritten);
|
|
|
|
#endif
|
1999-07-11 06:43:37 +04:00
|
|
|
#endif
|
1999-04-20 12:01:31 +04:00
|
|
|
printf("\n+++ Request is: %s\n", buffer);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
// Create the socket transport...
|
1999-06-08 01:33:30 +04:00
|
|
|
nsIChannel* transport;
|
1999-12-04 13:01:32 +03:00
|
|
|
rv = sts->CreateTransport(hostName, port, hostName, 0, 0, &transport);
|
1999-05-09 23:18:25 +04:00
|
|
|
|
|
|
|
// This stuff is used to test the output stream
|
|
|
|
#if 0
|
|
|
|
nsIOutputStream* outStr = nsnull;
|
|
|
|
rv = transport->OpenOutputStream(&outStr);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRUint32 bytes;
|
|
|
|
rv = outStr->Write("test", 4, &bytes);
|
|
|
|
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = outStr->Close();
|
|
|
|
#else
|
1999-04-19 11:17:37 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
TestWriteObserver* observer = new TestWriteObserver(transport);
|
|
|
|
|
1999-04-20 12:01:31 +04:00
|
|
|
gElapsedTime = PR_Now();
|
1999-07-20 12:46:33 +04:00
|
|
|
transport->AsyncWrite(stream, 0, bytesWritten, nsnull, observer);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
NS_RELEASE(transport);
|
|
|
|
}
|
1999-05-09 23:18:25 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
1999-06-03 22:12:13 +04:00
|
|
|
|
1999-05-09 23:18:25 +04:00
|
|
|
#ifdef WIN32
|
1999-04-19 11:17:37 +04:00
|
|
|
MSG msg;
|
|
|
|
|
1999-04-20 12:01:31 +04:00
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
1999-04-19 11:17:37 +04:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
1999-04-20 12:01:31 +04:00
|
|
|
} else {
|
|
|
|
gKeepRunning = FALSE;
|
1999-04-19 11:17:37 +04:00
|
|
|
}
|
1999-06-03 22:12:13 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
1999-06-04 02:12:26 +04:00
|
|
|
PLEvent *gEvent;
|
1999-06-03 22:12:13 +04:00
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
1999-06-08 16:24:10 +04:00
|
|
|
rv = gEventQ->HandleEvent(gEvent);
|
1999-06-03 22:12:13 +04:00
|
|
|
#endif
|
1999-04-19 11:17:37 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-11-02 00:57:14 +03:00
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
1999-12-29 03:20:10 +03:00
|
|
|
printf("Elapsed time: %d\n", (PRInt32)(endTime/1000UL-gElapsedTime/1000UL));
|
1999-04-20 12:01:31 +04:00
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
sts->Shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|