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