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/. */
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) \
20 if (NS_FAILED(rv)) { \
21 printf(what ": failed - %08x\n", rv); \
28 nsCOMPtr
<nsIChannel
> channel
;
29 nsCOMPtr
<nsIInputStream
> inputStream
;
31 uint32_t bytesRead
, totalRead
;
34 : t1(0), t2(0), bytesRead(0), totalRead(0)
35 { printf("TestContext [this=%p]\n", (void*)this); }
37 { printf("~TestContext [this=%p]\n", (void*)this); }
41 main(int argc
, char **argv
)
45 int i
, nc
=0, npending
=0;
49 printf("Usage: TestSyncHTTP <url-list>\n");
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
);
64 httpChannel
->SetOpenHasEventQueue(false);
66 // initialize these fields for reading
71 for (i
=0; i
<nc
; ++i
) {
74 rv
= c
[i
].channel
->Open(getter_AddRefs(c
[i
].inputStream
));
75 RETURN_IF_FAILED(rv
, "nsIChannel::OpenInputStream");
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) {
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);
103 NS_ShutdownXPCOM(nullptr);