X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/494eb3886664d6470d103f088e03f10ca364a977..80471a6a905e00bf80ad04fa2061f88ea81f15cb:/src/curl_download.c?ds=inline diff --git a/src/curl_download.c b/src/curl_download.c index c287fe7b..302f4b94 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -19,20 +19,34 @@ * */ -#include - #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include + +#include +#include +#include #include -#ifdef HAVE_LIBCURL #include #include "file.h" +#include "globals.h" #include "curl_download.h" +/* + * Even if writing to FILE* is supported by libcurl by default, + * it seems that it is non-portable (win32 DLL specific). + * + * So, we provide our own trivial CURLOPT_WRITEFUNCTION. + */ +static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + return fwrite(ptr, size, nmemb, stream); +} + static gchar *get_cookie_file(gboolean init) { static gchar *cookie_file = NULL; @@ -42,28 +56,44 @@ static gchar *get_cookie_file(gboolean init) mutex = g_mutex_new(); static gchar *cookie_fn = "cookies.txt"; const gchar *viking_dir = a_get_viking_dir(); - cookie_file = g_strdup_printf("%s/%s", viking_dir, cookie_fn); - unlink(cookie_file); + cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); + g_unlink(cookie_file); return NULL; } g_assert(cookie_file != NULL); g_mutex_lock(mutex); - if (access(cookie_file, F_OK)) { /* file not there */ - FILE * out_file = fopen("/dev/null", "w"); + if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */ + 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_WRITEFUNCTION, curl_write_func); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file); res = curl_easy_perform(curl); if (res != CURLE_OK) { - g_warning("%s() Curl perform failed: %s\n", __PRETTY_FUNCTION__, + g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__, curl_easy_strerror(res)); - unlink(cookie_file); + g_unlink(cookie_file); } 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); @@ -83,14 +113,25 @@ 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 ); - if (options != NULL && options->referer != NULL) - curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); - curl_easy_setopt ( curl, CURLOPT_USERAGENT, "viking/" VERSION " libcurl/7.15.4" ); + curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); + if (options != NULL) { + if(options->referer != NULL) + curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); + if(options->follow_location != 0) { + curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); + curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); + } + } + curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" ); if ((cookie_file = get_cookie_file(FALSE)) != NULL) curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); res = curl_easy_perform ( curl ); @@ -99,17 +140,16 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) return(res); } -int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options ) +int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp ) { int ret; gchar *full = NULL; /* Compose the full url */ - full = g_strdup_printf ( "http://%s%s", hostname, uri ); + full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); ret = curl_download_uri ( full, f, options ); g_free ( full ); full = NULL; return (ret ? -2 : 0); /* -2 HTTP error */ } -#endif /* HAVE_LIB_CURL */