]> git.street.me.uk Git - andy/viking.git/blame - src/curl_download.c
Correct OSM URL
[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
80d0ff2b
QT
22#include <unistd.h>
23
3292ba8b
GB
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
4c77d5e0 28#include <glib/gi18n.h>
3292ba8b
GB
29#include <gtk/gtk.h>
30
3292ba8b 31#include <curl/curl.h>
3292ba8b 32
80d0ff2b 33#include "file.h"
3292ba8b
GB
34#include "curl_download.h"
35
80d0ff2b
QT
36static gchar *get_cookie_file(gboolean init)
37{
38 static gchar *cookie_file = NULL;
39 static GMutex *mutex = NULL;
40
41 if (init) { /* to make sure it's thread safe */
42 mutex = g_mutex_new();
43 static gchar *cookie_fn = "cookies.txt";
44 const gchar *viking_dir = a_get_viking_dir();
6f2b61bb 45 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
80d0ff2b
QT
46 unlink(cookie_file);
47 return NULL;
48 }
49
50 g_assert(cookie_file != NULL);
51
52 g_mutex_lock(mutex);
53 if (access(cookie_file, F_OK)) { /* file not there */
54 FILE * out_file = fopen("/dev/null", "w");
55 CURLcode res;
56 CURL *curl = curl_easy_init();
57 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
58 curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
59 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
60 res = curl_easy_perform(curl);
61 if (res != CURLE_OK) {
4c77d5e0 62 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
80d0ff2b
QT
63 curl_easy_strerror(res));
64 unlink(cookie_file);
65 }
66 curl_easy_cleanup(curl);
67 }
68 g_mutex_unlock(mutex);
69
70 return(cookie_file);
71}
72
73/* This should to be called from main() to make sure thread safe */
74void curl_download_init()
75{
76 curl_global_init(CURL_GLOBAL_ALL);
77 get_cookie_file(TRUE);
78}
79
a3188993 80int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
3292ba8b 81{
3292ba8b 82 CURL *curl;
11f88b69 83 CURLcode res = CURLE_FAILED_INIT;
80d0ff2b 84 const gchar *cookie_file;
3292ba8b
GB
85
86 curl = curl_easy_init ();
87 if ( curl )
88 {
89 curl_easy_setopt ( curl, CURLOPT_URL, uri );
90 curl_easy_setopt ( curl, CURLOPT_FILE, f );
1a8143e6
BZ
91 if (options != NULL) {
92 if(options->referer != NULL)
93 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
94 if(options->follow_location != 0) {
95 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
96 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
97 }
98 }
c7fd05d2 99 curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" );
494eb388 100 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
80d0ff2b 101 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
3292ba8b
GB
102 res = curl_easy_perform ( curl );
103 curl_easy_cleanup ( curl );
104 }
11f88b69 105 return(res);
3292ba8b
GB
106}
107
0c1044e9 108int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp )
3292ba8b
GB
109{
110 int ret;
111 gchar *full = NULL;
112
113 /* Compose the full url */
0c1044e9 114 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
a3188993 115 ret = curl_download_uri ( full, f, options );
3292ba8b
GB
116 g_free ( full );
117 full = NULL;
118
11f88b69 119 return (ret ? -2 : 0); /* -2 HTTP error */
3292ba8b 120}