1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "tool_setup.h"
25
26 #ifdef HAVE_SYS_SELECT_H
27 #include <sys/select.h>
28 #endif
29
30 #include "curlx.h"
31
32 #include "tool_cfgable.h"
33 #include "tool_cb_rea.h"
34 #include "tool_operate.h"
35 #include "tool_util.h"
36 #include "tool_msgs.h"
37 #include "tool_sleep.h"
38
39 #include "memdebug.h" /* keep this as LAST include */
40
41 /*
42 ** callback for CURLOPT_READFUNCTION
43 */
44
tool_read_cb(char * buffer,size_t sz,size_t nmemb,void * userdata)45 size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
46 {
47 ssize_t rc = 0;
48 struct per_transfer *per = userdata;
49 struct OperationConfig *config = per->config;
50
51 if((per->uploadfilesize != -1) &&
52 (per->uploadedsofar == per->uploadfilesize)) {
53 /* done */
54 return 0;
55 }
56
57 if(config->timeout_ms) {
58 struct timeval now = tvnow();
59 long msdelta = tvdiff(now, per->start);
60
61 if(msdelta > config->timeout_ms)
62 /* timeout */
63 return 0;
64 #ifndef _WIN32
65 /* this logic waits on read activity on a file descriptor that is not a
66 socket which makes it not work with select() on Windows */
67 else {
68 fd_set bits;
69 struct timeval timeout;
70 long wait = config->timeout_ms - msdelta;
71
72 /* wait this long at the most */
73 timeout.tv_sec = wait/1000;
74 timeout.tv_usec = (int)((wait%1000)*1000);
75
76 FD_ZERO(&bits);
77 FD_SET(per->infd, &bits);
78 if(!select(per->infd + 1, &bits, NULL, NULL, &timeout))
79 return 0; /* timeout */
80 }
81 #endif
82 }
83
84 rc = read(per->infd, buffer, sz*nmemb);
85 if(rc < 0) {
86 if(errno == EAGAIN) {
87 errno = 0;
88 config->readbusy = TRUE;
89 return CURL_READFUNC_PAUSE;
90 }
91 /* since size_t is unsigned we cannot return negative values fine */
92 rc = 0;
93 }
94 if((per->uploadfilesize != -1) &&
95 (per->uploadedsofar + rc > per->uploadfilesize)) {
96 /* do not allow uploading more than originally set out to do */
97 curl_off_t delta = per->uploadedsofar + rc - per->uploadfilesize;
98 warnf(per->config->global, "File size larger in the end than when "
99 "started. Dropping at least %" CURL_FORMAT_CURL_OFF_T " bytes",
100 delta);
101 rc = (ssize_t)(per->uploadfilesize - per->uploadedsofar);
102 }
103 config->readbusy = FALSE;
104
105 /* when select() returned zero here, it timed out */
106 return (size_t)rc;
107 }
108
109 /*
110 ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
111 */
112
tool_readbusy_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)113 int tool_readbusy_cb(void *clientp,
114 curl_off_t dltotal, curl_off_t dlnow,
115 curl_off_t ultotal, curl_off_t ulnow)
116 {
117 struct per_transfer *per = clientp;
118 struct OperationConfig *config = per->config;
119
120 (void)dltotal; /* unused */
121 (void)dlnow; /* unused */
122 (void)ultotal; /* unused */
123 (void)ulnow; /* unused */
124
125 if(config->readbusy) {
126 /* lame code to keep the rate down because the input might not deliver
127 anything, get paused again and come back here immediately */
128 static long rate = 500;
129 static struct timeval prev;
130 static curl_off_t ulprev;
131
132 if(ulprev == ulnow) {
133 /* it did not upload anything since last call */
134 struct timeval now = tvnow();
135 if(prev.tv_sec)
136 /* get a rolling average rate */
137 /* rate = rate - rate/4 + tvdiff(now, prev)/4; */
138 rate -= rate/4 - tvdiff(now, prev)/4;
139 prev = now;
140 }
141 else {
142 rate = 50;
143 ulprev = ulnow;
144 }
145 if(rate >= 50) {
146 /* keeps the looping down to 20 times per second in the crazy case */
147 config->readbusy = FALSE;
148 curl_easy_pause(per->curl, CURLPAUSE_CONT);
149 }
150 else
151 /* sleep half a period */
152 tool_go_sleep(25);
153 }
154
155 return per->noprogress ? 0 : CURL_PROGRESSFUNC_CONTINUE;
156 }
157