michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: * Test synchronous HTTP. michael@0: */ michael@0: michael@0: #define RETURN_IF_FAILED(rv, what) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (NS_FAILED(rv)) { \ michael@0: printf(what ": failed - %08x\n", rv); \ michael@0: return -1; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: struct TestContext { michael@0: nsCOMPtr uri; michael@0: nsCOMPtr channel; michael@0: nsCOMPtr inputStream; michael@0: PRTime t1, t2; michael@0: uint32_t bytesRead, totalRead; michael@0: michael@0: TestContext() michael@0: : t1(0), t2(0), bytesRead(0), totalRead(0) michael@0: { printf("TestContext [this=%p]\n", (void*)this); } michael@0: ~TestContext() michael@0: { printf("~TestContext [this=%p]\n", (void*)this); } michael@0: }; michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: nsresult rv; michael@0: TestContext *c; michael@0: int i, nc=0, npending=0; michael@0: char buf[256]; michael@0: michael@0: if (argc < 2) { michael@0: printf("Usage: TestSyncHTTP \n"); michael@0: return -1; michael@0: } michael@0: michael@0: c = new TestContext[argc-1]; michael@0: michael@0: for (i=0; i<(argc-1); ++i, ++nc) { michael@0: rv = NS_NewURI(getter_AddRefs(c[i].uri), argv[i+1]); michael@0: RETURN_IF_FAILED(rv, "NS_NewURI"); michael@0: michael@0: rv = NS_OpenURI(getter_AddRefs(c[i].channel), c[i].uri, nullptr, nullptr); michael@0: RETURN_IF_FAILED(rv, "NS_OpenURI"); michael@0: michael@0: nsCOMPtr httpChannel = do_QueryInterface(c[i].channel); michael@0: if (httpChannel) michael@0: httpChannel->SetOpenHasEventQueue(false); michael@0: michael@0: // initialize these fields for reading michael@0: c[i].bytesRead = 1; michael@0: c[i].totalRead = 0; michael@0: } michael@0: michael@0: for (i=0; iOpen(getter_AddRefs(c[i].inputStream)); michael@0: RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream"); michael@0: } michael@0: michael@0: npending = nc; michael@0: while (npending) { michael@0: for (i=0; i 0) { michael@0: rv = c[i].inputStream->Read(buf, sizeof buf, &c[i].bytesRead); michael@0: RETURN_IF_FAILED(rv, "nsIInputStream::Read"); michael@0: c[i].totalRead += c[i].bytesRead; michael@0: michael@0: if (c[i].bytesRead == 0) { michael@0: c[i].t2 = PR_Now(); michael@0: printf("finished GET of: %s\n", argv[i+1]); michael@0: printf("total read: %u bytes\n", c[i].totalRead); michael@0: printf("total read time: %0.3f\n", michael@0: ((double) (c[i].t2 - c[i].t1))/1000000.0); michael@0: npending--; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: delete[] c; michael@0: michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }