]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Enable Cache conversion in the Python tool viking-cache.py
[andy/viking.git] / src / curl_download.c
index 2b7f206c49038037e9e1cf6ebdb53be40ebd1298..be54741cabf143324f5315318c29f9851e8f7f56 100644 (file)
@@ -1,7 +1,10 @@
 /*
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
- * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
+ * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
+ * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
+ * Copyright (C) 2009-2010, Jocelyn Jaubert <jocelyn.jaubert@gmail.com>
+ * Copyright (C) 2010, Sven Wegener <sven.wegener@stealer.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,6 +27,7 @@
 #endif
 
 #include <stdio.h>
+#include <string.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #include <curl/curl.h>
 
 #include "background.h"
+#include "dir.h"
 #include "file.h"
 #include "globals.h"
 #include "curl_download.h"
 
-gchar *curl_download_user_agent;
+gchar *curl_download_user_agent = NULL;
 
 /*
  * Even if writing to FILE* is supported by libcurl by default,
@@ -55,16 +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(void *ptr, size_t size, size_t nmemb, gchar **stream)
+static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
 {
+#define ETAG_KEYWORD "ETag: "
+#define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
+  gchar **etag = stream;
   size_t len = size*nmemb;
-  char *str = g_strstr_len(ptr, len, "ETag:");
+  char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD);
   if (str) {
-    char *etag_str = str + strlen("ETag: ");
-    char *end_str = g_strstr_len(etag_str, len, "\n");
+    char *etag_str = str + ETAG_LEN;
+    char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
     if (etag_str && end_str) {
-      end_str = '\0';
-      *stream = g_strndup(etag_str, len);
+      *etag = g_strndup(etag_str, end_str - etag_str);
+      g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
     }
   }
   return nmemb;
@@ -78,10 +86,10 @@ static int curl_progress_func(void *clientp, double dltotal, double dlnow, doubl
 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();
+  // Wipe any previous cookies set on startup (for some reason??)
+  // Startup is single threaded so don't care about mutexes
+  if (init) {
     static gchar *cookie_fn = "cookies.txt";
     const gchar *viking_dir = a_get_viking_dir();
     cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
@@ -89,42 +97,6 @@ static gchar *get_cookie_file(gboolean init)
     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);
 }
 
@@ -136,6 +108,12 @@ void curl_download_init()
   curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
 }
 
+/* This should to be called from main() to make sure thread safe */
+void curl_download_uninit()
+{
+  curl_global_cleanup();
+}
+
 int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
 {
   CURL *curl;
@@ -147,10 +125,15 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
 
   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);
@@ -173,8 +156,8 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
       if (options->use_etag) {
         if (file_options->etag != NULL) {
           /* add an header on the HTTP request */
-          char str[50];
-          g_snprintf(str, 50, "If-None-Match: %s", file_options->etag);
+          char str[60];
+          g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
           curl_send_headers = curl_slist_append(curl_send_headers, str);
           curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
         }
@@ -189,15 +172,11 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
     curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
   res = curl_easy_perform ( curl );
 
-  if (file_options->new_etag) {
-    printf ("curl_download - %s", file_options->new_etag);
-  }
-
   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;
@@ -205,20 +184,23 @@ 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 (time_condition = %ld)\n", __FUNCTION__, response, uri, file_options->time_condition);
-      res = DOWNLOAD_ERROR;
+      g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
+      res = CURL_DOWNLOAD_ERROR;
     }
   } else {
-    res = DOWNLOAD_ERROR;
+    res = CURL_DOWNLOAD_ERROR;
   }
   if (!handle)
      curl_easy_cleanup ( curl );
-  if (curl_send_headers)
+  if (curl_send_headers) {
     curl_slist_free_all(curl_send_headers);
+    curl_send_headers = NULL;
+    curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
+  }
   return res;
 }
 
@@ -227,10 +209,19 @@ int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, Down
   int ret;
   gchar *full = NULL;
 
-  /* Compose the full url */
-  full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
+  if ( strstr ( hostname, "://" ) != NULL )
+    /* Already full url */
+    full = (gchar *) hostname;
+  else if ( strstr ( uri, "://" ) != NULL )
+    /* Already full url */
+    full = (gchar *) uri;
+  else
+    /* 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 );
+  /* Free newly allocated memory, but do not free uri */
+  if ( hostname != full && uri != full )
+    g_free ( full );
   full = NULL;
 
   return ret;