]>
Commit | Line | Data |
---|---|---|
3292ba8b GB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #ifdef HAVE_CONFIG_H | |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
8c060406 MA |
26 | #include <stdio.h> |
27 | ||
6a4a29aa JJ |
28 | #ifdef HAVE_UNISTD_H |
29 | #include <unistd.h> | |
30 | #endif | |
31 | ||
45acf79e MA |
32 | #include <glib.h> |
33 | #include <glib/gstdio.h> | |
4c77d5e0 | 34 | #include <glib/gi18n.h> |
6a2692be | 35 | #include <glib/gprintf.h> |
3292ba8b GB |
36 | #include <gtk/gtk.h> |
37 | ||
3292ba8b | 38 | #include <curl/curl.h> |
3292ba8b | 39 | |
7711383f | 40 | #include "background.h" |
80d0ff2b | 41 | #include "file.h" |
2936913d | 42 | #include "globals.h" |
3292ba8b GB |
43 | #include "curl_download.h" |
44 | ||
d0b611d7 | 45 | gchar *curl_download_user_agent = NULL; |
6a2692be | 46 | |
3a0c074a MA |
47 | /* |
48 | * Even if writing to FILE* is supported by libcurl by default, | |
49 | * it seems that it is non-portable (win32 DLL specific). | |
50 | * | |
51 | * So, we provide our own trivial CURLOPT_WRITEFUNCTION. | |
52 | */ | |
53 | static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) | |
54 | { | |
55 | return fwrite(ptr, size, nmemb, stream); | |
56 | } | |
57 | ||
f039c5ff JJ |
58 | static size_t curl_get_etag_func(void *ptr, size_t size, size_t nmemb, gchar **stream) |
59 | { | |
a6bf52f9 GB |
60 | #define ETAG_KEYWORD "ETag: " |
61 | #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1) | |
f039c5ff | 62 | size_t len = size*nmemb; |
a6bf52f9 | 63 | char *str = g_strstr_len(ptr, len, ETAG_KEYWORD); |
f039c5ff | 64 | if (str) { |
a6bf52f9 GB |
65 | char *etag_str = str + ETAG_LEN; |
66 | char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n"); | |
f039c5ff | 67 | if (etag_str && end_str) { |
a6bf52f9 GB |
68 | *stream = g_strndup(etag_str, end_str - etag_str); |
69 | g_debug("%s: ETAG found: %s", __FUNCTION__, *stream); | |
f039c5ff JJ |
70 | } |
71 | } | |
72 | return nmemb; | |
73 | } | |
74 | ||
7711383f JJ |
75 | static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow) |
76 | { | |
77 | return a_background_testcancel(NULL); | |
78 | } | |
79 | ||
80d0ff2b QT |
80 | static gchar *get_cookie_file(gboolean init) |
81 | { | |
82 | static gchar *cookie_file = NULL; | |
83 | static GMutex *mutex = NULL; | |
84 | ||
85 | if (init) { /* to make sure it's thread safe */ | |
86 | mutex = g_mutex_new(); | |
87 | static gchar *cookie_fn = "cookies.txt"; | |
88 | const gchar *viking_dir = a_get_viking_dir(); | |
6f2b61bb | 89 | cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); |
45acf79e | 90 | g_unlink(cookie_file); |
80d0ff2b QT |
91 | return NULL; |
92 | } | |
93 | ||
94 | g_assert(cookie_file != NULL); | |
95 | ||
96 | g_mutex_lock(mutex); | |
45acf79e | 97 | if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */ |
109c6557 | 98 | gchar * name_tmp = NULL; |
c7f6cb25 | 99 | FILE * out_file = tmpfile(); |
109c6557 MA |
100 | if (out_file == NULL) { |
101 | // Something wrong with previous call (unsuported?) | |
102 | name_tmp = g_strdup_printf("%s.tmp", cookie_file); | |
103 | out_file = g_fopen(name_tmp, "w+b"); | |
104 | } | |
80d0ff2b QT |
105 | CURLcode res; |
106 | CURL *curl = curl_easy_init(); | |
2936913d GB |
107 | if (vik_verbose) |
108 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
80d0ff2b | 109 | curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */ |
da309dc2 | 110 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file ); |
3a0c074a | 111 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); |
80d0ff2b QT |
112 | curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file); |
113 | res = curl_easy_perform(curl); | |
114 | if (res != CURLE_OK) { | |
4c77d5e0 | 115 | g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__, |
80d0ff2b | 116 | curl_easy_strerror(res)); |
45acf79e | 117 | g_unlink(cookie_file); |
80d0ff2b QT |
118 | } |
119 | curl_easy_cleanup(curl); | |
8c060406 MA |
120 | fclose(out_file); |
121 | out_file = NULL; | |
109c6557 MA |
122 | if (name_tmp != NULL) { |
123 | g_remove(name_tmp); | |
124 | g_free(name_tmp); | |
125 | name_tmp = NULL; | |
126 | } | |
80d0ff2b QT |
127 | } |
128 | g_mutex_unlock(mutex); | |
129 | ||
130 | return(cookie_file); | |
131 | } | |
132 | ||
133 | /* This should to be called from main() to make sure thread safe */ | |
134 | void curl_download_init() | |
135 | { | |
136 | curl_global_init(CURL_GLOBAL_ALL); | |
137 | get_cookie_file(TRUE); | |
6a2692be | 138 | curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version()); |
80d0ff2b QT |
139 | } |
140 | ||
9a021e4f | 141 | int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle ) |
3292ba8b | 142 | { |
3292ba8b | 143 | CURL *curl; |
f039c5ff | 144 | struct curl_slist *curl_send_headers = NULL; |
11f88b69 | 145 | CURLcode res = CURLE_FAILED_INIT; |
80d0ff2b | 146 | const gchar *cookie_file; |
3292ba8b | 147 | |
2936913d GB |
148 | g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri); |
149 | ||
825413ba | 150 | curl = handle ? handle : curl_easy_init (); |
6a4a29aa JJ |
151 | if ( !curl ) { |
152 | return DOWNLOAD_ERROR; | |
153 | } | |
154 | if (vik_verbose) | |
155 | curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); | |
156 | curl_easy_setopt ( curl, CURLOPT_URL, uri ); | |
157 | curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f ); | |
158 | curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); | |
159 | curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 ); | |
160 | curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL ); | |
161 | curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func); | |
162 | if (options != NULL) { | |
163 | if(options->referer != NULL) | |
164 | curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer); | |
165 | if(options->follow_location != 0) { | |
166 | curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1); | |
167 | curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location); | |
168 | } | |
9a021e4f JJ |
169 | if (file_options != NULL) { |
170 | if(options->check_file_server_time && file_options->time_condition != 0) { | |
171 | /* if file exists, check against server if file is recent enough */ | |
172 | curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); | |
173 | curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition); | |
174 | } | |
f039c5ff JJ |
175 | if (options->use_etag) { |
176 | if (file_options->etag != NULL) { | |
177 | /* add an header on the HTTP request */ | |
9004d4ed GB |
178 | char str[60]; |
179 | g_snprintf(str, 60, "If-None-Match: %s", file_options->etag); | |
f039c5ff JJ |
180 | curl_send_headers = curl_slist_append(curl_send_headers, str); |
181 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers); | |
182 | } | |
183 | /* store the new etag from the server in an option value */ | |
184 | curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag)); | |
185 | curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func); | |
186 | } | |
3292ba8b | 187 | } |
6a4a29aa JJ |
188 | } |
189 | curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent ); | |
190 | if ((cookie_file = get_cookie_file(FALSE)) != NULL) | |
191 | curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file); | |
192 | res = curl_easy_perform ( curl ); | |
f039c5ff | 193 | |
6a4a29aa JJ |
194 | if (res == 0) { |
195 | glong response; | |
196 | curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response); | |
197 | if (response == 304) { // 304 = Not Modified | |
198 | res = DOWNLOAD_NO_NEWER_FILE; | |
6304fa62 JJ |
199 | } else if (response == 200 || // http: 200 = Ok |
200 | response == 226) { // ftp: 226 = sucess | |
6a4a29aa JJ |
201 | gdouble size; |
202 | /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION | |
203 | when the server has a (incorrect) time earlier than the time on the file we already have */ | |
204 | curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size); | |
205 | if (size == 0) | |
206 | res = DOWNLOAD_ERROR; | |
207 | else | |
208 | res = DOWNLOAD_NO_ERROR; | |
209 | } else { | |
9a021e4f | 210 | g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, file_options->time_condition); |
6a4a29aa JJ |
211 | res = DOWNLOAD_ERROR; |
212 | } | |
213 | } else { | |
214 | res = DOWNLOAD_ERROR; | |
215 | } | |
825413ba SW |
216 | if (!handle) |
217 | curl_easy_cleanup ( curl ); | |
dea59045 | 218 | if (curl_send_headers) { |
f039c5ff | 219 | curl_slist_free_all(curl_send_headers); |
d0b611d7 | 220 | curl_send_headers = NULL; |
dea59045 JB |
221 | curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL); |
222 | } | |
6a4a29aa | 223 | return res; |
3292ba8b GB |
224 | } |
225 | ||
9a021e4f | 226 | int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle ) |
3292ba8b GB |
227 | { |
228 | int ret; | |
229 | gchar *full = NULL; | |
230 | ||
231 | /* Compose the full url */ | |
0c1044e9 | 232 | full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri ); |
9a021e4f | 233 | ret = curl_download_uri ( full, f, options, file_options, handle ); |
3292ba8b GB |
234 | g_free ( full ); |
235 | full = NULL; | |
236 | ||
6a4a29aa | 237 | return ret; |
3292ba8b | 238 | } |
825413ba SW |
239 | |
240 | void * curl_download_handle_init () | |
241 | { | |
242 | return curl_easy_init(); | |
243 | } | |
244 | ||
245 | void curl_download_handle_cleanup ( void *handle ) | |
246 | { | |
247 | curl_easy_cleanup(handle); | |
248 | } |