netwerk/test/TestSyncHTTP.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestSyncHTTP.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <nsCOMPtr.h>
    1.10 +#include <nsString.h>
    1.11 +#include <nsIURI.h>
    1.12 +#include <nsIChannel.h>
    1.13 +#include <nsIHTTPChannel.h>
    1.14 +#include <nsIInputStream.h>
    1.15 +#include <nsNetUtil.h>
    1.16 +
    1.17 +/*
    1.18 + * Test synchronous HTTP.
    1.19 + */
    1.20 +
    1.21 +#define RETURN_IF_FAILED(rv, what) \
    1.22 +    PR_BEGIN_MACRO \
    1.23 +    if (NS_FAILED(rv)) { \
    1.24 +        printf(what ": failed - %08x\n", rv); \
    1.25 +        return -1; \
    1.26 +    } \
    1.27 +    PR_END_MACRO
    1.28 +
    1.29 +struct TestContext {
    1.30 +    nsCOMPtr<nsIURI> uri;
    1.31 +    nsCOMPtr<nsIChannel> channel;
    1.32 +    nsCOMPtr<nsIInputStream> inputStream;
    1.33 +    PRTime t1, t2;
    1.34 +    uint32_t bytesRead, totalRead;
    1.35 +
    1.36 +    TestContext()
    1.37 +        : t1(0), t2(0), bytesRead(0), totalRead(0)
    1.38 +        { printf("TestContext [this=%p]\n", (void*)this); }
    1.39 +   ~TestContext()
    1.40 +        { printf("~TestContext [this=%p]\n", (void*)this); }
    1.41 +};
    1.42 +
    1.43 +int
    1.44 +main(int argc, char **argv)
    1.45 +{
    1.46 +    nsresult rv;
    1.47 +    TestContext *c;
    1.48 +    int i, nc=0, npending=0;
    1.49 +    char buf[256];
    1.50 +
    1.51 +    if (argc < 2) {
    1.52 +        printf("Usage: TestSyncHTTP <url-list>\n");
    1.53 +        return -1;
    1.54 +    }
    1.55 +
    1.56 +    c = new TestContext[argc-1];
    1.57 +
    1.58 +    for (i=0; i<(argc-1); ++i, ++nc) {
    1.59 +        rv = NS_NewURI(getter_AddRefs(c[i].uri), argv[i+1]);
    1.60 +        RETURN_IF_FAILED(rv, "NS_NewURI");
    1.61 +
    1.62 +        rv = NS_OpenURI(getter_AddRefs(c[i].channel), c[i].uri, nullptr, nullptr);
    1.63 +        RETURN_IF_FAILED(rv, "NS_OpenURI");
    1.64 +
    1.65 +        nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(c[i].channel);
    1.66 +        if (httpChannel)
    1.67 +            httpChannel->SetOpenHasEventQueue(false);
    1.68 +
    1.69 +        // initialize these fields for reading
    1.70 +        c[i].bytesRead = 1;
    1.71 +        c[i].totalRead = 0;
    1.72 +    }
    1.73 +
    1.74 +    for (i=0; i<nc; ++i) {
    1.75 +        c[i].t1 = PR_Now();
    1.76 +
    1.77 +        rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream));
    1.78 +        RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream");
    1.79 +    }
    1.80 +
    1.81 +    npending = nc;
    1.82 +    while (npending) {
    1.83 +        for (i=0; i<nc; ++i) {
    1.84 +            //
    1.85 +            // read the response content...
    1.86 +            //
    1.87 +            if (c[i].bytesRead > 0) {
    1.88 +                rv = c[i].inputStream->Read(buf, sizeof buf, &c[i].bytesRead);
    1.89 +                RETURN_IF_FAILED(rv, "nsIInputStream::Read");
    1.90 +                c[i].totalRead += c[i].bytesRead;
    1.91 +
    1.92 +                if (c[i].bytesRead == 0) {
    1.93 +                    c[i].t2 = PR_Now();
    1.94 +                    printf("finished GET of: %s\n", argv[i+1]);
    1.95 +                    printf("total read: %u bytes\n", c[i].totalRead);
    1.96 +                    printf("total read time: %0.3f\n",
    1.97 +                            ((double) (c[i].t2 - c[i].t1))/1000000.0);
    1.98 +                    npending--;
    1.99 +                }
   1.100 +            }
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    delete[] c;
   1.105 +
   1.106 +    NS_ShutdownXPCOM(nullptr);
   1.107 +    return 0;
   1.108 +}

mercurial