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