blob: f59faa3d3fe68456c1ef00f5bc7b52070ca96868 [file] [log] [blame]
Kristian Monsen5ab50182010-05-14 18:53:44 +01001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Elliott Hughes0128fe42018-02-27 14:57:55 -08008 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
Kristian Monsen5ab50182010-05-14 18:53:44 +01009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymod15eaac2016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Kristian Monsen5ab50182010-05-14 18:53:44 +010013 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070023#include "curl_setup.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010024
25#include "urldata.h"
26#include "sendf.h"
Elliott Hughescac39802018-04-27 16:19:43 -070027#include "multiif.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010028#include "progress.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070029#include "curl_printf.h"
Kristian Monsen5ab50182010-05-14 18:53:44 +010030
Elliott Hughes1ef06ba2018-05-30 15:43:58 -070031/* check rate limits within this many recent milliseconds, at minimum. */
32#define MIN_RATE_LIMIT_PERIOD 3000
33
Kristian Monsen5ab50182010-05-14 18:53:44 +010034/* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
35 byte) */
36static void time2str(char *r, curl_off_t seconds)
37{
38 curl_off_t d, h, m, s;
39 if(seconds <= 0) {
40 strcpy(r, "--:--:--");
41 return;
42 }
43 h = seconds / CURL_OFF_T_C(3600);
44 if(h <= CURL_OFF_T_C(99)) {
45 m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
46 s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070047 snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
48 ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
Kristian Monsen5ab50182010-05-14 18:53:44 +010049 }
50 else {
51 /* this equals to more than 99 hours, switch to a more suitable output
52 format to fit within the limits. */
53 d = seconds / CURL_OFF_T_C(86400);
54 h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
55 if(d <= CURL_OFF_T_C(999))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070056 snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
57 "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
Kristian Monsen5ab50182010-05-14 18:53:44 +010058 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070059 snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
Kristian Monsen5ab50182010-05-14 18:53:44 +010060 }
61}
62
63/* The point of this function would be to return a string of the input data,
64 but never longer than 5 columns (+ one zero byte).
65 Add suffix k, M, G when suitable... */
66static char *max5data(curl_off_t bytes, char *max5)
67{
68#define ONE_KILOBYTE CURL_OFF_T_C(1024)
69#define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
70#define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
71#define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
72#define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
73
74 if(bytes < CURL_OFF_T_C(100000))
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070075 snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
Kristian Monsen5ab50182010-05-14 18:53:44 +010076
77 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070078 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +010079
80 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
81 /* 'XX.XM' is good as long as we're less than 100 megs */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070082 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
83 CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
Kristian Monsen5ab50182010-05-14 18:53:44 +010084 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
85
86#if (CURL_SIZEOF_CURL_OFF_T > 4)
87
88 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
89 /* 'XXXXM' is good until we're at 10000MB or above */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070090 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +010091
92 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
93 /* 10000 MB - 100 GB, we show it as XX.XG */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070094 snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
95 CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
Kristian Monsen5ab50182010-05-14 18:53:44 +010096 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
97
98 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
99 /* up to 10000GB, display without decimal: XXXXG */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700100 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100101
102 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
103 /* up to 10000TB, display without decimal: XXXXT */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700104 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100105
106 else
107 /* up to 10000PB, display without decimal: XXXXP */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700108 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100109
110 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
111 can hold, but our data type is signed so 8192PB will be the maximum. */
112
113#else
114
115 else
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700116 snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100117
118#endif
119
120 return max5;
121}
122
123/*
124
125 New proposed interface, 9th of February 2000:
126
127 pgrsStartNow() - sets start time
128 pgrsSetDownloadSize(x) - known expected download size
129 pgrsSetUploadSize(x) - known expected upload size
130 pgrsSetDownloadCounter() - amount of data currently downloaded
131 pgrsSetUploadCounter() - amount of data currently uploaded
132 pgrsUpdate() - show progress
133 pgrsDone() - transfer complete
134
135*/
136
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700137int Curl_pgrsDone(struct connectdata *conn)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100138{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700139 int rc;
Alex Deymoe3149cc2016-10-05 11:18:42 -0700140 struct Curl_easy *data = conn->data;
Alex Deymo486467e2017-12-19 19:04:07 +0100141 data->progress.lastshow = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700142 rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
143 if(rc)
144 return rc;
145
146 if(!(data->progress.flags & PGRS_HIDE) &&
147 !data->progress.callback)
148 /* only output if we don't use a progress callback and we're not
149 * hidden */
150 fprintf(data->set.err, "\n");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100151
152 data->progress.speeder_c = 0; /* reset the progress meter display */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700153 return 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100154}
155
Alex Deymo486467e2017-12-19 19:04:07 +0100156/* reset the known transfer sizes */
157void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100158{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700159 Curl_pgrsSetDownloadSize(data, -1);
160 Curl_pgrsSetUploadSize(data, -1);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100161}
162
Elliott Hughes82be86d2017-09-20 17:00:17 -0700163/*
164 * @unittest: 1399
165 */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700166void Curl_pgrsTime(struct Curl_easy *data, timerid timer)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100167{
Alex Deymo486467e2017-12-19 19:04:07 +0100168 struct curltime now = Curl_now();
Elliott Hughes82be86d2017-09-20 17:00:17 -0700169 time_t *delta = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700170
Kristian Monsen5ab50182010-05-14 18:53:44 +0100171 switch(timer) {
172 default:
173 case TIMER_NONE:
174 /* mistake filter */
175 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700176 case TIMER_STARTOP:
177 /* This is set at the start of a transfer */
178 data->progress.t_startop = now;
179 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100180 case TIMER_STARTSINGLE:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700181 /* This is set at the start of each single fetch */
182 data->progress.t_startsingle = now;
Alex Deymo486467e2017-12-19 19:04:07 +0100183 data->progress.is_t_startransfer_set = false;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700184 break;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700185 case TIMER_STARTACCEPT:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700186 data->progress.t_acceptdata = now;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100187 break;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100188 case TIMER_NAMELOOKUP:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700189 delta = &data->progress.t_nslookup;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100190 break;
191 case TIMER_CONNECT:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700192 delta = &data->progress.t_connect;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100193 break;
194 case TIMER_APPCONNECT:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700195 delta = &data->progress.t_appconnect;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100196 break;
197 case TIMER_PRETRANSFER:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700198 delta = &data->progress.t_pretransfer;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100199 break;
200 case TIMER_STARTTRANSFER:
Elliott Hughes82be86d2017-09-20 17:00:17 -0700201 delta = &data->progress.t_starttransfer;
202 /* prevent updating t_starttransfer unless:
203 * 1) this is the first time we're setting t_starttransfer
204 * 2) a redirect has occurred since the last time t_starttransfer was set
205 * This prevents repeated invocations of the function from incorrectly
206 * changing the t_starttransfer time.
207 */
Alex Deymo486467e2017-12-19 19:04:07 +0100208 if(data->progress.is_t_startransfer_set) {
Elliott Hughes82be86d2017-09-20 17:00:17 -0700209 return;
210 }
211 else {
Alex Deymo486467e2017-12-19 19:04:07 +0100212 data->progress.is_t_startransfer_set = true;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700213 break;
214 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100215 case TIMER_POSTRANSFER:
216 /* this is the normal end-of-transfer thing */
217 break;
218 case TIMER_REDIRECT:
Alex Deymo486467e2017-12-19 19:04:07 +0100219 data->progress.t_redirect = Curl_timediff_us(now, data->progress.start);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100220 break;
221 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700222 if(delta) {
Alex Deymo486467e2017-12-19 19:04:07 +0100223 timediff_t us = Curl_timediff_us(now, data->progress.t_startsingle);
224 if(us < 1)
225 us = 1; /* make sure at least one microsecond passed */
226 *delta += us;
Elliott Hughes82be86d2017-09-20 17:00:17 -0700227 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100228}
229
Alex Deymoe3149cc2016-10-05 11:18:42 -0700230void Curl_pgrsStartNow(struct Curl_easy *data)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100231{
232 data->progress.speeder_c = 0; /* reset the progress meter display */
Alex Deymo486467e2017-12-19 19:04:07 +0100233 data->progress.start = Curl_now();
234 data->progress.is_t_startransfer_set = false;
Elliott Hughescee03382017-06-23 12:17:18 -0700235 data->progress.ul_limit_start.tv_sec = 0;
236 data->progress.ul_limit_start.tv_usec = 0;
237 data->progress.dl_limit_start.tv_sec = 0;
238 data->progress.dl_limit_start.tv_usec = 0;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700239 /* clear all bits except HIDE and HEADERS_OUT */
240 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700241 Curl_ratelimit(data, data->progress.start);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100242}
243
Elliott Hughescee03382017-06-23 12:17:18 -0700244/*
Elliott Hughescac39802018-04-27 16:19:43 -0700245 * This is used to handle speed limits, calculating how many milliseconds to
246 * wait until we're back under the speed limit, if needed.
Elliott Hughescee03382017-06-23 12:17:18 -0700247 *
248 * The way it works is by having a "starting point" (time & amount of data
Elliott Hughes82be86d2017-09-20 17:00:17 -0700249 * transferred by then) used in the speed computation, to be used instead of
250 * the start of the transfer. This starting point is regularly moved as
251 * transfer goes on, to keep getting accurate values (instead of average over
252 * the entire transfer).
Elliott Hughescee03382017-06-23 12:17:18 -0700253 *
Elliott Hughes82be86d2017-09-20 17:00:17 -0700254 * This function takes the current amount of data transferred, the amount at
255 * the starting point, the limit (in bytes/s), the time of the starting point
256 * and the current time.
Elliott Hughescee03382017-06-23 12:17:18 -0700257 *
Elliott Hughescac39802018-04-27 16:19:43 -0700258 * Returns 0 if no waiting is needed or when no waiting is needed but the
259 * starting point should be reset (to current); or the number of milliseconds
260 * to wait to get back under the speed limit.
Elliott Hughescee03382017-06-23 12:17:18 -0700261 */
Elliott Hughescac39802018-04-27 16:19:43 -0700262timediff_t Curl_pgrsLimitWaitTime(curl_off_t cursize,
263 curl_off_t startsize,
264 curl_off_t limit,
265 struct curltime start,
266 struct curltime now)
Elliott Hughescee03382017-06-23 12:17:18 -0700267{
268 curl_off_t size = cursize - startsize;
269 time_t minimum;
270 time_t actual;
271
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700272 if(!limit || !size)
Elliott Hughescee03382017-06-23 12:17:18 -0700273 return 0;
274
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700275 /*
276 * 'minimum' is the number of milliseconds 'size' should take to download to
277 * stay below 'limit'.
278 */
Elliott Hughescac39802018-04-27 16:19:43 -0700279 if(size < CURL_OFF_T_MAX/1000)
280 minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit);
281 else {
282 minimum = (time_t) (size / limit);
283 if(minimum < TIME_T_MAX/1000)
284 minimum *= 1000;
285 else
286 minimum = TIME_T_MAX;
287 }
288
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700289 /*
290 * 'actual' is the time in milliseconds it took to actually download the
291 * last 'size' bytes.
292 */
Alex Deymo486467e2017-12-19 19:04:07 +0100293 actual = Curl_timediff(now, start);
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700294 if(actual < minimum) {
295 /* if it downloaded the data faster than the limit, make it wait the
296 difference */
Elliott Hughescac39802018-04-27 16:19:43 -0700297 return (minimum - actual);
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700298 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700299
300 return 0;
Elliott Hughescee03382017-06-23 12:17:18 -0700301}
302
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700303/*
304 * Set the number of downloaded bytes so far.
305 */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700306void Curl_pgrsSetDownloadCounter(struct Curl_easy *data, curl_off_t size)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100307{
308 data->progress.downloaded = size;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700309}
Elliott Hughescee03382017-06-23 12:17:18 -0700310
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700311/*
312 * Update the timestamp and sizestamp to use for rate limit calculations.
313 */
314void Curl_ratelimit(struct Curl_easy *data, struct curltime now)
315{
316 /* don't set a new stamp unless the time since last update is long enough */
317 if(data->set.max_recv_speed > 0) {
318 if(Curl_timediff(now, data->progress.dl_limit_start) >=
319 MIN_RATE_LIMIT_PERIOD) {
320 data->progress.dl_limit_start = now;
321 data->progress.dl_limit_size = data->progress.downloaded;
322 }
323 }
324 if(data->set.max_send_speed > 0) {
325 if(Curl_timediff(now, data->progress.ul_limit_start) >=
326 MIN_RATE_LIMIT_PERIOD) {
327 data->progress.ul_limit_start = now;
328 data->progress.ul_limit_size = data->progress.uploaded;
329 }
Elliott Hughescee03382017-06-23 12:17:18 -0700330 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100331}
332
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700333/*
334 * Set the number of uploaded bytes so far.
335 */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700336void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100337{
338 data->progress.uploaded = size;
339}
340
Alex Deymoe3149cc2016-10-05 11:18:42 -0700341void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100342{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700343 if(size >= 0) {
344 data->progress.size_dl = size;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100345 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700346 }
347 else {
348 data->progress.size_dl = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100349 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700350 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100351}
352
Alex Deymoe3149cc2016-10-05 11:18:42 -0700353void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size)
Kristian Monsen5ab50182010-05-14 18:53:44 +0100354{
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700355 if(size >= 0) {
356 data->progress.size_ul = size;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100357 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700358 }
359 else {
360 data->progress.size_ul = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100361 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700362 }
Kristian Monsen5ab50182010-05-14 18:53:44 +0100363}
364
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700365/*
366 * Curl_pgrsUpdate() returns 0 for success or the value returned by the
367 * progress callback!
368 */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100369int Curl_pgrsUpdate(struct connectdata *conn)
370{
Elliott Hughes82be86d2017-09-20 17:00:17 -0700371 struct curltime now;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100372 int result;
373 char max5[6][10];
Alex Deymo486467e2017-12-19 19:04:07 +0100374 curl_off_t dlpercen = 0;
375 curl_off_t ulpercen = 0;
376 curl_off_t total_percen = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100377 curl_off_t total_transfer;
378 curl_off_t total_expected_transfer;
379 curl_off_t timespent;
Elliott Hughes0128fe42018-02-27 14:57:55 -0800380 curl_off_t timespent_ms; /* milliseconds */
Alex Deymoe3149cc2016-10-05 11:18:42 -0700381 struct Curl_easy *data = conn->data;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100382 int nowindex = data->progress.speeder_c% CURR_TIME;
383 int checkindex;
384 int countindex; /* amount of seconds stored in the speeder array */
385 char time_left[10];
386 char time_total[10];
387 char time_spent[10];
Alex Deymo486467e2017-12-19 19:04:07 +0100388 curl_off_t ulestimate = 0;
389 curl_off_t dlestimate = 0;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100390 curl_off_t total_estimate;
Alex Deymo486467e2017-12-19 19:04:07 +0100391 bool shownow = FALSE;
Elliott Hughes0128fe42018-02-27 14:57:55 -0800392 curl_off_t dl = data->progress.downloaded;
393 curl_off_t ul = data->progress.uploaded;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100394
Alex Deymo486467e2017-12-19 19:04:07 +0100395 now = Curl_now(); /* what time is it */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100396
397 /* The time spent so far (from the start) */
Alex Deymo486467e2017-12-19 19:04:07 +0100398 data->progress.timespent = Curl_timediff_us(now, data->progress.start);
Elliott Hughes82be86d2017-09-20 17:00:17 -0700399 timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800400 timespent_ms = (curl_off_t)data->progress.timespent/1000; /* ms */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100401
402 /* The average download speed this far */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800403 if(dl < CURL_OFF_T_MAX/1000)
404 data->progress.dlspeed = (dl * 1000 / (timespent_ms>0?timespent_ms:1));
405 else
406 data->progress.dlspeed = (dl / (timespent>0?timespent:1));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100407
408 /* The average upload speed this far */
Elliott Hughes0128fe42018-02-27 14:57:55 -0800409 if(ul < CURL_OFF_T_MAX/1000)
410 data->progress.ulspeed = (ul * 1000 / (timespent_ms>0?timespent_ms:1));
411 else
412 data->progress.ulspeed = (ul / (timespent>0?timespent:1));
Kristian Monsen5ab50182010-05-14 18:53:44 +0100413
414 /* Calculations done at most once a second, unless end is reached */
Elliott Hughescee03382017-06-23 12:17:18 -0700415 if(data->progress.lastshow != now.tv_sec) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100416 shownow = TRUE;
417
418 data->progress.lastshow = now.tv_sec;
419
Elliott Hughes82be86d2017-09-20 17:00:17 -0700420 /* Let's do the "current speed" thing, with the dl + ul speeds
421 combined. Store the speed at entry 'nowindex'. */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100422 data->progress.speeder[ nowindex ] =
Elliott Hughes82be86d2017-09-20 17:00:17 -0700423 data->progress.downloaded + data->progress.uploaded;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100424
425 /* remember the exact time for this moment */
426 data->progress.speeder_time [ nowindex ] = now;
427
428 /* advance our speeder_c counter, which is increased every time we get
429 here and we expect it to never wrap as 2^32 is a lot of seconds! */
430 data->progress.speeder_c++;
431
432 /* figure out how many index entries of data we have stored in our speeder
433 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
434 transfer. Imagine, after one second we have filled in two entries,
435 after two seconds we've filled in three entries etc. */
Alex Deymo486467e2017-12-19 19:04:07 +0100436 countindex = ((data->progress.speeder_c >= CURR_TIME)?
Kristian Monsen5ab50182010-05-14 18:53:44 +0100437 CURR_TIME:data->progress.speeder_c) - 1;
438
439 /* first of all, we don't do this if there's no counted seconds yet */
440 if(countindex) {
Alex Deymo486467e2017-12-19 19:04:07 +0100441 timediff_t span_ms;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100442
443 /* Get the index position to compare with the 'nowindex' position.
444 Get the oldest entry possible. While we have less than CURR_TIME
445 entries, the first entry will remain the oldest. */
Alex Deymo486467e2017-12-19 19:04:07 +0100446 checkindex = (data->progress.speeder_c >= CURR_TIME)?
Kristian Monsen5ab50182010-05-14 18:53:44 +0100447 data->progress.speeder_c%CURR_TIME:0;
448
449 /* Figure out the exact time for the time span */
Alex Deymo486467e2017-12-19 19:04:07 +0100450 span_ms = Curl_timediff(now,
451 data->progress.speeder_time[checkindex]);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100452 if(0 == span_ms)
Alex Deymo486467e2017-12-19 19:04:07 +0100453 span_ms = 1; /* at least one millisecond MUST have passed */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100454
455 /* Calculate the average speed the last 'span_ms' milliseconds */
456 {
457 curl_off_t amount = data->progress.speeder[nowindex]-
458 data->progress.speeder[checkindex];
459
460 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
461 /* the 'amount' value is bigger than would fit in 32 bits if
462 multiplied with 1000, so we use the double math for this */
463 data->progress.current_speed = (curl_off_t)
464 ((double)amount/((double)span_ms/1000.0));
465 else
466 /* the 'amount' value is small enough to fit within 32 bits even
467 when multiplied with 1000 */
468 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
469 }
470 }
471 else
Elliott Hughes82be86d2017-09-20 17:00:17 -0700472 /* the first second we use the average */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100473 data->progress.current_speed =
Elliott Hughes82be86d2017-09-20 17:00:17 -0700474 data->progress.ulspeed + data->progress.dlspeed;
Kristian Monsen5ab50182010-05-14 18:53:44 +0100475
476 } /* Calculations end */
477
478 if(!(data->progress.flags & PGRS_HIDE)) {
Kristian Monsen5ab50182010-05-14 18:53:44 +0100479 /* progress meter has not been shut off */
480
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700481 if(data->set.fxferinfo) {
482 /* There's a callback set, call that */
Elliott Hughescac39802018-04-27 16:19:43 -0700483 Curl_set_in_callback(data, true);
Alex Deymo486467e2017-12-19 19:04:07 +0100484 result = data->set.fxferinfo(data->set.progress_client,
485 data->progress.size_dl,
486 data->progress.downloaded,
487 data->progress.size_ul,
488 data->progress.uploaded);
Elliott Hughescac39802018-04-27 16:19:43 -0700489 Curl_set_in_callback(data, false);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700490 if(result)
491 failf(data, "Callback aborted");
492 return result;
493 }
Elliott Hughes82be86d2017-09-20 17:00:17 -0700494 if(data->set.fprogress) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700495 /* The older deprecated callback is set, call that */
Elliott Hughescac39802018-04-27 16:19:43 -0700496 Curl_set_in_callback(data, true);
Alex Deymo486467e2017-12-19 19:04:07 +0100497 result = data->set.fprogress(data->set.progress_client,
498 (double)data->progress.size_dl,
499 (double)data->progress.downloaded,
500 (double)data->progress.size_ul,
501 (double)data->progress.uploaded);
Elliott Hughescac39802018-04-27 16:19:43 -0700502 Curl_set_in_callback(data, false);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100503 if(result)
504 failf(data, "Callback aborted");
505 return result;
506 }
507
508 if(!shownow)
509 /* only show the internal progress meter once per second */
510 return 0;
511
512 /* If there's no external callback set, use internal code to show
513 progress */
514
515 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
516 if(data->state.resume_from) {
517 fprintf(data->set.err,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700518 "** Resuming transfer from byte position %"
519 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
Kristian Monsen5ab50182010-05-14 18:53:44 +0100520 }
521 fprintf(data->set.err,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700522 " %% Total %% Received %% Xferd Average Speed "
523 "Time Time Time Current\n"
524 " Dload Upload "
525 "Total Spent Left Speed\n");
Kristian Monsen5ab50182010-05-14 18:53:44 +0100526 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
527 }
528
529 /* Figure out the estimated time of arrival for the upload */
530 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
531 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
532 ulestimate = data->progress.size_ul / data->progress.ulspeed;
533
534 if(data->progress.size_ul > CURL_OFF_T_C(10000))
535 ulpercen = data->progress.uploaded /
536 (data->progress.size_ul/CURL_OFF_T_C(100));
537 else if(data->progress.size_ul > CURL_OFF_T_C(0))
538 ulpercen = (data->progress.uploaded*100) /
539 data->progress.size_ul;
540 }
541
542 /* ... and the download */
543 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
544 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
545 dlestimate = data->progress.size_dl / data->progress.dlspeed;
546
547 if(data->progress.size_dl > CURL_OFF_T_C(10000))
548 dlpercen = data->progress.downloaded /
549 (data->progress.size_dl/CURL_OFF_T_C(100));
550 else if(data->progress.size_dl > CURL_OFF_T_C(0))
551 dlpercen = (data->progress.downloaded*100) /
552 data->progress.size_dl;
553 }
554
555 /* Now figure out which of them is slower and use that one for the
556 total estimate! */
557 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
558
559 /* create the three time strings */
560 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
561 time2str(time_total, total_estimate);
562 time2str(time_spent, timespent);
563
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700564 /* Get the total amount of data expected to get transferred */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100565 total_expected_transfer =
566 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
567 data->progress.size_ul:data->progress.uploaded)+
568 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
569 data->progress.size_dl:data->progress.downloaded);
570
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700571 /* We have transferred this much so far */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100572 total_transfer = data->progress.downloaded + data->progress.uploaded;
573
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700574 /* Get the percentage of data transferred so far */
Kristian Monsen5ab50182010-05-14 18:53:44 +0100575 if(total_expected_transfer > CURL_OFF_T_C(10000))
576 total_percen = total_transfer /
577 (total_expected_transfer/CURL_OFF_T_C(100));
578 else if(total_expected_transfer > CURL_OFF_T_C(0))
579 total_percen = (total_transfer*100) / total_expected_transfer;
580
581 fprintf(data->set.err,
582 "\r"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700583 "%3" CURL_FORMAT_CURL_OFF_T " %s "
584 "%3" CURL_FORMAT_CURL_OFF_T " %s "
585 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
Kristian Monsen5ab50182010-05-14 18:53:44 +0100586 total_percen, /* 3 letters */ /* total % */
587 max5data(total_expected_transfer, max5[2]), /* total size */
588 dlpercen, /* 3 letters */ /* rcvd % */
589 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
590 ulpercen, /* 3 letters */ /* xfer % */
591 max5data(data->progress.uploaded, max5[1]), /* xfer size */
592 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
593 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
594 time_total, /* 8 letters */ /* total time */
595 time_spent, /* 8 letters */ /* time spent */
596 time_left, /* 8 letters */ /* time left */
597 max5data(data->progress.current_speed, max5[5]) /* current speed */
598 );
599
600 /* we flush the output stream to make it appear as soon as possible */
601 fflush(data->set.err);
602
603 } /* !(data->progress.flags & PGRS_HIDE) */
604
605 return 0;
606}