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
25 #if !defined(LIB670) && !defined(LIB671)
26 #define CURL_DISABLE_DEPRECATION /* Using and testing the form api */
27 #endif
28
29 #include "test.h"
30
31 #include <time.h>
32
33 #include "memdebug.h"
34
35 #define PAUSE_TIME 5
36
37
38 static const char name[] = "field";
39
40 struct ReadThis {
41 CURL *easy;
42 time_t origin;
43 int count;
44 };
45
46
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)47 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
48 {
49 struct ReadThis *pooh = (struct ReadThis *) userp;
50 time_t delta;
51
52 if(size * nmemb < 1)
53 return 0;
54
55 switch(pooh->count++) {
56 case 0:
57 *ptr = '\x41'; /* ASCII A. */
58 return 1;
59 case 1:
60 pooh->origin = time(NULL);
61 return CURL_READFUNC_PAUSE;
62 case 2:
63 delta = time(NULL) - pooh->origin;
64 *ptr = delta >= PAUSE_TIME? '\x42': '\x41'; /* ASCII A or B. */
65 return 1;
66 case 3:
67 return 0;
68 }
69 fprintf(stderr, "Read callback called after EOF\n");
70 exit(1);
71 }
72
73 #if !defined(LIB670) && !defined(LIB672)
xferinfo(void * clientp,curl_off_t dltotal,curl_off_t dlnow,curl_off_t ultotal,curl_off_t ulnow)74 static int xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
75 curl_off_t ultotal, curl_off_t ulnow)
76 {
77 struct ReadThis *pooh = (struct ReadThis *) clientp;
78
79 (void) dltotal;
80 (void) dlnow;
81 (void) ultotal;
82 (void) ulnow;
83
84 if(pooh->origin) {
85 time_t delta = time(NULL) - pooh->origin;
86
87 if(delta >= 4 * PAUSE_TIME) {
88 fprintf(stderr, "unpausing failed: drain problem?\n");
89 return CURLE_ABORTED_BY_CALLBACK;
90 }
91
92 if(delta >= PAUSE_TIME)
93 curl_easy_pause(pooh->easy, CURLPAUSE_CONT);
94 }
95
96 return 0;
97 }
98 #endif
99
test(char * URL)100 CURLcode test(char *URL)
101 {
102 #if defined(LIB670) || defined(LIB671)
103 curl_mime *mime = NULL;
104 curl_mimepart *part;
105 #else
106 CURLFORMcode formrc;
107 struct curl_httppost *formpost = NULL;
108 struct curl_httppost *lastptr = NULL;
109 #endif
110 #if defined(LIB670) || defined(LIB672)
111 CURLM *multi = NULL;
112 CURLMcode mres;
113 CURLMsg *msg;
114 int msgs_left;
115 int still_running = 0;
116 #endif
117
118 struct ReadThis pooh;
119 CURLcode res = TEST_ERR_FAILURE;
120
121 /*
122 * Check proper pausing/unpausing from a mime or form read callback.
123 */
124
125 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
126 fprintf(stderr, "curl_global_init() failed\n");
127 return TEST_ERR_MAJOR_BAD;
128 }
129
130 pooh.origin = (time_t) 0;
131 pooh.count = 0;
132 pooh.easy = curl_easy_init();
133
134 /* First set the URL that is about to receive our POST. */
135 test_setopt(pooh.easy, CURLOPT_URL, URL);
136
137 /* get verbose debug output please */
138 test_setopt(pooh.easy, CURLOPT_VERBOSE, 1L);
139
140 /* include headers in the output */
141 test_setopt(pooh.easy, CURLOPT_HEADER, 1L);
142
143 #if defined(LIB670) || defined(LIB671)
144 /* Build the mime tree. */
145 mime = curl_mime_init(pooh.easy);
146 part = curl_mime_addpart(mime);
147 res = curl_mime_name(part, name);
148 if(res != CURLE_OK) {
149 fprintf(stderr,
150 "Something went wrong when building the mime structure: %d\n",
151 res);
152 goto test_cleanup;
153 }
154
155 res = curl_mime_data_cb(part, (curl_off_t) 2, read_callback,
156 NULL, NULL, &pooh);
157
158 /* Bind mime data to its easy handle. */
159 if(res == CURLE_OK)
160 test_setopt(pooh.easy, CURLOPT_MIMEPOST, mime);
161 #else
162 /* Build the form. */
163 formrc = curl_formadd(&formpost, &lastptr,
164 CURLFORM_COPYNAME, name,
165 CURLFORM_STREAM, &pooh,
166 CURLFORM_CONTENTLEN, (curl_off_t) 2,
167 CURLFORM_END);
168 if(formrc) {
169 fprintf(stderr, "curl_formadd() = %d\n", (int) formrc);
170 goto test_cleanup;
171 }
172
173 /* We want to use our own read function. */
174 test_setopt(pooh.easy, CURLOPT_READFUNCTION, read_callback);
175
176 /* Send a multi-part formpost. */
177 test_setopt(pooh.easy, CURLOPT_HTTPPOST, formpost);
178 #endif
179
180 #if defined(LIB670) || defined(LIB672)
181 /* Use the multi interface. */
182 multi = curl_multi_init();
183 mres = curl_multi_add_handle(multi, pooh.easy);
184 while(!mres) {
185 struct timeval timeout;
186 int rc = 0;
187 fd_set fdread;
188 fd_set fdwrite;
189 fd_set fdexcept;
190 int maxfd = -1;
191
192 mres = curl_multi_perform(multi, &still_running);
193 if(!still_running || mres != CURLM_OK)
194 break;
195
196 if(pooh.origin) {
197 time_t delta = time(NULL) - pooh.origin;
198
199 if(delta >= 4 * PAUSE_TIME) {
200 fprintf(stderr, "unpausing failed: drain problem?\n");
201 res = CURLE_OPERATION_TIMEDOUT;
202 break;
203 }
204
205 if(delta >= PAUSE_TIME)
206 curl_easy_pause(pooh.easy, CURLPAUSE_CONT);
207 }
208
209 FD_ZERO(&fdread);
210 FD_ZERO(&fdwrite);
211 FD_ZERO(&fdexcept);
212 timeout.tv_sec = 0;
213 timeout.tv_usec = 1000000 * PAUSE_TIME / 10;
214 mres = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcept, &maxfd);
215 if(mres)
216 break;
217 #if defined(_WIN32)
218 if(maxfd == -1)
219 Sleep(100);
220 else
221 #endif
222 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcept, &timeout);
223 if(rc == -1) {
224 fprintf(stderr, "Select error\n");
225 break;
226 }
227 }
228
229 if(mres != CURLM_OK)
230 for(;;) {
231 msg = curl_multi_info_read(multi, &msgs_left);
232 if(!msg)
233 break;
234 if(msg->msg == CURLMSG_DONE) {
235 res = msg->data.result;
236 }
237 }
238
239 curl_multi_remove_handle(multi, pooh.easy);
240 curl_multi_cleanup(multi);
241
242 #else
243 /* Use the easy interface. */
244 test_setopt(pooh.easy, CURLOPT_XFERINFODATA, &pooh);
245 test_setopt(pooh.easy, CURLOPT_XFERINFOFUNCTION, xferinfo);
246 test_setopt(pooh.easy, CURLOPT_NOPROGRESS, 0L);
247 res = curl_easy_perform(pooh.easy);
248 #endif
249
250
251 test_cleanup:
252 curl_easy_cleanup(pooh.easy);
253 #if defined(LIB670) || defined(LIB671)
254 curl_mime_free(mime);
255 #else
256 curl_formfree(formpost);
257 #endif
258
259 curl_global_cleanup();
260 return res;
261 }
262