@@ -407,6 +407,43 @@ class HttpClient {
407
407
}
408
408
409
409
std::string output_file_partial;
410
+
411
+ if (!output_file.empty ()) {
412
+ output_file_partial = output_file + " .partial" ;
413
+ }
414
+
415
+ if (download (url, headers, output_file_partial, progress, response_str)) {
416
+ return 1 ;
417
+ }
418
+
419
+ if (!output_file.empty ()) {
420
+ try {
421
+ std::filesystem::rename (output_file_partial, output_file);
422
+ } catch (const std::filesystem::filesystem_error & e) {
423
+ printe (" Failed to rename '%s' to '%s': %s\n " , output_file_partial.c_str (), output_file.c_str (), e.what ());
424
+ return 1 ;
425
+ }
426
+ }
427
+
428
+ return 0 ;
429
+ }
430
+
431
+ ~HttpClient () {
432
+ if (chunk) {
433
+ curl_slist_free_all (chunk);
434
+ }
435
+
436
+ if (curl) {
437
+ curl_easy_cleanup (curl);
438
+ }
439
+ }
440
+
441
+ private:
442
+ CURL * curl = nullptr ;
443
+ struct curl_slist * chunk = nullptr ;
444
+
445
+ int download (const std::string & url, const std::vector<std::string> & headers, const std::string & output_file,
446
+ const bool progress, std::string * response_str = nullptr ) {
410
447
curl = curl_easy_init ();
411
448
if (!curl) {
412
449
return 1 ;
@@ -415,8 +452,7 @@ class HttpClient {
415
452
progress_data data;
416
453
File out;
417
454
if (!output_file.empty ()) {
418
- output_file_partial = output_file + " .partial" ;
419
- if (!out.open (output_file_partial, " ab" )) {
455
+ if (!out.open (output_file, " ab" )) {
420
456
printe (" Failed to open file for writing\n " );
421
457
422
458
return 1 ;
@@ -430,35 +466,18 @@ class HttpClient {
430
466
}
431
467
432
468
set_write_options (response_str, out);
433
- data.file_size = set_resume_point (output_file_partial );
469
+ data.file_size = set_resume_point (output_file );
434
470
set_progress_options (progress, data);
435
471
set_headers (headers);
436
472
CURLcode res = perform (url);
437
473
if (res != CURLE_OK){
438
474
printe (" Fetching resource '%s' failed: %s\n " , url.c_str (), curl_easy_strerror (res));
439
475
return 1 ;
440
476
}
441
- if (!output_file.empty ()) {
442
- std::filesystem::rename (output_file_partial, output_file);
443
- }
444
477
445
478
return 0 ;
446
479
}
447
480
448
- ~HttpClient () {
449
- if (chunk) {
450
- curl_slist_free_all (chunk);
451
- }
452
-
453
- if (curl) {
454
- curl_easy_cleanup (curl);
455
- }
456
- }
457
-
458
- private:
459
- CURL * curl = nullptr ;
460
- struct curl_slist * chunk = nullptr ;
461
-
462
481
void set_write_options (std::string * response_str, const File & out) {
463
482
if (response_str) {
464
483
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, capture_data);
@@ -507,6 +526,9 @@ class HttpClient {
507
526
curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
508
527
curl_easy_setopt (curl, CURLOPT_DEFAULT_PROTOCOL, " https" );
509
528
curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1L );
529
+ #ifdef _WIN32
530
+ curl_easy_setopt (curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
531
+ #endif
510
532
return curl_easy_perform (curl);
511
533
}
512
534
0 commit comments