2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-23 03:53:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#ifdef OS2
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIStreamProvider.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIByteArrayInputStream.h"
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static PRLogModuleInfo *gTestSocketIOLog;
|
|
|
|
#define LOG(args) PR_LOG(gTestSocketIOLog, PR_LOG_DEBUG, args)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
static PRTime gElapsedTime;
|
|
|
|
static int gKeepRunning = 1;
|
2012-07-30 18:20:58 +04:00
|
|
|
static nsIEventQueue* gEventQ = nullptr;
|
2001-02-23 03:53:08 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Test Listener
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
class TestListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
2003-01-09 01:35:09 +03:00
|
|
|
TestListener() { }
|
2001-02-23 03:53:08 +03:00
|
|
|
virtual ~TestListener() {}
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2001-02-23 03:53:08 +03:00
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(TestListener,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsIRequestObserver,
|
2001-02-23 03:53:08 +03:00
|
|
|
nsIStreamListener);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestListener::OnStartRequest(nsIRequest* request, nsISupports* context)
|
|
|
|
{
|
|
|
|
LOG(("TestListener::OnStartRequest\n"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestListener::OnDataAvailable(nsIRequest* request,
|
|
|
|
nsISupports* context,
|
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
|
|
|
|
aSourceOffset, aLength));
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
|
|
|
while (1) {
|
|
|
|
aIStream->Read(buf, 1024, &amt);
|
|
|
|
if (amt == 0)
|
|
|
|
break;
|
|
|
|
buf[amt] = '\0';
|
|
|
|
//puts(buf);
|
|
|
|
printf("read %d bytes\n", amt);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestListener::OnStopRequest(nsIRequest* request, nsISupports* context,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
2001-02-23 03:53:08 +03:00
|
|
|
{
|
|
|
|
LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus));
|
|
|
|
//gKeepRunning = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Test Provider
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
class TestProvider : public nsIStreamProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestProvider(char *data);
|
|
|
|
virtual ~TestProvider();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2001-02-23 03:53:08 +03:00
|
|
|
NS_DECL_NSISTREAMPROVIDER
|
|
|
|
|
|
|
|
protected:
|
|
|
|
char *mData;
|
|
|
|
PRUint32 mOffset;
|
|
|
|
PRUint32 mDataLen;
|
|
|
|
PRUint32 mRequestCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(TestProvider,
|
|
|
|
nsIStreamProvider,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsIRequestObserver)
|
2001-02-23 03:53:08 +03:00
|
|
|
|
|
|
|
TestProvider::TestProvider(char *data)
|
|
|
|
{
|
|
|
|
mData = data;
|
|
|
|
mDataLen = strlen(data);
|
|
|
|
mOffset = 0;
|
|
|
|
mRequestCount = 0;
|
|
|
|
LOG(("Constructing TestProvider [this=%x]\n", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
TestProvider::~TestProvider()
|
|
|
|
{
|
|
|
|
LOG(("Destroying TestProvider [this=%x]\n", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestProvider::OnStartRequest(nsIRequest* request, nsISupports* context)
|
|
|
|
{
|
|
|
|
LOG(("TestProvider::OnStartRequest [this=%x]\n", this));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestProvider::OnStopRequest(nsIRequest* request, nsISupports* context,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
2001-02-23 03:53:08 +03:00
|
|
|
{
|
|
|
|
LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestProvider::OnDataWritable(nsIRequest *request, nsISupports *context,
|
|
|
|
nsIOutputStream *output, PRUint32 offset, PRUint32 count)
|
|
|
|
{
|
|
|
|
LOG(("TestProvider::OnDataWritable [offset=%u, count=%u]\n", offset, count));
|
|
|
|
|
|
|
|
// Stop at 5 requests
|
|
|
|
if (mRequestCount == 5)
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
PRUint32 writeCount, amount;
|
2011-06-25 18:06:02 +04:00
|
|
|
amount = NS_MIN(count, mDataLen - mOffset);
|
2001-02-23 03:53:08 +03:00
|
|
|
nsresult rv = output->Write(mData + mOffset, amount, &writeCount);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
printf("wrote %u bytes\n", writeCount);
|
|
|
|
mOffset += writeCount;
|
|
|
|
if (mOffset == mDataLen) {
|
|
|
|
printf("done sending packet %u\n", mRequestCount);
|
|
|
|
mOffset = 0;
|
|
|
|
mRequestCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Synchronous IO
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
WriteRequest(nsIOutputStream *os, const char *request)
|
|
|
|
{
|
|
|
|
LOG(("WriteRequest [request=%s]\n", request));
|
|
|
|
PRUint32 n;
|
|
|
|
return os->Write(request, strlen(request), &n);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ReadResponse(nsIInputStream *is)
|
|
|
|
{
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
char buf[2048];
|
|
|
|
do {
|
|
|
|
is->Read(buf, sizeof(buf), &bytesRead);
|
|
|
|
if (bytesRead > 0)
|
|
|
|
fwrite(buf, 1, bytesRead, stdout);
|
|
|
|
} while (bytesRead > 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Startup...
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
sighandler(int sig)
|
|
|
|
{
|
|
|
|
LOG(("got signal: %d\n", sig));
|
|
|
|
NS_BREAK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(char **argv)
|
|
|
|
{
|
|
|
|
printf("usage: %s <host> <path>\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
signal(SIGSEGV, sighandler);
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gTestSocketIOLog = PR_NewLogModule("TestSocketIO");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
usage(argv);
|
|
|
|
|
|
|
|
char *hostName = argv[1];
|
|
|
|
char *fileName = argv[2];
|
|
|
|
int port = 80;
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
2001-02-23 03:53:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to create: event queue service!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = eventQService->CreateMonitoredThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to create: thread event queue!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
|
|
|
|
|
|
|
// Create the Socket transport service...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsISocketTransportService> sts =
|
|
|
|
do_GetService(kSocketTransportServiceCID, &rv);
|
2001-02-23 03:53:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to create: socket transport service!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF
|
|
|
|
"host: %s" CRLF
|
|
|
|
"user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
|
|
|
|
"accept: */*" CRLF
|
|
|
|
"accept-language: en" CRLF
|
|
|
|
"accept-encoding: gzip,deflate,compress,identity" CRLF
|
|
|
|
"keep-alive: 300" CRLF
|
|
|
|
"connection: keep-alive" CRLF
|
|
|
|
CRLF,
|
|
|
|
fileName, hostName);
|
|
|
|
LOG(("Request [\n%s]\n", buffer));
|
|
|
|
|
|
|
|
// Create the socket transport...
|
|
|
|
nsCOMPtr<nsITransport> transport;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = sts->CreateTransport(hostName, port, nullptr, -1, 0, 0, getter_AddRefs(transport));
|
2001-02-23 03:53:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed to create: socket transport!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
gElapsedTime = PR_Now();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRequest> writeRequest, readRequest;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = transport->AsyncWrite(new TestProvider(buffer), nullptr, 0, 0, 0, getter_AddRefs(writeRequest));
|
2001-02-23 03:53:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed calling: AsyncWrite!");
|
|
|
|
return rv;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = transport->AsyncRead(new TestListener(), nullptr, 0, 0, 0, getter_AddRefs(readRequest));
|
2001-02-23 03:53:08 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("failed calling: AsyncWrite!");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the message pump
|
|
|
|
while ( gKeepRunning ) {
|
|
|
|
PLEvent *gEvent;
|
2003-04-04 20:01:51 +04:00
|
|
|
gEventQ->WaitForEvent(&gEvent);
|
|
|
|
gEventQ->HandleEvent(gEvent);
|
2001-02-23 03:53:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
|
|
|
LOG(("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL)));
|
|
|
|
|
|
|
|
sts->Shutdown();
|
|
|
|
return 0;
|
|
|
|
}
|