2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
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>
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.
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.
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
36 #include <glib/gstdio.h>
37 #include <glib/gi18n.h>
38 #include <glib/gprintf.h>
41 #include <curl/curl.h>
43 #include "background.h"
46 #include "curl_download.h"
48 gchar *curl_download_user_agent = NULL;
51 * Even if writing to FILE* is supported by libcurl by default,
52 * it seems that it is non-portable (win32 DLL specific).
54 * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
56 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
58 return fwrite(ptr, size, nmemb, stream);
61 static size_t curl_get_etag_func(void *ptr, size_t size, size_t nmemb, gchar **stream)
63 #define ETAG_KEYWORD "ETag: "
64 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
65 size_t len = size*nmemb;
66 char *str = g_strstr_len(ptr, len, ETAG_KEYWORD);
68 char *etag_str = str + ETAG_LEN;
69 char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
70 if (etag_str && end_str) {
71 *stream = g_strndup(etag_str, end_str - etag_str);
72 g_debug("%s: ETAG found: %s", __FUNCTION__, *stream);
78 static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
80 return a_background_testcancel(NULL);
83 static gchar *get_cookie_file(gboolean init)
85 static gchar *cookie_file = NULL;
86 static GMutex *mutex = NULL;
88 if (init) { /* to make sure it's thread safe */
89 mutex = g_mutex_new();
90 static gchar *cookie_fn = "cookies.txt";
91 const gchar *viking_dir = a_get_viking_dir();
92 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
93 g_unlink(cookie_file);
97 g_assert(cookie_file != NULL);
100 if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
101 gchar * name_tmp = NULL;
102 FILE * out_file = tmpfile();
103 if (out_file == NULL) {
104 // Something wrong with previous call (unsuported?)
105 name_tmp = g_strdup_printf("%s.tmp", cookie_file);
106 out_file = g_fopen(name_tmp, "w+b");
109 CURL *curl = curl_easy_init();
111 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
112 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
113 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file );
114 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
115 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
116 res = curl_easy_perform(curl);
117 if (res != CURLE_OK) {
118 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
119 curl_easy_strerror(res));
120 g_unlink(cookie_file);
122 curl_easy_cleanup(curl);
125 if (name_tmp != NULL) {
131 g_mutex_unlock(mutex);
136 /* This should to be called from main() to make sure thread safe */
137 void curl_download_init()
139 curl_global_init(CURL_GLOBAL_ALL);
140 get_cookie_file(TRUE);
141 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
144 int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
147 struct curl_slist *curl_send_headers = NULL;
148 CURLcode res = CURLE_FAILED_INIT;
149 const gchar *cookie_file;
151 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
153 curl = handle ? handle : curl_easy_init ();
155 return DOWNLOAD_ERROR;
158 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
159 curl_easy_setopt ( curl, CURLOPT_URL, uri );
160 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
161 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
162 curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
163 curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
164 curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
165 if (options != NULL) {
166 if(options->referer != NULL)
167 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
168 if(options->follow_location != 0) {
169 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
170 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
172 if (file_options != NULL) {
173 if(options->check_file_server_time && file_options->time_condition != 0) {
174 /* if file exists, check against server if file is recent enough */
175 curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
176 curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition);
178 if (options->use_etag) {
179 if (file_options->etag != NULL) {
180 /* add an header on the HTTP request */
182 g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
183 curl_send_headers = curl_slist_append(curl_send_headers, str);
184 curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
186 /* store the new etag from the server in an option value */
187 curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
188 curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
192 curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
193 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
194 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
195 res = curl_easy_perform ( curl );
199 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
200 if (response == 304) { // 304 = Not Modified
201 res = DOWNLOAD_NO_NEWER_FILE;
202 } else if (response == 200 || // http: 200 = Ok
203 response == 226) { // ftp: 226 = sucess
205 /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION
206 when the server has a (incorrect) time earlier than the time on the file we already have */
207 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
209 res = DOWNLOAD_ERROR;
211 res = DOWNLOAD_NO_ERROR;
213 g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, file_options->time_condition);
214 res = DOWNLOAD_ERROR;
217 res = DOWNLOAD_ERROR;
220 curl_easy_cleanup ( curl );
221 if (curl_send_headers) {
222 curl_slist_free_all(curl_send_headers);
223 curl_send_headers = NULL;
224 curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
229 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle )
234 /* Compose the full url */
235 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
236 ret = curl_download_uri ( full, f, options, file_options, handle );
243 void * curl_download_handle_init ()
245 return curl_easy_init();
248 void curl_download_handle_cleanup ( void *handle )
250 curl_easy_cleanup(handle);