From 0bda3b6c4c812a8edd46479cafdd82f11a4784f4 Mon Sep 17 00:00:00 2001 From: Markus Date: Sun, 13 Oct 2019 12:54:58 +0200 Subject: [PATCH] add download to file function for curl --- pageManager.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ pageManager.h | 2 ++ 2 files changed, 72 insertions(+) diff --git a/pageManager.cpp b/pageManager.cpp index 251181c..4e02260 100644 --- a/pageManager.cpp +++ b/pageManager.cpp @@ -26,6 +26,7 @@ void PageManager::setDebugMode(bool status) this->debugMode = status; } +//Save reply to string size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { //Function für CURL @@ -33,6 +34,13 @@ size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) return size * nmemb; } +//Write data to file +static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) +{ + size_t written = fwrite(ptr, size, nmemb, reinterpret_cast(stream)); + return written; +} + Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::string data, bool generateCookieFile) { CURL *curl; @@ -98,6 +106,68 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin return Reply(readBuffer, returnUrl); } + +int PageManager::downLoadToFile(std::string filePath, std::string url) +{ + CURL *curl_handle; + FILE *pagefile; + CURLcode res; + + /* open the file */ + pagefile = fopen(filePath.c_str(), "wb"); // w == write; b == binäre + if(!pagefile) { + perror("Open File filed"); + return 1; + } + + /* init the curl session */ + curl_handle = curl_easy_init(); + if(!curl_handle) { + perror("\33[2K\r => Error: Curl easy init failed"); + return 2; + } + + /* set URL to get here */ + curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); + + /* Switch on full protocol/debug output while testing */ + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, false); + + /* disable progress meter, set to 0L to enable and disable debug output */ + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, true); + + /* send all data to this function */ + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); + + //Sock5Proxy für Curl + curl_easy_setopt(curl_handle, CURLOPT_PROXY, sock5Proxy.c_str() ); + + //User Agent + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0"); + + /* write the page body to this file handle */ + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); + + + /* get it! */ + if( (res = curl_easy_perform(curl_handle)) != CURLE_OK) { + perror((std::string("\33[2K\r => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str()); + return 3; + } + + /* close the header file */ + if(fclose(pagefile) != 0) { + perror(" => Error: fclose failed"); + return 4; + } + + /* cleanup curl stuff */ + curl_easy_cleanup(curl_handle); + + + return 0; +} + int PageManager::login(Account account) { if(debugMode) diff --git a/pageManager.h b/pageManager.h index e5636e2..7931c5a 100644 --- a/pageManager.h +++ b/pageManager.h @@ -30,6 +30,8 @@ public: void setDebugMode(bool status); Reply getServerRequest(std::string Url, bool useCookies = false, std::string data = "", bool generateCookieFile = false); + int downLoadToFile(std::string filePath, std::string url); + int login(Account account); std::string getUrlAfterRedirect(std::string Url); std::string checkName(std::string Name);