-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathutils.cpp
More file actions
38 lines (34 loc) · 1.13 KB
/
utils.cpp
File metadata and controls
38 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "utils.h"
#include <stdexcept>
#include <curl/curl.h>
#include <stdio.h>
namespace utils {
bool DownloadFile(const std::string& url, const std::string& path) {
CURLcode ret{CURLE_OK};
CURL* hnd = curl_easy_init();
if (hnd != nullptr) {
FILE* fp = fopen(path.c_str(), "wb");
if (fp != nullptr) {
curl_easy_setopt(hnd, CURLOPT_URL, url.c_str());
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPIDLE, 15L);
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPINTVL, 30L);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(
hnd, CURLOPT_NOSIGNAL,
1); // Prevent "longjmp causes uninitialized stack frame" bug
curl_easy_setopt(hnd, CURLOPT_ACCEPT_ENCODING, "deflate");
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, nullptr);
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, fp);
// curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L);
ret = curl_easy_perform(hnd);
} else {
ret = CURLE_FAILED_INIT;
}
} else {
return false;
}
curl_easy_cleanup(hnd);
return ret == CURLE_OK;
}
} // namespace utils