From fdea23960ae9ea99fa28db49c4da5791a8affca7 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sun, 5 Feb 2012 22:18:15 +0100 Subject: download and cache version information from itunes.com --- src/download.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/download.c (limited to 'src/download.c') diff --git a/src/download.c b/src/download.c new file mode 100644 index 0000000..211987a --- /dev/null +++ b/src/download.c @@ -0,0 +1,104 @@ +/* + * download.c + * file download helper functions + * + * Copyright (c) 2012 Nikias Bassen. All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include +#include +#include +#include + +#include "download.h" + +typedef struct { + int length; + char* content; +} curl_response; + +static size_t download_write_buffer_callback(char* data, size_t size, size_t nmemb, curl_response* response) { + size_t total = size * nmemb; + if (total != 0) { + response->content = realloc(response->content, response->length + total + 1); + memcpy(response->content + response->length, data, total); + response->content[response->length + total] = '\0'; + response->length += total; + } + return total; +} + +int download_to_buffer(const char* url, char** buf, uint32_t* length) +{ + curl_global_init(CURL_GLOBAL_ALL); + CURL* handle = curl_easy_init(); + if (handle == NULL) { + error("ERROR: could not initialize CURL\n"); + return -1; + } + + curl_response response; + response.length = 0; + response.content = malloc(1); + response.content[0] = '\0'; + + 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_URL, url); + + curl_easy_perform(handle); + curl_easy_cleanup(handle); + + if (response.content) { + *length = response.length; + *buf = response.content; + } + + curl_global_cleanup(); + + return 0; +} + +int download_to_file(const char* url, const char* filename) +{ + curl_global_init(CURL_GLOBAL_ALL); + CURL* handle = curl_easy_init(); + if (handle == NULL) { + error("ERROR: could not initialize CURL\n"); + return -1; + } + + FILE* f = fopen(filename, "wb"); + if (!f) { + error("ERROR: cannot open '%s' for writing\n", filename); + return -1; + } + + 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_URL, url); + + curl_easy_perform(handle); + curl_easy_cleanup(handle); + + fclose(f); + + curl_global_cleanup(); + + return 0; +} -- cgit v1.1-32-gdbae