2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2000-04-19 17:47:45 +00:00
|
|
|
|
2004-10-21 20:57:17 +00:00
|
|
|
#include "TestCommon.h"
|
2000-04-19 17:47:45 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-05 21:28:38 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2000-04-19 17:47:45 +00:00
|
|
|
#include "nsIDNSService.h"
|
2012-06-06 03:18:25 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsNetUtil.h"
|
2005-11-08 19:23:00 +00:00
|
|
|
#include "nsStringAPI.h"
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool gError = false;
|
2006-05-10 17:30:15 +00:00
|
|
|
static PRInt32 gKeepRunning = 0;
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
#define NS_IEQUALS_IID \
|
|
|
|
{ 0x11c5c8ee, 0x1dd2, 0x11b2, \
|
|
|
|
{ 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }}
|
|
|
|
|
|
|
|
class nsIEquals : public nsISupports {
|
|
|
|
public:
|
2005-11-11 14:36:26 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID)
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD Equals(void *aPtr, bool *_retval) = 0;
|
2000-04-19 17:47:45 +00:00
|
|
|
};
|
|
|
|
|
2005-11-11 14:36:26 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID)
|
|
|
|
|
2012-06-06 03:18:25 +00:00
|
|
|
class ConsumerContext MOZ_FINAL : public nsIEquals {
|
2000-04-19 17:47:45 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2003-01-08 22:35:09 +00:00
|
|
|
ConsumerContext() { }
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD Equals(void *aPtr, bool *_retval) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = true;
|
|
|
|
if (aPtr != this) *_retval = false;
|
2000-04-19 17:47:45 +00:00
|
|
|
return NS_OK;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2003-09-07 23:12:15 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(ConsumerContext, nsIEquals)
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
class Consumer : public nsIStreamListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-04-10 06:01:08 +00:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2000-04-19 17:47:45 +00:00
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
Consumer();
|
|
|
|
virtual ~Consumer();
|
|
|
|
nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext);
|
2001-02-21 20:38:08 +00:00
|
|
|
nsresult Validate(nsIRequest *request, nsISupports *aContext);
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
// member data
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mOnStart; // have we received an OnStart?
|
|
|
|
bool mOnStop; // have we received an onStop?
|
2000-04-19 17:47:45 +00:00
|
|
|
PRInt32 mOnDataCount; // number of times OnData was called.
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
nsCOMPtr<nsIEquals> mContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
// nsISupports implementation
|
2003-09-07 23:12:15 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(Consumer, nsIStreamListener, nsIRequestObserver)
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
|
2001-04-10 06:01:08 +00:00
|
|
|
// nsIRequestObserver implementation
|
2000-04-19 17:47:45 +00:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) {
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "Consumer::OnStart() -> in\n\n");
|
|
|
|
|
|
|
|
if (mOnStart) {
|
|
|
|
fprintf(stderr, "INFO: multiple OnStarts received\n");
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mOnStart = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsresult rv = Validate(request, aContext);
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
fprintf(stderr, "Consumer::OnStart() -> out\n\n");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext,
|
2001-04-10 06:01:08 +00:00
|
|
|
nsresult aStatus) {
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "Consumer::OnStop() -> in\n\n");
|
|
|
|
|
|
|
|
if (!mOnStart) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gError = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No OnStart received\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOnStop) {
|
|
|
|
fprintf(stderr, "INFO: multiple OnStops received\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mOnStop = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsresult rv = Validate(request, aContext);
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
fprintf(stderr, "Consumer::OnStop() -> out\n\n");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamListener implementation
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext,
|
2000-04-19 17:47:45 +00:00
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aOffset, PRUint32 aLength) {
|
|
|
|
fprintf(stderr, "Consumer::OnData() -> in\n\n");
|
|
|
|
|
|
|
|
if (!mOnStart) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gError = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No OnStart received\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
mOnDataCount += 1;
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsresult rv = Validate(request, aContext);
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
fprintf(stderr, "Consumer::OnData() -> out\n\n");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consumer implementation
|
|
|
|
Consumer::Consumer() {
|
2011-10-17 14:59:28 +00:00
|
|
|
mOnStart = mOnStop = false;
|
2000-04-19 17:47:45 +00:00
|
|
|
mOnDataCount = 0;
|
2000-04-19 19:58:29 +00:00
|
|
|
gKeepRunning++;
|
2000-04-19 17:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Consumer::~Consumer() {
|
|
|
|
fprintf(stderr, "Consumer::~Consumer -> in\n\n");
|
|
|
|
|
|
|
|
if (!mOnStart) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gError = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Never got an OnStart\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mOnStop) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gError = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Never got an OnStop \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Consumer::~Consumer -> out\n\n");
|
2006-05-10 17:30:15 +00:00
|
|
|
if (--gKeepRunning == 0)
|
|
|
|
QuitPumpingEvents();
|
2000-04-19 17:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2001-02-21 20:38:08 +00:00
|
|
|
Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) {
|
2000-04-19 17:47:45 +00:00
|
|
|
mURI = aURI;
|
|
|
|
mChannel = aChannel;
|
|
|
|
mContext = do_QueryInterface(aContext);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2001-02-21 20:38:08 +00:00
|
|
|
Consumer::Validate(nsIRequest* request, nsISupports *aContext) {
|
2000-04-19 17:47:45 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
|
|
|
|
2000-04-19 17:47:45 +00:00
|
|
|
rv = aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool same = false;
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
rv = mURI->Equals(uri, &same);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!same)
|
|
|
|
fprintf(stderr, "INFO: URIs do not match\n");
|
|
|
|
|
|
|
|
rv = mContext->Equals((void*)aContext, &same);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!same) {
|
2011-10-17 14:59:28 +00:00
|
|
|
gError = true;
|
2000-04-19 17:47:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Contexts do not match\n");
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-11-07 06:24:10 +00:00
|
|
|
nsresult StartLoad(const char *);
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2004-10-21 20:57:17 +00:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2000-04-19 17:47:45 +00:00
|
|
|
nsresult rv = NS_OK;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cmdLineURL = false;
|
2000-04-19 19:58:29 +00:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
// run in signle url mode
|
2011-10-17 14:59:28 +00:00
|
|
|
cmdLineURL = true;
|
2000-04-19 19:58:29 +00:00
|
|
|
}
|
2000-04-19 17:47:45 +00:00
|
|
|
|
2001-10-16 03:35:52 +00:00
|
|
|
rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-04-19 19:58:29 +00:00
|
|
|
if (cmdLineURL) {
|
|
|
|
rv = StartLoad(argv[1]);
|
|
|
|
} else {
|
|
|
|
rv = StartLoad("http://badhostnamexyz/test.txt");
|
|
|
|
}
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
2006-05-10 17:30:15 +00:00
|
|
|
PumpEvents();
|
2000-04-19 17:47:45 +00:00
|
|
|
|
|
|
|
NS_ShutdownXPCOM(nsnull);
|
2000-04-19 19:58:29 +00:00
|
|
|
if (gError) {
|
|
|
|
fprintf(stderr, "\n\n-------ERROR-------\n\n");
|
|
|
|
}
|
2000-04-19 17:47:45 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-11-07 06:24:10 +00:00
|
|
|
nsresult StartLoad(const char *aURISpec) {
|
2000-04-19 17:47:45 +00:00
|
|
|
nsresult rv = NS_OK;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-04-19 17:47:45 +00:00
|
|
|
// create a context
|
|
|
|
ConsumerContext *context = new ConsumerContext;
|
|
|
|
nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
|
2004-11-07 23:59:35 +00:00
|
|
|
nsCOMPtr<nsIIOService> serv = do_GetService(kIOServiceCID, &rv);
|
2000-04-19 17:47:45 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2000-04-19 17:47:45 +00:00
|
|
|
// create a uri
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// create a channel
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = serv->NewChannelFromURI(uri, getter_AddRefs(channel));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
Consumer *consumer = new Consumer;
|
|
|
|
rv = consumer->Init(uri, channel, contextSup);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// kick off the load
|
2001-02-21 20:38:08 +00:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
2007-07-08 07:08:04 +00:00
|
|
|
return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup);
|
2000-04-19 17:47:45 +00:00
|
|
|
}
|