lib670.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. res = curl_mime_data_cb(part, (curl_off_t) 2, read_callback,
  119. NULL, NULL, &pooh);
  120. if(result) {
  121. fprintf(stderr,
  122. "Something went wrong when building the mime structure: %d\n",
  123. (int) result);
  124. goto test_cleanup;
  125. }
  126. /* Bind mime data to its easy handle. */
  127. if(!res)
  128. test_setopt(pooh.easy, CURLOPT_MIMEPOST, mime);
  129. #else
  130. /* Build the form. */
  131. formrc = curl_formadd(&formpost, &lastptr,
  132. CURLFORM_COPYNAME, name,
  133. CURLFORM_STREAM, &pooh,
  134. CURLFORM_CONTENTLEN, (curl_off_t) 2,
  135. CURLFORM_END);
  136. if(formrc) {
  137. fprintf(stderr, "curl_formadd() = %d\n", (int) formrc);
  138. goto test_cleanup;
  139. }
  140. /* We want to use our own read function. */
  141. test_setopt(pooh.easy, CURLOPT_READFUNCTION, read_callback);
  142. /* Send a multi-part formpost. */
  143. test_setopt(pooh.easy, CURLOPT_HTTPPOST, formpost);
  144. #endif
  145. #if defined(LIB670) || defined(LIB672)
  146. /* Use the multi interface. */
  147. multi = curl_multi_init();
  148. mres = curl_multi_add_handle(multi, pooh.easy);
  149. while(!mres) {
  150. struct timeval timeout;
  151. int rc = 0;
  152. fd_set fdread;
  153. fd_set fdwrite;
  154. fd_set fdexcept;
  155. int maxfd = -1;
  156. mres = curl_multi_perform(multi, &still_running);
  157. if(!still_running || mres != CURLM_OK)
  158. break;
  159. if(pooh.origin) {
  160. time_t delta = time(NULL) - pooh.origin;
  161. if(delta >= 4 * PAUSE_TIME) {
  162. fprintf(stderr, "unpausing failed: drain problem?\n");
  163. res = CURLE_OPERATION_TIMEDOUT;
  164. break;
  165. }
  166. if(delta >= PAUSE_TIME)
  167. curl_easy_pause(pooh.easy, CURLPAUSE_CONT);
  168. }
  169. FD_ZERO(&fdread);
  170. FD_ZERO(&fdwrite);
  171. FD_ZERO(&fdexcept);
  172. timeout.tv_sec = 0;
  173. timeout.tv_usec = 1000000 * PAUSE_TIME / 10;
  174. mres = curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcept, &maxfd);
  175. if(mres)
  176. break;
  177. #if defined(WIN32) || defined(_WIN32)
  178. if(maxfd == -1)
  179. Sleep(100);
  180. else
  181. #endif
  182. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcept, &timeout);
  183. if(rc == -1) {
  184. fprintf(stderr, "Select error\n");
  185. break;
  186. }
  187. }
  188. if(mres != CURLM_OK)
  189. for(;;) {
  190. msg = curl_multi_info_read(multi, &msgs_left);
  191. if(!msg)
  192. break;
  193. if(msg->msg == CURLMSG_DONE) {
  194. result = msg->data.result;
  195. res = (int) result;
  196. }
  197. }
  198. curl_multi_remove_handle(multi, pooh.easy);
  199. curl_multi_cleanup(multi);
  200. #else
  201. /* Use the easy interface. */
  202. test_setopt(pooh.easy, CURLOPT_XFERINFODATA, &pooh);
  203. test_setopt(pooh.easy, CURLOPT_XFERINFOFUNCTION, xferinfo);
  204. test_setopt(pooh.easy, CURLOPT_NOPROGRESS, 0L);
  205. result = curl_easy_perform(pooh.easy);
  206. res = (int) result;
  207. #endif
  208. test_cleanup:
  209. curl_easy_cleanup(pooh.easy);
  210. #if defined(LIB670) || defined(LIB671)
  211. curl_mime_free(mime);
  212. #else
  213. curl_formfree(formpost);
  214. #endif
  215. curl_global_cleanup();
  216. return res;
  217. }