X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/3a0c074a761e0c0dd783da13720107ce890d4ae7..5ff75d1ed34298a223c651d46fe2aa000db3e459:/src/curl_download.c diff --git a/src/curl_download.c b/src/curl_download.c index 4b13b688..f230dca0 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -28,13 +28,18 @@ #include #include #include +#include #include #include +#include "background.h" #include "file.h" +#include "globals.h" #include "curl_download.h" +gchar *curl_download_user_agent; + /* * Even if writing to FILE* is supported by libcurl by default, * it seems that it is non-portable (win32 DLL specific). @@ -46,6 +51,11 @@ static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream return fwrite(ptr, size, nmemb, stream); } +static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) +{ + return a_background_testcancel(NULL); +} + static gchar *get_cookie_file(gboolean init) { static gchar *cookie_file = NULL; @@ -64,11 +74,19 @@ static gchar *get_cookie_file(gboolean init) g_mutex_lock(mutex); if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */ - FILE * out_file = g_fopen("/dev/null", "w"); + gchar * name_tmp = NULL; + FILE * out_file = tmpfile(); + if (out_file == NULL) { + // Something wrong with previous call (unsuported?) + name_tmp = g_strdup_printf("%s.tmp", cookie_file); + out_file = g_fopen(name_tmp, "w+b"); + } CURLcode res; CURL *curl = curl_easy_init(); + if (vik_verbose) + curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */ - curl_easy_setopt ( curl, CURLOPT_FILE, out_file ); + curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file ); curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file); res = curl_easy_perform(curl); @@ -80,6 +98,11 @@ static gchar *get_cookie_file(gboolean init) curl_easy_cleanup(curl); fclose(out_file); out_file = NULL; + if (name_tmp != NULL) { + g_remove(name_tmp); + g_free(name_tmp); + name_tmp = NULL; + } } g_mutex_unlock(mutex); @@ -91,6 +114,7 @@ void curl_download_init() { curl_global_init(CURL_GLOBAL_ALL); get_cookie_file(TRUE); + curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version()); } int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) @@ -99,12 +123,19 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) CURLcode res = CURLE_FAILED_INIT; const gchar *cookie_file; + g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri); + curl = curl_easy_init (); if ( curl ) { + if (vik_verbose) + curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); curl_easy_setopt ( curl, CURLOPT_URL, uri ); - curl_easy_setopt ( curl, CURLOPT_FILE, f ); + curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); + curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 ); + curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL ); + curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); if (options != NULL) { if(options->referer != NULL) curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); @@ -113,7 +144,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); } } - curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" ); + curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent ); if ((cookie_file = get_cookie_file(FALSE)) != NULL) curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); res = curl_easy_perform ( curl );