@@ -407,6 +407,43 @@ class HttpClient {
407407 }
408408
409409 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 ) {
410447 curl = curl_easy_init ();
411448 if (!curl) {
412449 return 1 ;
@@ -415,8 +452,7 @@ class HttpClient {
415452 progress_data data;
416453 File out;
417454 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" )) {
420456 printe (" Failed to open file for writing\n " );
421457
422458 return 1 ;
@@ -430,35 +466,18 @@ class HttpClient {
430466 }
431467
432468 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 );
434470 set_progress_options (progress, data);
435471 set_headers (headers);
436472 CURLcode res = perform (url);
437473 if (res != CURLE_OK){
438474 printe (" Fetching resource '%s' failed: %s\n " , url.c_str (), curl_easy_strerror (res));
439475 return 1 ;
440476 }
441- if (!output_file.empty ()) {
442- std::filesystem::rename (output_file_partial, output_file);
443- }
444477
445478 return 0 ;
446479 }
447480
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-
462481 void set_write_options (std::string * response_str, const File & out) {
463482 if (response_str) {
464483 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, capture_data);
@@ -507,6 +526,9 @@ class HttpClient {
507526 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1L );
508527 curl_easy_setopt (curl, CURLOPT_DEFAULT_PROTOCOL, " https" );
509528 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1L );
529+ #ifdef _WIN32
530+ curl_easy_setopt (curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA);
531+ #endif
510532 return curl_easy_perform (curl);
511533 }
512534
0 commit comments