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 #define ENABLE_CURLX_PRINTF
31 /* use our own printf() functions */
32 #include "curlx.h"
33
34 #include "tool_cfgable.h"
35 #include "tool_cb_rea.h"
36 #include "tool_operate.h"
37 #include "tool_util.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 InStruct *in = userdata;
49 struct OperationConfig *config = in->config;
50
51 if(config->timeout_ms) {
52 struct timeval now = tvnow();
53 long msdelta = tvdiff(now, in->per->start);
54
55 if(msdelta > config->timeout_ms)
56 /* timeout */
57 return 0;
58 #ifndef WIN32
59 /* this logic waits on read activity on a file descriptor that is not a
60 socket which makes it not work with select() on Windows */
61 else {
62 fd_set bits;
63 struct timeval timeout;
64 long wait = config->timeout_ms - msdelta;
65
66 /* wait this long at the most */
67 timeout.tv_sec = wait/1000;
68 timeout.tv_usec = (wait%1000)*1000;
69
70 FD_ZERO(&bits);
71 FD_SET(in->fd, &bits);
72 if(!select(in->fd + 1, &bits, NULL, NULL, &timeout))
73 return 0; /* timeout */
74 }
75 #endif
76 }
77
78 rc = read(in->fd, buffer, sz*nmemb);
79 if(rc < 0) {
80 if(errno == EAGAIN) {
81 errno = 0;
82 in->config->readbusy = TRUE;
83 return CURL_READFUNC_PAUSE;
84 }
85 /* since size_t is unsigned we can't return negative values fine */
86 rc = 0;
87 }
88 in->config->readbusy = FALSE;
89
90 /* when select() rerturned zero here, it timed out */
91 return (size_t)rc;
92 }
93
94 /*
95 ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
96 */
97
tool_readbusy_cb(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)98 int tool_readbusy_cb(void *clientp,
99 curl_off_t dltotal, curl_off_t dlnow,
100 curl_off_t ultotal, curl_off_t ulnow)
101 {
102 struct per_transfer *per = clientp;
103 struct OperationConfig *config = per->config;
104
105 (void)dltotal; /* unused */
106 (void)dlnow; /* unused */
107 (void)ultotal; /* unused */
108 (void)ulnow; /* unused */
109
110 if(config->readbusy) {
111 config->readbusy = FALSE;
112 curl_easy_pause(per->curl, CURLPAUSE_CONT);
113 }
114
115 return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
116 }
117