]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Remove dependencies to gob2
[andy/viking.git] / src / curl_download.c
index ed822e501758e78a0782357e7ea2e33349b72ac8..302f4b94e125350b3d9b08fe43ad1d9a778f2756 100644 (file)
 #include <curl/curl.h>
 
 #include "file.h"
 #include <curl/curl.h>
 
 #include "file.h"
+#include "globals.h"
 #include "curl_download.h"
 
 #include "curl_download.h"
 
+/*
+ * 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 gchar *get_cookie_file(gboolean init)
 {
   static gchar *cookie_file = NULL;
 static gchar *get_cookie_file(gboolean init)
 {
   static gchar *cookie_file = NULL;
@@ -53,11 +65,20 @@ static gchar *get_cookie_file(gboolean init)
 
   g_mutex_lock(mutex);
   if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) {  /* file not there */
 
   g_mutex_lock(mutex);
   if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) {  /* file not there */
-    FILE * out_file = g_fopen("/dev/null", "w");
+    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_FILE, out_file );
     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_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) {
@@ -68,6 +89,11 @@ static gchar *get_cookie_file(gboolean init)
     curl_easy_cleanup(curl);
     fclose(out_file);
     out_file = NULL;
     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);
 
@@ -87,11 +113,16 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
   CURLcode res = CURLE_FAILED_INIT;
   const gchar *cookie_file;
 
   CURLcode res = CURLE_FAILED_INIT;
   const gchar *cookie_file;
 
+  g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
+
   curl = curl_easy_init ();
   if ( curl )
     {
   curl = curl_easy_init ();
   if ( curl )
     {
+      if (vik_verbose)
+        curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
       curl_easy_setopt ( curl, CURLOPT_URL, uri );
       curl_easy_setopt ( curl, CURLOPT_FILE, f );
       curl_easy_setopt ( curl, CURLOPT_URL, uri );
       curl_easy_setopt ( curl, CURLOPT_FILE, f );
+      curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
       if (options != NULL) {
         if(options->referer != NULL)
           curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
       if (options != NULL) {
         if(options->referer != NULL)
           curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);