2007-03-22 10:30:00 -07:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#define RETURN_IF_FAILED(rv, step) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (NS_FAILED(rv)) { \
|
|
|
|
printf(">>> %s failed: rv=%x\n", step, rv); \
|
|
|
|
return rv;\
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
2012-07-30 07:20:58 -07:00
|
|
|
static nsIEventQueue* gEventQ = nullptr;
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gKeepRunning = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStreamListener implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
MyListener() { }
|
|
|
|
virtual ~MyListener() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(MyListener,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
|
|
|
|
{
|
|
|
|
printf(">>> OnStartRequest\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
|
|
|
|
{
|
|
|
|
printf(">>> OnStopRequest status=%x\n", status);
|
2011-10-17 07:59:28 -07:00
|
|
|
gKeepRunning = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
|
|
|
|
nsIInputStream *stream,
|
2012-09-05 19:41:02 -07:00
|
|
|
uint64_t offset, uint32_t count)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
printf(">>> OnDataAvailable [count=%u]\n", count);
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesRead=0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (count) {
|
2013-01-15 04:22:03 -08:00
|
|
|
uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = stream->Read(buf, amount, &bytesRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf(">>> stream->Read failed with rv=%x\n", rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(buf, 1, bytesRead, stdout);
|
|
|
|
|
|
|
|
count -= bytesRead;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NotificationCallbacks implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyNotifications : public nsIInterfaceRequestor
|
|
|
|
, public nsIProgressEventSink
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIPROGRESSEVENTSINK
|
|
|
|
|
|
|
|
MyNotifications() { }
|
|
|
|
virtual ~MyNotifications() {}
|
|
|
|
};
|
|
|
|
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_IMPL_ISUPPORTS2(MyNotifications,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIProgressEventSink)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::GetInterface(const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
return QueryInterface(iid, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
|
2014-01-04 07:02:17 -08:00
|
|
|
nsresult status, const char16_t *statusText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
printf("status: %x\n", status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t progress, uint64_t progressMax)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
printf("progress: %llu/%llu\n", progress, progressMax);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// main, etc..
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("usage: TestHttp <url>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
2012-07-30 07:20:58 -07:00
|
|
|
registrar->AutoRegister(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
nsCOMPtr<nsIEventQueueService> eqs =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
|
|
|
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
|
|
|
|
|
|
|
|
rv = eqs->CreateMonitoredThreadEventQueue();
|
|
|
|
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
|
|
|
|
|
|
|
|
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
|
|
|
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsCOMPtr<nsIChannel> chan;
|
|
|
|
nsCOMPtr<nsIStreamListener> listener = new MyListener();
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
|
|
|
|
RETURN_IF_FAILED(rv, "NS_NewURI");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan), uri, nullptr, nullptr, callbacks);
|
2007-03-22 10:30:00 -07:00
|
|
|
RETURN_IF_FAILED(rv, "NS_OpenURI");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = chan->AsyncOpen(listener, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
RETURN_IF_FAILED(rv, "AsyncOpen");
|
|
|
|
|
|
|
|
while (gKeepRunning)
|
|
|
|
gEventQ->ProcessPendingEvents();
|
|
|
|
|
|
|
|
printf(">>> done\n");
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = NS_ShutdownXPCOM(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
|
|
|
return 0;
|
|
|
|
}
|