50    static size_t writer(
char* data, 
size_t size, 
size_t nmemb, std::string* writerData)
 
   52        if (writerData == NULL)
 
   55        writerData->append(data, size * nmemb);
 
   71    static size_t reader(
char *ptr, 
size_t size, 
size_t nmemb, 
void *stream)
 
   73        std::ifstream* filestream = 
static_cast<std::ifstream*
>(stream);
 
   74        size_t bytes = filestream->readsome(ptr, size * nmemb);
 
   88    static CURL* 
common_init(
const std::string& sUrl, 
const std::string& sUserName, 
const std::string& sPassWord)
 
   92        CURL* conn = curl_easy_init();
 
   95            throw Error(
"Failed to create CURL connection.");
 
   99            code = curl_easy_setopt(conn, CURLOPT_ERRORBUFFER, 
errorBuffer);
 
  101            if (code != CURLE_OK)
 
  102                throw Error(
"Failed to set error buffer [" + std::to_string(code) + 
"].");
 
  104            code = curl_easy_setopt(conn, CURLOPT_URL, sUrl.c_str());
 
  106            if (code != CURLE_OK)
 
  109            code = curl_easy_setopt(conn, CURLOPT_USERAGENT, 
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0");
 
  111            if (code != CURLE_OK)
 
  112                throw Error(
"Failed to set user agent [" + std::string(
errorBuffer) + 
"].");
 
  114            if (sUserName.length() && sPassWord.length())
 
  116                code = curl_easy_setopt(conn, CURLOPT_USERPWD, (sUserName + 
":" + sPassWord).c_str());
 
  118                if (code != CURLE_OK)
 
  119                    throw Error(
"Failed to set username and password [" + std::string(
errorBuffer) + 
"].");
 
  122            code = curl_easy_setopt(conn, CURLOPT_SSL_VERIFYPEER, 0L);
 
  124            if (code != CURLE_OK)
 
  125                throw Error(
"Failed to set SSL peer verify option [" + std::string(
errorBuffer) + 
"].");
 
  127            code = curl_easy_setopt(conn, CURLOPT_SSL_VERIFYHOST, 0L);
 
  129            if (code != CURLE_OK)
 
  130                throw Error(
"Failed to set SSL host verify option [" + std::string(
errorBuffer) + 
"].");
 
  132            code = curl_easy_setopt(conn, CURLOPT_FOLLOWLOCATION, 1L);
 
  134            if (code != CURLE_OK)
 
  135                throw Error(
"Failed to set redirect option [" + std::string(
errorBuffer) + 
"].");
 
  137            code = curl_easy_setopt(conn, CURLOPT_CONNECTTIMEOUT, 10L);
 
  139            if (code != CURLE_OK)
 
  140                throw Error(
"Failed to set connection time-out option [" + std::string(
errorBuffer) + 
"].");
 
  142            code = curl_easy_setopt(conn, CURLOPT_TIMEOUT, 10L);
 
  144            if (code != CURLE_OK)
 
  145                throw Error(
"Failed to set time-out option [" + std::string(
errorBuffer) + 
"].");
 
  149            curl_easy_cleanup(conn);
 
  168    static CURL* 
get_init(
const std::string& sUrl, 
const std::string& sUserName, 
const std::string& sPassWord, std::string* buffer)
 
  172        CURL* conn = 
common_init(sUrl, sUserName, sPassWord);
 
  176            code = curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, 
writer);
 
  178            if (code != CURLE_OK)
 
  181            code = curl_easy_setopt(conn, CURLOPT_WRITEDATA, buffer);
 
  183            if (code != CURLE_OK)
 
  184                throw Error(
"Failed to set write data [" + std::string(
errorBuffer) + 
"].");
 
  188            curl_easy_cleanup(conn);
 
  207    static CURL* 
put_init(
const std::string& sUrl, 
const std::string& sUserName, 
const std::string& sPassWord, std::ifstream* filestream, 
size_t filesize)
 
  211        CURL* conn = 
common_init(sUrl, sUserName, sPassWord);
 
  215            code = curl_easy_setopt(conn, CURLOPT_UPLOAD, 1L);
 
  217            if (code != CURLE_OK)
 
  218                throw Error(
"Failed to set upload option [" + std::string(
errorBuffer) + 
"].");
 
  220            code = curl_easy_setopt(conn, CURLOPT_READFUNCTION, 
reader);
 
  222            if (code != CURLE_OK)
 
  225            code = curl_easy_setopt(conn, CURLOPT_READDATA, filestream);
 
  227            if (code != CURLE_OK)
 
  228                throw Error(
"Failed to set filestream [" + std::string(
errorBuffer) + 
"].");
 
  230                curl_easy_setopt(conn, CURLOPT_INFILESIZE, filesize);
 
  234            curl_easy_cleanup(conn);
 
  251    std::string 
get(
const std::string& sUrl, 
const std::string& sUserName, 
const std::string& sPassWord)
 
  253        CURL* conn = 
nullptr;
 
  258        curl_global_init(CURL_GLOBAL_DEFAULT);
 
  261        conn = 
get_init(sUrl, sUserName, sPassWord, &buffer);
 
  264        code = curl_easy_perform(conn);
 
  265        curl_easy_cleanup(conn);
 
  267        if (code != CURLE_OK)
 
  268            throw Error(
"Failed to get '" + sUrl + 
"': " + std::string(
errorBuffer) + 
".");
 
  286    size_t put(
const std::string& sUrl, 
const std::string& sFileName, 
const std::string& sUserName, 
const std::string& sPassWord)
 
  288        CURL* conn = 
nullptr;
 
  290        std::ifstream filestream(sFileName.c_str(), std::ios_base::binary);
 
  292        if (!filestream.good())
 
  293            throw Error(
"Cannot open file '" + sFileName + 
"'.");
 
  295        filestream.seekg(0, std::ios_base::end);
 
  296        size_t s = filestream.tellg();
 
  297        filestream.seekg(0, std::ios_base::beg);
 
  300            throw Error(
"File size of '" + sFileName + 
"' is zero.");
 
  302        curl_global_init(CURL_GLOBAL_DEFAULT);
 
  305        conn = 
put_init(sUrl, sUserName, sPassWord, &filestream, s);
 
  308        code = curl_easy_perform(conn);
 
  309        curl_easy_cleanup(conn);
 
  311        if (code != CURLE_OK)
 
  312            throw Error(
"Failed to upload to '" + sUrl + 
"': " + std::string(
errorBuffer) + 
".");
 
A class for URL exceptions.
 
static size_t reader(char *ptr, size_t size, size_t nmemb, void *stream)
libcurl read callback function.
 
size_t put(const std::string &sUrl, const std::string &sFileName, const std::string &sUserName, const std::string &sPassWord)
Upload a file to a destination and return the transmitted bytes.
 
std::string get(const std::string &sUrl, const std::string &sUserName, const std::string &sPassWord)
Get the contents of a URL.
 
static CURL * common_init(const std::string &sUrl, const std::string &sUserName, const std::string &sPassWord)
Perform common CURL initialization.
 
static size_t writer(char *data, size_t size, size_t nmemb, std::string *writerData)
libcurl write callback function.
 
static CURL * get_init(const std::string &sUrl, const std::string &sUserName, const std::string &sPassWord, std::string *buffer)
libcurl get connection initialization.
 
static char errorBuffer[CURL_ERROR_SIZE]
libcurl variable for error strings.
 
static CURL * put_init(const std::string &sUrl, const std::string &sUserName, const std::string &sPassWord, std::ifstream *filestream, size_t filesize)
libcurl put connection initialization.