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