1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* libcurl backend for the URI plug-in
5
* Copyright (C) 2006 Mukund Sivaraman <muks@mukund.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <curl/curl.h>
27
#include <curl/types.h>
28
#include <curl/easy.h>
30
#include <libgimp/gimp.h>
31
#include <libgimp/gimpui.h>
33
#include "uri-backend.h"
35
#include "libgimp/stdplugins-intl.h"
38
/* private variables */
40
static gchar *supported_protocols = NULL;
41
static gchar *user_agent = NULL;
44
/* public functions */
47
uri_backend_init (const gchar *plugin_name,
53
curl_version_info_data *vinfo;
55
if (curl_global_init (CURL_GLOBAL_ALL))
57
g_set_error (error, 0, 0, _("Could not initialize libcurl"));
61
vinfo = curl_version_info (CURLVERSION_NOW);
63
protocols = g_string_new ("http:,ftp:");
65
if (vinfo->features & CURL_VERSION_SSL)
67
g_string_append (protocols, ",https:,ftps:");
70
supported_protocols = g_string_free (protocols, FALSE);
71
user_agent = g_strconcat ("GIMP/", GIMP_VERSION, NULL);
78
uri_backend_shutdown (void)
81
g_free (supported_protocols);
82
curl_global_cleanup ();
87
uri_backend_get_load_protocols (void)
89
return supported_protocols;
94
uri_backend_get_save_protocols (void)
101
progress_callback (void *clientp,
107
gchar *memsize = NULL;
111
memsize = gimp_memsize_to_string (dltotal);
112
gimp_progress_set_text_printf (_("Downloading %s of image data..."),
114
gimp_progress_update (dlnow / dltotal);
118
memsize = gimp_memsize_to_string (dlnow);
119
gimp_progress_set_text_printf (_("Downloaded %s of image data"),
121
gimp_progress_pulse ();
131
uri_backend_load_image (const gchar *uri,
132
const gchar *tmpname,
133
GimpRunMode run_mode,
141
gimp_progress_init (_("Connecting to server"));
143
if ((out_file = fopen(tmpname, "wb")) == NULL)
145
g_set_error (error, 0, 0,
146
_("Could not open output file for writing"));
150
curl_handle = curl_easy_init ();
151
curl_easy_setopt (curl_handle, CURLOPT_URL, uri);
152
curl_easy_setopt (curl_handle, CURLOPT_WRITEDATA, out_file);
153
curl_easy_setopt (curl_handle, CURLOPT_PROGRESSFUNCTION, progress_callback);
154
curl_easy_setopt (curl_handle, CURLOPT_NOPROGRESS, FALSE);
155
curl_easy_setopt (curl_handle, CURLOPT_USERAGENT, user_agent);
157
curl_easy_setopt (curl_handle, CURLOPT_FOLLOWLOCATION, TRUE);
158
curl_easy_setopt (curl_handle, CURLOPT_MAXREDIRS, 10);
159
curl_easy_setopt (curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
161
/* the following empty string causes libcurl to send a list of
162
* all supported encodings which turns on compression
163
* if libcurl has support for compression
166
curl_easy_setopt (curl_handle, CURLOPT_ENCODING, "");
168
if ((result = curl_easy_perform (curl_handle)) != 0)
171
g_set_error (error, 0, 0,
172
_("Could not open '%s' for reading: %s"),
173
uri, curl_easy_strerror (result));
174
curl_easy_cleanup (curl_handle);
178
curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
180
if (response_code != 200)
183
g_set_error (error, 0, 0,
184
_("Opening '%s' for reading resulted in HTTP "
185
"response code: %d"),
187
curl_easy_cleanup (curl_handle);
192
gimp_progress_update (1.0);
193
curl_easy_cleanup (curl_handle);
200
uri_backend_save_image (const gchar *uri,
201
const gchar *tmpname,
202
GimpRunMode run_mode,
205
g_set_error (error, 0, 0,
206
"EEK! uri_backend_save_image() should not have been called!");