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
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
33 #include <curl/curl.h>
36 #include "curl_download.h"
39 * Even if writing to FILE* is supported by libcurl by default,
40 * it seems that it is non-portable (win32 DLL specific).
42 * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
44 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
46 return fwrite(ptr, size, nmemb, stream);
49 static gchar *get_cookie_file(gboolean init)
51 static gchar *cookie_file = NULL;
52 static GMutex *mutex = NULL;
54 if (init) { /* to make sure it's thread safe */
55 mutex = g_mutex_new();
56 static gchar *cookie_fn = "cookies.txt";
57 const gchar *viking_dir = a_get_viking_dir();
58 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
59 g_unlink(cookie_file);
63 g_assert(cookie_file != NULL);
66 if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
67 FILE * out_file = g_fopen("/dev/null", "w");
69 CURL *curl = curl_easy_init();
70 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
71 curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
72 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
73 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
74 res = curl_easy_perform(curl);
75 if (res != CURLE_OK) {
76 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
77 curl_easy_strerror(res));
78 g_unlink(cookie_file);
80 curl_easy_cleanup(curl);
84 g_mutex_unlock(mutex);
89 /* This should to be called from main() to make sure thread safe */
90 void curl_download_init()
92 curl_global_init(CURL_GLOBAL_ALL);
93 get_cookie_file(TRUE);
96 int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
99 CURLcode res = CURLE_FAILED_INIT;
100 const gchar *cookie_file;
102 curl = curl_easy_init ();
105 curl_easy_setopt ( curl, CURLOPT_URL, uri );
106 curl_easy_setopt ( curl, CURLOPT_FILE, f );
107 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
108 if (options != NULL) {
109 if(options->referer != NULL)
110 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
111 if(options->follow_location != 0) {
112 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
113 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
116 curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" );
117 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
118 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
119 res = curl_easy_perform ( curl );
120 curl_easy_cleanup ( curl );
125 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp )
130 /* Compose the full url */
131 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
132 ret = curl_download_uri ( full, f, options );
136 return (ret ? -2 : 0); /* -2 HTTP error */