VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTHttp.cpp@ 83102

Last change on this file since 83102 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: RTHttp.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Utility for retriving URLs.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/http.h>
32
33#include <iprt/assert.h>
34#include <iprt/ctype.h>
35#include <iprt/errcore.h>
36#include <iprt/getopt.h>
37#include <iprt/initterm.h>
38#include <iprt/message.h>
39#include <iprt/path.h>
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <iprt/vfs.h>
43
44
45
46int main(int argc, char **argv)
47{
48 int rc = RTR3InitExe(argc, &argv, 0);
49 if (RT_FAILURE(rc))
50 return RTMsgInitFailure(rc);
51
52 /*
53 * Create a HTTP client instance.
54 */
55 RTHTTP hHttp;
56 rc = RTHttpCreate(&hHttp);
57 if (RT_FAILURE(rc))
58 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpCreate failed: %Rrc", rc);
59 rc = RTHttpSetFollowRedirects(hHttp, 8);
60 if (RT_FAILURE(rc))
61 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpSetFollowRedirects(,8) failed: %Rrc", rc);
62
63 /*
64 * Parse arguments.
65 */
66 static const RTGETOPTDEF s_aOptions[] =
67 {
68 { "--output", 'o', RTGETOPT_REQ_STRING },
69 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
70 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
71 { "--set-header", 's', RTGETOPT_REQ_STRING },
72 };
73
74 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
75 const char *pszOutput = NULL;
76 unsigned uVerbosityLevel = 1;
77
78 RTGETOPTUNION ValueUnion;
79 RTGETOPTSTATE GetState;
80 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
81 while ((rc = RTGetOpt(&GetState, &ValueUnion)))
82 {
83 switch (rc)
84 {
85 case 'o':
86 pszOutput = ValueUnion.psz;
87 break;
88
89 case 'q':
90 uVerbosityLevel--;
91 break;
92 case 'v':
93 uVerbosityLevel++;
94 break;
95
96 case 'h':
97 RTPrintf("Usage: %s [options] URL0 [URL1 [...]]\n"
98 "\n"
99 "Options:\n"
100 " -o,--output=file\n"
101 " Output file. If not given, the file is displayed on stdout.\n"
102 " -q, --quiet\n"
103 " -v, --verbose\n"
104 " Controls the verbosity level.\n"
105 " -h, -?, --help\n"
106 " Display this help text and exit successfully.\n"
107 " -V, --version\n"
108 " Display the revision and exit successfully.\n"
109 , RTPathFilename(argv[0]));
110 return RTEXITCODE_SUCCESS;
111
112 case 'V':
113 RTPrintf("$Revision: 82968 $\n");
114 return RTEXITCODE_SUCCESS;
115
116 case 's':
117 {
118 char *pszColon = (char *)strchr(ValueUnion.psz, ':');
119 if (!pszColon)
120 return RTMsgErrorExit(RTEXITCODE_FAILURE, "No colon in --set-header value: %s", ValueUnion.psz);
121 *pszColon = '\0'; /* evil */
122 const char *pszValue = pszColon + 1;
123 if (RT_C_IS_BLANK(*pszValue))
124 pszValue++;
125 rc = RTHttpAddHeader(hHttp, ValueUnion.psz, pszValue, RTSTR_MAX, RTHTTPADDHDR_F_BACK);
126 *pszColon = ':';
127 if (RT_FAILURE(rc))
128 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpAddHeader failed: %Rrc (on %s)", rc, ValueUnion.psz);
129 break;
130 }
131
132 case VINF_GETOPT_NOT_OPTION:
133 {
134 int rcHttp;
135 if (pszOutput && strcmp("-", pszOutput))
136 {
137 if (uVerbosityLevel > 0)
138 RTStrmPrintf(g_pStdErr, "Fetching '%s' into '%s'...\n", ValueUnion.psz, pszOutput);
139 rcHttp = RTHttpGetFile(hHttp, ValueUnion.psz, pszOutput);
140 }
141 else
142 {
143 if (uVerbosityLevel > 0)
144 RTStrmPrintf(g_pStdErr, "Fetching '%s'...\n", ValueUnion.psz);
145
146 void *pvResp;
147 size_t cbResp;
148 rcHttp = RTHttpGetBinary(hHttp, ValueUnion.psz, &pvResp, &cbResp);
149 if (RT_SUCCESS(rcHttp))
150 {
151 RTVFSIOSTREAM hVfsIos;
152 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT, 0, true /*fLeaveOpen*/, &hVfsIos);
153 if (RT_SUCCESS(rc))
154 {
155 rc = RTVfsIoStrmWrite(hVfsIos, pvResp, cbResp, true /*fBlocking*/, NULL);
156 if (RT_FAILURE(rc))
157 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error writing to stdout: %Rrc", rc);
158 RTVfsIoStrmRelease(hVfsIos);
159 }
160 else
161 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening stdout: %Rrc", rc);
162 RTHttpFreeResponse(pvResp);
163 }
164 }
165 if (RT_FAILURE(rcHttp))
166 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc getting '%s'", rcHttp, ValueUnion.psz);
167 break;
168 }
169
170 default:
171 return RTGetOptPrintError(rc, &ValueUnion);
172 }
173 }
174
175 RTHttpDestroy(hHttp);
176 return rcExit;
177}
178
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette