Changeset 46050 in vbox for trunk/src/VBox/Runtime/common/misc/http.cpp
- Timestamp:
- May 14, 2013 8:41:11 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/http.cpp
r45632 r46050 204 204 } 205 205 206 RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, uint32_t cHeaders, const char *pcszHeaders[])206 RTR3DECL(int) RTHttpSetHeaders(RTHTTP hHttp, size_t cHeaders, const char * const *papszHeaders) 207 207 { 208 208 PRTHTTPINTERNAL pHttpInt = hHttp; … … 218 218 219 219 struct curl_slist *pHeaders = NULL; 220 for ( unsignedi = 0; i < cHeaders; i++)221 pHeaders = curl_slist_append(pHeaders, p cszHeaders[i]);220 for (size_t i = 0; i < cHeaders; i++) 221 pHeaders = curl_slist_append(pHeaders, papszHeaders[i]); 222 222 223 223 pHttpInt->pHeaders = pHeaders; … … 305 305 } 306 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); 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 */ 315 static int rtHttpGetCalcStatus(PRTHTTPINTERNAL pHttpInt, int rcCurl) 316 { 352 317 int rc = VERR_INTERNAL_ERROR; 353 318 if (rcCurl == CURLE_OK) … … 408 373 } 409 374 375 return rc; 376 } 377 378 RTR3DECL(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); 410 424 *ppszResponse = chunk.pszMem; 411 425 412 426 return rc; 413 427 } 428 429 430 static 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 443 RTR3DECL(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
Note:
See TracChangeset
for help on using the changeset viewer.