netwerk/test/TestHttp.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #include "nsNetUtil.h"
michael@0 2 #include "nsIEventQueueService.h"
michael@0 3 #include "nsIServiceManager.h"
michael@0 4 #include "nsIComponentRegistrar.h"
michael@0 5 #include "nsIInterfaceRequestor.h"
michael@0 6 #include "nsIInterfaceRequestorUtils.h"
michael@0 7 #include "nsIProgressEventSink.h"
michael@0 8 #include <algorithm>
michael@0 9
michael@0 10 #define RETURN_IF_FAILED(rv, step) \
michael@0 11 PR_BEGIN_MACRO \
michael@0 12 if (NS_FAILED(rv)) { \
michael@0 13 printf(">>> %s failed: rv=%x\n", step, rv); \
michael@0 14 return rv;\
michael@0 15 } \
michael@0 16 PR_END_MACRO
michael@0 17
michael@0 18 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
michael@0 19 static nsIEventQueue* gEventQ = nullptr;
michael@0 20 static bool gKeepRunning = true;
michael@0 21
michael@0 22 //-----------------------------------------------------------------------------
michael@0 23 // nsIStreamListener implementation
michael@0 24 //-----------------------------------------------------------------------------
michael@0 25
michael@0 26 class MyListener : public nsIStreamListener
michael@0 27 {
michael@0 28 public:
michael@0 29 NS_DECL_ISUPPORTS
michael@0 30 NS_DECL_NSIREQUESTOBSERVER
michael@0 31 NS_DECL_NSISTREAMLISTENER
michael@0 32
michael@0 33 MyListener() { }
michael@0 34 virtual ~MyListener() {}
michael@0 35 };
michael@0 36
michael@0 37 NS_IMPL_ISUPPORTS(MyListener,
michael@0 38 nsIRequestObserver,
michael@0 39 nsIStreamListener)
michael@0 40
michael@0 41 NS_IMETHODIMP
michael@0 42 MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
michael@0 43 {
michael@0 44 printf(">>> OnStartRequest\n");
michael@0 45 return NS_OK;
michael@0 46 }
michael@0 47
michael@0 48 NS_IMETHODIMP
michael@0 49 MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
michael@0 50 {
michael@0 51 printf(">>> OnStopRequest status=%x\n", status);
michael@0 52 gKeepRunning = false;
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 NS_IMETHODIMP
michael@0 57 MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
michael@0 58 nsIInputStream *stream,
michael@0 59 uint64_t offset, uint32_t count)
michael@0 60 {
michael@0 61 printf(">>> OnDataAvailable [count=%u]\n", count);
michael@0 62
michael@0 63 char buf[256];
michael@0 64 nsresult rv;
michael@0 65 uint32_t bytesRead=0;
michael@0 66
michael@0 67 while (count) {
michael@0 68 uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
michael@0 69
michael@0 70 rv = stream->Read(buf, amount, &bytesRead);
michael@0 71 if (NS_FAILED(rv)) {
michael@0 72 printf(">>> stream->Read failed with rv=%x\n", rv);
michael@0 73 return rv;
michael@0 74 }
michael@0 75
michael@0 76 fwrite(buf, 1, bytesRead, stdout);
michael@0 77
michael@0 78 count -= bytesRead;
michael@0 79 }
michael@0 80 return NS_OK;
michael@0 81 }
michael@0 82
michael@0 83 //-----------------------------------------------------------------------------
michael@0 84 // NotificationCallbacks implementation
michael@0 85 //-----------------------------------------------------------------------------
michael@0 86
michael@0 87 class MyNotifications : public nsIInterfaceRequestor
michael@0 88 , public nsIProgressEventSink
michael@0 89 {
michael@0 90 public:
michael@0 91 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 92 NS_DECL_NSIINTERFACEREQUESTOR
michael@0 93 NS_DECL_NSIPROGRESSEVENTSINK
michael@0 94
michael@0 95 MyNotifications() { }
michael@0 96 virtual ~MyNotifications() {}
michael@0 97 };
michael@0 98
michael@0 99 NS_IMPL_ISUPPORTS(MyNotifications,
michael@0 100 nsIInterfaceRequestor,
michael@0 101 nsIProgressEventSink)
michael@0 102
michael@0 103 NS_IMETHODIMP
michael@0 104 MyNotifications::GetInterface(const nsIID &iid, void **result)
michael@0 105 {
michael@0 106 return QueryInterface(iid, result);
michael@0 107 }
michael@0 108
michael@0 109 NS_IMETHODIMP
michael@0 110 MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
michael@0 111 nsresult status, const char16_t *statusText)
michael@0 112 {
michael@0 113 printf("status: %x\n", status);
michael@0 114 return NS_OK;
michael@0 115 }
michael@0 116
michael@0 117 NS_IMETHODIMP
michael@0 118 MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
michael@0 119 uint64_t progress, uint64_t progressMax)
michael@0 120 {
michael@0 121 printf("progress: %llu/%llu\n", progress, progressMax);
michael@0 122 return NS_OK;
michael@0 123 }
michael@0 124
michael@0 125 //-----------------------------------------------------------------------------
michael@0 126 // main, etc..
michael@0 127 //-----------------------------------------------------------------------------
michael@0 128
michael@0 129
michael@0 130 int main(int argc, char **argv)
michael@0 131 {
michael@0 132 nsresult rv;
michael@0 133
michael@0 134 if (argc == 1) {
michael@0 135 printf("usage: TestHttp <url>\n");
michael@0 136 return -1;
michael@0 137 }
michael@0 138 {
michael@0 139 nsCOMPtr<nsIServiceManager> servMan;
michael@0 140 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 141 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
michael@0 142 NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
michael@0 143 if (registrar)
michael@0 144 registrar->AutoRegister(nullptr);
michael@0 145
michael@0 146 // Create the Event Queue for this thread...
michael@0 147 nsCOMPtr<nsIEventQueueService> eqs =
michael@0 148 do_GetService(kEventQueueServiceCID, &rv);
michael@0 149 RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
michael@0 150
michael@0 151 rv = eqs->CreateMonitoredThreadEventQueue();
michael@0 152 RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
michael@0 153
michael@0 154 rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
michael@0 155 RETURN_IF_FAILED(rv, "GetThreadEventQueue");
michael@0 156
michael@0 157 nsCOMPtr<nsIURI> uri;
michael@0 158 nsCOMPtr<nsIChannel> chan;
michael@0 159 nsCOMPtr<nsIStreamListener> listener = new MyListener();
michael@0 160 nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
michael@0 161
michael@0 162 rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
michael@0 163 RETURN_IF_FAILED(rv, "NS_NewURI");
michael@0 164
michael@0 165 rv = NS_NewChannel(getter_AddRefs(chan), uri, nullptr, nullptr, callbacks);
michael@0 166 RETURN_IF_FAILED(rv, "NS_OpenURI");
michael@0 167
michael@0 168 rv = chan->AsyncOpen(listener, nullptr);
michael@0 169 RETURN_IF_FAILED(rv, "AsyncOpen");
michael@0 170
michael@0 171 while (gKeepRunning)
michael@0 172 gEventQ->ProcessPendingEvents();
michael@0 173
michael@0 174 printf(">>> done\n");
michael@0 175 } // this scopes the nsCOMPtrs
michael@0 176 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
michael@0 177 rv = NS_ShutdownXPCOM(nullptr);
michael@0 178 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
michael@0 179 return 0;
michael@0 180 }

mercurial