Bumping manifests a=b2g-bump
[gecko.git] / netwerk / test / TestHttp.cpp
blobe1883758fac8030cd45e9f908e37f6f50556e3b2
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"
8 #include <algorithm>
10 #define RETURN_IF_FAILED(rv, step) \
11 PR_BEGIN_MACRO \
12 if (NS_FAILED(rv)) { \
13 printf(">>> %s failed: rv=%x\n", step, rv); \
14 return rv;\
15 } \
16 PR_END_MACRO
18 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
19 static nsIEventQueue* gEventQ = nullptr;
20 static bool gKeepRunning = true;
22 //-----------------------------------------------------------------------------
23 // nsIStreamListener implementation
24 //-----------------------------------------------------------------------------
26 class MyListener : public nsIStreamListener
28 public:
29 NS_DECL_ISUPPORTS
30 NS_DECL_NSIREQUESTOBSERVER
31 NS_DECL_NSISTREAMLISTENER
33 MyListener() { }
34 virtual ~MyListener() {}
37 NS_IMPL_ISUPPORTS(MyListener,
38 nsIRequestObserver,
39 nsIStreamListener)
41 NS_IMETHODIMP
42 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
44 printf(">>> OnStartRequest\n");
45 return NS_OK;
48 NS_IMETHODIMP
49 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
51 printf(">>> OnStopRequest status=%x\n", status);
52 gKeepRunning = false;
53 return NS_OK;
56 NS_IMETHODIMP
57 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
58 nsIInputStream *stream,
59 uint64_t offset, uint32_t count)
61 printf(">>> OnDataAvailable [count=%u]\n", count);
63 char buf[256];
64 nsresult rv;
65 uint32_t bytesRead=0;
67 while (count) {
68 uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
70 rv = stream->Read(buf, amount, &bytesRead);
71 if (NS_FAILED(rv)) {
72 printf(">>> stream->Read failed with rv=%x\n", rv);
73 return rv;
76 fwrite(buf, 1, bytesRead, stdout);
78 count -= bytesRead;
80 return NS_OK;
83 //-----------------------------------------------------------------------------
84 // NotificationCallbacks implementation
85 //-----------------------------------------------------------------------------
87 class MyNotifications : public nsIInterfaceRequestor
88 , public nsIProgressEventSink
90 public:
91 NS_DECL_THREADSAFE_ISUPPORTS
92 NS_DECL_NSIINTERFACEREQUESTOR
93 NS_DECL_NSIPROGRESSEVENTSINK
95 MyNotifications() { }
96 virtual ~MyNotifications() {}
99 NS_IMPL_ISUPPORTS(MyNotifications,
100 nsIInterfaceRequestor,
101 nsIProgressEventSink)
103 NS_IMETHODIMP
104 MyNotifications::GetInterface(const nsIID &iid, void **result)
106 return QueryInterface(iid, result);
109 NS_IMETHODIMP
110 MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
111 nsresult status, const char16_t *statusText)
113 printf("status: %x\n", status);
114 return NS_OK;
117 NS_IMETHODIMP
118 MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
119 uint64_t progress, uint64_t progressMax)
121 printf("progress: %llu/%llu\n", progress, progressMax);
122 return NS_OK;
125 //-----------------------------------------------------------------------------
126 // main, etc..
127 //-----------------------------------------------------------------------------
130 int main(int argc, char **argv)
132 nsresult rv;
134 if (argc == 1) {
135 printf("usage: TestHttp <url>\n");
136 return -1;
139 nsCOMPtr<nsIServiceManager> servMan;
140 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
141 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
142 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
143 if (registrar)
144 registrar->AutoRegister(nullptr);
146 // Create the Event Queue for this thread...
147 nsCOMPtr<nsIEventQueueService> eqs =
148 do_GetService(kEventQueueServiceCID, &rv);
149 RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
151 rv = eqs->CreateMonitoredThreadEventQueue();
152 RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
154 rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
155 RETURN_IF_FAILED(rv, "GetThreadEventQueue");
157 nsCOMPtr<nsIURI> uri;
158 nsCOMPtr<nsIChannel> chan;
159 nsCOMPtr<nsIStreamListener> listener = new MyListener();
160 nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
162 rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
163 RETURN_IF_FAILED(rv, "NS_NewURI");
165 rv = NS_NewChannel(getter_AddRefs(chan), uri, nullptr, nullptr, callbacks);
166 RETURN_IF_FAILED(rv, "NS_OpenURI");
168 rv = chan->AsyncOpen(listener, nullptr);
169 RETURN_IF_FAILED(rv, "AsyncOpen");
171 while (gKeepRunning)
172 gEventQ->ProcessPendingEvents();
174 printf(">>> done\n");
175 } // this scopes the nsCOMPtrs
176 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
177 rv = NS_ShutdownXPCOM(nullptr);
178 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
179 return 0;