22 #include <curl/curl.h>
25 #define ERR_NW LOG_STREAM(err, log_network)
26 #define DBG_NW LOG_STREAM(debug, log_network)
32 size_t amount =
size * nmemb;
33 static_cast<std::string*
>(buffer)->append(contents, amount);
34 DBG_NW <<
"Downloaded " << amount <<
" bytes.";
38 void download(
const std::string& url,
const std::string& local_path)
40 CURL* curl = curl_easy_init();
43 char error[CURL_ERROR_SIZE];
44 std::fill_n(error, CURL_ERROR_SIZE-1,
' ');
45 error[CURL_ERROR_SIZE-1] =
'\0';
48 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
50 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
51 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
52 curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);
53 curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
54 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
55 curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 5000L);
56 #if LIBCURL_VERSION_NUM >= 0x075500
57 curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, CURLPROTO_HTTPS);
59 curl_easy_setopt(curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
62 CURLcode res = curl_easy_perform(curl);
65 ERR_NW <<
"Error downloading file from url `" << url <<
"`.\n"
66 <<
"Short error: " << curl_easy_strerror(res) <<
"\n"
67 <<
"Long error: " << std::string(error);
68 gui2::show_message(
_(
"Download error"),
_(
"An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
79 DBG_NW <<
"Wrote downloaded file to: " << local_path;
81 ERR_NW <<
"io_exception writing downloaded data to file at: " << local_path
82 <<
"\n" <<
e.what() <<
" : " <<
e.message;
83 gui2::show_message(
_(
"Download error"),
_(
"An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
85 gui2::show_message(
_(
"Download complete"),
_(
"The file has been downloaded."), gui2::dialogs::message::button_style::auto_close);
88 curl_easy_cleanup(curl);
90 ERR_NW <<
"curl_easy_init failed initialization, unable to download file.";
91 gui2::show_message(
_(
"Download error"),
_(
"An error occurred when downloading the file. Check the game logs for more information."), gui2::dialogs::message::button_style::auto_close);
@ yes_no_buttons
Shows a yes and no button.
Declarations for File-IO.
static std::string _(const char *str)
Standard logging facilities (interface).
static bool file_exists(const bfs::path &fpath)
void write_file(const std::string &fname, const std::string &data, std::ios_base::openmode mode)
Throws io_exception if an error occurs.
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
@ OK
Dialog was closed with the OK button.
High level network layer for config object transport.
static size_t write_callback(char *contents, size_t size, size_t nmemb, void *buffer)
void download(const std::string &url, const std::string &local_path)
Initiates a standalone download of a single file from an HTTPS URL.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
static lg::log_domain log_network("network")
An exception object used when an IO error occurs.