lib1531.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. /* Allocate one CURL handle per transfer */
  37. easy = curl_easy_init();
  38. /* init a multi stack */
  39. multi_handle = curl_multi_init();
  40. /* add the individual transfer */
  41. curl_multi_add_handle(multi_handle, easy);
  42. /* set the options (I left out a few, you'll get the point anyway) */
  43. curl_easy_setopt(easy, CURLOPT_URL, URL);
  44. curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE,
  45. (curl_off_t)testDataSize);
  46. curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
  47. /* we start some action by calling perform right away */
  48. curl_multi_perform(multi_handle, &still_running);
  49. do {
  50. struct timeval timeout;
  51. int rc; /* select() return code */
  52. CURLMcode mc; /* curl_multi_fdset() return code */
  53. fd_set fdread;
  54. fd_set fdwrite;
  55. fd_set fdexcep;
  56. int maxfd = -1;
  57. long curl_timeo = -1;
  58. FD_ZERO(&fdread);
  59. FD_ZERO(&fdwrite);
  60. FD_ZERO(&fdexcep);
  61. /* set a suitable timeout to play around with */
  62. timeout.tv_sec = 1;
  63. timeout.tv_usec = 0;
  64. curl_multi_timeout(multi_handle, &curl_timeo);
  65. if(curl_timeo >= 0) {
  66. timeout.tv_sec = curl_timeo / 1000;
  67. if(timeout.tv_sec > 1)
  68. timeout.tv_sec = 1;
  69. else
  70. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  71. }
  72. /* get file descriptors from the transfers */
  73. mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  74. if(mc != CURLM_OK) {
  75. fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
  76. break;
  77. }
  78. /* On success the value of maxfd is guaranteed to be >= -1. We call
  79. select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
  80. no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
  81. to sleep 100ms, which is the minimum suggested value in the
  82. curl_multi_fdset() doc. */
  83. if(maxfd == -1) {
  84. #ifdef _WIN32
  85. Sleep(100);
  86. rc = 0;
  87. #else
  88. /* Portable sleep for platforms other than Windows. */
  89. struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
  90. rc = select(0, NULL, NULL, NULL, &wait);
  91. #endif
  92. }
  93. else {
  94. /* Note that on some platforms 'timeout' may be modified by select().
  95. If you need access to the original value save a copy beforehand. */
  96. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  97. }
  98. switch(rc) {
  99. case -1:
  100. /* select error */
  101. break;
  102. case 0: /* timeout */
  103. default: /* action */
  104. curl_multi_perform(multi_handle, &still_running);
  105. break;
  106. }
  107. } while(still_running);
  108. /* See how the transfers went */
  109. while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
  110. if(msg->msg == CURLMSG_DONE) {
  111. printf("HTTP transfer completed with status %d\n", msg->data.result);
  112. break;
  113. }
  114. }
  115. curl_multi_cleanup(multi_handle);
  116. /* Free the CURL handles */
  117. curl_easy_cleanup(easy);
  118. return 0;
  119. }