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

« back to all changes in this revision

Viewing changes to plug-ins/uri/uri-backend-wget.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
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
/* Author: Josh MacDonald. */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <string.h>
 
26
#include <errno.h>
 
27
#include <sys/param.h>
 
28
#include <sys/wait.h>
 
29
 
 
30
#ifdef HAVE_UNISTD_H
 
31
#include <unistd.h>
 
32
#endif
 
33
 
 
34
#include <libgimp/gimp.h>
 
35
#include <libgimp/gimpui.h>
 
36
 
 
37
#include "uri-backend.h"
 
38
 
 
39
#include "libgimp/stdplugins-intl.h"
 
40
 
 
41
 
 
42
#define TIMEOUT  300
 
43
#define BUFSIZE 1024
 
44
 
 
45
 
 
46
gboolean
 
47
uri_backend_init (const gchar  *plugin_name,
 
48
                  gboolean      run,
 
49
                  GimpRunMode   run_mode,
 
50
                  GError      **error)
 
51
{
 
52
  return TRUE;
 
53
}
 
54
 
 
55
void
 
56
uri_backend_shutdown (void)
 
57
{
 
58
}
 
59
 
 
60
const gchar *
 
61
uri_backend_get_load_protocols (void)
 
62
{
 
63
  return "http:,https:,ftp:";
 
64
}
 
65
 
 
66
const gchar *
 
67
uri_backend_get_save_protocols (void)
 
68
{
 
69
  return NULL;
 
70
}
 
71
 
 
72
gboolean
 
73
uri_backend_load_image (const gchar  *uri,
 
74
                        const gchar  *tmpname,
 
75
                        GimpRunMode   run_mode,
 
76
                        GError      **error)
 
