| 398 | class HttpClient { |
| 399 | public: |
| 400 | int init(const std::string & url, const std::vector<std::string> & headers, const std::string & output_file, |
| 401 | const bool progress, std::string * response_str = nullptr) { |
| 402 | if (std::filesystem::exists(output_file)) { |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | std::string output_file_partial; |
| 407 | curl = curl_easy_init(); |
| 408 | if (!curl) { |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | progress_data data; |
| 413 | File out; |
| 414 | if (!output_file.empty()) { |
| 415 | output_file_partial = output_file + ".partial"; |
| 416 | if (!out.open(output_file_partial, "ab")) { |
| 417 | printe("Failed to open file for writing\n"); |
| 418 | |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | if (out.lock()) { |
| 423 | printe("Failed to exclusively lock file\n"); |
| 424 | |
| 425 | return 1; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | set_write_options(response_str, out); |
| 430 | data.file_size = set_resume_point(output_file_partial); |
| 431 | set_progress_options(progress, data); |
| 432 | set_headers(headers); |
| 433 | CURLcode res = perform(url); |
| 434 | if (res != CURLE_OK){ |
| 435 | printe("Fetching resource '%s' failed: %s\n", url.c_str(), curl_easy_strerror(res)); |
| 436 | return 1; |
| 437 | } |
| 438 | if (!output_file.empty()) { |
| 439 | std::filesystem::rename(output_file_partial, output_file); |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | ~HttpClient() { |
| 446 | if (chunk) { |