6 #ifdef CHECK_FOR_UPDATES 12 #include <curl/curl.h> 13 #include <curl/easy.h> 15 #endif // CHECK_FOR_UPDATES 25 #ifndef CHECK_FOR_UPDATES 35 std::mutex initialize_mutex;
36 static const curl_off_t HALF_SEC = 500000;
37 static const int CONNECT_TIMEOUT = 5L;
39 struct progress_data {
40 curl_off_t last_run_time;
46 size_t stream_write_callback(
void *input_stream,
size_t size,
size_t nmemb,
void *output_stream)
48 if (input_stream && output_stream)
51 *((std::stringstream*)output_stream) <<
data;
57 size_t vector_write_callback(
void *input_stream,
size_t size,
size_t nmemb,
void *output_vec)
59 uint8_t* source_bytes(static_cast<uint8_t*>(input_stream));
61 if (input_stream && output_vec)
63 int total_size((
int)(size * nmemb));
64 while (total_size > 0)
66 static_cast<std::vector<uint8_t> *
>(output_vec)->push_back(*source_bytes);
76 size_t file_write_callback(
void *input_stream,
size_t size,
size_t nmemb,
void *output)
79 if (input_stream && output)
81 std::ofstream &out_stream(*static_cast<std::ofstream*> (output));
83 size_t num_of_bytem(nmemb*size);
84 out_stream.write((
char *)input_stream, num_of_bytem);
93 int progress_callback(
void *
p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
95 progress_data *myp =
static_cast<progress_data *
>(
p);
96 CURL *curl(myp->curl);
97 curl_off_t curtime(0);
98 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &curtime);
99 if (dltotal != 0 && (curtime - myp->last_run_time > HALF_SEC))
101 myp->last_run_time = curtime;
102 return myp->user_callback_func(static_cast<uint64_t>(dlnow),
114 std::lock_guard<std::mutex>
lock(initialize_mutex);
115 _curl = curl_easy_init();
120 std::lock_guard<std::mutex>
lock(initialize_mutex);
121 curl_easy_cleanup(
_curl);
126 if (!
_curl)
return false;
129 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, stream_write_callback);
130 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, &output);
131 curl_easy_setopt(
_curl, CURLOPT_SSL_VERIFYPEER ,0L);
132 curl_easy_setopt(
_curl, CURLOPT_SSL_VERIFYHOST ,0L);
134 progress_data progress_record;
135 if (user_callback_func)
139 auto res = curl_easy_perform(
_curl);
151 if (!
_curl)
return false;
154 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, vector_write_callback);
155 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, &output);
157 progress_data progress_record;
158 if (user_callback_func)
162 auto res = curl_easy_perform(
_curl);
175 if (!
_curl)
return false;
183 curl_easy_setopt(
_curl, CURLOPT_WRITEFUNCTION, file_write_callback);
184 curl_easy_setopt(
_curl, CURLOPT_WRITEDATA, &out_file);
186 progress_data progress_record;
187 if (user_callback_func)
191 auto res = curl_easy_perform(
_curl);
202 LOG_ERROR(
"Download error - Cannot open local file: " + file_name);
211 curl_easy_setopt(
_curl, CURLOPT_URL, url.c_str());
212 curl_easy_setopt(
_curl, CURLOPT_FOLLOWLOCATION, 1L);
213 curl_easy_setopt(
_curl, CURLOPT_NOSIGNAL, 1);
214 curl_easy_setopt(
_curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
215 curl_easy_setopt(
_curl, CURLOPT_FAILONERROR, 1L);
216 curl_easy_setopt(
_curl, CURLOPT_NOPROGRESS, 1L);
220 progress_record = { 0, user_callback_func,
_curl };
221 curl_easy_setopt(_curl, CURLOPT_XFERINFOFUNCTION, progress_callback);
222 curl_easy_setopt(_curl, CURLOPT_XFERINFODATA, &progress_record);
223 curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, 0L);
static const textual_icon lock
bool download_to_bytes_vector(const std::string &url, std::vector< uint8_t > &output, user_callback_func_type user_callback_func=user_callback_func_type())
GLsizei const GLchar *const * string
std::function< callback_result(uint64_t dl_current_bytes, uint64_t dl_total_bytes)> user_callback_func_type
bool download_to_stream(const std::string &url, std::stringstream &output, user_callback_func_type user_callback_func=user_callback_func_type())
void register_progress_call_back(progress_data &progress_record, user_callback_func_type user_callback_func)
void set_common_options(const std::string &url)
bool download_to_file(const std::string &url, const std::string &file_name, user_callback_func_type user_callback_func=user_callback_func_type())