]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Fix typo
[andy/viking.git] / src / curl_download.c
index 2e7a7ef9eeda0a11accceb100734c6e7610aafc4..85aac91ffb7997425f9a6bbdcfee192b28a0b356 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
@@ -42,7 +45,7 @@
 #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 +58,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;
@@ -136,6 +142,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;
@@ -151,6 +163,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);
@@ -173,8 +186,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);
         }
@@ -205,7 +218,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 {
@@ -215,6 +228,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
      curl_easy_cleanup ( curl );
   if (curl_send_headers) {
     curl_slist_free_all(curl_send_headers);
+    curl_send_headers = NULL;
     curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
   }
   return res;