~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to src/retr.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-06-26 16:46:25 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050626164625-jjcde8hyztx7xq7o
Tags: 1.10-2
* wget-fix_error--save-headers patch from upstream
  (closes: Bug#314728)
* don't pattern-match server redirects patch from upstream
  (closes: Bug#163243)
* correct de.po typos
  (closes: Bug#313883)
* wget-E_html_behind_file_counting fix problem with adding the
  numbers after the html extension
* updated Standards-Version: to 3.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
#include "connect.h"
55
55
#include "hash.h"
56
56
#include "convert.h"
57
 
 
58
 
#ifdef HAVE_SSL
59
 
# include "gen_sslfunc.h"       /* for ssl_iread */
60
 
#endif
 
57
#include "ptimer.h"
61
58
 
62
59
#ifndef errno
63
60
extern int errno;
64
61
#endif
65
62
 
66
 
/* See the comment in gethttp() why this is needed. */
67
 
int global_download_count;
68
 
 
69
63
/* Total size of downloaded files.  Used to enforce quota.  */
70
64
LARGE_INT total_downloaded_bytes;
71
65
 
 
66
/* If non-NULL, the stream to which output should be written.  This
 
67
   stream is initialized when `-O' is used.  */
 
68
FILE *output_stream;
 
69
 
 
70
/* Whether output_document is a regular file we can manipulate,
 
71
   i.e. not `-' or a device file. */
 
72
int output_stream_regular;
72
73
 
73
74
static struct {
74
 
  long chunk_bytes;
 
75
  wgint chunk_bytes;
75
76
  double chunk_start;
76
77
  double sleep_adjust;
77
78
} limit_data;
81
82
{
82
83
  limit_data.chunk_bytes = 0;
83
84
  limit_data.chunk_start = 0;
 
85
  limit_data.sleep_adjust = 0;
84
86
}
85
87
 
86
88
/* Limit the bandwidth by pausing the download for an amount of time.
87
 
   BYTES is the number of bytes received from the network, and DELTA
88
 
   is the number of milliseconds it took to receive them.  */
 
89
   BYTES is the number of bytes received from the network, and TIMER
 
90
   is the timer that started at the beginning of download.  */
89
91
 
90
92
static void
91
 
limit_bandwidth (long bytes, double *dltime, struct wget_timer *timer)
 
93
limit_bandwidth (wgint bytes, struct ptimer *timer)
92
94
{
93
 
  double delta_t = *dltime - limit_data.chunk_start;
 
95
  double delta_t = ptimer_read (timer) - limit_data.chunk_start;
94
96
  double expected;
95
97
 
96
98
  limit_data.chunk_bytes += bytes;
106
108
      double t0, t1;
107
109
      if (slp < 200)
108
110
        {
109
 
          DEBUGP (("deferring a %.2f ms sleep (%ld/%.2f).\n",
110
 
                   slp, limit_data.chunk_bytes, delta_t));
 
111
          DEBUGP (("deferring a %.2f ms sleep (%s/%.2f).\n",
 
112
                   slp, number_to_static_string (limit_data.chunk_bytes),
 
113
                   delta_t));
111
114
          return;
112
115
        }
113
 
      DEBUGP (("\nsleeping %.2f ms for %ld bytes, adjust %.2f ms\n",
114
 
               slp, limit_data.chunk_bytes, limit_data.sleep_adjust));
 
116
      DEBUGP (("\nsleeping %.2f ms for %s bytes, adjust %.2f ms\n",
 
117
               slp, number_to_static_string (limit_data.chunk_bytes),
 
118
               limit_data.sleep_adjust));
115
119
 
116
 
      t0 = *dltime;
117
 
      usleep ((unsigned long) (1000 * slp));
118
 
      t1 = wtimer_elapsed (timer);
 
120
      t0 = ptimer_read (timer);
 
121
      xsleep (slp / 1000);
 
122
      t1 = ptimer_measure (timer);
119
123
 
120
124
      /* Due to scheduling, we probably slept slightly longer (or
121
125
         shorter) than desired.  Calculate the difference between the
122
126
         desired and the actual sleep, and adjust the next sleep by
123
127
         that amount.  */
124
128
      limit_data.sleep_adjust = slp - (t1 - t0);
125
 
 
126
 
      /* Since we've called wtimer_elapsed, we might as well update
127
 
         the caller's dltime. */
128
 
      *dltime = t1;
 
129
      /* If sleep_adjust is very large, it's likely due to suspension
 
130
         and not clock inaccuracy.  Don't enforce those.  */
 
131
      if (limit_data.sleep_adjust > 500)
 
132
        limit_data.sleep_adjust = 500;
 
133
      else if (limit_data.sleep_adjust < -500)
 
134
        limit_data.sleep_adjust = -500;
129
135
    }
130
136
 
131
137
  limit_data.chunk_bytes = 0;
132
 
  limit_data.chunk_start = *dltime;
133
 
}
134
 
 
135
 
#define MIN(i, j) ((i) <= (j) ? (i) : (j))
136
 
 
137
 
/* Reads the contents of file descriptor FD, until it is closed, or a
138
 
   read error occurs.  The data is read in 8K chunks, and stored to
139
 
   stream fp, which should have been open for writing.  If BUF is
140
 
   non-NULL and its file descriptor is equal to FD, flush RBUF first.
141
 
   This function will *not* use the rbuf_* functions!
142
 
 
143
 
   The EXPECTED argument is passed to show_progress() unchanged, but
144
 
   otherwise ignored.
145
 
 
146
 
   If opt.verbose is set, the progress is also shown.  RESTVAL
147
 
   represents a value from which to start downloading (which will be
148
 
   shown accordingly).  If RESTVAL is non-zero, the stream should have
149
 
   been open for appending.
150
 
 
151
 
   The function exits and returns codes of 0, -1 and -2 if the
152
 
   connection was closed, there was a read error, or if it could not
153
 
   write to the output stream, respectively.
154
 
 
155
 
   IMPORTANT: The function flushes the contents of the buffer in
156
 
   rbuf_flush() before actually reading from fd.  If you wish to read
157
 
   from fd immediately, flush or discard the buffer.  */
 
138
  limit_data.chunk_start = ptimer_read (timer);
 
139
}
 
140
 
 
141
#ifndef MIN
 
142
# define MIN(i, j) ((i) <= (j) ? (i) : (j))
 
143
#endif
 
144
 
 
145
/* Write data in BUF to OUT.  However, if *SKIP is non-zero, skip that
 
146
   amount of data and decrease SKIP.  Increment *TOTAL by the amount
 
147
   of data written.  */
 
148
 
 
149
static int
 
150
write_data (FILE *out, const char *buf, int bufsize, wgint *skip,
 
151
            wgint *written)
 
152
{
 
153
  if (!out)
 
154
    return 1;
 
155
  if (*skip > bufsize)
 
156
    {
 
157
      *skip -= bufsize;
 
158
      return 1;
 
159
    }
 
160
  if (*skip)
 
161
    {
 
162
      buf += *skip;
 
163
      bufsize -= *skip;
 
164
      *skip = 0;
 
165
      if (bufsize == 0)
 
166
        return 1;
 
167
    }
 
168
 
 
169
  fwrite (buf, 1, bufsize, out);
 
170
  *written += bufsize;
 
171
 
 
172
  /* Immediately flush the downloaded data.  This should not hinder
 
173
     performance: fast downloads will arrive in large 16K chunks
 
174
     (which stdio would write out immediately anyway), and slow
 
175
     downloads wouldn't be limited by disk speed.  */
 
176
  fflush (out);
 
177
  return !ferror (out);
 
178
}
 
179
 
 
180
/* Read the contents of file descriptor FD until it the connection
 
181
   terminates or a read error occurs.  The data is read in portions of
 
182
   up to 16K and written to OUT as it arrives.  If opt.verbose is set,
 
183
   the progress is shown.
 
184
 
 
185
   TOREAD is the amount of data expected to arrive, normally only used
 
186
   by the progress gauge.
 
187
 
 
188
   STARTPOS is the position from which the download starts, used by
 
189
   the progress gauge.  If QTYREAD is non-NULL, the value it points to
 
190
   is incremented by the amount of data read from the network.  If
 
191
   QTYWRITTEN is non-NULL, the value it points to is incremented by
 
192
   the amount of data written to disk.  The time it took to download
 
193
   the data (in milliseconds) is stored to ELAPSED.
 
194
 
 
195
   The function exits and returns the amount of data read.  In case of
 
196
   error while reading data, -1 is returned.  In case of error while
 
197
   writing data, -2 is returned.  */
 
198
 
158
199
int
159
 
get_contents (int fd, FILE *fp, long *len, long restval, long expected,
160
 
              struct rbuf *rbuf, int use_expected, double *elapsed)
 
200
fd_read_body (int fd, FILE *out, wgint toread, wgint startpos,
 
201
              wgint *qtyread, wgint *qtywritten, double *elapsed, int flags)
161
202
{
162
 
  int res = 0;
 
203
  int ret = 0;
163
204
 
164
205
  static char dlbuf[16384];
165
206
  int dlbufsize = sizeof (dlbuf);
166
207
 
 
208
  struct ptimer *timer = NULL;
 
209
  double last_successful_read_tm = 0;
 
210
 
 
211
  /* The progress gauge, set according to the user preferences. */
167
212
  void *progress = NULL;
168
 
  struct wget_timer *timer = wtimer_allocate ();
169
 
  double dltime = 0;
170
 
 
171
 
  *len = restval;
 
213
 
 
214
  /* Non-zero if the progress gauge is interactive, i.e. if it can
 
215
     continually update the display.  When true, smaller timeout
 
216
     values are used so that the gauge can update the display when
 
217
     data arrives slowly. */
 
218
  int progress_interactive = 0;
 
219
 
 
220
  int exact = flags & rb_read_exactly;
 
221
  wgint skip = 0;
 
222
 
 
223
  /* How much data we've read/written.  */
 
224
  wgint sum_read = 0;
 
225
  wgint sum_written = 0;
 
226
 
 
227
  if (flags & rb_skip_startpos)
 
228
    skip = startpos;
172
229
 
173
230
  if (opt.verbose)
174
 
    progress = progress_create (restval, expected);
175
 
 
176
 
  if (rbuf && RBUF_FD (rbuf) == fd)
177
231
    {
178
 
      int sz = 0;
179
 
      while ((res = rbuf_flush (rbuf, dlbuf, sizeof (dlbuf))) != 0)
180
 
        {
181
 
          fwrite (dlbuf, 1, res, fp);
182
 
          *len += res;
183
 
          sz += res;
184
 
        }
185
 
      if (sz)
186
 
        fflush (fp);
187
 
      if (ferror (fp))
188
 
        {
189
 
          res = -2;
190
 
          goto out;
191
 
        }
192
 
      if (progress)
193
 
        progress_update (progress, sz, 0);
 
232
      /* If we're skipping STARTPOS bytes, pass 0 as the INITIAL
 
233
         argument to progress_create because the indicator doesn't
 
234
         (yet) know about "skipping" data.  */
 
235
      progress = progress_create (skip ? 0 : startpos, startpos + toread);
 
236
      progress_interactive = progress_interactive_p (progress);
194
237
    }
195
238
 
196
239
  if (opt.limit_rate)
197
240
    limit_bandwidth_reset ();
198
 
  wtimer_reset (timer);
 
241
 
 
242
  /* A timer is needed for tracking progress, for throttling, and for
 
243
     tracking elapsed time.  If either of these are requested, start
 
244
     the timer.  */
 
245
  if (progress || opt.limit_rate || elapsed)
 
246
    {
 
247
      timer = ptimer_new ();
 
248
      last_successful_read_tm = 0;
 
249
    }
199
250
 
200
251
  /* Use a smaller buffer for low requested bandwidths.  For example,
201
252
     with --limit-rate=2k, it doesn't make sense to slurp in 16K of
204
255
  if (opt.limit_rate && opt.limit_rate < dlbufsize)
205
256
    dlbufsize = opt.limit_rate;
206
257
 
207
 
  /* Read from fd while there is available data.
208
 
 
209
 
     Normally, if expected is 0, it means that it is not known how
210
 
     much data is expected.  However, if use_expected is specified,
211
 
     then expected being zero means exactly that.  */
212
 
  while (!use_expected || (*len < expected))
 
258
  /* Read from FD while there is data to read.  Normally toread==0
 
259
     means that it is unknown how much data is to arrive.  However, if
 
260
     EXACT is set, then toread==0 means what it says: that no data
 
261
     should be read.  */
 
262
  while (!exact || (sum_read < toread))
213
263
    {
214
 
      int amount_to_read = (use_expected
215
 
                            ? MIN (expected - *len, dlbufsize) : dlbufsize);
216
 
#ifdef HAVE_SSL
217
 
      if (rbuf->ssl!=NULL)
218
 
        res = ssl_iread (rbuf->ssl, dlbuf, amount_to_read);
219
 
      else
220
 
#endif /* HAVE_SSL */
221
 
        res = iread (fd, dlbuf, amount_to_read);
222
 
 
223
 
      if (res <= 0)
224
 
        break;
225
 
 
226
 
      fwrite (dlbuf, 1, res, fp);
227
 
      /* Always flush the contents of the network packet.  This should
228
 
         not hinder performance: fast downloads will be received in
229
 
         16K chunks (which stdio would write out anyway), and slow
230
 
         downloads won't be limited with disk performance.  */
231
 
      fflush (fp);
232
 
      if (ferror (fp))
233
 
        {
234
 
          res = -2;
235
 
          goto out;
236
 
        }
237
 
 
238
 
      dltime = wtimer_elapsed (timer);
 
264
      int rdsize = exact ? MIN (toread - sum_read, dlbufsize) : dlbufsize;
 
265
      double tmout = opt.read_timeout;
 
266
      if (progress_interactive)
 
267
        {
 
268
          /* For interactive progress gauges, always specify a ~1s
 
269
             timeout, so that the gauge can be updated regularly even
 
270
             when the data arrives very slowly or stalls.  */
 
271
          tmout = 0.95;
 
272
          if (opt.read_timeout)
 
273
            {
 
274
              double waittm;
 
275
              waittm = (ptimer_read (timer) - last_successful_read_tm) / 1000;
 
276
              if (waittm + tmout > opt.read_timeout)
 
277
                {
 
278
                  /* Don't let total idle time exceed read timeout. */
 
279
                  tmout = opt.read_timeout - waittm;
 
280
                  if (tmout < 0)
 
281
                    {
 
282
                      /* We've already exceeded the timeout. */
 
283
                      ret = -1, errno = ETIMEDOUT;
 
284
                      break;
 
285
                    }
 
286
                }
 
287
            }
 
288
        }
 
289
      ret = fd_read (fd, dlbuf, rdsize, tmout);
 
290
 
 
291
      if (progress_interactive && ret < 0 && errno == ETIMEDOUT)
 
292
        ret = 0;                /* interactive timeout, handled above */
 
293
      else if (ret <= 0)
 
294
        break;                  /* EOF or read error */
 
295
 
 
296
      if (progress || opt.limit_rate)
 
297
        {
 
298
          ptimer_measure (timer);
 
299
          if (ret > 0)
 
300
            last_successful_read_tm = ptimer_read (timer);
 
301
        }
 
302
 
 
303
      if (ret > 0)
 
304
        {
 
305
          sum_read += ret;
 
306
          if (!write_data (out, dlbuf, ret, &skip, &sum_written))
 
307
            {
 
308
              ret = -2;
 
309
              goto out_;
 
310
            }
 
311
        }
 
312
 
239
313
      if (opt.limit_rate)
240
 
        limit_bandwidth (res, &dltime, timer);
 
314
        limit_bandwidth (ret, timer);
241
315
 
242
 
      *len += res;
243
316
      if (progress)
244
 
        progress_update (progress, res, dltime);
 
317
        progress_update (progress, ret, ptimer_read (timer));
245
318
#ifdef WINDOWS
246
 
      if (use_expected && expected > 0)
247
 
        ws_percenttitle (100.0 * (double)(*len) / (double)expected);
 
319
      if (toread > 0 && !opt.quiet)
 
320
        ws_percenttitle (100.0 *
 
321
                         (startpos + sum_read) / (startpos + toread));
248
322
#endif
249
323
    }
250
 
  if (res < -1)
251
 
    res = -1;
 
324
  if (ret < -1)
 
325
    ret = -1;
252
326
 
253
 
 out:
 
327
 out_:
254
328
  if (progress)
255
 
    progress_finish (progress, dltime);
 
329
    progress_finish (progress, ptimer_read (timer));
 
330
 
256
331
  if (elapsed)
257
 
    *elapsed = dltime;
258
 
  wtimer_delete (timer);
259
 
 
260
 
  return res;
 
332
    *elapsed = ptimer_read (timer);
 
333
  if (timer)
 
334
    ptimer_destroy (timer);
 
335
 
 
336
  if (qtyread)
 
337
    *qtyread += sum_read;
 
338
  if (qtywritten)
 
339
    *qtywritten += sum_written;
 
340
 
 
341
  return ret;
 
342
}
 
343
 
 
344
/* Read a hunk of data from FD, up until a terminator.  The terminator
 
345
   is whatever the TERMINATOR function determines it to be; for
 
346
   example, it can be a line of data, or the head of an HTTP response.
 
347
   The function returns the data read allocated with malloc.
 
348
 
 
349
   In case of error, NULL is returned.  In case of EOF and no data
 
350
   read, NULL is returned and errno set to 0.  In case of EOF with
 
351
   data having been read, the data is returned, but it will
 
352
   (obviously) not contain the terminator.
 
353
 
 
354
   The idea is to be able to read a line of input, or otherwise a hunk
 
355
   of text, such as the head of an HTTP request, without crossing the
 
356
   boundary, so that the next call to fd_read etc. reads the data
 
357
   after the hunk.  To achieve that, this function does the following:
 
358
 
 
359
   1. Peek at available data.
 
360
 
 
361
   2. Determine whether the peeked data, along with the previously
 
362
      read data, includes the terminator.
 
363
 
 
364
      2a. If yes, read the data until the end of the terminator, and
 
365
          exit.
 
366
 
 
367
      2b. If no, read the peeked data and goto 1.
 
368
 
 
369
   The function is careful to assume as little as possible about the
 
370
   implementation of peeking.  For example, every peek is followed by
 
371
   a read.  If the read returns a different amount of data, the
 
372
   process is retried until all data arrives safely.
 
373
 
 
374
   SIZEHINT is the buffer size sufficient to hold all the data in the
 
375
   typical case (it is used as the initial buffer size).  MAXSIZE is
 
376
   the maximum amount of memory this function is allowed to allocate,
 
377
   or 0 if no upper limit is to be enforced.
 
378
 
 
379
   This function should be used as a building block for other
 
380
   functions -- see fd_read_line as a simple example.  */
 
381
 
 
382
char *
 
383
fd_read_hunk (int fd, hunk_terminator_t terminator, long sizehint, long maxsize)
 
384
{
 
385
  long bufsize = sizehint;
 
386
  char *hunk = xmalloc (bufsize);
 
387
  int tail = 0;                 /* tail position in HUNK */
 
388
 
 
389
  assert (maxsize >= bufsize);
 
390
 
 
391
  while (1)
 
392
    {
 
393
      const char *end;
 
394
      int pklen, rdlen, remain;
 
395
 
 
396
      /* First, peek at the available data. */
 
397
 
 
398
      pklen = fd_peek (fd, hunk + tail, bufsize - 1 - tail, -1.0);
 
399
      if (pklen < 0)
 
400
        {
 
401
          xfree (hunk);
 
402
          return NULL;
 
403
        }
 
404
      end = terminator (hunk, tail, pklen);
 
405
      if (end)
 
406
        {
 
407
          /* The data contains the terminator: we'll drain the data up
 
408
             to the end of the terminator.  */
 
409
          remain = end - (hunk + tail);
 
410
          if (remain == 0)
 
411
            {
 
412
              /* No more data needs to be read. */
 
413
              hunk[tail] = '\0';
 
414
              return hunk;
 
415
            }
 
416
          if (bufsize - 1 < tail + remain)
 
417
            {
 
418
              bufsize = tail + remain + 1;
 
419
              hunk = xrealloc (hunk, bufsize);
 
420
            }
 
421
        }
 
422
      else
 
423
        /* No terminator: simply read the data we know is (or should
 
424
           be) available.  */
 
425
        remain = pklen;
 
426
 
 
427
      /* Now, read the data.  Note that we make no assumptions about
 
428
         how much data we'll get.  (Some TCP stacks are notorious for
 
429
         read returning less data than the previous MSG_PEEK.)  */
 
430
 
 
431
      rdlen = fd_read (fd, hunk + tail, remain, 0.0);
 
432
      if (rdlen < 0)
 
433
        {
 
434
          xfree_null (hunk);
 
435
          return NULL;
 
436
        }
 
437
      tail += rdlen;
 
438
      hunk[tail] = '\0';
 
439
 
 
440
      if (rdlen == 0)
 
441
        {
 
442
          if (tail == 0)
 
443
            {
 
444
              /* EOF without anything having been read */
 
445
              xfree (hunk);
 
446
              errno = 0;
 
447
              return NULL;
 
448
            }
 
449
          else
 
450
            /* EOF seen: return the data we've read. */
 
451
            return hunk;
 
452
        }
 
453
      if (end && rdlen == remain)
 
454
        /* The terminator was seen and the remaining data drained --
 
455
           we got what we came for.  */
 
456
        return hunk;
 
457
 
 
458
      /* Keep looping until all the data arrives. */
 
459
 
 
460
      if (tail == bufsize - 1)
 
461
        {
 
462
          /* Double the buffer size, but refuse to allocate more than
 
463
             MAXSIZE bytes.  */
 
464
          if (maxsize && bufsize >= maxsize)
 
465
            {
 
466
              xfree (hunk);
 
467
              errno = ENOMEM;
 
468
              return NULL;
 
469
            }
 
470
          bufsize <<= 1;
 
471
          if (maxsize && bufsize > maxsize)
 
472
            bufsize = maxsize;
 
473
          hunk = xrealloc (hunk, bufsize);
 
474
        }
 
475
    }
 
476
}
 
477
 
 
478
static const char *
 
479
line_terminator (const char *hunk, int oldlen, int peeklen)
 
480
{
 
481
  const char *p = memchr (hunk + oldlen, '\n', peeklen);
 
482
  if (p)
 
483
    /* p+1 because we want the line to include '\n' */
 
484
    return p + 1;
 
485
  return NULL;
 
486
}
 
487
 
 
488
/* The maximum size of the single line we agree to accept.  This is
 
489
   not meant to impose an arbitrary limit, but to protect the user
 
490
   from Wget slurping up available memory upon encountering malicious
 
491
   or buggy server output.  Define it to 0 to remove the limit.  */
 
492
#define FD_READ_LINE_MAX 4096
 
493
 
 
494
/* Read one line from FD and return it.  The line is allocated using
 
495
   malloc, but is never larger than FD_READ_LINE_MAX.
 
496
 
 
497
   If an error occurs, or if no data can be read, NULL is returned.
 
498
   In the former case errno indicates the error condition, and in the
 
499
   latter case, errno is NULL.  */
 
500
 
 
501
char *
 
502
fd_read_line (int fd)
 
503
{
 
504
  return fd_read_hunk (fd, line_terminator, 128, FD_READ_LINE_MAX);
261
505
}
262
506
 
263
507
/* Return a printed representation of the download rate, as
264
508
   appropriate for the speed.  If PAD is non-zero, strings will be
265
509
   padded to the width of 7 characters (xxxx.xx).  */
266
510
char *
267
 
retr_rate (long bytes, double msecs, int pad)
 
511
retr_rate (wgint bytes, double msecs, int pad)
268
512
{
269
513
  static char res[20];
270
 
  static char *rate_names[] = {"B/s", "KB/s", "MB/s", "GB/s" };
 
514
  static const char *rate_names[] = {"B/s", "KB/s", "MB/s", "GB/s" };
271
515
  int units = 0;
272
516
 
273
517
  double dlrate = calc_rate (bytes, msecs, &units);
284
528
   UNITS is zero for B/s, one for KB/s, two for MB/s, and three for
285
529
   GB/s.  */
286
530
double
287
 
calc_rate (long bytes, double msecs, int *units)
 
531
calc_rate (wgint bytes, double msecs, int *units)
288
532
{
289
533
  double dlrate;
290
534
 
293
537
 
294
538
  if (msecs == 0)
295
539
    /* If elapsed time is exactly zero, it means we're under the
296
 
       granularity of the timer.  This often happens on systems that
297
 
       use time() for the timer.  */
298
 
    msecs = wtimer_granularity ();
 
540
       resolution of the timer.  This can easily happen on systems
 
541
       that use time() for the timer.  Since the interval lies between
 
542
       0 and the timer's resolution, assume half the resolution.  */
 
543
    msecs = ptimer_resolution () / 2.0;
299
544
 
300
 
  dlrate = (double)1000 * bytes / msecs;
 
545
  dlrate = 1000.0 * bytes / msecs;
301
546
  if (dlrate < 1024.0)
302
547
    *units = 0;
303
548
  else if (dlrate < 1024.0 * 1024.0)
424
669
  else if (u->scheme == SCHEME_FTP)
425
670
    {
426
671
      /* If this is a redirection, we must not allow recursive FTP
427
 
         retrieval, so we save recursion to oldrec, and restore it
428
 
         later.  */
 
672
         retrieval, so we save recursion to oldrec, and restore it
 
673
         later.  */
429
674
      int oldrec = opt.recursive;
430
675
      if (redirection_count)
431
 
        opt.recursive = 0;
 
676
        opt.recursive = 0;
432
677
      result = ftp_loop (u, dt, proxy_url);
433
678
      opt.recursive = oldrec;
434
679
 
472
717
      newloc_parsed = url_parse (mynewloc, &up_error_code);
473
718
      if (!newloc_parsed)
474
719
        {
475
 
          logprintf (LOG_NOTQUIET, "%s: %s.\n", mynewloc,
 
720
          logprintf (LOG_NOTQUIET, "%s: %s.\n", escnonprint_uri (mynewloc),
476
721
                     url_error (up_error_code));
477
722
          url_free (u);
478
723
          xfree (url);
531
776
  if (file)
532
777
    *file = local_file ? local_file : NULL;
533
778
  else
534
 
    FREE_MAYBE (local_file);
 
779
    xfree_null (local_file);
535
780
 
536
781
  url_free (u);
537
782
 
549
794
      xfree (url);
550
795
    }
551
796
 
552
 
  ++global_download_count;
553
797
  RESTORE_POST_DATA;
554
798
 
555
799
  return result;
559
803
   them.  If HTML is non-zero, treat the file as HTML, and construct
560
804
   the URLs accordingly.
561
805
 
562
 
   If opt.recursive is set, call recursive_retrieve() for each file.  */
 
806
   If opt.recursive is set, call retrieve_tree() for each file.  */
 
807
 
563
808
uerr_t
564
809
retrieve_from_file (const char *file, int html, int *count)
565
810
{
584
829
          status = QUOTEXC;
585
830
          break;
586
831
        }
587
 
      if (opt.recursive && cur_url->url->scheme != SCHEME_FTP)
 
832
      if ((opt.recursive || opt.page_requisites)
 
833
          && cur_url->url->scheme != SCHEME_FTP)
588
834
        status = retrieve_tree (cur_url->url->url);
589
835
      else
590
836
        status = retrieve_url (cur_url->url->url, &filename, &new_file, NULL, &dt);
591
837
 
592
838
      if (filename && opt.delete_after && file_exists_p (filename))
593
839
        {
594
 
          DEBUGP (("Removing file due to --delete-after in"
595
 
                   " retrieve_from_file():\n"));
 
840
          DEBUGP (("\
 
841
Removing file due to --delete-after in retrieve_from_file():\n"));
596
842
          logprintf (LOG_VERBOSE, _("Removing %s.\n"), filename);
597
843
          if (unlink (filename))
598
844
            logprintf (LOG_NOTQUIET, "unlink: %s\n", strerror (errno));
599
845
          dt &= ~RETROKF;
600
846
        }
601
847
 
602
 
      FREE_MAYBE (new_file);
603
 
      FREE_MAYBE (filename);
 
848
      xfree_null (new_file);
 
849
      xfree_null (filename);
604
850
    }
605
851
 
606
852
  /* Free the linked list of URL-s.  */
640
886
      /* If opt.waitretry is specified and this is a retry, wait for
641
887
         COUNT-1 number of seconds, or for opt.waitretry seconds.  */
642
888
      if (count <= opt.waitretry)
643
 
        sleep (count - 1);
 
889
        xsleep (count - 1.0);
644
890
      else
645
 
        usleep (1000000L * opt.waitretry);
 
891
        xsleep (opt.waitretry);
646
892
    }
647
893
  else if (opt.wait)
648
894
    {
650
896
        /* If random-wait is not specified, or if we are sleeping
651
897
           between retries of the same download, sleep the fixed
652
898
           interval.  */
653
 
        usleep (1000000L * opt.wait);
 
899
        xsleep (opt.wait);
654
900
      else
655
901
        {
656
902
          /* Sleep a random amount of time averaging in opt.wait
659
905
          double waitsecs = 2 * opt.wait * random_float ();
660
906
          DEBUGP (("sleep_between_retrievals: avg=%f,sleep=%f\n",
661
907
                   opt.wait, waitsecs));
662
 
          usleep (1000000L * waitsecs);
 
908
          xsleep (waitsecs);
663
909
        }
664
910
    }
665
911
}
673
919
      struct urlpos *next = l->next;
674
920
      if (l->url)
675
921
        url_free (l->url);
676
 
      FREE_MAYBE (l->local_name);
 
922
      xfree_null (l->local_name);
677
923
      xfree (l);
678
924
      l = next;
679
925
    }
686
932
  int maxlen = strlen (fname) + 1 + numdigit (opt.backups) + 1;
687
933
  char *from = (char *)alloca (maxlen);
688
934
  char *to = (char *)alloca (maxlen);
689
 
  struct stat sb;
 
935
  struct_stat sb;
690
936
  int i;
691
937
 
692
938
  if (stat (fname, &sb) == 0)
744
990
  rewritten_url = rewrite_shorthand_url (proxy);
745
991
  if (rewritten_url)
746
992
    {
747
 
      strncpy (rewritten_storage, rewritten_url, sizeof(rewritten_storage));
 
993
      strncpy (rewritten_storage, rewritten_url, sizeof (rewritten_storage));
748
994
      rewritten_storage[sizeof (rewritten_storage) - 1] = '\0';
749
995
      proxy = rewritten_storage;
750
996
    }
753
999
}
754
1000
 
755
1001
/* Should a host be accessed through proxy, concerning no_proxy?  */
756
 
int
 
1002
static int
757
1003
no_proxy_match (const char *host, const char **no_proxy)
758
1004
{
759
1005
  if (!no_proxy)