2001-05-12 01:17:17 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2001-05-12 01:17:17 +04:00
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
|
|
|
|
#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);
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
static PRBool gKeepRunning = PR_TRUE;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStreamListener implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
MyListener() { NS_INIT_ISUPPORTS(); }
|
|
|
|
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);
|
|
|
|
gKeepRunning = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
|
|
|
|
nsIInputStream *stream,
|
|
|
|
PRUint32 offset, PRUint32 count)
|
|
|
|
{
|
|
|
|
printf(">>> OnDataAvailable [count=%u]\n", count);
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 bytesRead=0;
|
|
|
|
|
|
|
|
while (count) {
|
|
|
|
PRUint32 amount = PR_MIN(count, sizeof(buf));
|
|
|
|
|
|
|
|
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:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIPROGRESSEVENTSINK
|
|
|
|
|
|
|
|
MyNotifications() { NS_INIT_ISUPPORTS(); }
|
|
|
|
virtual ~MyNotifications() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIProgressEventSink)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::GetInterface(const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
return QueryInterface(iid, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
|
|
|
|
nsresult status, const PRUnichar *statusText)
|
|
|
|
{
|
|
|
|
printf("status: %x\n", status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
|
|
|
|
PRUint32 progress, PRUint32 progressMax)
|
|
|
|
{
|
|
|
|
printf("progress: %u/%u\n", progress, progressMax);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// main, etc..
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
2001-12-19 03:12:41 +03:00
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManagerObsolete::NS_Startup, NULL /* default */);
|
2001-05-12 01:17:17 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
printf("usage: TestHttp <url>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_AutoregisterComponents();
|
|
|
|
RETURN_IF_FAILED(rv, "NS_AutoregisterComponents");
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIEventQueueService> eqs =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
2001-05-12 01:17:17 +04:00
|
|
|
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");
|
|
|
|
|
|
|
|
rv = NS_OpenURI(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
|
|
|
|
RETURN_IF_FAILED(rv, "NS_OpenURI");
|
|
|
|
|
|
|
|
rv = chan->AsyncOpen(listener, nsnull);
|
|
|
|
RETURN_IF_FAILED(rv, "AsyncOpen");
|
|
|
|
|
|
|
|
while (gKeepRunning)
|
|
|
|
gEventQ->ProcessPendingEvents();
|
|
|
|
|
|
|
|
printf(">>> done\n");
|
|
|
|
return 0;
|
|
|
|
}
|