lib1531.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "test.h"
  23. #include "testutil.h"
  24. #include "timediff.h"
  25. #include "warnless.h"
  26. #include "memdebug.h"
  27. #define TEST_HANG_TIMEOUT 60 * 1000
  28. static char const testData[] = ".abc\0xyz";
  29. static off_t const testDataSize = sizeof(testData) - 1;
  30. int test(char *URL)
  31. {
  32. CURL *easy;
  33. CURLM *multi_handle;
  34. int still_running; /* keep number of running handles */
  35. CURLMsg *msg; /* for picking up messages with the transfer status */
  36. int msgs_left; /* how many messages are left */
  37. int res = CURLE_OK;
  38. start_test_timing();
  39. global_init(CURL_GLOBAL_ALL);
  40. /* Allocate one CURL handle per transfer */
  41. easy = curl_easy_init();
  42. /* init a multi stack */
  43. multi_handle = curl_multi_init();
  44. /* add the individual transfer */
  45. curl_multi_add_handle(multi_handle, easy);
  46. /* set the options (I left out a few, you'll get the point anyway) */
  47. curl_easy_setopt(easy, CURLOPT_URL, URL);
  48. curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE,
  49. (curl_off_t)testDataSize);
  50. curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
  51. /* we start some action by calling perform right away */
  52. curl_multi_perform(multi_handle, &still_running);
  53. abort_on_test_timeout();
  54. do {
  55. struct timeval timeout;
  56. int rc; /* select() return code */
  57. CURLMcode mc; /* curl_multi_fdset() return code */
  58. fd_set fdread;
  59. fd_set fdwrite;
  60. fd_set fdexcep;
  61. int maxfd = -1;
  62. long curl_timeo = -1;
  63. FD_ZERO(&fdread);
  64. FD_ZERO(&fdwrite);
  65. FD_ZERO(&fdexcep);
  66. /* set a suitable timeout to play around with */
  67. timeout.tv_sec = 1;
  68. timeout.tv_usec = 0;
  69. curl_multi_timeout(multi_handle, &curl_timeo);
  70. if(curl_timeo >= 0) {
  71. curlx_mstotv(&timeout, curl_timeo);
  72. if(timeout.tv_sec > 1) {
  73. timeout.tv_sec = 1;
  74. timeout.tv_usec = 0;
  75. }
  76. }
  77. /* get file descriptors from the transfers */
  78. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  79. if(mc != CURLM_OK) {
  80. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  81. break;
  82. }
  83. /* On success the value of maxfd is guaranteed to be >= -1. We call
  84. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  85. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  86. to sleep 100ms, which is the minimum suggested value in the
  87. curl_multi_fdset() doc. */
  88. if(maxfd == -1) {
  89. #if defined(WIN32) || defined(_WIN32)
  90. Sleep(100);
  91. rc = 0;
  92. #else
  93. /* Portable sleep for platforms other than Windows. */
  94. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  95. rc = select(0, NULL, NULL, NULL, &wait);
  96. #endif
  97. }
  98. else {
  99. /* Note that on some platforms 'timeout' may be modified by select().
  100. If you need access to the original value save a copy beforehand. */
  101. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  102. }
  103. switch(rc) {
  104. case -1:
  105. /* select error */
  106. break;
  107. case 0: /* timeout */
  108. default: /* action */
  109. curl_multi_perform(multi_handle, &still_running);
  110. break;
  111. }
  112. abort_on_test_timeout();
  113. } while(still_running);
  114. /* See how the transfers went */
  115. do {
  116. msg = curl_multi_info_read(multi_handle, &msgs_left);
  117. if(msg && msg->msg == CURLMSG_DONE) {
  118. printf("HTTP transfer completed with status %d\n", msg->data.result);
  119. break;
  120. }
  121. abort_on_test_timeout();
  122. } while(msg);
  123. test_cleanup:
  124. curl_multi_cleanup(multi_handle);
  125. /* Free the CURL handles */
  126. curl_easy_cleanup(easy);
  127. curl_global_cleanup();
  128. return res;
  129. }