X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/11f88b69c2319c342fe9f577dba2ba01a1335e26..5dab4f3fa3c22facc679ba558dd6987d3d8f34e0:/src/curl_download.c diff --git a/src/curl_download.c b/src/curl_download.c index 980ba576..f19d5aa9 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -19,6 +19,8 @@ * */ +#include + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -27,38 +29,87 @@ #ifdef HAVE_LIBCURL #include -#endif +#include "file.h" #include "curl_download.h" -int curl_download_uri ( const char *uri, FILE *f ) +static gchar *get_cookie_file(gboolean init) +{ + static gchar *cookie_file = NULL; + static GMutex *mutex = NULL; + + if (init) { /* to make sure it's thread safe */ + 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); + 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"); + CURLcode res; + CURL *curl = curl_easy_init(); + 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_COOKIEJAR, cookie_file); + res = curl_easy_perform(curl); + if (res != CURLE_OK) { + fprintf(stderr, "%s() Curl perform failed: %s\n", __PRETTY_FUNCTION__, + curl_easy_strerror(res)); + unlink(cookie_file); + } + curl_easy_cleanup(curl); + } + g_mutex_unlock(mutex); + + return(cookie_file); +} + +/* This should to be called from main() to make sure thread safe */ +void curl_download_init() +{ + curl_global_init(CURL_GLOBAL_ALL); + get_cookie_file(TRUE); +} + +int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options ) { -#ifdef HAVE_LIBCURL CURL *curl; CURLcode res = CURLE_FAILED_INIT; + const gchar *cookie_file; curl = curl_easy_init (); if ( curl ) { 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" ); + if (cookie_file = get_cookie_file(FALSE)) + curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); res = curl_easy_perform ( curl ); curl_easy_cleanup ( curl ); } return(res); -#endif } -int curl_download_get_url ( const char *hostname, const char *uri, FILE *f ) +int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options ) { int ret; gchar *full = NULL; /* Compose the full url */ full = g_strdup_printf ( "http://%s%s", hostname, uri ); - ret = curl_download_uri ( full, f ); + ret = curl_download_uri ( full, f, options ); g_free ( full ); full = NULL; return (ret ? -2 : 0); /* -2 HTTP error */ } +#endif /* HAVE_LIB_CURL */