VirtualBox

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

Last change on this file since 103005 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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