netwerk/test/TestSyncHTTP.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 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <nsCOMPtr.h>
michael@0 7 #include <nsString.h>
michael@0 8 #include <nsIURI.h>
michael@0 9 #include <nsIChannel.h>
michael@0 10 #include <nsIHTTPChannel.h>
michael@0 11 #include <nsIInputStream.h>
michael@0 12 #include <nsNetUtil.h>
michael@0 13
michael@0 14 /*
michael@0 15 * Test synchronous HTTP.
michael@0 16 */
michael@0 17
michael@0 18 #define RETURN_IF_FAILED(rv, what) \
michael@0 19 PR_BEGIN_MACRO \
michael@0 20 if (NS_FAILED(rv)) { \
michael@0 21 printf(what ": failed - %08x\n", rv); \
michael@0 22 return -1; \
michael@0 23 } \
michael@0 24 PR_END_MACRO
michael@0 25
michael@0 26 struct TestContext {
michael@0 27 nsCOMPtr<nsIURI> uri;
michael@0 28 nsCOMPtr<nsIChannel> channel;
michael@0 29 nsCOMPtr<nsIInputStream> inputStream;
michael@0 30 PRTime t1, t2;
michael@0 31 uint32_t bytesRead, totalRead;
michael@0 32
michael@0 33 TestContext()
michael@0 34 : t1(0), t2(0), bytesRead(0), totalRead(0)
michael@0 35 { printf("TestContext [this=%p]\n", (void*)this); }
michael@0 36 ~TestContext()
michael@0 37 { printf("~TestContext [this=%p]\n", (void*)this); }
michael@0 38 };
michael@0 39
michael@0 40 int
michael@0 41 main(int argc, char **argv)
michael@0 42 {
michael@0 43 nsresult rv;
michael@0 44 TestContext *c;
michael@0 45 int i, nc=0, npending=0;
michael@0 46 char buf[256];
michael@0 47
michael@0 48 if (argc < 2) {
michael@0 49 printf("Usage: TestSyncHTTP <url-list>\n");
michael@0 50 return -1;
michael@0 51 }
michael@0 52
michael@0 53 c = new TestContext[argc-1];
michael@0 54
michael@0 55 for (i=0; i<(argc-1); ++i, ++nc) {
michael@0 56 rv = NS_NewURI(getter_AddRefs(c[i].uri), argv[i+1]);
michael@0 57 RETURN_IF_FAILED(rv, "NS_NewURI");
michael@0 58
michael@0 59 rv = NS_OpenURI(getter_AddRefs(c[i].channel), c[i].uri, nullptr, nullptr);
michael@0 60 RETURN_IF_FAILED(rv, "NS_OpenURI");
michael@0 61
michael@0 62 nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(c[i].channel);
michael@0 63 if (httpChannel)
michael@0 64 httpChannel->SetOpenHasEventQueue(false);
michael@0 65
michael@0 66 // initialize these fields for reading
michael@0 67 c[i].bytesRead = 1;
michael@0 68 c[i].totalRead = 0;
michael@0 69 }
michael@0 70
michael@0 71 for (i=0; i<nc; ++i) {
michael@0 72 c[i].t1 = PR_Now();
michael@0 73
michael@0 74 rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream));
michael@0 75 RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream");
michael@0 76 }
michael@0 77
michael@0 78 npending = nc;
michael@0 79 while (npending) {
michael@0 80 for (i=0; i<nc; ++i) {
michael@0 81 //
michael@0 82 // read the response content...
michael@0 83 //
michael@0 84 if (c[i].bytesRead > 0) {
michael@0 85 rv = c[i].inputStream->Read(buf, sizeof buf, &c[i].bytesRead);
michael@0 86 RETURN_IF_FAILED(rv, "nsIInputStream::Read");
michael@0 87 c[i].totalRead += c[i].bytesRead;
michael@0 88
michael@0 89 if (c[i].bytesRead == 0) {
michael@0 90 c[i].t2 = PR_Now();
michael@0 91 printf("finished GET of: %s\n", argv[i+1]);
michael@0 92 printf("total read: %u bytes\n", c[i].totalRead);
michael@0 93 printf("total read time: %0.3f\n",
michael@0 94 ((double) (c[i].t2 - c[i].t1))/1000000.0);
michael@0 95 npending--;
michael@0 96 }
michael@0 97 }
michael@0 98 }
michael@0 99 }
michael@0 100
michael@0 101 delete[] c;
michael@0 102
michael@0 103 NS_ShutdownXPCOM(nullptr);
michael@0 104 return 0;
michael@0 105 }

mercurial