]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Replace 'Add Track' tool with 'Create Route' tool.
[andy/viking.git] / src / curl_download.c
index dbbcfa6be3c2dc71f0d7243a7566d99ef8f893f4..2ef4c5bb960da158bed6f2349331b88444d8520c 100644 (file)
@@ -41,6 +41,7 @@
 #include <curl/curl.h>
 
 #include "background.h"
+#include "dir.h"
 #include "file.h"
 #include "globals.h"
 #include "curl_download.h"
@@ -62,14 +63,15 @@ static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *str
 {
 #define ETAG_KEYWORD "ETag: "
 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
+  gchar **etag = 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) {
-      stream = (void*) g_strndup(etag_str, end_str - etag_str);
-      g_debug("%s: ETAG found: %s", __FUNCTION__, (gchar*)stream);
+      *etag = g_strndup(etag_str, end_str - etag_str);
+      g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
     }
   }
   return nmemb;
@@ -83,10 +85,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);
@@ -94,42 +96,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);
 }
 
@@ -141,6 +107,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;
@@ -156,6 +128,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
   }
   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!
   curl_easy_setopt ( curl, CURLOPT_URL, uri );
   curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
   curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
@@ -210,7 +183,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
       else
         res = DOWNLOAD_NO_ERROR;
     } else {
-      g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, file_options->time_condition);
+      g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
       res = DOWNLOAD_ERROR;
     }
   } else {