1 | /* $Id: http.cpp 45454 2013-04-10 12:16:33Z 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 | const 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 | RTMemFree(pHttpInt);
|
---|
133 |
|
---|
134 | curl_global_cleanup();
|
---|
135 | }
|
---|
136 |
|
---|
137 | static DECLCALLBACK(size_t) rtHttpWriteData(void *pvBuf, size_t cb, size_t n, void *pvUser)
|
---|
138 | {
|
---|
139 | PRTHTTPMEMCHUNK pMem = (PRTHTTPMEMCHUNK)pvUser;
|
---|
140 | size_t cbAll = cb * n;
|
---|
141 |
|
---|
142 | pMem->pszMem = (char*)RTMemRealloc(pMem->pszMem, pMem->cb + cbAll + 1);
|
---|
143 | if (pMem->pszMem)
|
---|
144 | {
|
---|
145 | memcpy(&pMem->pszMem[pMem->cb], pvBuf, cbAll);
|
---|
146 | pMem->cb += cbAll;
|
---|
147 | pMem->pszMem[pMem->cb] = '\0';
|
---|
148 | }
|
---|
149 | return cbAll;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static DECLCALLBACK(int) rtHttpProgress(void *pData, double DlTotal, double DlNow,
|
---|
153 | double UlTotal, double UlNow)
|
---|
154 | {
|
---|
155 | PRTHTTPINTERNAL pHttpInt = (PRTHTTPINTERNAL)pData;
|
---|
156 | AssertReturn(pHttpInt->u32Magic == RTHTTP_MAGIC, 1);
|
---|
157 |
|
---|
158 | return pHttpInt->fAbort ? 1 : 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 | RTR3DECL(int) RTHttpAbort(RTHTTP hHttp)
|
---|
162 | {
|
---|
163 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
164 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
165 |
|
---|
166 | pHttpInt->fAbort = true;
|
---|
167 |
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | RTR3DECL(int) RTHttpSetProxy(RTHTTP hHttp, const char *pcszProxy, uint32_t uPort,
|
---|
172 | const char *pcszProxyUser, const char *pcszProxyPwd)
|
---|
173 | {
|
---|
174 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
175 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
176 | AssertPtrReturn(pcszProxy, VERR_INVALID_PARAMETER);
|
---|
177 |
|
---|
178 | int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXY, pcszProxy);
|
---|
179 | if (CURL_FAILED(rcCurl))
|
---|
180 | return VERR_INVALID_PARAMETER;
|
---|
181 |
|
---|
182 | if (uPort != 0)
|
---|
183 | {
|
---|
184 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYPORT, (long)uPort);
|
---|
185 | if (CURL_FAILED(rcCurl))
|
---|
186 | return VERR_INVALID_PARAMETER;
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (pcszProxyUser && pcszProxyPwd)
|
---|
190 | {
|
---|
191 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYUSERNAME, pcszProxyUser);
|
---|
192 | if (CURL_FAILED(rcCurl))
|
---|
193 | return VERR_INVALID_PARAMETER;
|
---|
194 |
|
---|
195 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROXYPASSWORD, pcszProxyPwd);
|
---|
196 | if (CURL_FAILED(rcCurl))
|
---|
197 | return VERR_INVALID_PARAMETER;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 | RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, uint32_t cHeaders, const char *pcszHeaders[])
|
---|
204 | {
|
---|
205 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
206 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
207 |
|
---|
208 | if (!cHeaders)
|
---|
209 | {
|
---|
210 | if (pHttpInt->pHeaders)
|
---|
211 | curl_slist_free_all(pHttpInt->pHeaders);
|
---|
212 | pHttpInt->pHeaders = 0;
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 | struct curl_slist *pHeaders = NULL;
|
---|
217 | for (unsigned i = 0; i < cHeaders; i++)
|
---|
218 | pHeaders = curl_slist_append(pHeaders, pcszHeaders[i]);
|
---|
219 |
|
---|
220 | pHttpInt->pHeaders = pHeaders;
|
---|
221 | int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_HTTPHEADER, pHeaders);
|
---|
222 | if (CURL_FAILED(rcCurl))
|
---|
223 | return VERR_INVALID_PARAMETER;
|
---|
224 |
|
---|
225 | return VINF_SUCCESS;
|
---|
226 | }
|
---|
227 |
|
---|
228 | RTR3DECL(int) RTHttpCertDigest(RTHTTP hHttp, char *pcszCert, size_t cbCert,
|
---|
229 | uint8_t **pabSha1, size_t *pcbSha1,
|
---|
230 | uint8_t **pabSha512, size_t *pcbSha512)
|
---|
231 | {
|
---|
232 | int rc = VINF_SUCCESS;
|
---|
233 |
|
---|
234 | BIO *cert = BIO_new_mem_buf(pcszCert, (int)cbCert);
|
---|
235 | if (cert)
|
---|
236 | {
|
---|
237 | X509 *crt = NULL;
|
---|
238 | if (PEM_read_bio_X509(cert, &crt, NULL, NULL))
|
---|
239 | {
|
---|
240 | unsigned cb;
|
---|
241 | unsigned char md[EVP_MAX_MD_SIZE];
|
---|
242 |
|
---|
243 | int rc1 = X509_digest(crt, EVP_sha1(), md, &cb);
|
---|
244 | if (rc1 > 0)
|
---|
245 | {
|
---|
246 | *pabSha1 = (uint8_t*)RTMemAlloc(cb);
|
---|
247 | if (*pabSha1)
|
---|
248 | {
|
---|
249 | memcpy(*pabSha1, md, cb);
|
---|
250 | *pcbSha1 = cb;
|
---|
251 |
|
---|
252 | rc1 = X509_digest(crt, EVP_sha512(), md, &cb);
|
---|
253 | if (rc1 > 0)
|
---|
254 | {
|
---|
255 | *pabSha512 = (uint8_t*)RTMemAlloc(cb);
|
---|
256 | if (*pabSha512)
|
---|
257 | {
|
---|
258 | memcpy(*pabSha512, md, cb);
|
---|
259 | *pcbSha512 = cb;
|
---|
260 | }
|
---|
261 | else
|
---|
262 | rc = VERR_NO_MEMORY;
|
---|
263 | }
|
---|
264 | else
|
---|
265 | rc = VERR_HTTP_CACERT_WRONG_FORMAT;
|
---|
266 | }
|
---|
267 | else
|
---|
268 | rc = VERR_NO_MEMORY;
|
---|
269 | }
|
---|
270 | else
|
---|
271 | rc = VERR_HTTP_CACERT_WRONG_FORMAT;
|
---|
272 | X509_free(crt);
|
---|
273 | }
|
---|
274 | else
|
---|
275 | rc = VERR_HTTP_CACERT_WRONG_FORMAT;
|
---|
276 | BIO_free(cert);
|
---|
277 | }
|
---|
278 | else
|
---|
279 | rc = VERR_INTERNAL_ERROR;
|
---|
280 |
|
---|
281 | if (RT_FAILURE(rc))
|
---|
282 | {
|
---|
283 | RTMemFree(*pabSha512);
|
---|
284 | RTMemFree(*pabSha1);
|
---|
285 | }
|
---|
286 |
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 | RTR3DECL(int) RTHttpSetCAFile(RTHTTP hHttp, const char *pcszCAFile)
|
---|
291 | {
|
---|
292 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
293 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
294 |
|
---|
295 | pHttpInt->pcszCAFile = pcszCAFile;
|
---|
296 |
|
---|
297 | return VINF_SUCCESS;
|
---|
298 | }
|
---|
299 |
|
---|
300 | RTR3DECL(int) RTHttpGet(RTHTTP hHttp, const char *pcszUrl, char **ppszResponse)
|
---|
301 | {
|
---|
302 | PRTHTTPINTERNAL pHttpInt = hHttp;
|
---|
303 | RTHTTP_VALID_RETURN(pHttpInt);
|
---|
304 |
|
---|
305 | pHttpInt->fAbort = false;
|
---|
306 |
|
---|
307 | int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_URL, pcszUrl);
|
---|
308 | if (CURL_FAILED(rcCurl))
|
---|
309 | return VERR_INVALID_PARAMETER;
|
---|
310 |
|
---|
311 | #if 0
|
---|
312 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_VERBOSE, 1);
|
---|
313 | if (CURL_FAILED(rcCurl))
|
---|
314 | return VERR_INVALID_PARAMETER;
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | const char *pcszCAFile = "/etc/ssl/certs/ca-certificates.crt";
|
---|
318 | if (pHttpInt->pcszCAFile)
|
---|
319 | pcszCAFile = pHttpInt->pcszCAFile;
|
---|
320 | if (RTFileExists(pcszCAFile))
|
---|
321 | {
|
---|
322 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, pcszCAFile);
|
---|
323 | if (CURL_FAILED(rcCurl))
|
---|
324 | return VERR_INTERNAL_ERROR;
|
---|
325 | }
|
---|
326 |
|
---|
327 | RTHTTPMEMCHUNK chunk = { NULL, 0 };
|
---|
328 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEFUNCTION, &rtHttpWriteData);
|
---|
329 | if (CURL_FAILED(rcCurl))
|
---|
330 | return VERR_INTERNAL_ERROR;
|
---|
331 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEDATA, (void*)&chunk);
|
---|
332 | if (CURL_FAILED(rcCurl))
|
---|
333 | return VERR_INTERNAL_ERROR;
|
---|
334 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSFUNCTION, &rtHttpProgress);
|
---|
335 | if (CURL_FAILED(rcCurl))
|
---|
336 | return VERR_INTERNAL_ERROR;
|
---|
337 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_PROGRESSDATA, (void*)pHttpInt);
|
---|
338 | if (CURL_FAILED(rcCurl))
|
---|
339 | return VERR_INTERNAL_ERROR;
|
---|
340 | rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_NOPROGRESS, (long)0);
|
---|
341 | if (CURL_FAILED(rcCurl))
|
---|
342 | return VERR_INTERNAL_ERROR;
|
---|
343 |
|
---|
344 | rcCurl = curl_easy_perform(pHttpInt->pCurl);
|
---|
345 | int rc = VERR_INTERNAL_ERROR;
|
---|
346 | if (rcCurl == CURLE_OK)
|
---|
347 | {
|
---|
348 | curl_easy_getinfo(pHttpInt->pCurl, CURLINFO_RESPONSE_CODE, &pHttpInt->lLastResp);
|
---|
349 | switch (pHttpInt->lLastResp)
|
---|
350 | {
|
---|
351 | case 200:
|
---|
352 | /* OK, request was fulfilled */
|
---|
353 | case 204:
|
---|
354 | /* empty response */
|
---|
355 | rc = VINF_SUCCESS;
|
---|
356 | break;
|
---|
357 | case 400:
|
---|
358 | /* bad request */
|
---|
359 | rc = VERR_HTTP_BAD_REQUEST;
|
---|
360 | break;
|
---|
361 | case 403:
|
---|
362 | /* forbidden, authorization will not help */
|
---|
363 | rc = VERR_HTTP_ACCESS_DENIED;
|
---|
364 | break;
|
---|
365 | case 404:
|
---|
366 | /* URL not found */
|
---|
367 | rc = VERR_HTTP_NOT_FOUND;
|
---|
368 | break;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | else
|
---|
372 | {
|
---|
373 | switch (rcCurl)
|
---|
374 | {
|
---|
375 | case CURLE_URL_MALFORMAT:
|
---|
376 | case CURLE_COULDNT_RESOLVE_HOST:
|
---|
377 | rc = VERR_HTTP_NOT_FOUND;
|
---|
378 | break;
|
---|
379 | case CURLE_COULDNT_CONNECT:
|
---|
380 | rc = VERR_HTTP_COULDNT_CONNECT;
|
---|
381 | break;
|
---|
382 | case CURLE_SSL_CONNECT_ERROR:
|
---|
383 | rc = VERR_HTTP_SSL_CONNECT_ERROR;
|
---|
384 | break;
|
---|
385 | case CURLE_SSL_CACERT:
|
---|
386 | /* The peer certificate cannot be authenticated with the CA certificates
|
---|
387 | * set by RTHttpSetCAFile(). We need other or additional CA certificates. */
|
---|
388 | rc = VERR_HTTP_CACERT_CANNOT_AUTHENTICATE;
|
---|
389 | break;
|
---|
390 | case CURLE_SSL_CACERT_BADFILE:
|
---|
391 | /* CAcert file (see RTHttpSetCAFile()) has wrong format */
|
---|
392 | rc = VERR_HTTP_CACERT_WRONG_FORMAT;
|
---|
393 | break;
|
---|
394 | case CURLE_ABORTED_BY_CALLBACK:
|
---|
395 | /* forcefully aborted */
|
---|
396 | rc = VERR_HTTP_ABORTED;
|
---|
397 | break;
|
---|
398 | default:
|
---|
399 | break;
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | *ppszResponse = chunk.pszMem;
|
---|
404 |
|
---|
405 | return rc;
|
---|
406 | }
|
---|