2
0

lib670.c 6.4 KB

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