libcurl is a requirement
[viking/guyou.git] / src / curl_download.c
blob5082744cd10a8065b052fde0e8e7a0b34770f71c
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
22 #include <unistd.h>
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <gtk/gtk.h>
30 #include <curl/curl.h>
32 #include "file.h"
33 #include "curl_download.h"
35 static gchar *get_cookie_file(gboolean init)
37 static gchar *cookie_file = NULL;
38 static GMutex *mutex = NULL;
40 if (init) { /* to make sure it's thread safe */
41 mutex = g_mutex_new();
42 static gchar *cookie_fn = "cookies.txt";
43 const gchar *viking_dir = a_get_viking_dir();
44 cookie_file = g_strdup_printf("%s/%s", viking_dir, cookie_fn);
45 unlink(cookie_file);
46 return NULL;
49 g_assert(cookie_file != NULL);
51 g_mutex_lock(mutex);
52 if (access(cookie_file, F_OK)) { /* file not there */
53 FILE * out_file = fopen("/dev/null", "w");
54 CURLcode res;
55 CURL *curl = curl_easy_init();
56 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
57 curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
58 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
59 res = curl_easy_perform(curl);
60 if (res != CURLE_OK) {
61 g_warning("%s() Curl perform failed: %s\n", __PRETTY_FUNCTION__,
62 curl_easy_strerror(res));
63 unlink(cookie_file);
65 curl_easy_cleanup(curl);
67 g_mutex_unlock(mutex);
69 return(cookie_file);
72 /* This should to be called from main() to make sure thread safe */
73 void curl_download_init()
75 curl_global_init(CURL_GLOBAL_ALL);
76 get_cookie_file(TRUE);
79 int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
81 CURL *curl;
82 CURLcode res = CURLE_FAILED_INIT;
83 const gchar *cookie_file;
85 curl = curl_easy_init ();
86 if ( curl )
88 curl_easy_setopt ( curl, CURLOPT_URL, uri );
89 curl_easy_setopt ( curl, CURLOPT_FILE, f );
90 if (options != NULL && options->referer != NULL)
91 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
92 curl_easy_setopt ( curl, CURLOPT_USERAGENT, "viking/" VERSION " libcurl/7.15.4" );
93 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
94 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
95 res = curl_easy_perform ( curl );
96 curl_easy_cleanup ( curl );
98 return(res);
101 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options )
103 int ret;
104 gchar *full = NULL;
106 /* Compose the full url */
107 full = g_strdup_printf ( "http://%s%s", hostname, uri );
108 ret = curl_download_uri ( full, f, options );
109 g_free ( full );
110 full = NULL;
112 return (ret ? -2 : 0); /* -2 HTTP error */