2
0

lib1531.c 4.5 KB

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