/
curl_download.c
186 lines (166 loc) · 5.8 KB
/
curl_download.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* viking -- GPS Data and Topo Analyzer, Explorer, and Manager
*
* Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <curl/curl.h>
#include "file.h"
#include "globals.h"
#include "curl_download.h"
/*
* Even if writing to FILE* is supported by libcurl by default,
* it seems that it is non-portable (win32 DLL specific).
*
* So, we provide our own trivial CURLOPT_WRITEFUNCTION.
*/
static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
return fwrite(ptr, size, nmemb, stream);
}
static gchar *get_cookie_file(gboolean init)
{
static gchar *cookie_file = NULL;
static GMutex *mutex = NULL;
if (init) { /* to make sure it's thread safe */
mutex = g_mutex_new();
static gchar *cookie_fn = "cookies.txt";
const gchar *viking_dir = a_get_viking_dir();
cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
g_unlink(cookie_file);
return NULL;
}
g_assert(cookie_file != NULL);
g_mutex_lock(mutex);
if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
gchar * name_tmp = NULL;
FILE * out_file = tmpfile();
if (out_file == NULL) {
// Something wrong with previous call (unsuported?)
name_tmp = g_strdup_printf("%s.tmp", cookie_file);
out_file = g_fopen(name_tmp, "w+b");
}
CURLcode res;
CURL *curl = curl_easy_init();
if (vik_verbose)
curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
curl_easy_strerror(res));
g_unlink(cookie_file);
}
curl_easy_cleanup(curl);
fclose(out_file);
out_file = NULL;
if (name_tmp != NULL) {
g_remove(name_tmp);
g_free(name_tmp);
name_tmp = NULL;
}
}
g_mutex_unlock(mutex);
return(cookie_file);
}
/* This should to be called from main() to make sure thread safe */
void curl_download_init()
{
curl_global_init(CURL_GLOBAL_ALL);
get_cookie_file(TRUE);
}
int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options, time_t time_condition )
{
CURL *curl;
CURLcode res = CURLE_FAILED_INIT;
const gchar *cookie_file;
g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
curl = curl_easy_init ();
if ( !curl ) {
return DOWNLOAD_ERROR;
}
if (vik_verbose)
curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
curl_easy_setopt ( curl, CURLOPT_URL, uri );
curl_easy_setopt ( curl, CURLOPT_FILE, f );
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
if (options != NULL) {
if(options->referer != NULL)
curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
if(options->follow_location != 0) {
curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
}
if(options->check_file_server_time && time_condition != 0) {
/* if file exists, check against server if file is recent enough */
curl_easy_setopt ( curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE);
curl_easy_setopt ( curl, CURLOPT_TIMEVALUE, time_condition);
}
}
curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" );
if ((cookie_file = get_cookie_file(FALSE)) != NULL)
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
res = curl_easy_perform ( curl );
if (res == 0) {
glong response;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
if (response == 304) { // 304 = Not Modified
res = DOWNLOAD_NO_NEWER_FILE;
} else if (response == 200) { // 200 = Ok
gdouble size;
/* verify if curl sends us any data - this is a workaround on using CURLOPT_TIMECONDITION
when the server has a (incorrect) time earlier than the time on the file we already have */
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &size);
if (size == 0)
res = DOWNLOAD_ERROR;
else
res = DOWNLOAD_NO_ERROR;
} else {
g_warning("%s: http response: %ld for uri %s (time_condition = %ld)\n", __FUNCTION__, response, uri, time_condition);
res = DOWNLOAD_ERROR;
}
} else {
res = DOWNLOAD_ERROR;
}
curl_easy_cleanup ( curl );
return res;
}
int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp, time_t time_condition )
{
int ret;
gchar *full = NULL;
/* Compose the full url */
full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
ret = curl_download_uri ( full, f, options, time_condition );
g_free ( full );
full = NULL;
return ret;
}