]> sjero.net Git - wget/blob - src/retr.c
[svn] * *.{gmo,po,pot}: Regenerated after modifying wget --help output.
[wget] / src / retr.c
1 /* File retrieval.
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of Wget.
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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif /* HAVE_UNISTD_H */
28 #include <errno.h>
29 #ifdef HAVE_STRING_H
30 # include <string.h>
31 #else
32 # include <strings.h>
33 #endif /* HAVE_STRING_H */
34 #include <ctype.h>
35 #include <assert.h>
36
37 #include "wget.h"
38 #include "utils.h"
39 #include "retr.h"
40 #include "url.h"
41 #include "recur.h"
42 #include "ftp.h"
43 #include "host.h"
44 #include "connect.h"
45
46 /* Internal variables used by the timer.  */
47 static long internal_secs, internal_msecs;
48
49 void logflush PARAMS ((void));
50
51 /* From http.c.  */
52 uerr_t http_loop PARAMS ((struct urlinfo *, char **, int *));
53 \f
54 /* Flags for show_progress().  */
55 enum spflags { SP_NONE, SP_INIT, SP_FINISH };
56
57 static int show_progress PARAMS ((long, long, enum spflags));
58
59 /* Reads the contents of file descriptor FD, until it is closed, or a
60    read error occurs.  The data is read in 8K chunks, and stored to
61    stream fp, which should have been open for writing.  If BUF is
62    non-NULL and its file descriptor is equal to FD, flush RBUF first.
63    This function will *not* use the rbuf_* functions!
64
65    The EXPECTED argument is passed to show_progress() unchanged, but
66    otherwise ignored.
67
68    If opt.verbose is set, the progress is also shown.  RESTVAL
69    represents a value from which to start downloading (which will be
70    shown accordingly).  If RESTVAL is non-zero, the stream should have
71    been open for appending.
72
73    The function exits and returns codes of 0, -1 and -2 if the
74    connection was closed, there was a read error, or if it could not
75    write to the output stream, respectively.
76
77    IMPORTANT: The function flushes the contents of the buffer in
78    rbuf_flush() before actually reading from fd.  If you wish to read
79    from fd immediately, flush or discard the buffer.  */
80 int
81 get_contents (int fd, FILE *fp, long *len, long restval, long expected,
82               struct rbuf *rbuf)
83 {
84   int res;
85   static char c[8192];
86
87   *len = restval;
88   if (opt.verbose)
89     show_progress (restval, expected, SP_INIT);
90   if (rbuf && RBUF_FD (rbuf) == fd)
91     {
92       while ((res = rbuf_flush (rbuf, c, sizeof (c))) != 0)
93         {
94           if (fwrite (c, sizeof (char), res, fp) < res)
95             return -2;
96           if (opt.verbose)
97             {
98               if (show_progress (res, expected, SP_NONE))
99                 fflush (fp);
100             }
101           *len += res;
102         }
103     }
104   /* Read from fd while there is available data.  */
105   do
106     {
107       res = iread (fd, c, sizeof (c));
108       if (res > 0)
109         {
110           if (fwrite (c, sizeof (char), res, fp) < res)
111             return -2;
112           if (opt.verbose)
113             {
114               if (show_progress (res, expected, SP_NONE))
115                 fflush (fp);
116             }
117           *len += res;
118         }
119     } while (res > 0);
120   if (res < -1)
121     res = -1;
122   if (opt.verbose)
123     show_progress (0, expected, SP_FINISH);
124   return res;
125 }
126
127 static void
128 print_percentage (long bytes, long expected)
129 {
130   int percentage = (int)(100.0 * bytes / expected);
131   logprintf (LOG_VERBOSE, " [%3d%%]", percentage);
132 }
133
134 /* Show the dotted progress report of file loading.  Called with
135    length and a flag to tell it whether to reset or not.  It keeps the
136    offset information in static local variables.
137
138    Return value: 1 or 0, designating whether any dots have been drawn.
139
140    If the init argument is set, the routine will initialize.
141
142    If the res is non-zero, res/line_bytes lines are skipped
143    (meaning the appropriate number ok kilobytes), and the number of
144    "dots" fitting on the first line are drawn as ','.  */
145 static int
146 show_progress (long res, long expected, enum spflags flags)
147 {
148   static long line_bytes;
149   static long offs;
150   static int ndot, nrow;
151   int any_output = 0;
152
153   if (flags == SP_FINISH)
154     {
155       if (expected)
156         {
157           int dot = ndot;
158           char *tmpstr = (char *)alloca (2 * opt.dots_in_line + 1);
159           char *tmpp = tmpstr;
160           for (; dot < opt.dots_in_line; dot++)
161             {
162               if (!(dot % opt.dot_spacing))
163                 *tmpp++ = ' ';
164               *tmpp++ = ' ';
165             }
166           *tmpp = '\0';
167           logputs (LOG_VERBOSE, tmpstr);
168           print_percentage (nrow * line_bytes + ndot * opt.dot_bytes + offs,
169                             expected);
170         }
171       logputs (LOG_VERBOSE, "\n\n");
172       return 0;
173     }
174
175   /* Temporarily disable flushing.  */
176   opt.no_flush = 1;
177   /* init set means initialization.  If res is set, it also means that
178      the retrieval is *not* done from the beginning.  The part that
179      was already retrieved is not shown again.  */
180   if (flags == SP_INIT)
181     {
182       /* Generic initialization of static variables.  */
183       offs = 0L;
184       ndot = nrow = 0;
185       line_bytes = (long)opt.dots_in_line * opt.dot_bytes;
186       if (res)
187         {
188           if (res >= line_bytes)
189             {
190               nrow = res / line_bytes;
191               res %= line_bytes;
192               logprintf (LOG_VERBOSE,
193                          _("\n          [ skipping %dK ]"),
194                          (int) ((nrow * line_bytes) / 1024));
195               ndot = 0;
196             }
197         }
198       logprintf (LOG_VERBOSE, "\n%5ldK ->", nrow * line_bytes / 1024);
199     }
200   /* Offset gets incremented by current value.  */
201   offs += res;
202   /* While offset is >= opt.dot_bytes, print dots, taking care to
203      precede every 50th dot with a status message.  */
204   for (; offs >= opt.dot_bytes; offs -= opt.dot_bytes)
205     {
206       if (!(ndot % opt.dot_spacing))
207         logputs (LOG_VERBOSE, " ");
208       any_output = 1;
209       logputs (LOG_VERBOSE, flags == SP_INIT ? "," : ".");
210       ++ndot;
211       if (ndot == opt.dots_in_line)
212         {
213           ndot = 0;
214           ++nrow;
215           if (expected)
216             print_percentage (nrow * line_bytes, expected);
217           logprintf (LOG_VERBOSE, "\n%5ldK ->", nrow * line_bytes / 1024);
218         }
219     }
220   /* Reenable flushing.  */
221   opt.no_flush = 0;
222   if (any_output)
223     /* Force flush.  #### Oh, what a kludge!  */
224     logflush ();
225   return any_output;
226 }
227 \f
228 /* Reset the internal timer.  */
229 void
230 reset_timer (void)
231 {
232 #ifdef HAVE_GETTIMEOFDAY
233   struct timeval t;
234   gettimeofday (&t, NULL);
235   internal_secs = t.tv_sec;
236   internal_msecs = t.tv_usec / 1000;
237 #else
238   internal_secs = time (NULL);
239   internal_msecs = 0;
240 #endif
241 }
242
243 /* Return the time elapsed from the last call to reset_timer(), in
244    milliseconds.  */
245 long
246 elapsed_time (void)
247 {
248 #ifdef HAVE_GETTIMEOFDAY
249   struct timeval t;
250   gettimeofday (&t, NULL);
251   return ((t.tv_sec - internal_secs) * 1000
252           + (t.tv_usec / 1000 - internal_msecs));
253 #else
254   return 1000 * ((long)time (NULL) - internal_secs);
255 #endif
256 }
257
258 /* Print out the appropriate download rate.  Appropriate means that if
259    rate is > 1024 bytes per second, kilobytes are used, and if rate >
260    1024 * 1024 bps, megabytes are used.  */
261 char *
262 rate (long bytes, long msecs)
263 {
264   static char res[15];
265   double dlrate;
266
267   if (!msecs)
268     ++msecs;
269   dlrate = (double)1000 * bytes / msecs;
270   /* #### Should these strings be translatable?  */
271   if (dlrate < 1024.0)
272     sprintf (res, "%.2f B/s", dlrate);
273   else if (dlrate < 1024.0 * 1024.0)
274     sprintf (res, "%.2f KB/s", dlrate / 1024.0);
275   else
276     sprintf (res, "%.2f MB/s", dlrate / (1024.0 * 1024.0));
277   return res;
278 }
279 \f
280 #define USE_PROXY_P(u) (opt.use_proxy && getproxy((u)->proto)           \
281                         && no_proxy_match((u)->host,                    \
282                                           (const char **)opt.no_proxy))
283
284 /* Retrieve the given URL.  Decides which loop to call -- HTTP, FTP,
285    or simply copy it with file:// (#### the latter not yet
286    implemented!).  */
287 uerr_t
288 retrieve_url (const char *origurl, char **file, char **newloc,
289               const char *refurl, int *dt)
290 {
291   uerr_t result;
292   char *url;
293   int location_changed, already_redirected, dummy;
294   int local_use_proxy;
295   char *mynewloc, *proxy;
296   struct urlinfo *u;
297
298
299   /* If dt is NULL, just ignore it.  */
300   if (!dt)
301     dt = &dummy;
302   url = xstrdup (origurl);
303   if (newloc)
304     *newloc = NULL;
305   if (file)
306     *file = NULL;
307   already_redirected = 0;
308
309  again:
310   u = newurl ();
311   /* Parse the URL.  RFC2068 requires `Location' to contain an
312      absoluteURI, but many sites break this requirement.  #### We
313      should be liberal and accept a relative location, too.  */
314   result = parseurl (url, u, already_redirected);
315   if (result != URLOK)
316     {
317       freeurl (u, 1);
318       logprintf (LOG_NOTQUIET, "%s: %s.\n", url, uerrmsg (result));
319       return result;
320     }
321
322   /* Set the referer.  */
323   if (refurl)
324     u->referer = xstrdup (refurl);
325   else
326     {
327       if (opt.referer)
328         u->referer = xstrdup (opt.referer);
329       else
330         u->referer = NULL;
331     }
332
333   local_use_proxy = USE_PROXY_P (u);
334   if (local_use_proxy)
335     {
336       struct urlinfo *pu = newurl ();
337
338       /* Copy the original URL to new location.  */
339       memcpy (pu, u, sizeof (*u));
340       pu->proxy = NULL; /* A minor correction :) */
341       /* Initialize u to nil.  */
342       memset (u, 0, sizeof (*u));
343       u->proxy = pu;
344       /* Get the appropriate proxy server, appropriate for the
345          current protocol.  */
346       proxy = getproxy (pu->proto);
347       if (!proxy)
348         {
349           logputs (LOG_NOTQUIET, _("Could not find proxy host.\n"));
350           freeurl (u, 1);
351           return PROXERR;
352         }
353       /* Parse the proxy URL.  */
354       result = parseurl (proxy, u, 0);
355       if (result != URLOK || u->proto != URLHTTP)
356         {
357           if (u->proto == URLHTTP)
358             logprintf (LOG_NOTQUIET, "Proxy %s: %s.\n", proxy, uerrmsg(result));
359           else
360             logprintf (LOG_NOTQUIET, _("Proxy %s: Must be HTTP.\n"), proxy);
361           freeurl (u, 1);
362           return PROXERR;
363         }
364       u->proto = URLHTTP;
365     }
366
367   assert (u->proto != URLFILE); /* #### Implement me!  */
368   mynewloc = NULL;
369
370   if (u->proto == URLHTTP)
371     result = http_loop (u, &mynewloc, dt);
372   else if (u->proto == URLFTP)
373     {
374       /* If this is a redirection, we must not allow recursive FTP
375          retrieval, so we save recursion to oldrec, and restore it
376          later.  */
377       int oldrec = opt.recursive;
378       if (already_redirected)
379         opt.recursive = 0;
380       result = ftp_loop (u, dt);
381       opt.recursive = oldrec;
382       /* There is a possibility of having HTTP being redirected to
383          FTP.  In these cases we must decide whether the text is HTML
384          according to the suffix.  The HTML suffixes are `.html' and
385          `.htm', case-insensitive.
386
387          #### All of this is, of course, crap.  These types should be
388          determined through mailcap.  */
389       if (already_redirected && u->local && (u->proto == URLFTP ))
390         {
391           char *suf = suffix (u->local);
392           if (suf && (!strcasecmp (suf, "html") || !strcasecmp (suf, "htm")))
393             *dt |= TEXTHTML;
394           FREE_MAYBE (suf);
395         }
396     }
397   location_changed = (result == NEWLOCATION);
398   if (location_changed)
399     {
400       /* Check for redirection to oneself.  */
401       if (url_equal (url, mynewloc))
402         {
403           logprintf (LOG_NOTQUIET, _("%s: Redirection to itself.\n"),
404                      mynewloc);
405           return WRONGCODE;
406         }
407       if (mynewloc)
408         {
409           free (url);
410           url = mynewloc;
411         }
412       freeurl (u, 1);
413       already_redirected = 1;
414       goto again;
415     }
416   if (file)
417     {
418       if (u->local)
419         *file = xstrdup (u->local);
420       else
421         *file = NULL;
422     }
423   freeurl (u, 1);
424
425   if (newloc)
426     *newloc = url;
427   else
428     free (url);
429
430   return result;
431 }
432
433 /* Find the URLs in the file and call retrieve_url() for each of
434    them.  If HTML is non-zero, treat the file as HTML, and construct
435    the URLs accordingly.
436
437    If opt.recursive is set, call recursive_retrieve() for each file.  */
438 uerr_t
439 retrieve_from_file (const char *file, int html, int *count)
440 {
441   uerr_t status;
442   urlpos *url_list, *cur_url;
443
444   /* If spider-mode is on, we do not want get_urls_html barfing
445      errors on baseless links.  */
446   url_list = (html ? get_urls_html (file, NULL, opt.spider, FALSE)
447               : get_urls_file (file));
448   status = RETROK;             /* Suppose everything is OK.  */
449   *count = 0;                  /* Reset the URL count.  */
450   recursive_reset ();
451   for (cur_url = url_list; cur_url; cur_url = cur_url->next, ++*count)
452     {
453       char *filename, *new_file;
454       int dt;
455
456       if (opt.quota && opt.downloaded > opt.quota)
457         {
458           status = QUOTEXC;
459           break;
460         }
461       status = retrieve_url (cur_url->url, &filename, &new_file, NULL, &dt);
462       if (opt.recursive && status == RETROK && (dt & TEXTHTML))
463         status = recursive_retrieve (filename, new_file ? new_file
464                                                         : cur_url->url);
465
466       if (filename && opt.delete_after && file_exists_p (filename))
467         {
468           logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
469           if (unlink (filename))
470             logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
471           dt &= ~RETROKF;
472         }
473
474       FREE_MAYBE (new_file);
475       FREE_MAYBE (filename);
476     }
477
478   /* Free the linked list of URL-s.  */
479   free_urlpos (url_list);
480
481   return status;
482 }
483
484 /* Print `giving up', or `retrying', depending on the impending
485    action.  N1 and N2 are the attempt number and the attempt limit.  */
486 void
487 printwhat (int n1, int n2)
488 {
489   logputs (LOG_VERBOSE, (n1 == n2) ? _("Giving up.\n\n") : _("Retrying.\n\n"));
490 }