]> git.street.me.uk Git - andy/viking.git/blob - src/curl_download.c
Add ability to append other type of track or route to a route or track.
[andy/viking.git] / src / curl_download.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5  * Copyright (C) 2007, Quy Tonthat <qtonthat@gmail.com>
6  * Copyright (C) 2009-2010, Jocelyn Jaubert <jocelyn.jaubert@gmail.com>
7  * Copyright (C) 2010, Sven Wegener <sven.wegener@stealer.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <glib.h>
36 #include <glib/gstdio.h>
37 #include <glib/gi18n.h>
38 #include <glib/gprintf.h>
39 #include <gtk/gtk.h>
40
41 #include <curl/curl.h>
42
43 #include "background.h"
44 #include "dir.h"
45 #include "file.h"
46 #include "globals.h"
47 #include "curl_download.h"
48
49 gchar *curl_download_user_agent = NULL;
50
51 /*
52  * Even if writing to FILE* is supported by libcurl by default,
53  * it seems that it is non-portable (win32 DLL specific).
54  *
55  * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
56  */
57 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
58 {
59   return fwrite(ptr, size, nmemb, stream);
60 }
61
62 static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
63 {
64 #define ETAG_KEYWORD "ETag: "
65 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
66   gchar **etag = stream;
67   size_t len = size*nmemb;
68   char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD);
69   if (str) {
70     char *etag_str = str + ETAG_LEN;
71     char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
72     if (etag_str && end_str) {
73       *etag = g_strndup(etag_str, end_str - etag_str);
74       g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
75     }
76   }
77   return nmemb;
78 }
79
80 static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
81 {
82   return a_background_testcancel(NULL);
83 }
84
85 static gchar *get_cookie_file(gboolean init)
86 {
87   static gchar *cookie_file = NULL;
88
89   // Wipe any previous cookies set on startup (for some reason??)
90   // Startup is single threaded so don't care about mutexes
91   if (init) {
92     static gchar *cookie_fn = "cookies.txt";
93     const gchar *viking_dir = a_get_viking_dir();
94     cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
95     g_unlink(cookie_file);
96     return NULL;
97   }
98
99   return(cookie_file);
100 }
101
102 /* This should to be called from main() to make sure thread safe */
103 void curl_download_init()
104 {
105   curl_global_init(CURL_GLOBAL_ALL);
106   get_cookie_file(TRUE);
107   curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
108 }
109
110 /* This should to be called from main() to make sure thread safe */
111 void curl_download_uninit()
112 {
113   curl_global_cleanup();
114 }
115
116 int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
117 {
118   CURL *curl;
119   struct curl_slist *curl_send_headers = NULL;
120   CURLcode res = CURLE_FAILED_INIT;
121   const gchar *cookie_file;
122
123   g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
124
125   curl = handle ? handle : curl_easy_init ();
126   if ( !curl ) {
127     return DOWNLOAD_ERROR;
128   }
129   if (vik_verbose)
130     curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
131   curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up!
132   curl_easy_setopt ( curl, CURLOPT_URL, uri );
133   curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
134   curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
135   curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
136   curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
137   curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
138   if (options != NULL) {
139     if(options->referer != NULL)
140       curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
141     if(options->follow_location != 0) {
142       curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
143       curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
144     }
145     if (file_options != NULL) {
146       if(options->check_file_server_time && file_options->time_condition != 0) {
147         /* if file exists, check against server if file is recent enough */
148         curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
149         curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition);
150       }
151       if (options->use_etag) {
152         if (file_options->etag != NULL) {
153           /* add an header on the HTTP request */
154           char str[60];
155           g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
156           curl_send_headers = curl_slist_append(curl_send_headers, str);
157           curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
158         }
159         /* store the new etag from the server in an option value */
160         curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
161         curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
162       }
163     }
164   }
165   curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
166   if ((cookie_file = get_cookie_file(FALSE)) != NULL)
167     curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
168   res = curl_easy_perform ( curl );
169
170   if (res == 0) {
171     glong response;
172     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
173     if (response == 304) {         // 304 = Not Modified
174       res = DOWNLOAD_NO_NEWER_FILE;
175     } else if (response == 200 ||  // http: 200 = Ok
176                response == 226) {  // ftp:  226 = sucess
177       gdouble size;
178       /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION 
179          when the server has a (incorrect) time earlier than the time on the file we already have */
180       curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
181       if (size == 0)
182         res = DOWNLOAD_ERROR;
183       else
184         res = DOWNLOAD_NO_ERROR;
185     } else {
186       g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
187       res = DOWNLOAD_ERROR;
188     }
189   } else {
190     res = DOWNLOAD_ERROR;
191   }
192   if (!handle)
193      curl_easy_cleanup ( curl );
194   if (curl_send_headers) {
195     curl_slist_free_all(curl_send_headers);
196     curl_send_headers = NULL;
197     curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
198   }
199   return res;
200 }
201
202 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle )
203 {
204   int ret;
205   gchar *full = NULL;
206
207   /* Compose the full url */
208   full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
209   ret = curl_download_uri ( full, f, options, file_options, handle );
210   g_free ( full );
211   full = NULL;
212
213   return ret;
214 }
215
216 void * curl_download_handle_init ()
217 {
218   return curl_easy_init();
219 }
220
221 void curl_download_handle_cleanup ( void *handle )
222 {
223   curl_easy_cleanup(handle);
224 }