|
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/. */ |
|
5 |
|
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> |
|
13 |
|
14 /* |
|
15 * Test synchronous HTTP. |
|
16 */ |
|
17 |
|
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 |
|
25 |
|
26 struct TestContext { |
|
27 nsCOMPtr<nsIURI> uri; |
|
28 nsCOMPtr<nsIChannel> channel; |
|
29 nsCOMPtr<nsIInputStream> inputStream; |
|
30 PRTime t1, t2; |
|
31 uint32_t bytesRead, totalRead; |
|
32 |
|
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); } |
|
38 }; |
|
39 |
|
40 int |
|
41 main(int argc, char **argv) |
|
42 { |
|
43 nsresult rv; |
|
44 TestContext *c; |
|
45 int i, nc=0, npending=0; |
|
46 char buf[256]; |
|
47 |
|
48 if (argc < 2) { |
|
49 printf("Usage: TestSyncHTTP <url-list>\n"); |
|
50 return -1; |
|
51 } |
|
52 |
|
53 c = new TestContext[argc-1]; |
|
54 |
|
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"); |
|
58 |
|
59 rv = NS_OpenURI(getter_AddRefs(c[i].channel), c[i].uri, nullptr, nullptr); |
|
60 RETURN_IF_FAILED(rv, "NS_OpenURI"); |
|
61 |
|
62 nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(c[i].channel); |
|
63 if (httpChannel) |
|
64 httpChannel->SetOpenHasEventQueue(false); |
|
65 |
|
66 // initialize these fields for reading |
|
67 c[i].bytesRead = 1; |
|
68 c[i].totalRead = 0; |
|
69 } |
|
70 |
|
71 for (i=0; i<nc; ++i) { |
|
72 c[i].t1 = PR_Now(); |
|
73 |
|
74 rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream)); |
|
75 RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream"); |
|
76 } |
|
77 |
|
78 npending = nc; |
|
79 while (npending) { |
|
80 for (i=0; i<nc; ++i) { |
|
81 // |
|
82 // read the response content... |
|
83 // |
|
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; |
|
88 |
|
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--; |
|
96 } |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 delete[] c; |
|
102 |
|
103 NS_ShutdownXPCOM(nullptr); |
|
104 return 0; |
|
105 } |