]> git.street.me.uk Git - andy/viking.git/blob - src/curl_download.c
Portability: remove "/dev/null" reference
[andy/viking.git] / src / curl_download.c
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
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27
28 #include <glib.h>
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
31 #include <gtk/gtk.h>
32
33 #include <curl/curl.h>
34
35 #include "file.h"
36 #include "globals.h"
37 #include "curl_download.h"
38
39 /*
40  * Even if writing to FILE* is supported by libcurl by default,
41  * it seems that it is non-portable (win32 DLL specific).
42  *
43  * So, we provide our own trivial CURLOPT_WRITEFUNCTION.
44  */
45 static size_t curl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
46 {
47   return fwrite(ptr, size, nmemb, stream);
48 }
49
50 static gchar *get_cookie_file(gboolean init)
51 {
52   static gchar *cookie_file = NULL;
53   static GMutex *mutex = NULL;
54
55   if (init) { /* to make sure  it's thread safe */
56     mutex = g_mutex_new();
57     static gchar *cookie_fn = "cookies.txt";
58     const gchar *viking_dir = a_get_viking_dir();
59     cookie_file = g_build_filename(viking_dir, cookie_fn, NULL);
60     g_unlink(cookie_file);
61     return NULL;
62   }
63
64   g_assert(cookie_file != NULL);
65
66   g_mutex_lock(mutex);
67   if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) {  /* file not there */
68     FILE * out_file = tmpfile();
69     CURLcode res;
70     CURL *curl = curl_easy_init();
71     if (vik_verbose)
72       curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
73     curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */
74     curl_easy_setopt ( curl, CURLOPT_FILE, out_file );
75     curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
76     curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file);
77     res = curl_easy_perform(curl);
78     if (res != CURLE_OK) {
79       g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__,
80           curl_easy_strerror(res));
81       g_unlink(cookie_file);
82     }
83     curl_easy_cleanup(curl);
84     fclose(out_file);
85     out_file = NULL;
86   }
87   g_mutex_unlock(mutex);
88
89   return(cookie_file);
90 }
91
92 /* This should to be called from main() to make sure thread safe */
93 void curl_download_init()
94 {
95   curl_global_init(CURL_GLOBAL_ALL);
96   get_cookie_file(TRUE);
97 }
98
99 int curl_download_uri ( const char *uri, FILE *f, DownloadOptions *options )
100 {
101   CURL *curl;
102   CURLcode res = CURLE_FAILED_INIT;
103   const gchar *cookie_file;
104
105   g_debug("%s: uri=%s", __PRETTY_FUNCTION__, uri);
106
107   curl = curl_easy_init ();
108   if ( curl )
109     {
110       if (vik_verbose)
111         curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 );
112       curl_easy_setopt ( curl, CURLOPT_URL, uri );
113       curl_easy_setopt ( curl, CURLOPT_FILE, f );
114       curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func);
115       if (options != NULL) {
116         if(options->referer != NULL)
117           curl_easy_setopt ( curl, CURLOPT_REFERER, options->referer);
118         if(options->follow_location != 0) {
119           curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1);
120           curl_easy_setopt ( curl, CURLOPT_MAXREDIRS, options->follow_location);
121         }
122       }
123       curl_easy_setopt ( curl, CURLOPT_USERAGENT, PACKAGE "/" VERSION " libcurl/7.15.4" );
124       if ((cookie_file = get_cookie_file(FALSE)) != NULL)
125         curl_easy_setopt(curl, CURLOPT_COOKIEFILE, cookie_file);
126       res = curl_easy_perform ( curl );
127       curl_easy_cleanup ( curl );
128     }
129   return(res);
130 }
131
132 int curl_download_get_url ( const char *hostname, const char *uri, FILE *f, DownloadOptions *options, gboolean ftp )
133 {
134   int ret;
135   gchar *full = NULL;
136
137   /* Compose the full url */
138   full = g_strdup_printf ( "%s://%s%s", (ftp?"ftp":"http"), hostname, uri );
139   ret = curl_download_uri ( full, f, options );
140   g_free ( full );
141   full = NULL;
142
143   return (ret ? -2 : 0);   /* -2 HTTP error */
144 }