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"
47 #include "curl_download.h"
49 gchar *curl_download_user_agent = NULL;
52 * Even if writing to FILE* is supported by libcurl by default,
53 * it seems that it is non-portable (win32 DLL specific).
55 * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
57 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
59 return fwrite(ptr, size, nmemb, stream);
62 static size_t curl_get_etag_func(char *ptr, size_t size, size_t nmemb, void *stream)
64 #define ETAG_KEYWORD "ETag: "
65 #define ETAG_LEN (sizeof(ETAG_KEYWORD)-1)
66 gchar **etag = stream;
67 size_t len = size*nmemb;
68 char *str = g_strstr_len((const char*)ptr, len, ETAG_KEYWORD);
70 char *etag_str = str + ETAG_LEN;
71 char *end_str = g_strstr_len(etag_str, len - ETAG_LEN, "\r\n");
72 if (etag_str && end_str) {
73 *etag = g_strndup(etag_str, end_str - etag_str);
74 g_debug("%s: ETAG found: %s", __FUNCTION__, *etag);
80 static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
82 return a_background_testcancel(NULL);
85 static gchar *get_cookie_file(gboolean init)
87 static gchar *cookie_file = NULL;
89 // Wipe any previous cookies set on startup (for some reason??)
90 // Startup is single threaded so don't care about mutexes
92 static gchar *cookie_fn = "cookies.txt";
93 const gchar *viking_dir = a_get_viking_dir();
94 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
95 g_unlink(cookie_file);
102 /* This should to be called from main() to make sure thread safe */
103 void curl_download_init()
105 curl_global_init(CURL_GLOBAL_ALL);
106 get_cookie_file(TRUE);
107 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
110 /* This should to be called from main() to make sure thread safe */
111 void curl_download_uninit()
113 curl_global_cleanup();
116 int curl_download_uri ( const char *uri, FILE *f, DownloadMapOptions *options, DownloadFileOptions *file_options, void *handle )
119 struct curl_slist *curl_send_headers = NULL;
120 CURLcode res = CURLE_FAILED_INIT;
121 const gchar *cookie_file;
123 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
125 curl = handle ? handle : curl_easy_init ();
127 return DOWNLOAD_ERROR;
130 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
131 curl_easy_setopt ( curl, CURLOPT_NOSIGNAL, 1 ); // Yep, we're a multi-threaded program so don't let signals mess it up!
132 curl_easy_setopt ( curl, CURLOPT_URL, uri );
133 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
134 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
135 curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
136 curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
137 curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
138 if (options != NULL) {
139 if(options->referer != NULL)
140 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
141 if(options->follow_location != 0) {
142 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
143 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
145 if (file_options != NULL) {
146 if(options->check_file_server_time && file_options->time_condition != 0) {
147 /* if file exists, check against server if file is recent enough */
148 curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
149 curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, file_options->time_condition);
151 if (options->use_etag) {
152 if (file_options->etag != NULL) {
153 /* add an header on the HTTP request */
155 g_snprintf(str, 60, "If-None-Match: %s", file_options->etag);
156 curl_send_headers = curl_slist_append(curl_send_headers, str);
157 curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , curl_send_headers);
159 /* store the new etag from the server in an option value */
160 curl_easy_setopt ( curl, CURLOPT_WRITEHEADER, &(file_options->new_etag));
161 curl_easy_setopt ( curl, CURLOPT_HEADERFUNCTION, curl_get_etag_func);
165 curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
166 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
167 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
168 res = curl_easy_perform ( curl );
172 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
173 if (response == 304) { // 304 = Not Modified
174 res = DOWNLOAD_NO_NEWER_FILE;
175 } else if (response == 200 || // http: 200 = Ok
176 response == 226) { // ftp: 226 = sucess
178 /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION
179 when the server has a (incorrect) time earlier than the time on the file we already have */
180 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
182 res = DOWNLOAD_ERROR;
184 res = DOWNLOAD_NO_ERROR;
186 g_warning("%s: http response: %ld for uri %s\n", __FUNCTION__, response, uri);
187 res = DOWNLOAD_ERROR;
190 res = DOWNLOAD_ERROR;
193 curl_easy_cleanup ( curl );
194 if (curl_send_headers) {
195 curl_slist_free_all(curl_send_headers);
196 curl_send_headers = NULL;
197 curl_easy_setopt ( curl, CURLOPT_HTTPHEADER , NULL);
202 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadMapOptions *options, gboolean ftp, DownloadFileOptions *file_options, void *handle )
207 /* Compose the full url */
208 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
209 ret = curl_download_uri ( full, f, options, file_options, handle );
216 void * curl_download_handle_init ()
218 return curl_easy_init();
221 void curl_download_handle_cleanup ( void *handle )
223 curl_easy_cleanup(handle);