]>
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 | 29 | #include <stdio.h> |
1aefc7eb | 30 | #include <string.h> |
8c060406 | 31 | |
6a4a29aa JJ |
32 | #ifdef HAVE_UNISTD_H |
33 | #include <unistd.h> | |
34 | #endif | |
35 | ||
45acf79e MA |
36 | #include <glib.h> |
37 | #include <glib/gstdio.h> | |
4c77d5e0 | 38 | #include <glib/gi18n.h> |
6a2692be | 39 | #include <glib/gprintf.h> |
3292ba8b GB |
40 | #include <gtk/gtk.h> |
41 | ||
3292ba8b | 42 | #include <curl/curl.h> |
3292ba8b | 43 | |
7711383f | 44 | #include "background.h" |
29f1598c | 45 | #include "dir.h" |
80d0ff2b | 46 | #include "file.h" |
2936913d | 47 | #include "globals.h" |
3292ba8b GB |
48 | #include "curl_download.h" |
49 | ||
d0b611d7 | 50 | gchar *curl_download_user_agent = NULL; |
6a2692be | 51 | |
3a0c074a MA |
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 | ||
a8a3d5e0 | 63 | static size_t curl_get_etag_func(void *ptr, size_t size, size_t nmemb, void *stream) |
f039c5ff | 64 | { |
a6bf52f9 GB |
65 | #define ETAG_KEYWORD "ETag: " |
66 | #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1) | |
a8a3d5e0 | 67 | DownloadFileOptions *file_options = (DownloadFileOptions*)stream; |
f039c5ff | 68 | size_t len = size*nmemb; |
fba991f6 | 69 | char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD); |
f039c5ff | 70 | if (str) { |
a6bf52f9 GB |
71 | char *etag_str = str + ETAG_LEN; |
72 | char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n"); | |
f039c5ff | 73 | if (etag_str && end_str) { |
a8a3d5e0 RN |
74 | file_options->new_etag = g_strndup(etag_str, end_str - etag_str); |
75 | g_debug("%s: ETAG found: %s", __FUNCTION__, file_options->new_etag); | |
f039c5ff JJ |
76 | } |
77 | } | |
78 | return nmemb; | |
79 | } | |
80 | ||
7711383f JJ |
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 | ||
80d0ff2b QT |
86 | static gchar *get_cookie_file(gboolean init) |
87 | { | |
88 | static gchar *cookie_file = NULL; | |
80d0ff2b | 89 | |
2da56da5 RN |
90 | // Wipe any previous cookies set on startup (for some reason??) |
91 | // Startup is single threaded so don't care about mutexes | |
92 | if (init) { | |
80d0ff2b QT |
93 | static gchar *cookie_fn = "cookies.txt"; |
94 | const gchar *viking_dir = a_get_viking_dir(); | |
6f2b61bb | 95 | cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); |
45acf79e | 96 | g_unlink(cookie_file); |
80d0ff2b QT |
97 | return NULL; |
98 | } | |
99 | ||
80d0ff2b QT |
100 | return(cookie_file); |
101 | } | |
102 | ||
103 | /* This should to be called from main() to make sure thread safe */ | |
104 | void curl_download_init() | |
105 | { | |
106 | curl_global_init(CURL_GLOBAL_ALL); | |
107 | get_cookie_file(TRUE); | |
6a2692be | 108 | curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version()); |
80d0ff2b QT |
109 | } |
110 | ||
3eba9bbf RN |
111 | /* This should to be called from main() to make sure thread safe */ |
112 | void curl_download_uninit() | |
113 | { | |
114 | curl_global_cleanup(); | |
115 | } | |
116 | ||
9a021e4f | 117 | int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle ) |
3292ba8b | 118 | { |
3292ba8b | 119 | CURL *curl; |
f039c5ff | 120 | struct curl_slist *curl_send_headers = NULL; |
11f88b69 | 121 | CURLcode res = CURLE_FAILED_INIT; |
80d0ff2b | 122 | const gchar *cookie_file; |
3292ba8b | 123 | |
2936913d GB |
124 | g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri); |
125 | ||
825413ba | 126 | curl = handle ? handle : curl_easy_init (); |
6a4a29aa | 127 | if ( !curl ) { |
4e815e90 | 128 | return CURL_DOWNLOAD_ERROR; |
6a4a29aa JJ |
129 | } |
130 | if (vik_verbose) | |
131 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
8cd9c991 | 132 | curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up! |
0807f24b RN |
133 | if ( options != NULL && options->user_pass ) { |
134 | curl_easy_setopt ( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); | |
135 | curl_easy_setopt ( curl, CURLOPT_USERPWD, options->user_pass ); | |
136 | } | |
6a4a29aa JJ |
137 | curl_easy_setopt ( curl, CURLOPT_URL, uri ); |
138 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); | |
139 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); | |
140 | curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 ); | |
141 | curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL ); | |
142 | curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); | |
143 | if (options != NULL) { | |
144 | if(options->referer != NULL) | |
145 | curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); | |
146 | if(options->follow_location != 0) { | |
147 | curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); | |
148 | curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); | |
149 | } | |
9a021e4f JJ |
150 | if (file_options != NULL) { |
151 | if(options->check_file_server_time && file_options->time_condition != 0) { | |
152 | /* if file exists, check against server if file is recent enough */ | |
153 | curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
154 | curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition); | |
155 | } | |
f039c5ff JJ |
156 | if (options->use_etag) { |
157 | if (file_options->etag != NULL) { | |
158 | /* add an header on the HTTP request */ | |
9004d4ed GB |
159 | char str[60]; |
160 | g_snprintf(str, 60, "If-None-Match: %s", file_options->etag); | |
f039c5ff JJ |
161 | curl_send_headers = curl_slist_append(curl_send_headers, str); |
162 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers); | |
163 | } | |
164 | /* store the new etag from the server in an option value */ | |
a8a3d5e0 | 165 | curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, file_options); |
f039c5ff JJ |
166 | curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func); |
167 | } | |
3292ba8b | 168 | } |
6a4a29aa JJ |
169 | } |
170 | curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent ); | |
171 | if ((cookie_file = get_cookie_file(FALSE)) != NULL) | |
172 | curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); | |
173 | res = curl_easy_perform ( curl ); | |
f039c5ff | 174 | |
6a4a29aa JJ |
175 | if (res == 0) { |
176 | glong response; | |
177 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); | |
178 | if (response == 304) { // 304 = Not Modified | |
4e815e90 | 179 | res = CURL_DOWNLOAD_NO_NEWER_FILE; |
6304fa62 JJ |
180 | } else if (response == 200 || // http: 200 = Ok |
181 | response == 226) { // ftp: 226 = sucess | |
6a4a29aa JJ |
182 | gdouble size; |
183 | /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION | |
184 | when the server has a (incorrect) time earlier than the time on the file we already have */ | |
185 | curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); | |
186 | if (size == 0) | |
4e815e90 | 187 | res = CURL_DOWNLOAD_ERROR; |
6a4a29aa | 188 | else |
4e815e90 | 189 | res = CURL_DOWNLOAD_NO_ERROR; |
6a4a29aa | 190 | } else { |
bda3c239 | 191 | g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri); |
4e815e90 | 192 | res = CURL_DOWNLOAD_ERROR; |
6a4a29aa JJ |
193 | } |
194 | } else { | |
4e815e90 | 195 | res = CURL_DOWNLOAD_ERROR; |
6a4a29aa | 196 | } |
825413ba SW |
197 | if (!handle) |
198 | curl_easy_cleanup ( curl ); | |
dea59045 | 199 | if (curl_send_headers) { |
f039c5ff | 200 | curl_slist_free_all(curl_send_headers); |
d0b611d7 | 201 | curl_send_headers = NULL; |
dea59045 JB |
202 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL); |
203 | } | |
6a4a29aa | 204 | return res; |
3292ba8b GB |
205 | } |
206 | ||
9a021e4f | 207 | int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle ) |
3292ba8b GB |
208 | { |
209 | int ret; | |
210 | gchar *full = NULL; | |
211 | ||
1aefc7eb GB |
212 | if ( strstr ( hostname, "://" ) != NULL ) |
213 | /* Already full url */ | |
214 | full = (gchar *) hostname; | |
215 | else if ( strstr ( uri, "://" ) != NULL ) | |
216 | /* Already full url */ | |
217 | full = (gchar *) uri; | |
218 | else | |
219 | /* Compose the full url */ | |
220 | full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); | |
9a021e4f | 221 | ret = curl_download_uri ( full, f, options, file_options, handle ); |
1aefc7eb GB |
222 | /* Free newly allocated memory, but do not free uri */ |
223 | if ( hostname != full && uri != full ) | |
224 | g_free ( full ); | |
3292ba8b GB |
225 | full = NULL; |
226 | ||
6a4a29aa | 227 | return ret; |
3292ba8b | 228 | } |
825413ba SW |
229 | |
230 | void * curl_download_handle_init () | |
231 | { | |
232 | return curl_easy_init(); | |
233 | } | |
234 | ||
235 | void curl_download_handle_cleanup ( void *handle ) | |
236 | { | |
237 | curl_easy_cleanup(handle); | |
238 | } |