Document previous patch
[viking.git] / src / curl_download.c
blob8009557561de26cfdbb62c7f1ad2ae18f5c6ad28
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include <stdio.h>
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
31 #include <glib/gprintf.h>
32 #include <gtk/gtk.h>
34 #include <curl/curl.h>
36 #include "file.h"
37 #include "globals.h"
38 #include "curl_download.h"
40 gchar *curl_download_user_agent;
43 * Even if writing to FILE* is supported by libcurl by default,
44 * it seems that it is non-portable (win32 DLL specific).
46 * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
48 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
50 return fwrite(ptr, size, nmemb, stream);
53 static gchar *get_cookie_file(gboolean init)
55 static gchar *cookie_file = NULL;
56 static GMutex *mutex = NULL;
58 if (init) { /* to make sure it's thread safe */
59 mutex = g_mutex_new();
60 static gchar *cookie_fn = "cookies.txt";
61 const gchar *viking_dir = a_get_viking_dir();
62 cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
63 g_unlink(cookie_file);
64 return NULL;
67 g_assert(cookie_file != NULL);
69 g_mutex_lock(mutex);
70 if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */
71 gchar * name_tmp = NULL;
72 FILE * out_file = tmpfile();
73 if (out_file == NULL) {
74 // Something wrong with previous call (unsuported?)
75 name_tmp = g_strdup_printf("%s.tmp", cookie_file);
76 out_file = g_fopen(name_tmp, "w+b");
78 CURLcode res;
79 CURL *curl = curl_easy_init();
80 if (vik_verbose)
81 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
82 curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
83 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file );
84 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
85 curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
86 res = curl_easy_perform(curl);
87 if (res != CURLE_OK) {
88 g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
89 curl_easy_strerror(res));
90 g_unlink(cookie_file);
92 curl_easy_cleanup(curl);
93 fclose(out_file);
94 out_file = NULL;
95 if (name_tmp != NULL) {
96 g_remove(name_tmp);
97 g_free(name_tmp);
98 name_tmp = NULL;
101 g_mutex_unlock(mutex);
103 return(cookie_file);
106 /* This should to be called from main() to make sure thread safe */
107 void curl_download_init()
109 curl_global_init(CURL_GLOBAL_ALL);
110 get_cookie_file(TRUE);
111 curl_download_user_agent = g_strdup_printf ("%s/%s %s", PACKAGE, VERSION, curl_version());
114 int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
116 CURL *curl;
117 CURLcode res = CURLE_FAILED_INIT;
118 const gchar *cookie_file;
120 g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
122 curl = curl_easy_init ();
123 if ( curl )
125 if (vik_verbose)
126 curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
127 curl_easy_setopt ( curl, CURLOPT_URL, uri );
128 curl_easy_setopt ( curl, CURLOPT_WRITEDATA, f );
129 curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
130 if (options != NULL) {
131 if(options->referer != NULL)
132 curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
133 if(options->follow_location != 0) {
134 curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
135 curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
138 curl_easy_setopt ( curl, CURLOPT_USERAGENT, curl_download_user_agent );
139 if ((cookie_file = get_cookie_file(FALSE)) != NULL)
140 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
141 res = curl_easy_perform ( curl );
142 curl_easy_cleanup ( curl );
144 return(res);
147 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp )
149 int ret;
150 gchar *full = NULL;
152 /* Compose the full url */
153 full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
154 ret = curl_download_uri ( full, f, options );
155 g_free ( full );
156 full = NULL;
158 return (ret ? -2 : 0); /* -2 HTTP error */