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