Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / test / TestSyncHTTP.cpp
blob8a6f8dfffa20dee9ee47156d6f737ac0e5e7b851
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 "nsContentUtils.h"
13 #include <nsNetUtil.h>
16 * Test synchronous HTTP.
19 #define RETURN_IF_FAILED(rv, what) \
20 PR_BEGIN_MACRO \
21 if (NS_FAILED(rv)) { \
22 printf(what ": failed - %08x\n", rv); \
23 return -1; \
24 } \
25 PR_END_MACRO
27 struct TestContext {
28 nsCOMPtr<nsIURI> uri;
29 nsCOMPtr<nsIChannel> channel;
30 nsCOMPtr<nsIInputStream> inputStream;
31 PRTime t1, t2;
32 uint32_t bytesRead, totalRead;
34 TestContext()
35 : t1(0), t2(0), bytesRead(0), totalRead(0)
36 { printf("TestContext [this=%p]\n", (void*)this); }
37 ~TestContext()
38 { printf("~TestContext [this=%p]\n", (void*)this); }
41 int
42 main(int argc, char **argv)
44 nsresult rv;
45 TestContext *c;
46 int i, nc=0, npending=0;
47 char buf[256];
49 if (argc < 2) {
50 printf("Usage: TestSyncHTTP <url-list>\n");
51 return -1;
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,
61 c[i].uri,
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);
69 if (httpChannel)
70 httpChannel->SetOpenHasEventQueue(false);
72 // initialize these fields for reading
73 c[i].bytesRead = 1;
74 c[i].totalRead = 0;
77 for (i=0; i<nc; ++i) {
78 c[i].t1 = PR_Now();
80 rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream));
81 RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream");
84 npending = nc;
85 while (npending) {
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) {
96 c[i].t2 = PR_Now();
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);
101 npending--;
107 delete[] c;
109 NS_ShutdownXPCOM(nullptr);
110 return 0;