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 "nsContentUtils.h"
13 #include <nsNetUtil.h>
16 * Test synchronous HTTP.
19 #define RETURN_IF_FAILED(rv, what) \
21 if (NS_FAILED(rv)) { \
22 printf(what ": failed - %08x\n", rv); \
29 nsCOMPtr
<nsIChannel
> channel
;
30 nsCOMPtr
<nsIInputStream
> inputStream
;
32 uint32_t bytesRead
, totalRead
;
35 : t1(0), t2(0), bytesRead(0), totalRead(0)
36 { printf("TestContext [this=%p]\n", (void*)this); }
38 { printf("~TestContext [this=%p]\n", (void*)this); }
42 main(int argc
, char **argv
)
46 int i
, nc
=0, npending
=0;
50 printf("Usage: TestSyncHTTP <url-list>\n");
54 c
= new TestContext
[argc
-1];
56 for (i
=0; i
<(argc
-1); ++i
, ++nc
) {
57 rv
= NS_NewURI(getter_AddRefs(c
[i
].uri
), argv
[i
+1]);
58 RETURN_IF_FAILED(rv
, "NS_NewURI");
60 rv
= NS_OpenURI(getter_AddRefs(c
[i
].channel
,
62 nsContentUtils::GetSystemPrincipal(),
63 nsILoadInfo::SEC_NORMAL
,
64 nsIContentPolicy::TYPE_OTHER
);
66 RETURN_IF_FAILED(rv
, "NS_OpenURI");
68 nsCOMPtr
<nsIHTTPChannel
> httpChannel
= do_QueryInterface(c
[i
].channel
);
70 httpChannel
->SetOpenHasEventQueue(false);
72 // initialize these fields for reading
77 for (i
=0; i
<nc
; ++i
) {
80 rv
= c
[i
].channel
->Open(getter_AddRefs(c
[i
].inputStream
));
81 RETURN_IF_FAILED(rv
, "nsIChannel::OpenInputStream");
86 for (i
=0; i
<nc
; ++i
) {
88 // read the response content...
90 if (c
[i
].bytesRead
> 0) {
91 rv
= c
[i
].inputStream
->Read(buf
, sizeof buf
, &c
[i
].bytesRead
);
92 RETURN_IF_FAILED(rv
, "nsIInputStream::Read");
93 c
[i
].totalRead
+= c
[i
].bytesRead
;
95 if (c
[i
].bytesRead
== 0) {
97 printf("finished GET of: %s\n", argv
[i
+1]);
98 printf("total read: %u bytes\n", c
[i
].totalRead
);
99 printf("total read time: %0.3f\n",
100 ((double) (c
[i
].t2
- c
[i
].t1
))/1000000.0);
109 NS_ShutdownXPCOM(nullptr);