diff options
-rw-r--r-- | src/download.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/download.c b/src/download.c index 211987a..bd794b1 100644 --- a/src/download.c +++ b/src/download.c @@ -43,6 +43,7 @@ static size_t download_write_buffer_callback(char* data, size_t size, size_t nme int download_to_buffer(const char* url, char** buf, uint32_t* length) { + int res = 0; curl_global_init(CURL_GLOBAL_ALL); CURL* handle = curl_easy_init(); if (handle == NULL) { @@ -58,23 +59,27 @@ int download_to_buffer(const char* url, char** buf, uint32_t* length) curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, (curl_write_callback)&download_write_buffer_callback); curl_easy_setopt(handle, CURLOPT_WRITEDATA, &response); curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0"); + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_perform(handle); curl_easy_cleanup(handle); - if (response.content) { + if (response.length > 0) { *length = response.length; *buf = response.content; + } else { + res = -1; } curl_global_cleanup(); - return 0; + return res; } int download_to_file(const char* url, const char* filename) { + int res = 0; curl_global_init(CURL_GLOBAL_ALL); CURL* handle = curl_easy_init(); if (handle == NULL) { @@ -91,14 +96,21 @@ int download_to_file(const char* url, const char* filename) curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, (curl_write_callback)&fwrite); curl_easy_setopt(handle, CURLOPT_WRITEDATA, f); curl_easy_setopt(handle, CURLOPT_USERAGENT, "InetURL/1.0"); + curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(handle, CURLOPT_URL, url); curl_easy_perform(handle); curl_easy_cleanup(handle); + off_t sz = ftello(f); + if ((sz == 0) || (sz == (off_t)-1)) { + res = -1; + remove(filename); + } + fclose(f); curl_global_cleanup(); - return 0; + return res; } |