lib670.c 6.5 KB

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