77
{
 
78
  gint pid;
 
79
  gint p[2];
 
80
 
 
81
  if (pipe (p) != 0)
 
82
    {
 
83
      g_set_error (error, 0, 0, "pipe() failed: %s", g_strerror (errno));
 
84
      return FALSE;
 
85
    }
 
86
 
 
87
  /*  open a process group, so killing the plug-in will kill wget too  */
 
88
  setpgid (0, 0);
 
89
 
 
90
  if ((pid = fork()) < 0)
 
91
    {
 
92
      g_set_error (error, 0, 0, "fork() failed: %s", g_strerror (errno));
 
93
      return FALSE;
 
94
    }
 
95
  else if (pid == 0)
 
96
    {
 
97
      gchar timeout_str[16];
 
98
 
 
99
      close (p[0]);
 
100
      close (2);
 
101
      dup (p[1]);
 
102
      close (p[1]);
 
103
 
 
104
#ifdef HAVE_PUTENV
 
105
      /* produce deterministic output */
 
106
      putenv ("LANGUAGE=C");
 
107
      putenv ("LC_ALL=C");
 
108
      putenv ("LANG=C");
 
109
#endif
 
110
 
 
111
      g_snprintf (timeout_str, sizeof (timeout_str), "%d", TIMEOUT);
 
112
 
 
113
      execlp ("wget",
 
114
              "wget", "-v", "-e", "server-response=off", "-T", timeout_str,
 
115
              uri, "-O", tmpname, NULL);
 
116
      g_set_error (error, 0, 0, "exec() failed: wget: %s", g_strerror (errno));
 
117
      _exit (127);
 
118
    }
 
119
  else
 
120
    {
 
121
      FILE     *input;
 
122
      gchar     buf[BUFSIZE];
 
123
      gboolean  seen_resolve = FALSE;
 
124
      gboolean  connected    = FALSE;
 
125
      gboolean  redirect     = FALSE;
 
126
      gboolean  file_found   = FALSE;
 
127
      gchar     sizestr[37];
 
128
      gchar    *endptr;
 
129
      guint64   size         = 0;
 
130
      gint      i, j;
 
131
      gchar     dot;
 
132
      guint64   kilobytes    = 0;
 
133
      gboolean  finished     = FALSE;
 
134
      gboolean  debug        = FALSE;
 
135
      gchar    *memsize;
 
136
      gchar    *message;
 
137
      gchar    *timeout_msg;
 
138
 
 
139
#define DEBUG(x) if (debug) g_printerr (x)
 
140
 
 
141
      close (p[1]);
 
142
 
 
143
      input = fdopen (p[0], "r");
 
144
 
 
145
      /*  hardcoded and not-really-foolproof scanning of wget putput  */
 
146
 
 
147
    wget_begin:
 
148
      /* Eat any Location lines */
 
149
      if (redirect && fgets (buf, sizeof (buf), input) == NULL)
 
150
        {
 
151
          g_set_error (error, 0, 0,
 
152
                       _("wget exited abnormally on URI '%s'"), uri);
 
153
          return FALSE;
 
154
        }
 
155
 
 
156
      redirect = FALSE;
 
157
 
 
158
      if (fgets (buf, sizeof (buf), input) == NULL)
 
159
        {
 
160
          /*  no message here because failing on the first line means
 
161
           *  that wget was not found
 
162
           */
 
163
          return FALSE;
 
164
        }
 
165
 
 
166
      DEBUG (buf);
 
167
 
 
168
      /*  The second line is the local copy of the file  */
 
169
      if (fgets (buf, sizeof (buf), input) == NULL)
 
170
        {
 
171
          g_set_error (error, 0, 0,
 
172
                       _("wget exited abnormally on URI '%s'"), uri);
 
173
          return FALSE;
 
174
        }
 
175
 
 
176
      DEBUG (buf);
 
177
 
 
178
      /*  The third line is "Connecting to..."  */
 
179
 
 
180
      timeout_msg = g_strdup_printf (ngettext ("(timeout is %d second)",
 
181
                                               "(timeout is %d seconds)",
 
182
                                               TIMEOUT), TIMEOUT);
 
183
 
 
184
      gimp_progress_init_printf ("%s %s",
 
185
                                 _("Connecting to server"), timeout_msg);
 
186
 
 
187
    read_connect:
 
188
      if (fgets (buf, sizeof (buf), input) == NULL)
 
189
        {
 
190
          g_set_error (error, 0, 0,
 
191
                       _("wget exited abnormally on URI '%s'"), uri);
 
192
          return FALSE;
 
193
        }
 
194
      else if (strstr (buf, "connected"))
 
195
        {
 
196
          connected = TRUE;
 
197
        }
 
198
      /* newer wgets have a "Resolving foo" line, so eat it */
 
199
      else if (!seen_resolve && strstr (buf, "Resolving"))
 
200
        {
 
201
          seen_resolve = TRUE;
 
202
          goto read_connect;
 
203
        }
 
204
 
 
205
      DEBUG (buf);
 
206
 
 
207
      /*  The fourth line is either the network request or an error  */
 
208
 
 
209
      gimp_progress_set_text_printf ("%s %s", _("Opening URI"), timeout_msg);
 
210
 
 
211
      if (fgets (buf, sizeof (buf), input) == NULL)
 
212
        {
 
213
          g_set_error (error, 0, 0,
 
214
                       _("wget exited abnormally on URI '%s'"), uri);
 
215
          return FALSE;
 
216
        }
 
217
      else if (! connected)
 
218
        {
 
219
          g_set_error (error, 0, 0,
 
220
                       _("A network error occurred: %s"), buf);
 
221
 
 
222
          DEBUG (buf);
 
223
 
 
224
          return FALSE;
 
225
        }
 
226
      else if (strstr (buf, "302 Found"))
 
227
        {
 
228
          DEBUG (buf);
 
229
 
 
230
          connected = FALSE;
 
231
          seen_resolve = FALSE;
 
232
 
 
233
          redirect = TRUE;
 
234
          goto wget_begin;
 
235
        }
 
236
 
 
237
      DEBUG (buf);
 
238
 
 
239
      /*  The fifth line is either the length of the file or an error  */
 
240
      if (fgets (buf, sizeof (buf), input) == NULL)
 
241
        {
 
242
          g_set_error (error, 0, 0,
 
243
                       _("wget exited abnormally on URI '%s'"), uri);
 
244
          return FALSE;
 
245
        }
 
246
      else if (strstr (buf, "Length"))
 
247
        {
 
248
          file_found = TRUE;
 
249
        }
 
250
      else
 
251
        {
 
252
          g_set_error (error, 0, 0,
 
253
                       _("A network error occurred: %s"), buf);
 
254
 
 
255
          DEBUG (buf);
 
256
 
 
257
          return FALSE;
 
258
        }
 
259
 
 
260
      DEBUG (buf);
 
261
 
 
262
      if (sscanf (buf, "Length: %37s", sizestr) != 1)
 
263
        {
 
264
          g_set_error (error, 0, 0,
 
265
                       "Could not parse wget's file length message");
 
266
          return FALSE;
 
267
        }
 
268
 
 
269
      /*  strip away commas  */
 
270
      for (i = 0, j = 0; i < sizeof (sizestr); i++, j++)
 
271
        {
 
272
          if (sizestr[i] == ',')
 
273
            i++;
 
274
 
 
275
          sizestr[j] = sizestr[i];
 
276
 
 
277
          if (sizestr[j] == '\0')
 
278
            break;
 
279
        }
 
280
 
 
281
      if (*sizestr != '\0')
 
282
        {
 
283
          size = g_ascii_strtoull (sizestr, &endptr, 10);
 
284
 
 
285
          if (*endptr != '\0' || size == G_MAXUINT64)
 
286
            size = 0;
 
287
        }
 
288
 
 
289
      /*  Start the actual download...  */
 
290
      if (size > 0)
 
291
        {
 
292
          memsize = gimp_memsize_to_string (size);
 
293
          message = g_strdup_printf (_("Downloading %s of image data"),
 
294
                                     memsize);
 
295
        }
 
296
      else
 
297
        {
 
298
          message = g_strdup (_("Downloading unknown amount of image data"));
 
299
          memsize = NULL;
 
300
        }
 
301
 
 
302
      gimp_progress_set_text_printf ("%s %s", message, timeout_msg);
 
303
 
 
304
      g_free (message);
 
305
      g_free (memsize);
 
306
 
 
307
      /*  Switch to byte parsing wget's output...  */
 
308
 
 
309
      while (1)
 
310
        {
 
311
          dot = fgetc (input);
 
312
 
 
313
          if (feof (input))
 
314
            break;
 
315
 
 
316
          if (debug)
 
317
            {
 
318
              fputc (dot, stderr);
 
319
              fflush (stderr);
 
320
            }
 
321
 
 
322
          if (dot == '.')  /* one kilobyte */
 
323
            {
 
324
              kilobytes++;
 
325
 
 
326
              if (size > 0)
 
327
                {
 
328
                  gimp_progress_update ((gdouble) (kilobytes * 1024) /
 
329
                                        (gdouble) size);
 
330
                }
 
331
              else
 
332
                {
 
333
                  memsize = gimp_memsize_to_string (kilobytes * 1024);
 
334
 
 
335
                  gimp_progress_set_text_printf
 
336
                    (_("Downloaded %s of image data"), memsize);
 
337
                  gimp_progress_pulse ();
 
338
 
 
339
                  g_free (memsize);
 
340
                }
 
341
            }
 
342
          else if (dot == ':')  /* the time string contains a ':' */
 
343
            {
 
344
              fgets (buf, sizeof (buf), input);
 
345
 
 
346
              DEBUG (buf);
 
347
 
 
348
              if (! strstr (buf, "error"))
 
349
                {
 
350
                  finished = TRUE;
 
351
                  gimp_progress_update (1.0);
 
352
                }
 
353
 
 
354
              break;
 
355
            }
 
356
        }
 
357
 
 
358
      if (! finished)
 
359
        {
 
360
          g_set_error (error, 0, 0,
 
361
                       "wget exited before finishing downloading URI\n'%s'",
 
362
                       uri);
 
363
          return FALSE;
 
364
        }
 
365
    }
 
366
 
 
367
  return TRUE;
 
368
}
 
369
 
 
370
gboolean
 
371
uri_backend_save_image (const gchar  *uri,
 
372
                        const gchar  *tmpname,
 
373
                        GimpRunMode   run_mode,
 
374
                        GError      **error)
 
375
{
 
376
  g_set_error (error, 0, 0, "EEK");
 
377
 
 
378
  return FALSE;
 
379
}