lib670.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "test.h"
  25. #include <time.h>
  26. #include "memdebug.h"
  27. #define PAUSE_TIME 5
  28. static const char testname[] = "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. CURLcode 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 res = TEST_ERR_FAILURE;
  97. /*
  98. * Check proper pausing/unpausing from a mime or form read callback.
  99. */
  100. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  101. fprintf(stderr, "curl_global_init() failed\n");
  102. return TEST_ERR_MAJOR_BAD;
  103. }
  104. pooh.origin = (time_t) 0;
  105. pooh.count = 0;
  106. pooh.easy = curl_easy_init();
  107. /* First set the URL that is about to receive our POST. */
  108. test_setopt(pooh.easy, CURLOPT_URL, URL);
  109. /* get verbose debug output please */
  110. test_setopt(pooh.easy, CURLOPT_VERBOSE, 1L);
  111. /* include headers in the output */
  112. test_setopt(pooh.easy, CURLOPT_HEADER, 1L);
  113. #if defined(LIB670) || defined(LIB671)
  114. /* Build the mime tree. */
  115. mime = curl_mime_init(pooh.easy);
  116. part = curl_mime_addpart(mime);
  117. res = curl_mime_name(part, testname);
  118. if(res != CURLE_OK) {
  119. fprintf(stderr,
  120. "Something went wrong when building the mime structure: %d\n",
  121. res);
  122. goto test_cleanup;
  123. }
  124. res = curl_mime_data_cb(part, (curl_off_t) 2, read_callback,
  125. NULL, NULL, &pooh);
  126. /* Bind mime data to its easy handle. */
  127. if(res == CURLE_OK)
  128. test_setopt(pooh.easy, CURLOPT_MIMEPOST, mime);
  129. #else
  130. CURL_IGNORE_DEPRECATION(
  131. /* Build the form. */
  132. formrc = curl_formadd(&formpost, &lastptr,
  133. CURLFORM_COPYNAME, testname,
  134. CURLFORM_STREAM, &pooh,
  135. CURLFORM_CONTENTLEN, (curl_off_t) 2,
  136. CURLFORM_END);
  137. )
  138. if(formrc) {
  139. fprintf(stderr, "curl_formadd() = %d\n", (int) formrc);
  140. goto test_cleanup;
  141. }
  142. /* We want to use our own read function. */
  143. test_setopt(pooh.easy, CURLOPT_READFUNCTION, read_callback);
  144. CURL_IGNORE_DEPRECATION(
  145. /* Send a multi-part formpost. */
  146. test_setopt(pooh.easy, CURLOPT_HTTPPOST, formpost);
  147. )
  148. #endif
  149. #if defined(LIB670) || defined(LIB672)
  150. /* Use the multi interface. */
  151. multi = curl_multi_init();
  152. mres = curl_multi_add_handle(multi, pooh.easy);
  153. while(!mres) {
  154. struct timeval timeout;
  155. int rc = 0;
  156. fd_set fdread;
  157. fd_set fdwrite;
  158. fd_set fdexcept;
  159. int maxfd = -1;
  160. mres = curl_multi_perform(multi, &still_running);
  161. if(!still_running || mres != CURLM_OK)
  162. break;
  163. if(pooh.origin) {
  164. time_t delta = time(NULL) - pooh.origin;
  165. if(delta >= 4 * PAUSE_TIME) {
  166. fprintf(stderr, "unpausing failed: drain problem?\n");
  167. res = CURLE_OPERATION_TIMEDOUT;
  168. break;
  169. }
  170. if(delta >= PAUSE_TIME)
  171. curl_easy_pause(pooh.easy, CURLPAUSE_CONT);
  172. }
  173. FD_ZERO(&fdread);
  174. FD_ZERO(&fdwrite);
  175. FD_ZERO(&fdexcept);
  176. timeout.tv_sec = 0;
  177. timeout.tv_usec = 1000000 * PAUSE_TIME / 10;
  178. mres = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcept, &maxfd);
  179. if(mres)
  180. break;
  181. #if defined(_WIN32)
  182. if(maxfd == -1)
  183. Sleep(100);
  184. else
  185. #endif
  186. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcept, &timeout);
  187. if(rc == -1) {
  188. fprintf(stderr, "Select error\n");
  189. break;
  190. }
  191. }
  192. if(mres != CURLM_OK)
  193. for(;;) {
  194. msg = curl_multi_info_read(multi, &msgs_left);
  195. if(!msg)
  196. break;
  197. if(msg->msg == CURLMSG_DONE) {
  198. res = msg->data.result;
  199. }
  200. }
  201. curl_multi_remove_handle(multi, pooh.easy);
  202. curl_multi_cleanup(multi);
  203. #else
  204. /* Use the easy interface. */
  205. test_setopt(pooh.easy, CURLOPT_XFERINFODATA, &pooh);
  206. test_setopt(pooh.easy, CURLOPT_XFERINFOFUNCTION, xferinfo);
  207. test_setopt(pooh.easy, CURLOPT_NOPROGRESS, 0L);
  208. res = curl_easy_perform(pooh.easy);
  209. #endif
  210. test_cleanup:
  211. curl_easy_cleanup(pooh.easy);
  212. #if defined(LIB670) || defined(LIB671)
  213. curl_mime_free(mime);
  214. #else
  215. CURL_IGNORE_DEPRECATION(
  216. curl_formfree(formpost);
  217. )
  218. #endif
  219. curl_global_cleanup();
  220. return res;
  221. }