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 "TestCommon.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsIURI.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsIHttpChannel.h" michael@0: #include "nsIInputStream.h" michael@0: #include "nsNetUtil.h" michael@0: #include "mozilla/unused.h" michael@0: michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: michael@0: /* michael@0: * Test synchronous Open. 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", static_cast(rv)); \ michael@0: return -1; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: char buf[256]; michael@0: michael@0: if (argc != 3) { michael@0: printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n"); michael@0: return -1; michael@0: } michael@0: michael@0: nsCOMPtr uri; michael@0: nsCOMPtr stream; michael@0: michael@0: rv = NS_NewURI(getter_AddRefs(uri), argv[1]); michael@0: RETURN_IF_FAILED(rv, "NS_NewURI"); michael@0: michael@0: rv = NS_OpenURI(getter_AddRefs(stream), uri); michael@0: RETURN_IF_FAILED(rv, "NS_OpenURI"); michael@0: michael@0: FILE* outfile = fopen(argv[2], "wb"); michael@0: if (!outfile) { michael@0: printf("error opening %s\n", argv[2]); michael@0: return 1; michael@0: } michael@0: michael@0: uint32_t read; michael@0: while (NS_SUCCEEDED(stream->Read(buf, sizeof(buf), &read)) && read) { michael@0: unused << fwrite(buf, 1, read, outfile); michael@0: } michael@0: printf("Done\n"); michael@0: michael@0: fclose(outfile); michael@0: michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }