|
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/. */ |
|
5 |
|
6 #include "TestCommon.h" |
|
7 #include "nsCOMPtr.h" |
|
8 #include "nsStringAPI.h" |
|
9 #include "nsIURI.h" |
|
10 #include "nsIChannel.h" |
|
11 #include "nsIHttpChannel.h" |
|
12 #include "nsIInputStream.h" |
|
13 #include "nsNetUtil.h" |
|
14 #include "mozilla/unused.h" |
|
15 |
|
16 #include <stdio.h> |
|
17 |
|
18 using namespace mozilla; |
|
19 |
|
20 /* |
|
21 * Test synchronous Open. |
|
22 */ |
|
23 |
|
24 #define RETURN_IF_FAILED(rv, what) \ |
|
25 PR_BEGIN_MACRO \ |
|
26 if (NS_FAILED(rv)) { \ |
|
27 printf(what ": failed - %08x\n", static_cast<uint32_t>(rv)); \ |
|
28 return -1; \ |
|
29 } \ |
|
30 PR_END_MACRO |
|
31 |
|
32 int |
|
33 main(int argc, char **argv) |
|
34 { |
|
35 if (test_common_init(&argc, &argv) != 0) |
|
36 return -1; |
|
37 |
|
38 nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
|
39 if (NS_FAILED(rv)) return -1; |
|
40 |
|
41 char buf[256]; |
|
42 |
|
43 if (argc != 3) { |
|
44 printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n"); |
|
45 return -1; |
|
46 } |
|
47 |
|
48 nsCOMPtr<nsIURI> uri; |
|
49 nsCOMPtr<nsIInputStream> stream; |
|
50 |
|
51 rv = NS_NewURI(getter_AddRefs(uri), argv[1]); |
|
52 RETURN_IF_FAILED(rv, "NS_NewURI"); |
|
53 |
|
54 rv = NS_OpenURI(getter_AddRefs(stream), uri); |
|
55 RETURN_IF_FAILED(rv, "NS_OpenURI"); |
|
56 |
|
57 FILE* outfile = fopen(argv[2], "wb"); |
|
58 if (!outfile) { |
|
59 printf("error opening %s\n", argv[2]); |
|
60 return 1; |
|
61 } |
|
62 |
|
63 uint32_t read; |
|
64 while (NS_SUCCEEDED(stream->Read(buf, sizeof(buf), &read)) && read) { |
|
65 unused << fwrite(buf, 1, read, outfile); |
|
66 } |
|
67 printf("Done\n"); |
|
68 |
|
69 fclose(outfile); |
|
70 |
|
71 NS_ShutdownXPCOM(nullptr); |
|
72 return 0; |
|
73 } |