VirtualBox

source: vbox/trunk/include/iprt/http.h@ 73903

Last change on this file since 73903 was 73899, checked in by vboxsync, 7 years ago

IPRT/http: Prototyped RTHttpPerform function. bugref:9167

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* $Id: http.h 73899 2018-08-26 20:16:55Z vboxsync $ */
2/** @file
3 * IPRT - Simple HTTP/HTTPS Client API.
4 */
5
6/*
7 * Copyright (C) 2012-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#ifndef ___iprt_http_h
28#define ___iprt_http_h
29
30#include <iprt/types.h>
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_http RTHttp - Simple HTTP/HTTPS Client API
35 * @ingroup grp_rt
36 * @{
37 */
38
39/** @todo the following three definitions may move the iprt/types.h later. */
40/** HTTP/HTTPS client handle. */
41typedef R3PTRTYPE(struct RTHTTPINTERNAL *) RTHTTP;
42/** Pointer to a HTTP/HTTPS client handle. */
43typedef RTHTTP *PRTHTTP;
44/** Nil HTTP/HTTPS client handle. */
45#define NIL_RTHTTP ((RTHTTP)0)
46/** Callback function to be called during RTHttpGet*(). Register it using RTHttpSetDownloadProgressCallback(). */
47typedef DECLCALLBACK(void) FNRTHTTPDOWNLDPROGRCALLBACK(RTHTTP hHttp, void *pvUser, uint64_t cbDownloadTotal, uint64_t cbDownloaded);
48typedef FNRTHTTPDOWNLDPROGRCALLBACK *PFNRTHTTPDOWNLDPROGRCALLBACK;
49
50
51
52/**
53 * Creates a HTTP client instance.
54 *
55 * @returns IPRT status code.
56 * @param phHttp Where to store the HTTP handle.
57 */
58RTR3DECL(int) RTHttpCreate(PRTHTTP phHttp);
59
60/**
61 * Resets a HTTP client instance.
62 *
63 * @returns IPRT status code.
64 * @param hHttp Handle to the HTTP interface.
65 */
66RTR3DECL(int) RTHttpReset(RTHTTP hHttp);
67
68/**
69 * Destroys a HTTP client instance.
70 *
71 * @returns IPRT status code.
72 * @param hHttp Handle to the HTTP interface.
73 */
74RTR3DECL(int) RTHttpDestroy(RTHTTP hHttp);
75
76
77/**
78 * Retrieve the redir location for 301 responses.
79 *
80 * @param hHttp Handle to the HTTP interface.
81 * @param ppszRedirLocation Where to store the string. To be freed with
82 * RTStrFree().
83 */
84RTR3DECL(int) RTHttpGetRedirLocation(RTHTTP hHttp, char **ppszRedirLocation);
85
86/**
87 * Perform a simple blocking HTTP GET request.
88 *
89 * This is a just a convenient wrapper around RTHttpGetBinary that returns a
90 * different type and sheds a parameter.
91 *
92 * @returns iprt status code.
93 *
94 * @param hHttp The HTTP client instance.
95 * @param pszUrl URL.
96 * @param ppszNotUtf8 Where to return the pointer to the HTTP response.
97 * The string is of course zero terminated. Use
98 * RTHttpFreeReponseText to free.
99 *
100 * @remarks BIG FAT WARNING!
101 *
102 * This function does not guarantee the that returned string is valid UTF-8 or
103 * any other kind of text encoding!
104 *
105 * The caller must determine and validate the string encoding _before_
106 * passing it along to functions that expect UTF-8!
107 *
108 * Also, this function does not guarantee that the returned string
109 * doesn't have embedded zeros and provides the caller no way of
110 * finding out! If you are worried about the response from the HTTPD
111 * containing embedded zero's, use RTHttpGetBinary instead.
112 */
113RTR3DECL(int) RTHttpGetText(RTHTTP hHttp, const char *pszUrl, char **ppszNotUtf8);
114
115/**
116 * Perform a simple blocking HTTP HEAD request.
117 *
118 * This is a just a convenient wrapper around RTHttpGetBinary that returns a
119 * different type and sheds a parameter.
120 *
121 * @returns iprt status code.
122 *
123 * @param hHttp The HTTP client instance.
124 * @param pszUrl URL.
125 * @param ppszNotUtf8 Where to return the pointer to the HTTP response.
126 * The string is of course zero terminated. Use
127 * RTHttpFreeReponseText to free.
128 *
129 * @remarks BIG FAT WARNING!
130 *
131 * This function does not guarantee the that returned string is valid UTF-8 or
132 * any other kind of text encoding!
133 *
134 * The caller must determine and validate the string encoding _before_
135 * passing it along to functions that expect UTF-8!
136 *
137 * Also, this function does not guarantee that the returned string
138 * doesn't have embedded zeros and provides the caller no way of
139 * finding out! If you are worried about the response from the HTTPD
140 * containing embedded zero's, use RTHttpGetHeaderBinary instead.
141 */
142RTR3DECL(int) RTHttpGetHeaderText(RTHTTP hHttp, const char *pszUrl, char **ppszNotUtf8);
143
144/**
145 * Frees memory returned by RTHttpGetText.
146 *
147 * @param pszNotUtf8 What RTHttpGetText returned.
148 */
149RTR3DECL(void) RTHttpFreeResponseText(char *pszNotUtf8);
150
151/**
152 * Perform a simple blocking HTTP GET request.
153 *
154 * @returns iprt status code.
155 *
156 * @param hHttp The HTTP client instance.
157 * @param pszUrl The URL.
158 * @param ppvResponse Where to store the HTTP response data. Use
159 * RTHttpFreeResponse to free.
160 * @param pcb Size of the returned buffer.
161 */
162RTR3DECL(int) RTHttpGetBinary(RTHTTP hHttp, const char *pszUrl, void **ppvResponse, size_t *pcb);
163
164/**
165 * Perform a simple blocking HTTP HEAD request.
166 *
167 * @returns iprt status code.
168 *
169 * @param hHttp The HTTP client instance.
170 * @param pszUrl The URL.
171 * @param ppvResponse Where to store the HTTP response data. Use
172 * RTHttpFreeResponse to free.
173 * @param pcb Size of the returned buffer.
174 */
175RTR3DECL(int) RTHttpGetHeaderBinary(RTHTTP hHttp, const char *pszUrl, void **ppvResponse, size_t *pcb);
176
177/**
178 * Frees memory returned by RTHttpGetBinary.
179 *
180 * @param pvResponse What RTHttpGetBinary returned.
181 */
182RTR3DECL(void) RTHttpFreeResponse(void *pvResponse);
183
184/**
185 * Perform a simple blocking HTTP request, writing the output to a file.
186 *
187 * @returns iprt status code.
188 *
189 * @param hHttp The HTTP client instance.
190 * @param pszUrl The URL.
191 * @param pszDstFile The destination file name.
192 */
193RTR3DECL(int) RTHttpGetFile(RTHTTP hHttp, const char *pszUrl, const char *pszDstFile);
194
195/** HTTP methods. */
196typedef enum RTHTTPMETHOD
197{
198 RTHTTPMETHOD_INVALID = 0,
199 RTHTTPMETHOD_GET,
200 RTHTTPMETHOD_PUT,
201 RTHTTPMETHOD_POST,
202 RTHTTPMETHOD_PATCH,
203 RTHTTPMETHOD_DELETE,
204 RTHTTPMETHOD_HEAD,
205 RTHTTPMETHOD_OPTIONS,
206 RTHTTPMETHOD_TRACE,
207 RTHTTPMETHOD_END,
208 RTHTTPMETHOD_32BIT_HACK = 0x7fffffff
209} RTHTTPMETHOD;
210
211/**
212 * Performs generic blocking HTTP request, optionally returning the body and headers.
213 *
214 * @returns IPRT status code.
215 * @param hHttp The HTTP client instance.
216 * @param pszUrl The URL.
217 * @param enmMethod The HTTP method for the request.
218 * @param puHttpStatus Where to return the HTTP status code. Optional.
219 * @param ppvHeaders Where to return the headers. Optional.
220 * @param pcbHeaders Where to return the header size.
221 * @param ppvBody Where to return the body. Optional.
222 * @param pcbBody Where to return the body size.
223 */
224RTR3DECL(int) RTHttpPerform(RTHTTP hHttp, const char *pszUrl, RTHTTPMETHOD enmMethod,
225 uint32_t *puHttpStatus, void **ppvHeaders, size_t *pcbHeaders, void **ppvBody, size_t *pcbBody);
226
227
228/**
229 * Abort a pending HTTP request. A blocking RTHttpGet() call will return with
230 * VERR_HTTP_ABORTED. It may take some time (current cURL implementation needs
231 * up to 1 second) before the request is aborted.
232 *
233 * @returns iprt status code.
234 *
235 * @param hHttp The HTTP client instance.
236 */
237RTR3DECL(int) RTHttpAbort(RTHTTP hHttp);
238
239/**
240 * Tells the HTTP interface to use the system proxy configuration.
241 *
242 * @returns iprt status code.
243 * @param hHttp The HTTP client instance.
244 */
245RTR3DECL(int) RTHttpUseSystemProxySettings(RTHTTP hHttp);
246
247/**
248 * Specify proxy settings.
249 *
250 * @returns iprt status code.
251 *
252 * @param hHttp The HTTP client instance.
253 * @param pszProxyUrl URL of the proxy server.
254 * @param uPort port number of the proxy, use 0 for not specifying a port.
255 * @param pszProxyUser Username, pass NULL for no authentication.
256 * @param pszProxyPwd Password, pass NULL for no authentication.
257 *
258 * @todo This API does not allow specifying the type of proxy server... We're
259 * currently assuming it's a HTTP proxy.
260 */
261RTR3DECL(int) RTHttpSetProxy(RTHTTP hHttp, const char *pszProxyUrl, uint32_t uPort,
262 const char *pszProxyUser, const char *pszProxyPwd);
263
264/**
265 * Set follow redirects (3xx)
266 *
267 * @returns iprt status code.
268 *
269 * @param hHttp The HTTP client instance.
270 * @param cMaxRedirects Max number of redirects to follow. Zero if no
271 * redirects should be followed but instead returned
272 * to caller.
273 */
274RTR3DECL(int) RTHttpSetFollowRedirects(RTHTTP hHttp, uint32_t cMaxRedirects);
275
276/**
277 * Set custom raw headers.
278 *
279 * @returns iprt status code.
280 *
281 * @param hHttp The HTTP client instance.
282 * @param cHeaders Number of custom headers.
283 * @param papszHeaders Array of headers in form "foo: bar".
284 */
285RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, size_t cHeaders, const char * const *papszHeaders);
286
287/**
288 * Appends a raw header.
289 *
290 * @returns IPRT status code.
291 * @param hHttp The HTTP client instance.
292 * @param pszHeader Header string on the form "foo: bar".
293 */
294RTR3DECL(int) RTHttpAppendRawHeader(RTHTTP hHttp, const char *pszHeader);
295
296/**
297 * Appends a header field and value.
298 *
299 * @returns IPRT status code.
300 * @param hHttp The HTTP client instance.
301 * @param pszField The header field name.
302 * @param pszValue The header field value.
303 * @param fFlags Flags reserved for controlling encoding, MBZ.
304 */
305RTR3DECL(int) RTHttpAppendHeader(RTHTTP hHttp, const char *pszField, const char *pszValue, uint32_t fFlags);
306
307/**
308 * Tells the HTTP client instance to gather system CA certificates into a
309 * temporary file and use it for HTTPS connections.
310 *
311 * This will be called automatically if a 'https' URL is presented and
312 * RTHttpSetCaFile hasn't been called yet.
313 *
314 * @returns IPRT status code.
315 * @param hHttp The HTTP client instance.
316 * @param pErrInfo Where to store additional error/warning information.
317 * Optional.
318 */
319RTR3DECL(int) RTHttpUseTemporaryCaFile(RTHTTP hHttp, PRTERRINFO pErrInfo);
320
321/**
322 * Set a custom certification authority file, containing root certificates.
323 *
324 * @returns iprt status code.
325 *
326 * @param hHttp The HTTP client instance.
327 * @param pszCAFile File name containing root certificates.
328 *
329 * @remarks For portable HTTPS support, use RTHttpGatherCaCertsInFile and pass
330 */
331RTR3DECL(int) RTHttpSetCAFile(RTHTTP hHttp, const char *pszCAFile);
332
333/**
334 * Gathers certificates into a cryptographic (certificate) store
335 *
336 * This is a just a combination of RTHttpGatherCaCertsInStore and
337 * RTCrStoreCertExportAsPem.
338 *
339 * @returns IPRT status code.
340 * @param hStore The certificate store to gather the certificates
341 * in.
342 * @param fFlags RTHTTPGATHERCACERT_F_XXX.
343 * @param pErrInfo Where to store additional error/warning information.
344 * Optional.
345 */
346RTR3DECL(int) RTHttpGatherCaCertsInStore(RTCRSTORE hStore, uint32_t fFlags, PRTERRINFO pErrInfo);
347
348/**
349 * Gathers certificates into a file that can be used with RTHttpSetCAFile.
350 *
351 * This is a just a combination of RTHttpGatherCaCertsInStore and
352 * RTCrStoreCertExportAsPem.
353 *
354 * @returns IPRT status code.
355 * @param pszCaFile The output file.
356 * @param fFlags RTHTTPGATHERCACERT_F_XXX.
357 * @param pErrInfo Where to store additional error/warning information.
358 * Optional.
359 */
360RTR3DECL(int) RTHttpGatherCaCertsInFile(const char *pszCaFile, uint32_t fFlags, PRTERRINFO pErrInfo);
361
362/**
363 * Set a callback function which is called during RTHttpGet*()
364 *
365 * @returns IPRT status code.
366 * @param hHttp The HTTP client instance.
367 * @param pfnDownloadProgress Progress function to be called. Set it to
368 * NULL to disable the callback.
369 * @param pvUser Convenience pointer for the callback function.
370 */
371RTR3DECL(int) RTHttpSetDownloadProgressCallback(RTHTTP hHttp, PFNRTHTTPDOWNLDPROGRCALLBACK pfnDownloadProgress, void *pvUser);
372
373
374/** @name thin wrappers for setting one or a few related curl options
375 * @remarks Subject to change.
376 * @{ */
377typedef size_t FNRTHTTPREADCALLBACK(void *pbDst, size_t cbItem, size_t cItems, void *pvUser);
378typedef FNRTHTTPREADCALLBACK *PFNRTHTTPREADCALLBACK;
379
380#define RT_HTTP_READCALLBACK_ABORT 0x10000000 /* CURL_READFUNC_ABORT */
381
382RTR3DECL(int) RTHttpSetReadCallback(RTHTTP hHttp, PFNRTHTTPREADCALLBACK pfnRead, void *pvUser);
383
384
385typedef size_t FNRTHTTPWRITECALLBACK(char *pbSrc, size_t cbItem, size_t cItems, void *pvUser);
386typedef FNRTHTTPWRITECALLBACK *PFNRTHTTPWRITECALLBACK;
387
388RTR3DECL(int) RTHttpSetWriteCallback(RTHTTP hHttp, PFNRTHTTPWRITECALLBACK pfnWrite, void *pvUser);
389RTR3DECL(int) RTHttpSetWriteHeaderCallback(RTHTTP hHttp, PFNRTHTTPWRITECALLBACK pfnWrite, void *pvUser);
390
391
392RTR3DECL(int) RTHttpRawSetUrl(RTHTTP hHttp, const char *pszUrl);
393
394RTR3DECL(int) RTHttpRawSetGet(RTHTTP hHttp);
395RTR3DECL(int) RTHttpRawSetHead(RTHTTP hHttp);
396RTR3DECL(int) RTHttpRawSetPost(RTHTTP hHttp);
397RTR3DECL(int) RTHttpRawSetPut(RTHTTP hHttp);
398RTR3DECL(int) RTHttpRawSetDelete(RTHTTP hHttp);
399RTR3DECL(int) RTHttpRawSetCustomRequest(RTHTTP hHttp, const char *pszVerb);
400
401RTR3DECL(int) RTHttpRawSetPostFields(RTHTTP hHttp, const void *pv, size_t cb);
402RTR3DECL(int) RTHttpRawSetInfileSize(RTHTTP hHttp, RTFOFF cb);
403
404RTR3DECL(int) RTHttpRawSetVerbose(RTHTTP hHttp, bool fValue);
405RTR3DECL(int) RTHttpRawSetTimeout(RTHTTP hHttp, long sec);
406
407RTR3DECL(int) RTHttpRawPerform(RTHTTP hHttp);
408
409RTR3DECL(int) RTHttpRawGetResponseCode(RTHTTP hHttp, long *plCode);
410/** @} */
411
412/** @} */
413
414RT_C_DECLS_END
415
416#endif
417
Note: See TracBrowser for help on using the repository browser.

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