VirtualBox

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

Last change on this file since 73531 was 73334, checked in by vboxsync, 6 years ago

IPRT: Added RTHttpSetFollowRedirects (translates to CURLOPT_FOLLOWLOCATION+CURLOPT_MAXREDIRS) and make use of it RTDbgCfg and RTHttp[.exe]. This should fix the current pdb download issues as we're getting 302 redirects from the servers. Also, changed RTHttp to return VERR_HTTP_REDIRECTED when receiving 302, 303, 307 & 308 HTTP statuses.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: RTHttp.cpp 73334 2018-07-23 16:52:04Z vboxsync $ */
2/** @file
3 * IPRT - Utility for retriving URLs.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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/err.h>
35#include <iprt/getopt.h>
36#include <iprt/initterm.h>
37#include <iprt/message.h>
38#include <iprt/path.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41#include <iprt/vfs.h>
42
43
44
45int main(int argc, char **argv)
46{
47 int rc = RTR3InitExe(argc, &argv, 0);
48 if (RT_FAILURE(rc))
49 return RTMsgInitFailure(rc);
50
51 /*
52 * Create a HTTP client instance.
53 */
54 RTHTTP hHttp;
55 rc = RTHttpCreate(&hHttp);
56 if (RT_FAILURE(rc))
57 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpCreate failed: %Rrc", rc);
58 rc = RTHttpSetFollowRedirects(hHttp, 8);
59 if (RT_FAILURE(rc))
60 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTHttpSetFollowRedirects(,8) failed: %Rrc", rc);
61
62 /*
63 * Parse arguments.
64 */
65 static const RTGETOPTDEF s_aOptions[] =
66 {
67 { "--output", 'o', RTGETOPT_REQ_STRING },
68 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
69 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
70 };
71
72 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
73 const char *pszOutput = NULL;
74 unsigned uVerbosityLevel = 1;
75
76 RTGETOPTUNION ValueUnion;
77 RTGETOPTSTATE GetState;
78 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
79 while ((rc = RTGetOpt(&GetState, &ValueUnion)))
80 {
81 switch (rc)
82 {
83 case 'o':
84 pszOutput = ValueUnion.psz;
85 break;
86
87 case 'q':
88 uVerbosityLevel--;
89 break;
90 case 'v':
91 uVerbosityLevel++;
92 break;
93
94 case 'h':
95 RTPrintf("Usage: %s [options] URL0 [URL1 [...]]\n"
96 "\n"
97 "Options:\n"
98 " -o,--output=file\n"
99 " Output file. If not given, the file is displayed on stdout.\n"
100 " -q, --quiet\n"
101 " -v, --verbose\n"
102 " Controls the verbosity level.\n"
103 " -h, -?, --help\n"
104 " Display this help text and exit successfully.\n"
105 " -V, --version\n"
106 " Display the revision and exit successfully.\n"
107 , RTPathFilename(argv[0]));
108 return RTEXITCODE_SUCCESS;
109
110 case 'V':
111 RTPrintf("$Revision: 73334 $\n");
112 return RTEXITCODE_SUCCESS;
113
114 case VINF_GETOPT_NOT_OPTION:
115 {
116 int rcHttp;
117 if (pszOutput && strcmp("-", pszOutput))
118 {
119 if (uVerbosityLevel > 0)
120 RTStrmPrintf(g_pStdErr, "Fetching '%s' into '%s'...\n", ValueUnion.psz, pszOutput);
121 rcHttp = RTHttpGetFile(hHttp, ValueUnion.psz, pszOutput);
122 }
123 else
124 {
125 if (uVerbosityLevel > 0)
126 RTStrmPrintf(g_pStdErr, "Fetching '%s'...\n", ValueUnion.psz);
127
128 void *pvResp;
129 size_t cbResp;
130 rcHttp = RTHttpGetBinary(hHttp, ValueUnion.psz, &pvResp, &cbResp);
131 if (RT_SUCCESS(rcHttp))
132 {
133 RTVFSIOSTREAM hVfsIos;
134 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT, 0, true /*fLeaveOpen*/, &hVfsIos);
135 if (RT_SUCCESS(rc))
136 {
137 rc = RTVfsIoStrmWrite(hVfsIos, pvResp, cbResp, true /*fBlocking*/, NULL);
138 if (RT_FAILURE(rc))
139 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error writing to stdout: %Rrc", rc);
140 RTVfsIoStrmRelease(hVfsIos);
141 }
142 else
143 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening stdout: %Rrc", rc);
144 RTHttpFreeResponse(pvResp);
145 }
146 }
147 if (RT_FAILURE(rcHttp))
148 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Error %Rrc getting '%s'", rcHttp, ValueUnion.psz);
149 break;
150 }
151
152 default:
153 return RTGetOptPrintError(rc, &ValueUnion);
154 }
155 }
156
157 return rcExit;
158}
159
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