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