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
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));
117
usleep ((unsigned long) (1000 * slp));
118
t1 = wtimer_elapsed (timer);
120
t0 = ptimer_read (timer);
122
t1 = ptimer_measure (timer);
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
124
128
limit_data.sleep_adjust = slp - (t1 - t0);
126
/* Since we've called wtimer_elapsed, we might as well update
127
the caller's dltime. */
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;
131
137
limit_data.chunk_bytes = 0;
132
limit_data.chunk_start = *dltime;
135
#define MIN(i, j) ((i) <= (j) ? (i) : (j))
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!
143
The EXPECTED argument is passed to show_progress() unchanged, but
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.
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.
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);
142
# define MIN(i, j) ((i) <= (j) ? (i) : (j))
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
150
write_data (FILE *out, const char *buf, int bufsize, wgint *skip,
169
fwrite (buf, 1, bufsize, out);
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. */
177
return !ferror (out);
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.
185
TOREAD is the amount of data expected to arrive, normally only used
186
by the progress gauge.
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.
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. */
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)
164
205
static char dlbuf[16384];
165
206
int dlbufsize = sizeof (dlbuf);
208
struct ptimer *timer = NULL;
209
double last_successful_read_tm = 0;
211
/* The progress gauge, set according to the user preferences. */
167
212
void *progress = NULL;
168
struct wget_timer *timer = wtimer_allocate ();
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;
220
int exact = flags & rb_read_exactly;
223
/* How much data we've read/written. */
225
wgint sum_written = 0;
227
if (flags & rb_skip_startpos)
174
progress = progress_create (restval, expected);
176
if (rbuf && RBUF_FD (rbuf) == fd)
179
while ((res = rbuf_flush (rbuf, dlbuf, sizeof (dlbuf))) != 0)
181
fwrite (dlbuf, 1, res, fp);
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);
196
239
if (opt.limit_rate)
197
240
limit_bandwidth_reset ();
198
wtimer_reset (timer);
242
/* A timer is needed for tracking progress, for throttling, and for
243
tracking elapsed time. If either of these are requested, start
245
if (progress || opt.limit_rate || elapsed)
247
timer = ptimer_new ();
248
last_successful_read_tm = 0;
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;
207
/* Read from fd while there is available data.
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
262
while (!exact || (sum_read < toread))
214
int amount_to_read = (use_expected
215
? MIN (expected - *len, dlbufsize) : dlbufsize);
218
res = ssl_iread (rbuf->ssl, dlbuf, amount_to_read);
220
#endif /* HAVE_SSL */
221
res = iread (fd, dlbuf, amount_to_read);
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. */
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)
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. */
272
if (opt.read_timeout)
275
waittm = (ptimer_read (timer) - last_successful_read_tm) / 1000;
276
if (waittm + tmout > opt.read_timeout)
278
/* Don't let total idle time exceed read timeout. */
279
tmout = opt.read_timeout - waittm;
282
/* We've already exceeded the timeout. */
283
ret = -1, errno = ETIMEDOUT;
289
ret = fd_read (fd, dlbuf, rdsize, tmout);
291
if (progress_interactive && ret < 0 && errno == ETIMEDOUT)
292
ret = 0; /* interactive timeout, handled above */
294
break; /* EOF or read error */
296
if (progress || opt.limit_rate)
298
ptimer_measure (timer);
300
last_successful_read_tm = ptimer_read (timer);
306
if (!write_data (out, dlbuf, ret, &skip, &sum_written))
239
313
if (opt.limit_rate)
240
limit_bandwidth (res, &dltime, timer);
314
limit_bandwidth (ret, timer);
244
progress_update (progress, res, dltime);
317
progress_update (progress, ret, ptimer_read (timer));
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));
255
progress_finish (progress, dltime);
329
progress_finish (progress, ptimer_read (timer));
258
wtimer_delete (timer);
332
*elapsed = ptimer_read (timer);
334
ptimer_destroy (timer);
337
*qtyread += sum_read;
339
*qtywritten += sum_written;
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.
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.
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:
359
1. Peek at available data.
361
2. Determine whether the peeked data, along with the previously
362
read data, includes the terminator.
364
2a. If yes, read the data until the end of the terminator, and
367
2b. If no, read the peeked data and goto 1.
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.
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.
379
This function should be used as a building block for other
380
functions -- see fd_read_line as a simple example. */
383
fd_read_hunk (int fd, hunk_terminator_t terminator, long sizehint, long maxsize)
385
long bufsize = sizehint;
386
char *hunk = xmalloc (bufsize);
387
int tail = 0; /* tail position in HUNK */
389
assert (maxsize >= bufsize);
394
int pklen, rdlen, remain;
396
/* First, peek at the available data. */
398
pklen = fd_peek (fd, hunk + tail, bufsize - 1 - tail, -1.0);
404
end = terminator (hunk, tail, pklen);
407
/* The data contains the terminator: we'll drain the data up
408
to the end of the terminator. */
409
remain = end - (hunk + tail);
412
/* No more data needs to be read. */
416
if (bufsize - 1 < tail + remain)
418
bufsize = tail + remain + 1;
419
hunk = xrealloc (hunk, bufsize);
423
/* No terminator: simply read the data we know is (or should
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.) */
431
rdlen = fd_read (fd, hunk + tail, remain, 0.0);
444
/* EOF without anything having been read */
450
/* EOF seen: return the data we've read. */
453
if (end && rdlen == remain)
454
/* The terminator was seen and the remaining data drained --
455
we got what we came for. */
458
/* Keep looping until all the data arrives. */
460
if (tail == bufsize - 1)
462
/* Double the buffer size, but refuse to allocate more than
464
if (maxsize && bufsize >= maxsize)
471
if (maxsize && bufsize > maxsize)
473
hunk = xrealloc (hunk, bufsize);
479
line_terminator (const char *hunk, int oldlen, int peeklen)
481
const char *p = memchr (hunk + oldlen, '\n', peeklen);
483
/* p+1 because we want the line to include '\n' */
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
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.
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. */
502
fd_read_line (int fd)
504
return fd_read_hunk (fd, line_terminator, 128, FD_READ_LINE_MAX);
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). */
267
retr_rate (long bytes, double msecs, int pad)
511
retr_rate (wgint bytes, double msecs, int pad)
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" };
273
517
double dlrate = calc_rate (bytes, msecs, &units);