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