]> git.street.me.uk Git - andy/viking.git/blame - src/curl_download.c
SF#356778: Download Map Tiles using F5
[andy/viking.git] / src / curl_download.c
CommitLineData
3292ba8b
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
a482007a
GB
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>
3292ba8b
GB
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
8c060406
MA
29#include <stdio.h>
30
6a4a29aa
JJ
31#ifdef HAVE_UNISTD_H
32#include <unistd.h>
33#endif
34
45acf79e
MA
35#include <glib.h>
36#include <glib/gstdio.h>
4c77d5e0 37#include <glib/gi18n.h>
6a2692be 38#include <glib/gprintf.h>
3292ba8b
GB
39#include <gtk/gtk.h>
40
3292ba8b 41#include <curl/curl.h>
3292ba8b 42
7711383f 43#include "background.h"
29f1598c 44#include "dir.h"
80d0ff2b 45#include "file.h"
2936913d 46#include "globals.h"
3292ba8b
GB
47#include "curl_download.h"
48
d0b611d7 49gchar *curl_download_user_agent = NULL;
6a2692be 50
3a0c074a
MA
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 */
57static 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
fba991f6 62static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
f039c5ff 63{
a6bf52f9
GB
64#define ETAG_KEYWORD "ETag: "
65#define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
c4a6adde 66 gchar **etag = stream;
f039c5ff 67 size_t len = size*nmemb;
fba991f6 68 char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD);
f039c5ff 69 if (str) {
a6bf52f9
GB
70 char *etag_str = str + ETAG_LEN;
71 char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
f039c5ff 72 if (etag_str && end_str) {
c4a6adde
SW
73 *etag = g_strndup(etag_str, end_str - etag_str);
74 g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
f039c5ff
JJ
75 }
76 }
77 return nmemb;
78}
79
7711383f
JJ
80static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
81{
82 return a_background_testcancel(NULL);
83}
84
80d0ff2b
QT
85static gchar *get_cookie_file(gboolean init)
86{
87 static gchar *cookie_file = NULL;
80d0ff2b 88
2da56da5
RN
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) {
80d0ff2b
QT
92 static gchar *cookie_fn = "cookies.txt";
93 const gchar *viking_dir = a_get_viking_dir();
6f2b61bb 94 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
45acf79e 95 g_unlink(cookie_file);
80d0ff2b
QT
96 return NULL;
97 }
98
80d0ff2b
QT
99 return(cookie_file);
100}
101
102/* This should to be called from main() to make sure thread safe */
103void curl_download_init()
104{
105 curl_global_init(CURL_GLOBAL_ALL);
106 get_cookie_file(TRUE);
6a2692be 107 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
80d0ff2b
QT
108}
109
3eba9bbf
RN
110/* This should to be called from main() to make sure thread safe */
111void curl_download_uninit()
112{
113 curl_global_cleanup();
114}
115
9a021e4f 116int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
3292ba8b 117{
3292ba8b 118 CURL *curl;
f039c5ff 119 struct curl_slist *curl_send_headers = NULL;
11f88b69 120 CURLcode res = CURLE_FAILED_INIT;
80d0ff2b 121 const gchar *cookie_file;
3292ba8b 122
2936913d
GB
123 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
124
825413ba 125 curl = handle ? handle : curl_easy_init ();
6a4a29aa
JJ
126 if ( !curl ) {
127 return DOWNLOAD_ERROR;
128 }
129 if (vik_verbose)
130 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
8cd9c991 131 curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up!
6a4a29aa
JJ
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 }
9a021e4f
JJ
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 }
f039c5ff
JJ
151 if (options->use_etag) {
152 if (file_options->etag != NULL) {
153 /* add an header on the HTTP request */
9004d4ed
GB
154 char str[60];
155 g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
f039c5ff
JJ
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 }
3292ba8b 163 }
6a4a29aa
JJ
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 );
f039c5ff 169
6a4a29aa
JJ
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;
6304fa62
JJ
175 } else if (response == 200 || // http: 200 = Ok
176 response == 226) { // ftp: 226 = sucess
6a4a29aa
JJ
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 {
bda3c239 186 g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
6a4a29aa
JJ
187 res = DOWNLOAD_ERROR;
188 }
189 } else {
190 res = DOWNLOAD_ERROR;
191 }
825413ba
SW
192 if (!handle)
193 curl_easy_cleanup ( curl );
dea59045 194 if (curl_send_headers) {
f039c5ff 195 curl_slist_free_all(curl_send_headers);
d0b611d7 196 curl_send_headers = NULL;
dea59045
JB
197 curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
198 }
6a4a29aa 199 return res;
3292ba8b
GB
200}
201
9a021e4f 202int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle )
3292ba8b
GB
203{
204 int ret;
205 gchar *full = NULL;
206
207 /* Compose the full url */
0c1044e9 208 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
9a021e4f 209 ret = curl_download_uri ( full, f, options, file_options, handle );
3292ba8b
GB
210 g_free ( full );
211 full = NULL;
212
6a4a29aa 213 return ret;
3292ba8b 214}
825413ba
SW
215
216void * curl_download_handle_init ()
217{
218 return curl_easy_init();
219}
220
221void curl_download_handle_cleanup ( void *handle )
222{
223 curl_easy_cleanup(handle);
224}