VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/http.cpp@ 46050

最後變更 在這個檔案從46050是 46050,由 vboxsync 提交於 12 年 前

More symbol work

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 15.2 KB
 
1/* $Id: http.cpp 46050 2013-05-14 08:41:11Z vboxsync $ */
2/** @file
3 * IPRT - HTTP communication API.
4 */
5
6/*
7 * Copyright (C) 2012-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#include <iprt/assert.h>
33#include <iprt/err.h>
34#include <iprt/mem.h>
35#include <iprt/string.h>
36#include <iprt/file.h>
37#include <iprt/stream.h>
38
39#include <curl/curl.h>
40#include <openssl/ssl.h>
41#include "internal/magics.h"
42
43
44/*******************************************************************************
45* Structures and Typedefs *
46*******************************************************************************/
47typedef struct RTHTTPINTERNAL
48{
49 /** magic value */
50 uint32_t u32Magic;
51 /** cURL handle */
52 CURL *pCurl;
53 long lLastResp;
54 /** custom headers */
55 struct curl_slist *pHeaders;
56 /** CA certificate for HTTPS authentication check */
57 char *pcszCAFile;
58 /** abort the current HTTP request if true */
59 bool fAbort;
60} RTHTTPINTERNAL;
61typedef RTHTTPINTERNAL *PRTHTTPINTERNAL;
62
63typedef struct RTHTTPMEMCHUNK
64{
65 char *pszMem;
66 size_t cb;
67} RTHTTPMEMCHUNK;
68typedef RTHTTPMEMCHUNK *PRTHTTPMEMCHUNK;
69
70/*******************************************************************************
71* Defined Constants And Macros *
72*******************************************************************************/
73#define CURL_FAILED(rcCurl) (RT_UNLIKELY(rcCurl != CURLE_OK))
74
75/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
76#define RTHTTP_VALID_RETURN_RC(hHttp, rcCurl) \
77 do { \
78 AssertPtrReturn((hHttp), (rcCurl)); \
79 AssertReturn((hHttp)->u32Magic == RTHTTP_MAGIC, (rcCurl)); \
80 } while (0)
81
82/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
83#define RTHTTP_VALID_RETURN(hHTTP) RTHTTP_VALID_RETURN_RC((hHttp), VERR_INVALID_HANDLE)
84
85/** Validates a handle and returns (void) if not valid. */
86#define RTHTTP_VALID_RETURN_VOID(hHttp) \
87 do { \
88 AssertPtrReturnVoid(hHttp); \
89 AssertReturnVoid((hHttp)->u32Magic == RTHTTP_MAGIC); \
90 } while (0)
91
92
93RTR3DECL(int) RTHttpCreate(PRTHTTP phHttp)
94{
95 AssertPtrReturn(phHttp, VERR_INVALID_PARAMETER);
96
97 CURLcode rcCurl = curl_global_init(CURL_GLOBAL_ALL);
98 if (CURL_FAILED(rcCurl))
99 return VERR_HTTP_INIT_FAILED;
100
101 CURL *pCurl = curl_easy_init();
102 if (!pCurl)
103 return VERR_HTTP_INIT_FAILED;
104
105 PRTHTTPINTERNAL pHttpInt = (PRTHTTPINTERNAL)RTMemAllocZ(sizeof(RTHTTPINTERNAL));
106 if (!pHttpInt)
107 return VERR_NO_MEMORY;
108
109 pHttpInt->u32Magic = RTHTTP_MAGIC;
110 pHttpInt->pCurl = pCurl;
111
112 *phHttp = (RTHTTP)pHttpInt;
113
114 return VINF_SUCCESS;
115}
116
117RTR3DECL(void) RTHttpDestroy(RTHTTP hHttp)
118{
119 if (!hHttp)
120 return;
121
122 PRTHTTPINTERNAL pHttpInt = hHttp;
123 RTHTTP_VALID_RETURN_VOID(pHttpInt);
124
125 pHttpInt->u32Magic = RTHTTP_MAGIC_DEAD;
126
127 curl_easy_cleanup(pHttpInt->pCurl);
128
129 if (pHttpInt->pHeaders)
130 curl_slist_free_all(pHttpInt->pHeaders);
131
132 if (pHttpInt->pcszCAFile)
133 RTStrFree(pHttpInt->pcszCAFile);
134
135 RTMemFree(pHttpInt);
136
137 curl_global_cleanup();
138}
139
140static DECLCALLBACK(size_t) rtHttpWriteData(void *pvBuf, size_t cb, size_t n, void *pvUser)
141{
142 PRTHTTPMEMCHUNK pMem = (PRTHTTPMEMCHUNK)pvUser;
143 size_t cbAll = cb * n;
144
145 pMem->pszMem = (char*)RTMemRealloc(pMem->pszMem, pMem->cb + cbAll + 1);
146 if (pMem->pszMem)
147 {
148 memcpy(&pMem->pszMem[pMem->cb], pvBuf, cbAll);
149 pMem->cb += cbAll;
150 pMem->pszMem[pMem->cb] = '\0';
151 }
152 return cbAll;
153}
154
155static DECLCALLBACK(int) rtHttpProgress(void *pData, double DlTotal, double DlNow,
156 double UlTotal, double UlNow)
157{
158 PRTHTTPINTERNAL pHttpInt = (PRTHTTPINTERNAL)pData;
159 AssertReturn(pHttpInt->u32Magic == RTHTTP_MAGIC, 1);
160
161 return pHttpInt->fAbort ? 1 : 0;
162}
163
164RTR3DECL(int) RTHttpAbort(RTHTTP hHttp)
165{
166 PRTHTTPINTERNAL pHttpInt = hHttp;
167 RTHTTP_VALID_RETURN(pHttpInt);
168
169 pHttpInt->fAbort = true;
170
171 return VINF_SUCCESS;
172}
173
174RTR3DECL(int) RTHttpSetProxy(RTHTTP hHttp, const char *pcszProxy, uint32_t uPort,
175 const char *pcszProxyUser, const char *pcszProxyPwd)
176{
177 PRTHTTPINTERNAL pHttpInt = hHttp;
178 RTHTTP_VALID_RETURN(pHttpInt);
179 AssertPtrReturn(pcszProxy, VERR_INVALID_PARAMETER);
180
181 int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXY, pcszProxy);
182 if (CURL_FAILED(rcCurl))
183 return VERR_INVALID_PARAMETER;
184
185 if (uPort != 0)
186 {
187 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYPORT, (long)uPort);
188 if (CURL_FAILED(rcCurl))
189 return VERR_INVALID_PARAMETER;
190 }
191
192 if (pcszProxyUser && pcszProxyPwd)
193 {
194 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYUSERNAME, pcszProxyUser);
195 if (CURL_FAILED(rcCurl))
196 return VERR_INVALID_PARAMETER;
197
198 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYPASSWORD, pcszProxyPwd);
199 if (CURL_FAILED(rcCurl))
200 return VERR_INVALID_PARAMETER;
201 }
202
203 return VINF_SUCCESS;
204}
205
206RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, size_t cHeaders, const char * const *papszHeaders)
207{
208 PRTHTTPINTERNAL pHttpInt = hHttp;
209 RTHTTP_VALID_RETURN(pHttpInt);
210
211 if (!cHeaders)
212 {
213 if (pHttpInt->pHeaders)
214 curl_slist_free_all(pHttpInt->pHeaders);
215 pHttpInt->pHeaders = 0;
216 return VINF_SUCCESS;
217 }
218
219 struct curl_slist *pHeaders = NULL;
220 for (size_t i = 0; i < cHeaders; i++)
221 pHeaders = curl_slist_append(pHeaders, papszHeaders[i]);
222
223 pHttpInt->pHeaders = pHeaders;
224 int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_HTTPHEADER, pHeaders);
225 if (CURL_FAILED(rcCurl))
226 return VERR_INVALID_PARAMETER;
227
228 return VINF_SUCCESS;
229}
230
231RTR3DECL(int) RTHttpCertDigest(RTHTTP hHttp, char *pcszCert, size_t cbCert,
232 uint8_t **pabSha1, size_t *pcbSha1,
233 uint8_t **pabSha512, size_t *pcbSha512)
234{
235 int rc = VINF_SUCCESS;
236
237 BIO *cert = BIO_new_mem_buf(pcszCert, (int)cbCert);
238 if (cert)
239 {
240 X509 *crt = NULL;
241 if (PEM_read_bio_X509(cert, &crt, NULL, NULL))
242 {
243 unsigned cb;
244 unsigned char md[EVP_MAX_MD_SIZE];
245
246 int rc1 = X509_digest(crt, EVP_sha1(), md, &cb);
247 if (rc1 > 0)
248 {
249 *pabSha1 = (uint8_t*)RTMemAlloc(cb);
250 if (*pabSha1)
251 {
252 memcpy(*pabSha1, md, cb);
253 *pcbSha1 = cb;
254
255 rc1 = X509_digest(crt, EVP_sha512(), md, &cb);
256 if (rc1 > 0)
257 {
258 *pabSha512 = (uint8_t*)RTMemAlloc(cb);
259 if (*pabSha512)
260 {
261 memcpy(*pabSha512, md, cb);
262 *pcbSha512 = cb;
263 }
264 else
265 rc = VERR_NO_MEMORY;
266 }
267 else
268 rc = VERR_HTTP_CACERT_WRONG_FORMAT;
269 }
270 else
271 rc = VERR_NO_MEMORY;
272 }
273 else
274 rc = VERR_HTTP_CACERT_WRONG_FORMAT;
275 X509_free(crt);
276 }
277 else
278 rc = VERR_HTTP_CACERT_WRONG_FORMAT;
279 BIO_free(cert);
280 }
281 else
282 rc = VERR_INTERNAL_ERROR;
283
284 if (RT_FAILURE(rc))
285 {
286 RTMemFree(*pabSha512);
287 RTMemFree(*pabSha1);
288 }
289
290 return rc;
291}
292
293RTR3DECL(int) RTHttpSetCAFile(RTHTTP hHttp, const char *pcszCAFile)
294{
295 PRTHTTPINTERNAL pHttpInt = hHttp;
296 RTHTTP_VALID_RETURN(pHttpInt);
297
298 if (pHttpInt->pcszCAFile)
299 RTStrFree(pHttpInt->pcszCAFile);
300 pHttpInt->pcszCAFile = RTStrDup(pcszCAFile);
301 if (!pHttpInt->pcszCAFile)
302 return VERR_NO_MEMORY;
303
304 return VINF_SUCCESS;
305}
306
307
308/**
309 * Figures out the IPRT status code for a GET.
310 *
311 * @returns IPRT status code.
312 * @param pHttpInt HTTP instance.
313 * @param rcCurl What curl returned.
314 */
315static int rtHttpGetCalcStatus(PRTHTTPINTERNAL pHttpInt, int rcCurl)
316{
317 int rc = VERR_INTERNAL_ERROR;
318 if (rcCurl == CURLE_OK)
319 {
320 curl_easy_getinfo(pHttpInt->pCurl, CURLINFO_RESPONSE_CODE, &pHttpInt->lLastResp);
321 switch (pHttpInt->lLastResp)
322 {
323 case 200:
324 /* OK, request was fulfilled */
325 case 204:
326 /* empty response */
327 rc = VINF_SUCCESS;
328 break;
329 case 400:
330 /* bad request */
331 rc = VERR_HTTP_BAD_REQUEST;
332 break;
333 case 403:
334 /* forbidden, authorization will not help */
335 rc = VERR_HTTP_ACCESS_DENIED;
336 break;
337 case 404:
338 /* URL not found */
339 rc = VERR_HTTP_NOT_FOUND;
340 break;
341 }
342 }
343 else
344 {
345 switch (rcCurl)
346 {
347 case CURLE_URL_MALFORMAT:
348 case CURLE_COULDNT_RESOLVE_HOST:
349 rc = VERR_HTTP_NOT_FOUND;
350 break;
351 case CURLE_COULDNT_CONNECT:
352 rc = VERR_HTTP_COULDNT_CONNECT;
353 break;
354 case CURLE_SSL_CONNECT_ERROR:
355 rc = VERR_HTTP_SSL_CONNECT_ERROR;
356 break;
357 case CURLE_SSL_CACERT:
358 /* The peer certificate cannot be authenticated with the CA certificates
359 * set by RTHttpSetCAFile(). We need other or additional CA certificates. */
360 rc = VERR_HTTP_CACERT_CANNOT_AUTHENTICATE;
361 break;
362 case CURLE_SSL_CACERT_BADFILE:
363 /* CAcert file (see RTHttpSetCAFile()) has wrong format */
364 rc = VERR_HTTP_CACERT_WRONG_FORMAT;
365 break;
366 case CURLE_ABORTED_BY_CALLBACK:
367 /* forcefully aborted */
368 rc = VERR_HTTP_ABORTED;
369 break;
370 default:
371 break;
372 }
373 }
374
375 return rc;
376}
377
378RTR3DECL(int) RTHttpGet(RTHTTP hHttp, const char *pcszUrl, char **ppszResponse)
379{
380 PRTHTTPINTERNAL pHttpInt = hHttp;
381 RTHTTP_VALID_RETURN(pHttpInt);
382
383 pHttpInt->fAbort = false;
384
385 int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_URL, pcszUrl);
386 if (CURL_FAILED(rcCurl))
387 return VERR_INVALID_PARAMETER;
388
389#if 0
390 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_VERBOSE, 1);
391 if (CURL_FAILED(rcCurl))
392 return VERR_INVALID_PARAMETER;
393#endif
394
395 const char *pcszCAFile = "/etc/ssl/certs/ca-certificates.crt";
396 if (pHttpInt->pcszCAFile)
397 pcszCAFile = pHttpInt->pcszCAFile;
398 if (RTFileExists(pcszCAFile))
399 {
400 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, pcszCAFile);
401 if (CURL_FAILED(rcCurl))
402 return VERR_INTERNAL_ERROR;
403 }
404
405 RTHTTPMEMCHUNK chunk = { NULL, 0 };
406 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEFUNCTION, &rtHttpWriteData);
407 if (CURL_FAILED(rcCurl))
408 return VERR_INTERNAL_ERROR;
409 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEDATA, (void*)&chunk);
410 if (CURL_FAILED(rcCurl))
411 return VERR_INTERNAL_ERROR;
412 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSFUNCTION, &rtHttpProgress);
413 if (CURL_FAILED(rcCurl))
414 return VERR_INTERNAL_ERROR;
415 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSDATA, (void*)pHttpInt);
416 if (CURL_FAILED(rcCurl))
417 return VERR_INTERNAL_ERROR;
418 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_NOPROGRESS, (long)0);
419 if (CURL_FAILED(rcCurl))
420 return VERR_INTERNAL_ERROR;
421
422 rcCurl = curl_easy_perform(pHttpInt->pCurl);
423 int rc = rtHttpGetCalcStatus(pHttpInt, rcCurl);
424 *ppszResponse = chunk.pszMem;
425
426 return rc;
427}
428
429
430static size_t rtHttpWriteDataToFile(void *pvBuf, size_t cb, size_t n, void *pvUser)
431{
432 size_t cbAll = cb * n;
433 RTFILE hFile = (RTFILE)(intptr_t)pvUser;
434
435 size_t cbWritten = 0;
436 int rc = RTFileWrite(hFile, pvBuf, cbAll, &cbWritten);
437 if (RT_SUCCESS(rc))
438 return cbWritten;
439 return 0;
440}
441
442
443RTR3DECL(int) RTHttpGetFile(RTHTTP hHttp, const char *pszUrl, const char *pszDstFile)
444{
445 PRTHTTPINTERNAL pHttpInt = hHttp;
446 RTHTTP_VALID_RETURN(pHttpInt);
447
448 /*
449 * Set up the request.
450 */
451 pHttpInt->fAbort = false;
452
453 int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_URL, pszUrl);
454 if (CURL_FAILED(rcCurl))
455 return VERR_INVALID_PARAMETER;
456
457#if 0
458 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_VERBOSE, 1);
459 if (CURL_FAILED(rcCurl))
460 return VERR_INVALID_PARAMETER;
461#endif
462
463 const char *pcszCAFile = "/etc/ssl/certs/ca-certificates.crt";
464 if (pHttpInt->pcszCAFile)
465 pcszCAFile = pHttpInt->pcszCAFile;
466 if (RTFileExists(pcszCAFile))
467 {
468 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, pcszCAFile);
469 if (CURL_FAILED(rcCurl))
470 return VERR_INTERNAL_ERROR;
471 }
472
473 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEFUNCTION, &rtHttpWriteDataToFile);
474 if (CURL_FAILED(rcCurl))
475 return VERR_INTERNAL_ERROR;
476 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSFUNCTION, &rtHttpProgress);
477 if (CURL_FAILED(rcCurl))
478 return VERR_INTERNAL_ERROR;
479 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSDATA, (void*)pHttpInt);
480 if (CURL_FAILED(rcCurl))
481 return VERR_INTERNAL_ERROR;
482 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_NOPROGRESS, (long)0);
483 if (CURL_FAILED(rcCurl))
484 return VERR_INTERNAL_ERROR;
485
486 /*
487 * Open the output file.
488 */
489 RTFILE hFile;
490 int rc = RTFileOpen(&hFile, pszDstFile, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_READWRITE);
491 if (RT_SUCCESS(rc))
492 {
493 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEDATA, (void *)(uintptr_t)hFile);
494 if (!CURL_FAILED(rcCurl))
495 {
496 /*
497 * Perform the request.
498 */
499 rcCurl = curl_easy_perform(pHttpInt->pCurl);
500 rc = rtHttpGetCalcStatus(pHttpInt, rcCurl);
501 }
502 else
503 rc = VERR_INTERNAL_ERROR;
504
505 int rc2 = RTFileClose(hFile);
506 if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
507 rc = rc2;
508 }
509
510 return rc;
511}
512
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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