diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-28 01:02:16 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-28 01:27:11 +0100 |
commit | 0cf81a4421f9a4d267245a3041508b617a01d68d (patch) | |
tree | 1eb816c05cc005abe317439516ba6b06c4dfbdfb /src | |
parent | 5100c0659e39dd82a3b979b859704e7dbf01e2f1 (diff) |
Add curl get, add packages file
Diffstat (limited to 'src')
-rw-r--r-- | src/curl.cpp | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/src/curl.cpp b/src/curl.cpp index e141e78..56c19ec 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -18,6 +18,11 @@ public: { curl_global_init(CURL_GLOBAL_ALL); } + + ~CurlSession() + { + curl_global_cleanup(); + } }; static CurlSession curlSession; @@ -26,9 +31,17 @@ namespace sibs { // TODO: Instead of writing to file, reading from file and extracting it; // we can extract to file directly by putting libarchive code here - size_t writeToFile(void *ptr, size_t size, size_t nmemb, void *stream) + size_t writeToFile(char *data, size_t size, size_t nmemb, FILE *stream) { - return fwrite(ptr, size, nmemb, (FILE*)stream); + if(!stream) return 0; + return fwrite(data, size, nmemb, stream); + } + + size_t writeToString(char *data, size_t size, size_t nmemb, string *writerData) + { + if(!writerData) return 0; + writerData->append(data, size * nmemb); + return size * nmemb; } Result<bool> curl::downloadFile(const char *url, const char *filepath) @@ -46,6 +59,7 @@ namespace sibs curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, noProgressMeter); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeToFile); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "SIBS"); FILE *file = fopen(filepath, "wb"); if(!file) @@ -85,6 +99,9 @@ namespace sibs errMsg += url; errMsg += "\nReason: Expected http response code 200 (OK), got: "; errMsg += to_string(httpCode); + errMsg += " ("; + errMsg += curl_easy_strerror(curlResponse); + errMsg += ")"; return Result<bool>::Err(errMsg); } else @@ -96,4 +113,60 @@ namespace sibs return Result<bool>::Err(errMsg); } } + + HttpResult curl::get(const char *url) + { + HttpResult result; + + CURL *curl_handle = curl_easy_init(); + curl_easy_setopt(curl_handle, CURLOPT_URL, url); +#ifdef CURL_DEBUG + long verbose = 1L; + long noProgressMeter = 0L; +#else + long verbose = 0L; + long noProgressMeter = 1L; +#endif + curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, verbose); + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, noProgressMeter); + curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeToString); + curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, true); + curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Hacker"); + + curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &result.str); + printf("Downloading from url: %s\n", url); + CURLcode curlResponse = curl_easy_perform(curl_handle); + + long httpCode = 0; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpCode); + curl_easy_cleanup(curl_handle); + + result.httpCode = httpCode; + if(httpCode == 200 && curlResponse == CURLE_OK) + { + result.success = true; + return result; + } + + result.success = false; + if(httpCode != 200) + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: Expected http response code 200 (OK), got: "; + errMsg += to_string(httpCode); + errMsg += " ("; + errMsg += curl_easy_strerror(curlResponse); + errMsg += ")"; + return result; + } + else + { + string errMsg = "Failed to download file from url: "; + errMsg += url; + errMsg += "\nReason: "; + errMsg += curl_easy_strerror(curlResponse); + return result; + } + } }
\ No newline at end of file |