Awoo-Installer/source/util/curl.cpp
2019-11-29 03:23:11 -05:00

86 lines
No EOL
3.1 KiB
C++
Executable file

#include "util/curl.hpp"
#include "util/config.hpp"
#include <curl/curl.h>
#include <string>
#include <sstream>
#include <iostream>
static size_t writeDataFile(void *ptr, size_t size, size_t nmemb, void *stream) {
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
size_t writeDataBuffer(char *ptr, size_t size, size_t nmemb, void *userdata) {
std::ostringstream *stream = (std::ostringstream*)userdata;
size_t count = size * nmemb;
stream->write(ptr, count);
return count;
}
namespace inst::curl {
bool downloadFile (const std::string ourUrl, const char *pagefilename) {
CURL *curl_handle;
CURLcode result;
FILE *pagefile;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, ourUrl.c_str());
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 5000L);
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 5000L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeDataFile);
pagefile = fopen(pagefilename, "wb");
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);
result = curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
fclose(pagefile);
if (result == CURLE_OK) return true;
else {
printf(curl_easy_strerror(result));
return false;
}
}
std::string downloadToBuffer (const std::string ourUrl, int firstRange, int secondRange) {
CURL *curl_handle;
CURLcode result;
std::ostringstream stream;
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, ourUrl.c_str());
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 0L);
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT_MS, 5000L);
curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 5000L);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeDataBuffer);
if (firstRange && secondRange) {
const char * ourRange = (std::to_string(firstRange) + "-" + std::to_string(secondRange)).c_str();
curl_easy_setopt(curl_handle, CURLOPT_RANGE, ourRange);
}
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &stream);
result = curl_easy_perform(curl_handle);
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
if (result == CURLE_OK) return stream.str();
else {
printf(curl_easy_strerror(result));
return "";
}
}
}