X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/5a9cffb11d58c725f425828c09c814d1139bbae1..ccab0e9e7869f120d20bd41bac6043b37c88b2ec:/src/curl_download.c diff --git a/src/curl_download.c b/src/curl_download.c index 85aac91f..1f5deff0 100644 --- a/src/curl_download.c +++ b/src/curl_download.c @@ -27,6 +27,7 @@ #endif #include +#include #ifdef HAVE_UNISTD_H #include @@ -41,6 +42,7 @@ #include #include "background.h" +#include "dir.h" #include "file.h" #include "globals.h" #include "curl_download.h" @@ -58,19 +60,19 @@ static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream return fwrite(ptr, size, nmemb, stream); } -static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream) +static size_t curl_get_etag_func(void *ptr, size_t size, size_t nmemb, void *stream) { #define ETAG_KEYWORD "ETag: " #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1) - gchar **etag = stream; + CurlDownloadOptions *cdo = (CurlDownloadOptions*)stream; size_t len = size*nmemb; char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD); if (str) { char *etag_str = str + ETAG_LEN; char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n"); if (etag_str && end_str) { - *etag = g_strndup(etag_str, end_str - etag_str); - g_debug("%s: ETAG found: %s", __FUNCTION__, *etag); + cdo->new_etag = g_strndup(etag_str, end_str - etag_str); + g_debug("%s: ETAG found: %s", __FUNCTION__, cdo->new_etag); } } return nmemb; @@ -81,64 +83,10 @@ static int curl_progress_func(void *clientp, double dltotal, double dlnow, doubl return a_background_testcancel(NULL); } -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_build_filename(viking_dir, cookie_fn, NULL); - g_unlink(cookie_file); - return NULL; - } - - g_assert(cookie_file != NULL); - - g_mutex_lock(mutex); - 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_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); - if (res != CURLE_OK) { - g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__, - curl_easy_strerror(res)); - 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); - - 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); curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version()); } @@ -148,22 +96,28 @@ void curl_download_uninit() curl_global_cleanup(); } -int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle ) +/** + * + */ +CURL_download_t curl_download_uri ( const char *uri, FILE *f, DownloadFileOptions *options, CurlDownloadOptions *cdo, void *handle ) { CURL *curl; struct curl_slist *curl_send_headers = NULL; CURLcode res = CURLE_FAILED_INIT; - const gchar *cookie_file; g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri); curl = handle ? handle : curl_easy_init (); if ( !curl ) { - return DOWNLOAD_ERROR; + return CURL_DOWNLOAD_ERROR; } if (vik_verbose) curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up! + if ( options != NULL && options->user_pass ) { + curl_easy_setopt ( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); + curl_easy_setopt ( curl, CURLOPT_USERPWD, options->user_pass ); + } curl_easy_setopt ( curl, CURLOPT_URL, uri ); curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); @@ -177,36 +131,34 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); } - if (file_options != NULL) { - if(options->check_file_server_time && file_options->time_condition != 0) { + if (cdo != NULL) { + if(options->check_file_server_time && cdo->time_condition != 0) { /* if file exists, check against server if file is recent enough */ curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); - curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition); + curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, cdo->time_condition); } if (options->use_etag) { - if (file_options->etag != NULL) { + if (cdo->etag != NULL) { /* add an header on the HTTP request */ char str[60]; - g_snprintf(str, 60, "If-None-Match: %s", file_options->etag); + g_snprintf(str, 60, "If-None-Match: %s", cdo->etag); curl_send_headers = curl_slist_append(curl_send_headers, str); curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers); } /* store the new etag from the server in an option value */ - curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag)); + curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, cdo); curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func); } } } 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 ); if (res == 0) { glong response; curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); if (response == 304) { // 304 = Not Modified - res = DOWNLOAD_NO_NEWER_FILE; + res = CURL_DOWNLOAD_NO_NEWER_FILE; } else if (response == 200 || // http: 200 = Ok response == 226) { // ftp: 226 = sucess gdouble size; @@ -214,15 +166,15 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D when the server has a (incorrect) time earlier than the time on the file we already have */ curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); if (size == 0) - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; else - res = DOWNLOAD_NO_ERROR; + res = CURL_DOWNLOAD_NO_ERROR; } else { g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri); - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; } } else { - res = DOWNLOAD_ERROR; + res = CURL_DOWNLOAD_ERROR; } if (!handle) curl_easy_cleanup ( curl ); @@ -234,16 +186,32 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D return res; } -int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle ) +/** + * curl_download_get_url: + * Either hostname and/or uri should be defined + * + */ +CURL_download_t curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadFileOptions *options, gboolean ftp, CurlDownloadOptions *cdo, void *handle ) { - int ret; gchar *full = NULL; - /* Compose the full url */ - full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); - ret = curl_download_uri ( full, f, options, file_options, handle ); - g_free ( full ); - full = NULL; + if ( hostname && strstr ( hostname, "://" ) != NULL ) + /* Already full url */ + full = (gchar *) hostname; + else if ( uri && strstr ( uri, "://" ) != NULL ) + /* Already full url */ + full = (gchar *) uri; + else if ( hostname && uri ) + /* Compose the full url */ + full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); + else { + return CURL_DOWNLOAD_ERROR; + } + + CURL_download_t ret = curl_download_uri ( full, f, options, cdo, handle ); + // Only free newly allocated memory + if ( hostname != full && uri != full ) + g_free ( full ); return ret; }