lib670.c 6.4 KB

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