2
0

lib1531.c 4.5 KB

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