On x86 compilers without fastcall, simulate it when invoking traces and un-simulate...
[wine-gecko.git] / netwerk / test / TestHttp.cpp
bloba4b2982ad67230423b741577f3fad333e5ccfdea
1 #include "nsNetUtil.h"
2 #include "nsIEventQueueService.h"
3 #include "nsIServiceManager.h"
4 #include "nsIComponentRegistrar.h"
5 #include "nsIInterfaceRequestor.h"
6 #include "nsIInterfaceRequestorUtils.h"
7 #include "nsIProgressEventSink.h"
9 #define RETURN_IF_FAILED(rv, step) \
10 PR_BEGIN_MACRO \
11 if (NS_FAILED(rv)) { \
12 printf(">>> %s failed: rv=%x\n", step, rv); \
13 return rv;\
14 } \
15 PR_END_MACRO
17 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
18 static nsIEventQueue* gEventQ = nsnull;
19 static PRBool gKeepRunning = PR_TRUE;
21 //-----------------------------------------------------------------------------
22 // nsIStreamListener implementation
23 //-----------------------------------------------------------------------------
25 class MyListener : public nsIStreamListener
27 public:
28 NS_DECL_ISUPPORTS
29 NS_DECL_NSIREQUESTOBSERVER
30 NS_DECL_NSISTREAMLISTENER
32 MyListener() { }
33 virtual ~MyListener() {}
36 NS_IMPL_ISUPPORTS2(MyListener,
37 nsIRequestObserver,
38 nsIStreamListener)
40 NS_IMETHODIMP
41 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
43 printf(">>> OnStartRequest\n");
44 return NS_OK;
47 NS_IMETHODIMP
48 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
50 printf(">>> OnStopRequest status=%x\n", status);
51 gKeepRunning = PR_FALSE;
52 return NS_OK;
55 NS_IMETHODIMP
56 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
57 nsIInputStream *stream,
58 PRUint32 offset, PRUint32 count)
60 printf(">>> OnDataAvailable [count=%u]\n", count);
62 char buf[256];
63 nsresult rv;
64 PRUint32 bytesRead=0;
66 while (count) {
67 PRUint32 amount = PR_MIN(count, sizeof(buf));
69 rv = stream->Read(buf, amount, &bytesRead);
70 if (NS_FAILED(rv)) {
71 printf(">>> stream->Read failed with rv=%x\n", rv);
72 return rv;
75 fwrite(buf, 1, bytesRead, stdout);
77 count -= bytesRead;
79 return NS_OK;
82 //-----------------------------------------------------------------------------
83 // NotificationCallbacks implementation
84 //-----------------------------------------------------------------------------
86 class MyNotifications : public nsIInterfaceRequestor
87 , public nsIProgressEventSink
89 public:
90 NS_DECL_ISUPPORTS
91 NS_DECL_NSIINTERFACEREQUESTOR
92 NS_DECL_NSIPROGRESSEVENTSINK
94 MyNotifications() { }
95 virtual ~MyNotifications() {}
98 NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
99 nsIInterfaceRequestor,
100 nsIProgressEventSink)
102 NS_IMETHODIMP
103 MyNotifications::GetInterface(const nsIID &iid, void **result)
105 return QueryInterface(iid, result);
108 NS_IMETHODIMP
109 MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
110 nsresult status, const PRUnichar *statusText)
112 printf("status: %x\n", status);
113 return NS_OK;
116 NS_IMETHODIMP
117 MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
118 PRUint64 progress, PRUint64 progressMax)
120 printf("progress: %llu/%llu\n", progress, progressMax);
121 return NS_OK;
124 //-----------------------------------------------------------------------------
125 // main, etc..
126 //-----------------------------------------------------------------------------
129 int main(int argc, char **argv)
131 nsresult rv;
133 if (argc == 1) {
134 printf("usage: TestHttp <url>\n");
135 return -1;
138 nsCOMPtr<nsIServiceManager> servMan;
139 NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
140 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
141 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
142 if (registrar)
143 registrar->AutoRegister(nsnull);
145 // Create the Event Queue for this thread...
146 nsCOMPtr<nsIEventQueueService> eqs =
147 do_GetService(kEventQueueServiceCID, &rv);
148 RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
150 rv = eqs->CreateMonitoredThreadEventQueue();
151 RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
153 rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
154 RETURN_IF_FAILED(rv, "GetThreadEventQueue");
156 nsCOMPtr<nsIURI> uri;
157 nsCOMPtr<nsIChannel> chan;
158 nsCOMPtr<nsIStreamListener> listener = new MyListener();
159 nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
161 rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
162 RETURN_IF_FAILED(rv, "NS_NewURI");
164 rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
165 RETURN_IF_FAILED(rv, "NS_OpenURI");
167 rv = chan->AsyncOpen(listener, nsnull);
168 RETURN_IF_FAILED(rv, "AsyncOpen");
170 while (gKeepRunning)
171 gEventQ->ProcessPendingEvents();
173 printf(">>> done\n");
174 } // this scopes the nsCOMPtrs
175 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
176 rv = NS_ShutdownXPCOM(nsnull);
177 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
178 return 0;