~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/uri/uri-backend-libcurl.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * libcurl backend for the URI plug-in
 
5
 * Copyright (C) 2006 Mukund Sivaraman <muks@mukund.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdio.h>
 
25
 
 
26
#include <curl/curl.h>
 
27
#include <curl/types.h>
 
28
#include <curl/easy.h>
 
29
 
 
30
#include <libgimp/gimp.h>
 
31
#include <libgimp/gimpui.h>
 
32
 
 
33
#include "uri-backend.h"
 
34
 
 
35
#include "libgimp/stdplugins-intl.h"
 
36
 
 
37
 
 
38
/*  private variables  */
 
39
 
 
40
static gchar *supported_protocols = NULL;
 
41
static gchar *user_agent          = NULL;
 
42
 
 
43
 
 
44
/*  public functions  */
 
45
 
 
46
gboolean
 
47
uri_backend_init (const gchar  *plugin_name,
 
48
                  gboolean      run,
 
49
                  GimpRunMode   run_mode,
 
50
                  GError      **error)
 
51
{
 
52
  GString                *protocols;
 
53
  curl_version_info_data *vinfo;
 
54
 
 
55
  if (curl_global_init (CURL_GLOBAL_ALL))
 
56
    {
 
57
      g_set_error (error, 0, 0, _("Could not initialize libcurl"));
 
58
      return FALSE;
 
59
    }
 
60
 
 
61
  vinfo = curl_version_info (CURLVERSION_NOW);
 
62
 
 
63
  protocols = g_string_new ("http:,ftp:");
 
64
 
 
65
  if (vinfo->features & CURL_VERSION_SSL)
 
66
    {
 
67
      g_string_append (protocols, ",https:,ftps:");
 
68
    }
 
69
 
 
70
  supported_protocols = g_string_free (protocols, FALSE);
 
71
  user_agent = g_strconcat ("GIMP/", GIMP_VERSION, NULL);
 
72
 
 
73
  return TRUE;
 
74
}
 
75
 
 
76
 
 
77
void
 
78
uri_backend_shutdown (void)
 
79
{
 
80
  g_free (user_agent);
 
81
  g_free (supported_protocols);
 
82
  curl_global_cleanup ();
 
83
}
 
84
 
 
85
 
 
86
const gchar *
 
87
uri_backend_get_load_protocols (void)
 
88
{
 
89
  return supported_protocols;
 
90
}
 
91
 
 
92
 
 
93
const gchar *
 
94
uri_backend_get_save_protocols (void)
 
95
{
 
96
  return NULL;
 
97
}
 
98
 
 
99
 
 
100
static int
 
101
progress_callback (void   *clientp,
 
102
                   double  dltotal,
 
103
                   double  dlnow,
 
104
                   double  ultotal,
 
105
                   double  ulnow)
 
106
{
 
107
  gchar *memsize = NULL;
 
108
 
 
109
  if (dltotal > 0.0)
 
110
    {
 
111
      memsize = gimp_memsize_to_string (dltotal);
 
112
      gimp_progress_set_text_printf (_("Downloading %s of image data..."),
 
113
                                     memsize);
 
114
      gimp_progress_update (dlnow / dltotal);
 
115
    }
 
116
  else
 
117
    {
 
118
      memsize = gimp_memsize_to_string (dlnow);
 
119
      gimp_progress_set_text_printf (_("Downloaded %s of image data"),
 
120
                                     memsize);
 
121
      gimp_progress_pulse ();
 
122
    }
 
123
 
 
124
  g_free (memsize);
 
125
 
 
126
  return 0;
 
127
}
 
128
 
 
129
 
 
130
gboolean
 
131
uri_backend_load_image (const gchar  *uri,
 
132
                        const gchar  *tmpname,
 
133
                        GimpRunMode   run_mode,
 
134
                        GError      **error)
 
135
{
 
136
  FILE      *out_file;
 
137
  CURL      *curl_handle;
 
138
  CURLcode   result;
 
139
  gint       response_code;
 
140
 
 
141
  gimp_progress_init (_("Connecting to server"));
 
142
 
 
143
  if ((out_file = fopen(tmpname, "wb")) == NULL)
 
144
    {
 
145
      g_set_error (error, 0, 0,
 
146
                   _("Could not open output file for writing"));
 
147
      return FALSE;
 
148
    }
 
149
 
 
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);
 
156
 
 
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);
 
160
 
 
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
 
164
   */
 
165
 
 
166
  curl_easy_setopt (curl_handle, CURLOPT_ENCODING, "");
 
167
 
 
168
  if ((result = curl_easy_perform (curl_handle)) != 0)
 
169
    {
 
170
      fclose (out_file);
 
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);
 
175
      return FALSE;
 
176
    }
 
177
 
 
178
  curl_easy_getinfo (curl_handle, CURLINFO_RESPONSE_CODE, &response_code);
 
179
 
 
180
  if (response_code != 200)
 
181
    {
 
182
      fclose (out_file);
 
183
      g_set_error (error, 0, 0,
 
184
                   _("Opening '%s' for reading resulted in HTTP "
 
185
                     "response code: %d"),
 
186
                   uri, response_code);
 
187
      curl_easy_cleanup (curl_handle);
 
188
      return FALSE;
 
189
    }
 
190
 
 
191
  fclose (out_file);
 
192
  gimp_progress_update (1.0);
 
193
  curl_easy_cleanup (curl_handle);
 
194
 
 
195
  return TRUE;
 
196
}
 
197
 
 
198
 
 
199
gboolean
 
200
uri_backend_save_image (const gchar  *uri,
 
201
                        const gchar  *tmpname,
 
202
                        GimpRunMode   run_mode,
 
203
                        GError      **error)
 
204
{
 
205
  g_set_error (error, 0, 0,
 
206
               "EEK! uri_backend_save_image() should not have been called!");
 
207
 
 
208
  return FALSE;
 
209
}