]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Fix #2958601: Support for non ascii characters in "Go-to" search
[andy/viking.git] / src / curl_download.c
index b741bdb52434c89b0f99b3c00e3c36d558e43bb4..ff6a7db6c45b3f9cb6ddd59be3f50a25eeaba3bb 100644 (file)
  *
  */
 
  *
  */
 
-#include <unistd.h>
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include <stdio.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <glib/gprintf.h>
 #include <gtk/gtk.h>
 
 #include <curl/curl.h>
 
 #include <gtk/gtk.h>
 
 #include <curl/curl.h>
 
+#include "background.h"
 #include "file.h"
 #include "file.h"
+#include "globals.h"
 #include "curl_download.h"
 
 #include "curl_download.h"
 
+gchar *curl_download_user_agent;
+
+/*
+ * 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 int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
+{
+  return a_background_testcancel(NULL);
+}
+
 static gchar *get_cookie_file(gboolean init)
 {
   static gchar *cookie_file = NULL;
 static gchar *get_cookie_file(gboolean init)
 {
   static gchar *cookie_file = NULL;
@@ -41,28 +69,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();
     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);
     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();
     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_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
-    curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
+    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) {
     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));
           curl_easy_strerror(res));
-      unlink(cookie_file);
+      g_unlink(cookie_file);
     }
     curl_easy_cleanup(curl);
     }
     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);
 
   }
   g_mutex_unlock(mutex);
 
@@ -74,46 +118,93 @@ void curl_download_init()
 {
   curl_global_init(CURL_GLOBAL_ALL);
   get_cookie_file(TRUE);
 {
   curl_global_init(CURL_GLOBAL_ALL);
   get_cookie_file(TRUE);
+  curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
 }
 
 }
 
-int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
+int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition, void *handle )
 {
   CURL *curl;
   CURLcode res = CURLE_FAILED_INIT;
   const gchar *cookie_file;
 
 {
   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) {
-        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, "viking/" 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 );
-      curl_easy_cleanup ( curl );
+  g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
+
+  curl = handle ? handle : curl_easy_init ();
+  if ( !curl ) {
+    return DOWNLOAD_ERROR;
+  }
+  if (vik_verbose)
+    curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
+  curl_easy_setopt ( curl, CURLOPT_URL, uri );
+  curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
+  curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
+  curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
+  curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
+  curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_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);
+    }
+    if(options->check_file_server_time && 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, time_condition);
+    }
+  }
+  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;
+    } else if (response == 200 ||  // http: 200 = Ok
+               response == 226) {  // ftp:  226 = sucess
+      gdouble size;
+      /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION 
+         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;
+      else
+        res = DOWNLOAD_NO_ERROR;
+    } else {
+      g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, time_condition);
+      res = DOWNLOAD_ERROR;
     }
     }
-  return(res);
+  } else {
+    res = DOWNLOAD_ERROR;
+  }
+  if (!handle)
+     curl_easy_cleanup ( curl );
+  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, time_t time_condition, void *handle )
 {
   int ret;
   gchar *full = NULL;
 
   /* Compose the full url */
 {
   int ret;
   gchar *full = NULL;
 
   /* Compose the full url */
-  full = g_strdup_printf ( "http://%s%s", hostname, uri );
-  ret = curl_download_uri ( full, f, options );
+  full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
+  ret = curl_download_uri ( full, f, options, time_condition, handle );
   g_free ( full );
   full = NULL;
 
   g_free ( full );
   full = NULL;
 
-  return (ret ? -2 : 0);   /* -2 HTTP error */
+  return ret;
+}
+
+void * curl_download_handle_init ()
+{
+  return curl_easy_init();
+}
+
+void curl_download_handle_cleanup ( void *handle )
+{
+  curl_easy_cleanup(handle);
 }
 }