]>
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" |
29f1598c | 44 | #include "dir.h" |
80d0ff2b | 45 | #include "file.h" |
2936913d | 46 | #include "globals.h" |
3292ba8b GB |
47 | #include "curl_download.h" |
48 | ||
d0b611d7 | 49 | gchar *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 | */ | |
57 | static 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 | 62 | static 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 |
80 | static 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 |
85 | static 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 */ | |
103 | void 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 */ |
111 | void curl_download_uninit() | |
112 | { | |
113 | curl_global_cleanup(); | |
114 | } | |
115 | ||
9a021e4f | 116 | int 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! |
0807f24b RN |
132 | if ( options != NULL && options->user_pass ) { |
133 | curl_easy_setopt ( curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); | |
134 | curl_easy_setopt ( curl, CURLOPT_USERPWD, options->user_pass ); | |
135 | } | |
6a4a29aa JJ |
136 | curl_easy_setopt ( curl, CURLOPT_URL, uri ); |
137 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); | |
138 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); | |
139 | curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 ); | |
140 | curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL ); | |
141 | curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); | |
142 | if (options != NULL) { | |
143 | if(options->referer != NULL) | |
144 | curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); | |
145 | if(options->follow_location != 0) { | |
146 | curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); | |
147 | curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); | |
148 | } | |
9a021e4f JJ |
149 | if (file_options != NULL) { |
150 | if(options->check_file_server_time && file_options->time_condition != 0) { | |
151 | /* if file exists, check against server if file is recent enough */ | |
152 | curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
153 | curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition); | |
154 | } | |
f039c5ff JJ |
155 | if (options->use_etag) { |
156 | if (file_options->etag != NULL) { | |
157 | /* add an header on the HTTP request */ | |
9004d4ed GB |
158 | char str[60]; |
159 | g_snprintf(str, 60, "If-None-Match: %s", file_options->etag); | |
f039c5ff JJ |
160 | curl_send_headers = curl_slist_append(curl_send_headers, str); |
161 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers); | |
162 | } | |
163 | /* store the new etag from the server in an option value */ | |
164 | curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag)); | |
165 | curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func); | |
166 | } | |
3292ba8b | 167 | } |
6a4a29aa JJ |
168 | } |
169 | curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent ); | |
170 | if ((cookie_file = get_cookie_file(FALSE)) != NULL) | |
171 | curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); | |
172 | res = curl_easy_perform ( curl ); | |
f039c5ff | 173 | |
6a4a29aa JJ |
174 | if (res == 0) { |
175 | glong response; | |
176 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); | |
177 | if (response == 304) { // 304 = Not Modified | |
178 | res = DOWNLOAD_NO_NEWER_FILE; | |
6304fa62 JJ |
179 | } else if (response == 200 || // http: 200 = Ok |
180 | response == 226) { // ftp: 226 = sucess | |
6a4a29aa JJ |
181 | gdouble size; |
182 | /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION | |
183 | when the server has a (incorrect) time earlier than the time on the file we already have */ | |
184 | curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); | |
185 | if (size == 0) | |
186 | res = DOWNLOAD_ERROR; | |
187 | else | |
188 | res = DOWNLOAD_NO_ERROR; | |
189 | } else { | |
bda3c239 | 190 | g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri); |
6a4a29aa JJ |
191 | res = DOWNLOAD_ERROR; |
192 | } | |
193 | } else { | |
194 | res = DOWNLOAD_ERROR; | |
195 | } | |
825413ba SW |
196 | if (!handle) |
197 | curl_easy_cleanup ( curl ); | |
dea59045 | 198 | if (curl_send_headers) { |
f039c5ff | 199 | curl_slist_free_all(curl_send_headers); |
d0b611d7 | 200 | curl_send_headers = NULL; |
dea59045 JB |
201 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL); |
202 | } | |
6a4a29aa | 203 | return res; |
3292ba8b GB |
204 | } |
205 | ||
9a021e4f | 206 | int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle ) |
3292ba8b GB |
207 | { |
208 | int ret; | |
209 | gchar *full = NULL; | |
210 | ||
211 | /* Compose the full url */ | |
0c1044e9 | 212 | full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); |
9a021e4f | 213 | ret = curl_download_uri ( full, f, options, file_options, handle ); |
3292ba8b GB |
214 | g_free ( full ); |
215 | full = NULL; | |
216 | ||
6a4a29aa | 217 | return ret; |
3292ba8b | 218 | } |
825413ba SW |
219 | |
220 | void * curl_download_handle_init () | |
221 | { | |
222 | return curl_easy_init(); | |
223 | } | |
224 | ||
225 | void curl_download_handle_cleanup ( void *handle ) | |
226 | { | |
227 | curl_easy_cleanup(handle); | |
228 | } |