From c0f481c0bb576505f736437e587aca993727a714 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 9 Feb 2012 10:50:24 +0100 Subject: download: better error handling --- src/download.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/download.c') 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; } -- cgit v1.1-32-gdbae