]> git.street.me.uk Git - andy/viking.git/blob - src/curl_download.c
Do not use preset Google cookie when we don't even use google maps (images) anymore.
[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 "file.h"
45 #include "globals.h"
46 #include "curl_download.h"
47
48 gchar *curl_download_user_agent = NULL;
49
50 /*
51  * Even if writing to FILE* is supported by libcurl by default,
52  * it seems that it is non-portable (win32 DLL specific).
53  *
54  * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
55  */
56 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
57 {
58   return fwrite(ptr, size, nmemb, stream);
59 }
60
61 static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
62 {
63 #define ETAG_KEYWORD "ETag: "
64 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
65   gchar **etag = stream;
66   size_t len = size*nmemb;
67   char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD);
68   if (str) {
69     char *etag_str = str + ETAG_LEN;
70     char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
71     if (etag_str && end_str) {
72       *etag = g_strndup(etag_str, end_str - etag_str);
73       g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
74     }
75   }
76   return nmemb;
77 }
78
79 static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
80 {
81   return a_background_testcancel(NULL);
82 }
83
84 static gchar *get_cookie_file(gboolean init)
85 {
86   static gchar *cookie_file = NULL;
87
88   // Wipe any previous cookies set on startup (for some reason??)
89   // Startup is single threaded so don't care about mutexes
90   if (init) {
91     static gchar *cookie_fn = "cookies.txt";
92     const gchar *viking_dir = a_get_viking_dir();
93     cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
94     g_unlink(cookie_file);
95     return NULL;
96   }
97
98   return(cookie_file);
99 }
100
101 /* This should to be called from main() to make sure thread safe */
102 void curl_download_init()
103 {
104   curl_global_init(CURL_GLOBAL_ALL);
105   get_cookie_file(TRUE);
106   curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
107 }
108
109 /* This should to be called from main() to make sure thread safe */
110 void curl_download_uninit()
111 {
112   curl_global_cleanup();
113 }
114
115 int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
116 {
117   CURL *curl;
118   struct curl_slist *curl_send_headers = NULL;
119   CURLcode res = CURLE_FAILED_INIT;
120   const gchar *cookie_file;
121
122   g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
123
124   curl = handle ? handle : curl_easy_init ();
125   if ( !curl ) {
126     return DOWNLOAD_ERROR;
127   }
128   if (vik_verbose)
129     curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
130   curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up!
131   curl_easy_setopt ( curl, CURLOPT_URL, uri );
132   curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
133   curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
134   curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
135   curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
136   curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
137   if (options != NULL) {
138     if(options->referer != NULL)
139       curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
140     if(options->follow_location != 0) {
141       curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
142       curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
143     }
144     if (file_options != NULL) {
145       if(options->check_file_server_time && file_options->time_condition != 0) {
146         /* if file exists, check against server if file is recent enough */
147         curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
148         curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition);
149       }
150       if (options->use_etag) {
151         if (file_options->etag != NULL) {
152           /* add an header on the HTTP request */
153           char str[60];
154           g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
155           curl_send_headers = curl_slist_append(curl_send_headers, str);
156           curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
157         }
158         /* store the new etag from the server in an option value */
159         curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
160         curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
161       }
162     }
163   }
164   curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
165   if ((cookie_file = get_cookie_file(FALSE)) != NULL)
166     curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
167   res = curl_easy_perform ( curl );
168
169   if (res == 0) {
170     glong response;
171     curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
172     if (response == 304) {         // 304 = Not Modified
173       res = DOWNLOAD_NO_NEWER_FILE;
174     } else if (response == 200 ||  // http: 200 = Ok
175                response == 226) {  // ftp:  226 = sucess
176       gdouble size;
177       /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION 
178          when the server has a (incorrect) time earlier than the time on the file we already have */
179       curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
180       if (size == 0)
181         res = DOWNLOAD_ERROR;
182       else
183         res = DOWNLOAD_NO_ERROR;
184     } else {
185       g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
186       res = DOWNLOAD_ERROR;
187     }
188   } else {
189     res = DOWNLOAD_ERROR;
190   }
191   if (!handle)
192      curl_easy_cleanup ( curl );
193   if (curl_send_headers) {
194     curl_slist_free_all(curl_send_headers);
195     curl_send_headers = NULL;
196     curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
197   }
198   return res;
199 }
200
201 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle )
202 {
203   int ret;
204   gchar *full = NULL;
205
206   /* Compose the full url */
207   full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
208   ret = curl_download_uri ( full, f, options, file_options, handle );
209   g_free ( full );
210   full = NULL;
211
212   return ret;
213 }
214
215 void * curl_download_handle_init ()
216 {
217   return curl_easy_init();
218 }
219
220 void curl_download_handle_cleanup ( void *handle )
221 {
222   curl_easy_cleanup(handle);
223 }