]> git.street.me.uk Git - andy/viking.git/blobdiff - src/curl_download.c
Save bfilter values used for subsequent reuse in a session.
[andy/viking.git] / src / curl_download.c
index cbaa4031999c6d3f5944c1eb0d6332b3e7f80fa4..14a9a6e6675bb1abbe62d12793838047ae144bd9 100644 (file)
@@ -27,6 +27,7 @@
 #endif
 
 #include <stdio.h>
 #endif
 
 #include <stdio.h>
+#include <string.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -59,19 +60,19 @@ static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream
   return fwrite(ptr, size, nmemb, stream);
 }
 
   return fwrite(ptr, size, nmemb, stream);
 }
 
-static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
+static size_t curl_get_etag_func(void *ptr, size_t size, size_t nmemb, void *stream)
 {
 #define ETAG_KEYWORD "ETag: "
 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
 {
 #define ETAG_KEYWORD "ETag: "
 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
-  gchar **etag = stream;
+  DownloadFileOptions *file_options = (DownloadFileOptions*)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) {
   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) {
-      *etag = g_strndup(etag_str, end_str - etag_str);
-      g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
+      file_options->new_etag = g_strndup(etag_str, end_str - etag_str);
+      g_debug("%s: ETAG found: %s", __FUNCTION__, file_options->new_etag);
     }
   }
   return nmemb;
     }
   }
   return nmemb;
@@ -124,7 +125,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
 
   curl = handle ? handle : curl_easy_init ();
   if ( !curl ) {
 
   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 );
   }
   if (vik_verbose)
     curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
@@ -161,7 +162,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
           curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
         }
         /* store the new etag from the server in an option value */
           curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
         }
         /* store the new etag from the server in an option value */
-        curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
+        curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, file_options);
         curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
       }
     }
         curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
       }
     }
@@ -175,7 +176,7 @@ int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, D
     glong response;
     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
     if (response == 304) {         // 304 = Not Modified
     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;
     } else if (response == 200 ||  // http: 200 = Ok
                response == 226) {  // ftp:  226 = sucess
       gdouble size;
@@ -183,15 +184,15 @@ 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)
          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
       else
-        res = DOWNLOAD_NO_ERROR;
+        res = CURL_DOWNLOAD_NO_ERROR;
     } else {
       g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
     } else {
       g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
-      res = DOWNLOAD_ERROR;
+      res = CURL_DOWNLOAD_ERROR;
     }
   } else {
     }
   } else {
-    res = DOWNLOAD_ERROR;
+    res = CURL_DOWNLOAD_ERROR;
   }
   if (!handle)
      curl_easy_cleanup ( curl );
   }
   if (!handle)
      curl_easy_cleanup ( curl );
@@ -208,10 +209,19 @@ int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, Down
   int ret;
   gchar *full = NULL;
 
   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 );
   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;
   full = NULL;
 
   return ret;