]> git.street.me.uk Git - andy/viking.git/blame - src/curl_download.c
When manually creating a track, automatically give it a default name.
[andy/viking.git] / src / curl_download.c
CommitLineData
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
6a2692be
JJ
45gchar *curl_download_user_agent;
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 */
53static 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
7711383f
JJ
58static int curl_progress_func(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
59{
60 return a_background_testcancel(NULL);
61}
62
80d0ff2b
QT
63static gchar *get_cookie_file(gboolean init)
64{
65 static gchar *cookie_file = NULL;
66 static GMutex *mutex = NULL;
67
68 if (init) { /* to make sure it's thread safe */
69 mutex = g_mutex_new();
70 static gchar *cookie_fn = "cookies.txt";
71 const gchar *viking_dir = a_get_viking_dir();
6f2b61bb 72 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
45acf79e 73 g_unlink(cookie_file);
80d0ff2b
QT
74 return NULL;
75 }
76
77 g_assert(cookie_file != NULL);
78
79 g_mutex_lock(mutex);
45acf79e 80 if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
109c6557 81 gchar * name_tmp = NULL;
c7f6cb25 82 FILE * out_file = tmpfile();
109c6557
MA
83 if (out_file == NULL) {
84 // Something wrong with previous call (unsuported?)
85 name_tmp = g_strdup_printf("%s.tmp", cookie_file);
86 out_file = g_fopen(name_tmp, "w+b");
87 }
80d0ff2b
QT
88 CURLcode res;
89 CURL *curl = curl_easy_init();
2936913d
GB
90 if (vik_verbose)
91 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
80d0ff2b 92 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
da309dc2 93 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file );
3a0c074a 94 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
80d0ff2b
QT
95 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
96 res = curl_easy_perform(curl);
97 if (res != CURLE_OK) {
4c77d5e0 98 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
80d0ff2b 99 curl_easy_strerror(res));
45acf79e 100 g_unlink(cookie_file);
80d0ff2b
QT
101 }
102 curl_easy_cleanup(curl);
8c060406
MA
103 fclose(out_file);
104 out_file = NULL;
109c6557
MA
105 if (name_tmp != NULL) {
106 g_remove(name_tmp);
107 g_free(name_tmp);
108 name_tmp = NULL;
109 }
80d0ff2b
QT
110 }
111 g_mutex_unlock(mutex);
112
113 return(cookie_file);
114}
115
116/* This should to be called from main() to make sure thread safe */
117void curl_download_init()
118{
119 curl_global_init(CURL_GLOBAL_ALL);
120 get_cookie_file(TRUE);
6a2692be 121 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
80d0ff2b
QT
122}
123
825413ba 124int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition, void *handle )
3292ba8b 125{
3292ba8b 126 CURL *curl;
11f88b69 127 CURLcode res = CURLE_FAILED_INIT;
80d0ff2b 128 const gchar *cookie_file;
3292ba8b 129
2936913d
GB
130 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
131
825413ba 132 curl = handle ? handle : curl_easy_init ();
6a4a29aa
JJ
133 if ( !curl ) {
134 return DOWNLOAD_ERROR;
135 }
136 if (vik_verbose)
137 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
138 curl_easy_setopt ( curl, CURLOPT_URL, uri );
139 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
140 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
141 curl_easy_setopt ( curl, CURLOPT_NOPROGRESS, 0 );
142 curl_easy_setopt ( curl, CURLOPT_PROGRESSDATA, NULL );
143 curl_easy_setopt ( curl, CURLOPT_PROGRESSFUNCTION, curl_progress_func);
144 if (options != NULL) {
145 if(options->referer != NULL)
146 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
147 if(options->follow_location != 0) {
148 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
149 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
150 }
151 if(options->check_file_server_time && 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, time_condition);
3292ba8b 155 }
6a4a29aa
JJ
156 }
157 curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
158 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
159 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
160 res = curl_easy_perform ( curl );
161 if (res == 0) {
162 glong response;
163 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
164 if (response == 304) { // 304 = Not Modified
165 res = DOWNLOAD_NO_NEWER_FILE;
6304fa62
JJ
166 } else if (response == 200 || // http: 200 = Ok
167 response == 226) { // ftp: 226 = sucess
6a4a29aa
JJ
168 gdouble size;
169 /* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION
170 when the server has a (incorrect) time earlier than the time on the file we already have */
171 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
172 if (size == 0)
173 res = DOWNLOAD_ERROR;
174 else
175 res = DOWNLOAD_NO_ERROR;
176 } else {
177 g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, time_condition);
178 res = DOWNLOAD_ERROR;
179 }
180 } else {
181 res = DOWNLOAD_ERROR;
182 }
825413ba
SW
183 if (!handle)
184 curl_easy_cleanup ( curl );
6a4a29aa 185 return res;
3292ba8b
GB
186}
187
825413ba 188int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition, void *handle )
3292ba8b
GB
189{
190 int ret;
191 gchar *full = NULL;
192
193 /* Compose the full url */
0c1044e9 194 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
825413ba 195 ret = curl_download_uri ( full, f, options, time_condition, handle );
3292ba8b
GB
196 g_free ( full );
197 full = NULL;
198
6a4a29aa 199 return ret;
3292ba8b 200}
825413ba
SW
201
202void * curl_download_handle_init ()
203{
204 return curl_easy_init();
205}
206
207void curl_download_handle_cleanup ( void *handle )
208{
209 curl_easy_cleanup(handle);
210}