Support Curl Error Buffer

Allow user to get error message when download fails
This commit is contained in:
Ten10 2019-11-26 16:14:46 +02:00 committed by Howard Hinnant
parent 018a50bcd0
commit 940f4a5ceb
2 changed files with 8 additions and 5 deletions

View File

@ -1314,7 +1314,8 @@ DATE_API void set_install(const std::string& install);
#if HAS_REMOTE_API #if HAS_REMOTE_API
DATE_API std::string remote_version(); DATE_API std::string remote_version();
DATE_API bool remote_download(const std::string& version); // if provided error_buffer size should be at least CURL_ERROR_SIZE
DATE_API bool remote_download(const std::string& version, char* error_buffer = nullptr);
DATE_API bool remote_install(const std::string& version); DATE_API bool remote_install(const std::string& version);
#endif #endif

View File

@ -2836,13 +2836,15 @@ namespace
static static
bool bool
download_to_file(const std::string& url, const std::string& local_filename, download_to_file(const std::string& url, const std::string& local_filename,
download_file_options opts) download_file_options opts, char* error_buffer)
{ {
auto curl = curl_init(); auto curl = curl_init();
if (!curl) if (!curl)
return false; return false;
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str()); curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false); curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, false);
if (error_buffer)
curl_easy_setopt(curl.get(), CURLOPT_ERRORBUFFER, error_buffer);
curl_write_callback write_cb = [](char* contents, std::size_t size, std::size_t nmemb, curl_write_callback write_cb = [](char* contents, std::size_t size, std::size_t nmemb,
void* userp) -> std::size_t void* userp) -> std::size_t
{ {
@ -3262,7 +3264,7 @@ extract_gz_file(const std::string&, const std::string& gz_file, const std::strin
# endif // !_WIN32 # endif // !_WIN32
bool bool
remote_download(const std::string& version) remote_download(const std::string& version, char* error_buffer)
{ {
assert(!version.empty()); assert(!version.empty());
@ -3281,7 +3283,7 @@ remote_download(const std::string& version)
auto url = "https://data.iana.org/time-zones/releases/tzdata" + version + auto url = "https://data.iana.org/time-zones/releases/tzdata" + version +
".tar.gz"; ".tar.gz";
bool result = download_to_file(url, get_download_gz_file(version), bool result = download_to_file(url, get_download_gz_file(version),
download_file_options::binary); download_file_options::binary, error_buffer);
# ifdef _WIN32 # ifdef _WIN32
if (result) if (result)
{ {
@ -3289,7 +3291,7 @@ remote_download(const std::string& version)
result = download_to_file( result = download_to_file(
"https://raw.githubusercontent.com/unicode-org/cldr/master/" "https://raw.githubusercontent.com/unicode-org/cldr/master/"
"common/supplemental/windowsZones.xml", "common/supplemental/windowsZones.xml",
mapping_file, download_file_options::text); mapping_file, download_file_options::text, error_buffer);
} }
# endif // _WIN32 # endif // _WIN32
return result; return result;