1 | /* $Id: http.cpp 45632 2013-04-19 08:38:55Z 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.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 | #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 | *******************************************************************************/
|
---|
47 | typedef 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;
|
---|
61 | typedef RTHTTPINTERNAL *PRTHTTPINTERNAL;
|
---|
62 |
|
---|
63 | typedef struct RTHTTPMEMCHUNK
|
---|
64 | {
|
---|
65 | char *pszMem;
|
---|
66 | size_t cb;
|
---|
67 | } RTHTTPMEMCHUNK;
|
---|
68 | typedef 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 |
|
---|
93 | RTR3DECL(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 |
|
---|
117 | RTR3DECL(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 |
|
---|
140 | static 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 |
|
---|
155 | static 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 |
|
---|
164 | RTR3DECL(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 |
|
---|
174 | RTR3DECL(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 |
|
---|
206 | RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, uint32_t cHeaders, const char *pcszHeaders[])
|
---|
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 (unsigned i = 0; i < cHeaders; i++)
|
---|
221 | pHeaders = curl_slist_append(pHeaders, pcszHeaders[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 |
|
---|
231 | RTR3DECL(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 |
|
---|
293 | RTR3DECL(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 | RTR3DECL(int) RTHttpGet(RTHTTP hHttp, const char *pcszUrl, char **ppszResponse)
|
---|
308 | {
|
---|
309 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
310 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
311 |
|
---|
312 | pHttpInt->fAbort = false;
|
---|
313 |
|
---|
314 | int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_URL, pcszUrl);
|
---|
315 | if (CURL_FAILED(rcCurl))
|
---|
316 | return VERR_INVALID_PARAMETER;
|
---|
317 |
|
---|
318 | #if 0
|
---|
319 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_VERBOSE, 1);
|
---|
320 | if (CURL_FAILED(rcCurl))
|
---|
321 | return VERR_INVALID_PARAMETER;
|
---|
322 | #endif
|
---|
323 |
|
---|
324 | const char *pcszCAFile = "/etc/ssl/certs/ca-certificates.crt";
|
---|
325 | if (pHttpInt->pcszCAFile)
|
---|
326 | pcszCAFile = pHttpInt->pcszCAFile;
|
---|
327 | if (RTFileExists(pcszCAFile))
|
---|
328 | {
|
---|
329 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, pcszCAFile);
|
---|
330 | if (CURL_FAILED(rcCurl))
|
---|
331 | return VERR_INTERNAL_ERROR;
|
---|
332 | }
|
---|
333 |
|
---|
334 | RTHTTPMEMCHUNK chunk = { NULL, 0 };
|
---|
335 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEFUNCTION, &rtHttpWriteData);
|
---|
336 | if (CURL_FAILED(rcCurl))
|
---|
337 | return VERR_INTERNAL_ERROR;
|
---|
338 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEDATA, (void*)&chunk);
|
---|
339 | if (CURL_FAILED(rcCurl))
|
---|
340 | return VERR_INTERNAL_ERROR;
|
---|
341 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSFUNCTION, &rtHttpProgress);
|
---|
342 | if (CURL_FAILED(rcCurl))
|
---|
343 | return VERR_INTERNAL_ERROR;
|
---|
344 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSDATA, (void*)pHttpInt);
|
---|
345 | if (CURL_FAILED(rcCurl))
|
---|
346 | return VERR_INTERNAL_ERROR;
|
---|
347 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_NOPROGRESS, (long)0);
|
---|
348 | if (CURL_FAILED(rcCurl))
|
---|
349 | return VERR_INTERNAL_ERROR;
|
---|
350 |
|
---|
351 | rcCurl = curl_easy_perform(pHttpInt->pCurl);
|
---|
352 | int rc = VERR_INTERNAL_ERROR;
|
---|
353 | if (rcCurl == CURLE_OK)
|
---|
354 | {
|
---|
355 | curl_easy_getinfo(pHttpInt->pCurl, CURLINFO_RESPONSE_CODE, &pHttpInt->lLastResp);
|
---|
356 | switch (pHttpInt->lLastResp)
|
---|
357 | {
|
---|
358 | case 200:
|
---|
359 | /* OK, request was fulfilled */
|
---|
360 | case 204:
|
---|
361 | /* empty response */
|
---|
362 | rc = VINF_SUCCESS;
|
---|
363 | break;
|
---|
364 | case 400:
|
---|
365 | /* bad request */
|
---|
366 | rc = VERR_HTTP_BAD_REQUEST;
|
---|
367 | break;
|
---|
368 | case 403:
|
---|
369 | /* forbidden, authorization will not help */
|
---|
370 | rc = VERR_HTTP_ACCESS_DENIED;
|
---|
371 | break;
|
---|
372 | case 404:
|
---|
373 | /* URL not found */
|
---|
374 | rc = VERR_HTTP_NOT_FOUND;
|
---|
375 | break;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | else
|
---|
379 | {
|
---|
380 | switch (rcCurl)
|
---|
381 | {
|
---|
382 | case CURLE_URL_MALFORMAT:
|
---|
383 | case CURLE_COULDNT_RESOLVE_HOST:
|
---|
384 | rc = VERR_HTTP_NOT_FOUND;
|
---|
385 | break;
|
---|
386 | case CURLE_COULDNT_CONNECT:
|
---|
387 | rc = VERR_HTTP_COULDNT_CONNECT;
|
---|
388 | break;
|
---|
389 | case CURLE_SSL_CONNECT_ERROR:
|
---|
390 | rc = VERR_HTTP_SSL_CONNECT_ERROR;
|
---|
391 | break;
|
---|
392 | case CURLE_SSL_CACERT:
|
---|
393 | /* The peer certificate cannot be authenticated with the CA certificates
|
---|
394 | * set by RTHttpSetCAFile(). We need other or additional CA certificates. */
|
---|
395 | rc = VERR_HTTP_CACERT_CANNOT_AUTHENTICATE;
|
---|
396 | break;
|
---|
397 | case CURLE_SSL_CACERT_BADFILE:
|
---|
398 | /* CAcert file (see RTHttpSetCAFile()) has wrong format */
|
---|
399 | rc = VERR_HTTP_CACERT_WRONG_FORMAT;
|
---|
400 | break;
|
---|
401 | case CURLE_ABORTED_BY_CALLBACK:
|
---|
402 | /* forcefully aborted */
|
---|
403 | rc = VERR_HTTP_ABORTED;
|
---|
404 | break;
|
---|
405 | default:
|
---|
406 | break;
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | *ppszResponse = chunk.pszMem;
|
---|
411 |
|
---|
412 | return rc;
|
---|
413 | }
|
---|