lib670.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #if !defined(LIB670) && !defined(LIB671)
  25. #define CURL_DISABLE_DEPRECATION /* Using and testing the form api */
  26. #endif
  27. #include "test.h"
  28. #include <time.h>
  29. #include "memdebug.h"
  30. #define PAUSE_TIME 2
  31. static const char name[] = "field";
  32. struct ReadThis {
  33. CURL *easy;
  34. time_t origin;
  35. int count;
  36. };
  37. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  38. {
  39. struct ReadThis *pooh = (struct ReadThis *) userp;
  40. time_t delta;
  41. if(size * nmemb < 1)
  42. return 0;
  43. switch(pooh->count++) {
  44. case 0:
  45. *ptr = '\x41'; /* ASCII A. */
  46. return 1;
  47. case 1:
  48. pooh->origin = time(NULL);
  49. return CURL_READFUNC_PAUSE;
  50. case 2:
  51. delta = time(NULL) - pooh->origin;
  52. *ptr = delta >= PAUSE_TIME? '\x42': '\x41'; /* ASCII A or B. */
  53. return 1;
  54. case 3:
  55. return 0;
  56. }
  57. fprintf(stderr, "Read callback called after EOF\n");
  58. exit(1);
  59. }
  60. #if !defined(LIB670) && !defined(LIB672)
  61. static int xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
  62. curl_off_t ultotal, curl_off_t ulnow)
  63. {
  64. struct ReadThis *pooh = (struct ReadThis *) clientp;
  65. (void) dltotal;
  66. (void) dlnow;
  67. (void) ultotal;
  68. (void) ulnow;
  69. if(pooh->origin) {
  70. time_t delta = time(NULL) - pooh->origin;
  71. if(delta >= 4 * PAUSE_TIME) {
  72. fprintf(stderr, "unpausing failed: drain problem?\n");
  73. return CURLE_ABORTED_BY_CALLBACK;
  74. }
  75. if(delta >= PAUSE_TIME)
  76. curl_easy_pause(pooh->easy, CURLPAUSE_CONT);
  77. }
  78. return 0;
  79. }
  80. #endif
  81. int test(char *URL)
  82. {
  83. #if defined(LIB670) || defined(LIB671)
  84. curl_mime *mime = NULL;
  85. curl_mimepart *part;
  86. #else
  87. CURLFORMcode formrc;
  88. struct curl_httppost *formpost = NULL;
  89. struct curl_httppost *lastptr = NULL;
  90. #endif
  91. #if defined(LIB670) || defined(LIB672)
  92. CURLM *multi = NULL;
  93. CURLMcode mres;
  94. CURLMsg *msg;
  95. int msgs_left;
  96. int still_running = 0;
  97. #endif
  98. struct ReadThis pooh;
  99. CURLcode result;
  100. int res = TEST_ERR_FAILURE;
  101. /*
  102. * Check proper pausing/unpausing from a mime or form read callback.
  103. */
  104. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  105. fprintf(stderr, "curl_global_init() failed\n");
  106. return TEST_ERR_MAJOR_BAD;
  107. }
  108. pooh.origin = (time_t) 0;
  109. pooh.count = 0;
  110. pooh.easy = curl_easy_init();
  111. /* First set the URL that is about to receive our POST. */
  112. test_setopt(pooh.easy, CURLOPT_URL, URL);
  113. /* get verbose debug output please */
  114. test_setopt(pooh.easy, CURLOPT_VERBOSE, 1L);
  115. /* include headers in the output */
  116. test_setopt(pooh.easy, CURLOPT_HEADER, 1L);
  117. #if defined(LIB670) || defined(LIB671)
  118. /* Build the mime tree. */
  119. mime = curl_mime_init(pooh.easy);
  120. part = curl_mime_addpart(mime);
  121. result = curl_mime_name(part, name);
  122. if(result) {
  123. fprintf(stderr,
  124. "Something went wrong when building the mime structure: %d\n",
  125. (int) result);
  126. goto test_cleanup;
  127. }
  128. res = curl_mime_data_cb(part, (curl_off_t) 2, read_callback,
  129. NULL, NULL, &pooh);
  130. /* Bind mime data to its easy handle. */
  131. if(!res)
  132. test_setopt(pooh.easy, CURLOPT_MIMEPOST, mime);
  133. #else
  134. /* Build the form. */
  135. formrc = curl_formadd(&formpost, &lastptr,
  136. CURLFORM_COPYNAME, name,
  137. CURLFORM_STREAM, &pooh,
  138. CURLFORM_CONTENTLEN, (curl_off_t) 2,
  139. CURLFORM_END);
  140. if(formrc) {
  141. fprintf(stderr, "curl_formadd() = %d\n", (int) formrc);
  142. goto test_cleanup;
  143. }
  144. /* We want to use our own read function. */
  145. test_setopt(pooh.easy, CURLOPT_READFUNCTION, read_callback);
  146. /* Send a multi-part formpost. */
  147. test_setopt(pooh.easy, CURLOPT_HTTPPOST, formpost);
  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) || 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. result = msg->data.result;
  199. res = (int) result;
  200. }
  201. }
  202. curl_multi_remove_handle(multi, pooh.easy);
  203. curl_multi_cleanup(multi);
  204. #else
  205. /* Use the easy interface. */
  206. test_setopt(pooh.easy, CURLOPT_XFERINFODATA, &pooh);
  207. test_setopt(pooh.easy, CURLOPT_XFERINFOFUNCTION, xferinfo);
  208. test_setopt(pooh.easy, CURLOPT_NOPROGRESS, 0L);
  209. result = curl_easy_perform(pooh.easy);
  210. res = (int) result;
  211. #endif
  212. test_cleanup:
  213. curl_easy_cleanup(pooh.easy);
  214. #if defined(LIB670) || defined(LIB671)
  215. curl_mime_free(mime);
  216. #else
  217. curl_formfree(formpost);
  218. #endif
  219. curl_global_cleanup();
  220. return res;
  221. }