10-at-a-time.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /* <DESC>
  25. * Download many files in parallel, in the same thread.
  26. * </DESC>
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <curl/curl.h>
  31. static const char *urls[] = {
  32. "https://www.microsoft.com",
  33. "https://opensource.org",
  34. "https://www.google.com",
  35. "https://www.yahoo.com",
  36. "https://www.ibm.com",
  37. "https://www.mysql.com",
  38. "https://www.oracle.com",
  39. "https://www.ripe.net",
  40. "https://www.iana.org",
  41. "https://www.amazon.com",
  42. "https://www.netcraft.com",
  43. "https://www.heise.de",
  44. "https://www.chip.de",
  45. "https://www.ca.com",
  46. "https://www.cnet.com",
  47. "https://www.mozilla.org",
  48. "https://www.cnn.com",
  49. "https://www.wikipedia.org",
  50. "https://www.dell.com",
  51. "https://www.hp.com",
  52. "https://www.cert.org",
  53. "https://www.mit.edu",
  54. "https://www.nist.gov",
  55. "https://www.ebay.com",
  56. "https://www.playstation.com",
  57. "https://www.uefa.com",
  58. "https://www.ieee.org",
  59. "https://www.apple.com",
  60. "https://www.symantec.com",
  61. "https://www.zdnet.com",
  62. "https://www.fujitsu.com/global/",
  63. "https://www.supermicro.com",
  64. "https://www.hotmail.com",
  65. "https://www.ietf.org",
  66. "https://www.bbc.co.uk",
  67. "https://news.google.com",
  68. "https://www.foxnews.com",
  69. "https://www.msn.com",
  70. "https://www.wired.com",
  71. "https://www.sky.com",
  72. "https://www.usatoday.com",
  73. "https://www.cbs.com",
  74. "https://www.nbc.com/",
  75. "https://slashdot.org",
  76. "https://www.informationweek.com",
  77. "https://apache.org",
  78. "https://www.un.org",
  79. };
  80. #define MAX_PARALLEL 10 /* number of simultaneous transfers */
  81. #define NUM_URLS sizeof(urls)/sizeof(char *)
  82. static size_t write_cb(char *data, size_t n, size_t l, void *userp)
  83. {
  84. /* take care of the data here, ignored in this example */
  85. (void)data;
  86. (void)userp;
  87. return n*l;
  88. }
  89. static void add_transfer(CURLM *cm, unsigned int i, int *left)
  90. {
  91. CURL *eh = curl_easy_init();
  92. curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb);
  93. curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
  94. curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
  95. curl_multi_add_handle(cm, eh);
  96. (*left)++;
  97. }
  98. int main(void)
  99. {
  100. CURLM *cm;
  101. CURLMsg *msg;
  102. unsigned int transfers = 0;
  103. int msgs_left = -1;
  104. int left = 0;
  105. curl_global_init(CURL_GLOBAL_ALL);
  106. cm = curl_multi_init();
  107. /* Limit the amount of simultaneous connections curl should allow: */
  108. curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
  109. for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS;
  110. transfers++)
  111. add_transfer(cm, transfers, &left);
  112. do {
  113. int still_alive = 1;
  114. curl_multi_perform(cm, &still_alive);
  115. /* !checksrc! disable EQUALSNULL 1 */
  116. while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) {
  117. if(msg->msg == CURLMSG_DONE) {
  118. char *url;
  119. CURL *e = msg->easy_handle;
  120. curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
  121. fprintf(stderr, "R: %d - %s <%s>\n",
  122. msg->data.result, curl_easy_strerror(msg->data.result), url);
  123. curl_multi_remove_handle(cm, e);
  124. curl_easy_cleanup(e);
  125. left--;
  126. }
  127. else {
  128. fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
  129. }
  130. if(transfers < NUM_URLS)
  131. add_transfer(cm, transfers++, &left);
  132. }
  133. if(left)
  134. curl_multi_wait(cm, NULL, 0, 1000, NULL);
  135. } while(left);
  136. curl_multi_cleanup(cm);
  137. curl_global_cleanup();
  138. return EXIT_SUCCESS;
  139. }