netwerk/test/TestHttp.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestHttp.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,180 @@
     1.4 +#include "nsNetUtil.h"
     1.5 +#include "nsIEventQueueService.h"
     1.6 +#include "nsIServiceManager.h"
     1.7 +#include "nsIComponentRegistrar.h"
     1.8 +#include "nsIInterfaceRequestor.h"
     1.9 +#include "nsIInterfaceRequestorUtils.h"
    1.10 +#include "nsIProgressEventSink.h"
    1.11 +#include <algorithm>
    1.12 +
    1.13 +#define RETURN_IF_FAILED(rv, step) \
    1.14 +    PR_BEGIN_MACRO \
    1.15 +    if (NS_FAILED(rv)) { \
    1.16 +        printf(">>> %s failed: rv=%x\n", step, rv); \
    1.17 +        return rv;\
    1.18 +    } \
    1.19 +    PR_END_MACRO
    1.20 +
    1.21 +static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
    1.22 +static nsIEventQueue* gEventQ = nullptr;
    1.23 +static bool gKeepRunning = true;
    1.24 +
    1.25 +//-----------------------------------------------------------------------------
    1.26 +// nsIStreamListener implementation
    1.27 +//-----------------------------------------------------------------------------
    1.28 +
    1.29 +class MyListener : public nsIStreamListener
    1.30 +{
    1.31 +public:
    1.32 +    NS_DECL_ISUPPORTS
    1.33 +    NS_DECL_NSIREQUESTOBSERVER
    1.34 +    NS_DECL_NSISTREAMLISTENER
    1.35 +
    1.36 +    MyListener() { }
    1.37 +    virtual ~MyListener() {}
    1.38 +};
    1.39 +
    1.40 +NS_IMPL_ISUPPORTS(MyListener,
    1.41 +                  nsIRequestObserver,
    1.42 +                  nsIStreamListener)
    1.43 +
    1.44 +NS_IMETHODIMP
    1.45 +MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
    1.46 +{
    1.47 +    printf(">>> OnStartRequest\n");
    1.48 +    return NS_OK;
    1.49 +}
    1.50 +
    1.51 +NS_IMETHODIMP
    1.52 +MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
    1.53 +{
    1.54 +    printf(">>> OnStopRequest status=%x\n", status);
    1.55 +    gKeepRunning = false;
    1.56 +    return NS_OK;
    1.57 +}
    1.58 +
    1.59 +NS_IMETHODIMP
    1.60 +MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
    1.61 +                            nsIInputStream *stream,
    1.62 +                            uint64_t offset, uint32_t count)
    1.63 +{
    1.64 +    printf(">>> OnDataAvailable [count=%u]\n", count);
    1.65 +
    1.66 +    char buf[256];
    1.67 +    nsresult rv;
    1.68 +    uint32_t bytesRead=0;
    1.69 +
    1.70 +    while (count) {
    1.71 +        uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
    1.72 +
    1.73 +        rv = stream->Read(buf, amount, &bytesRead);
    1.74 +        if (NS_FAILED(rv)) {
    1.75 +            printf(">>> stream->Read failed with rv=%x\n", rv);
    1.76 +            return rv;
    1.77 +        }
    1.78 +
    1.79 +        fwrite(buf, 1, bytesRead, stdout);
    1.80 +
    1.81 +        count -= bytesRead;
    1.82 +    }
    1.83 +    return NS_OK;
    1.84 +}
    1.85 +
    1.86 +//-----------------------------------------------------------------------------
    1.87 +// NotificationCallbacks implementation
    1.88 +//-----------------------------------------------------------------------------
    1.89 +
    1.90 +class MyNotifications : public nsIInterfaceRequestor
    1.91 +                      , public nsIProgressEventSink
    1.92 +{
    1.93 +public:
    1.94 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.95 +    NS_DECL_NSIINTERFACEREQUESTOR
    1.96 +    NS_DECL_NSIPROGRESSEVENTSINK
    1.97 +
    1.98 +    MyNotifications() { }
    1.99 +    virtual ~MyNotifications() {}
   1.100 +};
   1.101 +
   1.102 +NS_IMPL_ISUPPORTS(MyNotifications,
   1.103 +                  nsIInterfaceRequestor,
   1.104 +                  nsIProgressEventSink)
   1.105 +
   1.106 +NS_IMETHODIMP
   1.107 +MyNotifications::GetInterface(const nsIID &iid, void **result)
   1.108 +{
   1.109 +    return QueryInterface(iid, result);
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
   1.114 +                          nsresult status, const char16_t *statusText)
   1.115 +{
   1.116 +    printf("status: %x\n", status);
   1.117 +    return NS_OK;
   1.118 +}
   1.119 +
   1.120 +NS_IMETHODIMP
   1.121 +MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
   1.122 +                            uint64_t progress, uint64_t progressMax)
   1.123 +{
   1.124 +    printf("progress: %llu/%llu\n", progress, progressMax);
   1.125 +    return NS_OK;
   1.126 +}
   1.127 +
   1.128 +//-----------------------------------------------------------------------------
   1.129 +// main, etc..
   1.130 +//-----------------------------------------------------------------------------
   1.131 +
   1.132 +
   1.133 +int main(int argc, char **argv)
   1.134 +{
   1.135 +    nsresult rv;
   1.136 +
   1.137 +    if (argc == 1) {
   1.138 +        printf("usage: TestHttp <url>\n");
   1.139 +        return -1;
   1.140 +    }
   1.141 +    {
   1.142 +        nsCOMPtr<nsIServiceManager> servMan;
   1.143 +        NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
   1.144 +        nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
   1.145 +        NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
   1.146 +        if (registrar)
   1.147 +            registrar->AutoRegister(nullptr);
   1.148 +
   1.149 +        // Create the Event Queue for this thread...
   1.150 +        nsCOMPtr<nsIEventQueueService> eqs =
   1.151 +                 do_GetService(kEventQueueServiceCID, &rv);
   1.152 +        RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
   1.153 +
   1.154 +        rv = eqs->CreateMonitoredThreadEventQueue();
   1.155 +        RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
   1.156 +
   1.157 +        rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
   1.158 +        RETURN_IF_FAILED(rv, "GetThreadEventQueue");
   1.159 +
   1.160 +        nsCOMPtr<nsIURI> uri;
   1.161 +        nsCOMPtr<nsIChannel> chan;
   1.162 +        nsCOMPtr<nsIStreamListener> listener = new MyListener();
   1.163 +        nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
   1.164 +
   1.165 +        rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
   1.166 +        RETURN_IF_FAILED(rv, "NS_NewURI");
   1.167 +
   1.168 +        rv = NS_NewChannel(getter_AddRefs(chan), uri, nullptr, nullptr, callbacks);
   1.169 +        RETURN_IF_FAILED(rv, "NS_OpenURI");
   1.170 +
   1.171 +        rv = chan->AsyncOpen(listener, nullptr);
   1.172 +        RETURN_IF_FAILED(rv, "AsyncOpen");
   1.173 +
   1.174 +        while (gKeepRunning)
   1.175 +            gEventQ->ProcessPendingEvents();
   1.176 +
   1.177 +        printf(">>> done\n");
   1.178 +    } // this scopes the nsCOMPtrs
   1.179 +    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
   1.180 +    rv = NS_ShutdownXPCOM(nullptr);
   1.181 +    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
   1.182 +    return 0;
   1.183 +}

mercurial