| 186 | |
| 187 | |
| 188 | bool requestDownload(const std::string& url, const std::string& filename, float* progress, const CookieMap& cookies) { |
| 189 | CURL* curl = createCurl(); |
| 190 | |
| 191 | FILE* file = std::fopen(filename.c_str(), "wb"); |
| 192 | if (!file) |
| 193 | return false; |
| 194 | |
| 195 | curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 196 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, false); |
| 197 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); |
| 198 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); |
| 199 | curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferInfoCallback); |
| 200 | curl_easy_setopt(curl, CURLOPT_XFERINFODATA, progress); |
| 201 | // Fail on 4xx and 5xx HTTP codes |
| 202 | curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); |
| 203 | |
| 204 | // Cookies |
| 205 | if (!cookies.empty()) { |
| 206 | curl_easy_setopt(curl, CURLOPT_COOKIE, getCookieString(cookies).c_str()); |
| 207 | } |
| 208 | |
| 209 | INFO("Requesting download %s", url.c_str()); |
| 210 | CURLcode res = curl_easy_perform(curl); |
| 211 | curl_easy_cleanup(curl); |
| 212 | |
| 213 | std::fclose(file); |
| 214 | |
| 215 | if (res != CURLE_OK) { |
| 216 | system::remove(filename); |
| 217 | WARN("Could not download %s: %s", url.c_str(), curl_easy_strerror(res)); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | std::string encodeUrl(const std::string& s) { |
no test coverage detected