]>
Commit | Line | Data |
---|---|---|
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" |
80d0ff2b | 44 | #include "file.h" |
2936913d | 45 | #include "globals.h" |
3292ba8b GB |
46 | #include "curl_download.h" |
47 | ||
d0b611d7 | 48 | gchar *curl_download_user_agent = NULL; |
6a2692be | 49 | |
3a0c074a MA |
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 | ||
fba991f6 | 61 | static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream) |
f039c5ff | 62 | { |
a6bf52f9 GB |
63 | #define ETAG_KEYWORD "ETag: " |
64 | #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1) | |
c4a6adde | 65 | gchar **etag = stream; |
f039c5ff | 66 | size_t len = size*nmemb; |
fba991f6 | 67 | char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD); |
f039c5ff | 68 | if (str) { |
a6bf52f9 GB |
69 | char *etag_str = str + ETAG_LEN; |
70 | char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n"); | |
f039c5ff | 71 | if (etag_str && end_str) { |
c4a6adde SW |
72 | *etag = g_strndup(etag_str, end_str - etag_str); |
73 | g_debug("%s: ETAG found: %s", __FUNCTION__, *etag); | |
f039c5ff JJ |
74 | } |
75 | } | |
76 | return nmemb; | |
77 | } | |
78 | ||
7711383f JJ |
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 | ||
80d0ff2b QT |
84 | static gchar *get_cookie_file(gboolean init) |
85 | { | |
86 | static gchar *cookie_file = NULL; | |
87 | static GMutex *mutex = NULL; | |
88 | ||
89 | if (init) { /* to make sure it's thread safe */ | |
90 | mutex = g_mutex_new(); | |
91 | static gchar *cookie_fn = "cookies.txt"; | |
92 | const gchar *viking_dir = a_get_viking_dir(); | |
6f2b61bb | 93 | cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); |
45acf79e | 94 | g_unlink(cookie_file); |
80d0ff2b QT |
95 | return NULL; |
96 | } | |
97 | ||
98 | g_assert(cookie_file != NULL); | |
99 | ||
100 | g_mutex_lock(mutex); | |
45acf79e | 101 | if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */ |
109c6557 | 102 | gchar * name_tmp = NULL; |
c7f6cb25 | 103 | FILE * out_file = tmpfile(); |
109c6557 MA |
104 | if (out_file == NULL) { |
105 | // Something wrong with previous call (unsuported?) | |
106 | name_tmp = g_strdup_printf("%s.tmp", cookie_file); | |
107 | out_file = g_fopen(name_tmp, "w+b"); | |
108 | } | |
80d0ff2b QT |
109 | CURLcode res; |
110 | CURL *curl = curl_easy_init(); | |
2936913d GB |
111 | if (vik_verbose) |
112 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
80d0ff2b | 113 | curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */ |
da309dc2 | 114 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file ); |
3a0c074a | 115 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); |
80d0ff2b QT |
116 | curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file); |
117 | res = curl_easy_perform(curl); | |
118 | if (res != CURLE_OK) { | |
4c77d5e0 | 119 | g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__, |
80d0ff2b | 120 | curl_easy_strerror(res)); |
45acf79e | 121 | g_unlink(cookie_file); |
80d0ff2b QT |
122 | } |
123 | curl_easy_cleanup(curl); | |
8c060406 MA |
124 | fclose(out_file); |
125 | out_file = NULL; | |
109c6557 MA |
126 | if (name_tmp != NULL) { |
127 | g_remove(name_tmp); | |
128 | g_free(name_tmp); | |
129 | name_tmp = NULL; | |
130 | } | |
80d0ff2b QT |
131 | } |
132 | g_mutex_unlock(mutex); | |
133 | ||
134 | return(cookie_file); | |
135 | } | |
136 | ||
137 | /* This should to be called from main() to make sure thread safe */ | |
138 | void curl_download_init() | |
139 | { | |
140 | curl_global_init(CURL_GLOBAL_ALL); | |
141 | get_cookie_file(TRUE); | |
6a2692be | 142 | curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version()); |
80d0ff2b QT |
143 | } |
144 | ||
3eba9bbf RN |
145 | /* This should to be called from main() to make sure thread safe */ |
146 | void curl_download_uninit() | |
147 | { | |
148 | curl_global_cleanup(); | |
149 | } | |
150 | ||
9a021e4f | 151 | int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle ) |
3292ba8b | 152 | { |
3292ba8b | 153 | CURL *curl; |
f039c5ff | 154 | struct curl_slist *curl_send_headers = NULL; |
11f88b69 | 155 | CURLcode res = CURLE_FAILED_INIT; |
80d0ff2b | 156 | const gchar *cookie_file; |
3292ba8b | 157 | |
2936913d GB |
158 | g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri); |
159 | ||
825413ba | 160 | curl = handle ? handle : curl_easy_init (); |
6a4a29aa JJ |
161 | if ( !curl ) { |
162 | return DOWNLOAD_ERROR; | |
163 | } | |
164 | if (vik_verbose) | |
165 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
8cd9c991 | 166 | curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up! |
6a4a29aa JJ |
167 | curl_easy_setopt ( curl, CURLOPT_URL, uri ); |
168 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); | |
169 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); | |
170 | curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 ); | |
171 | curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL ); | |
172 | curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); | |
173 | if (options != NULL) { | |
174 | if(options->referer != NULL) | |
175 | curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); | |
176 | if(options->follow_location != 0) { | |
177 | curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); | |
178 | curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); | |
179 | } | |
9a021e4f JJ |
180 | if (file_options != NULL) { |
181 | if(options->check_file_server_time && file_options->time_condition != 0) { | |
182 | /* if file exists, check against server if file is recent enough */ | |
183 | curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
184 | curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition); | |
185 | } | |
f039c5ff JJ |
186 | if (options->use_etag) { |
187 | if (file_options->etag != NULL) { | |
188 | /* add an header on the HTTP request */ | |
9004d4ed GB |
189 | char str[60]; |
190 | g_snprintf(str, 60, "If-None-Match: %s", file_options->etag); | |
f039c5ff JJ |
191 | curl_send_headers = curl_slist_append(curl_send_headers, str); |
192 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers); | |
193 | } | |
194 | /* store the new etag from the server in an option value */ | |
195 | curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag)); | |
196 | curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func); | |
197 | } | |
3292ba8b | 198 | } |
6a4a29aa JJ |
199 | } |
200 | curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent ); | |
201 | if ((cookie_file = get_cookie_file(FALSE)) != NULL) | |
202 | curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); | |
203 | res = curl_easy_perform ( curl ); | |
f039c5ff | 204 | |
6a4a29aa JJ |
205 | if (res == 0) { |
206 | glong response; | |
207 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); | |
208 | if (response == 304) { // 304 = Not Modified | |
209 | res = DOWNLOAD_NO_NEWER_FILE; | |
6304fa62 JJ |
210 | } else if (response == 200 || // http: 200 = Ok |
211 | response == 226) { // ftp: 226 = sucess | |
6a4a29aa JJ |
212 | gdouble size; |
213 | /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION | |
214 | when the server has a (incorrect) time earlier than the time on the file we already have */ | |
215 | curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); | |
216 | if (size == 0) | |
217 | res = DOWNLOAD_ERROR; | |
218 | else | |
219 | res = DOWNLOAD_NO_ERROR; | |
220 | } else { | |
bda3c239 | 221 | g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri); |
6a4a29aa JJ |
222 | res = DOWNLOAD_ERROR; |
223 | } | |
224 | } else { | |
225 | res = DOWNLOAD_ERROR; | |
226 | } | |
825413ba SW |
227 | if (!handle) |
228 | curl_easy_cleanup ( curl ); | |
dea59045 | 229 | if (curl_send_headers) { |
f039c5ff | 230 | curl_slist_free_all(curl_send_headers); |
d0b611d7 | 231 | curl_send_headers = NULL; |
dea59045 JB |
232 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL); |
233 | } | |
6a4a29aa | 234 | return res; |
3292ba8b GB |
235 | } |
236 | ||
9a021e4f | 237 | int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle ) |
3292ba8b GB |
238 | { |
239 | int ret; | |
240 | gchar *full = NULL; | |
241 | ||
242 | /* Compose the full url */ | |
0c1044e9 | 243 | full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); |
9a021e4f | 244 | ret = curl_download_uri ( full, f, options, file_options, handle ); |
3292ba8b GB |
245 | g_free ( full ); |
246 | full = NULL; | |
247 | ||
6a4a29aa | 248 | return ret; |
3292ba8b | 249 | } |
825413ba SW |
250 | |
251 | void * curl_download_handle_init () | |
252 | { | |
253 | return curl_easy_init(); | |
254 | } | |
255 | ||
256 | void curl_download_handle_cleanup ( void *handle ) | |
257 | { | |
258 | curl_easy_cleanup(handle); | |
259 | } |