1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker * _ _ ____ _
3*6236dae4SAndroid Build Coastguard Worker * Project ___| | | | _ \| |
4*6236dae4SAndroid Build Coastguard Worker * / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker * | (__| |_| | _ <| |___
6*6236dae4SAndroid Build Coastguard Worker * \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker *
8*6236dae4SAndroid Build Coastguard Worker * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker *
10*6236dae4SAndroid Build Coastguard Worker * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker *
14*6236dae4SAndroid Build Coastguard Worker * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker *
18*6236dae4SAndroid Build Coastguard Worker * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker *
21*6236dae4SAndroid Build Coastguard Worker * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker *
23*6236dae4SAndroid Build Coastguard Worker ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker /* <DESC>
25*6236dae4SAndroid Build Coastguard Worker * Download many files in parallel, in the same thread.
26*6236dae4SAndroid Build Coastguard Worker * </DESC>
27*6236dae4SAndroid Build Coastguard Worker */
28*6236dae4SAndroid Build Coastguard Worker
29*6236dae4SAndroid Build Coastguard Worker #include <stdlib.h>
30*6236dae4SAndroid Build Coastguard Worker #include <string.h>
31*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
32*6236dae4SAndroid Build Coastguard Worker
33*6236dae4SAndroid Build Coastguard Worker static const char *urls[] = {
34*6236dae4SAndroid Build Coastguard Worker "https://www.microsoft.com",
35*6236dae4SAndroid Build Coastguard Worker "https://opensource.org",
36*6236dae4SAndroid Build Coastguard Worker "https://www.google.com",
37*6236dae4SAndroid Build Coastguard Worker "https://www.yahoo.com",
38*6236dae4SAndroid Build Coastguard Worker "https://www.ibm.com",
39*6236dae4SAndroid Build Coastguard Worker "https://www.mysql.com",
40*6236dae4SAndroid Build Coastguard Worker "https://www.oracle.com",
41*6236dae4SAndroid Build Coastguard Worker "https://www.ripe.net",
42*6236dae4SAndroid Build Coastguard Worker "https://www.iana.org",
43*6236dae4SAndroid Build Coastguard Worker "https://www.amazon.com",
44*6236dae4SAndroid Build Coastguard Worker "https://www.netcraft.com",
45*6236dae4SAndroid Build Coastguard Worker "https://www.heise.de",
46*6236dae4SAndroid Build Coastguard Worker "https://www.chip.de",
47*6236dae4SAndroid Build Coastguard Worker "https://www.ca.com",
48*6236dae4SAndroid Build Coastguard Worker "https://www.cnet.com",
49*6236dae4SAndroid Build Coastguard Worker "https://www.mozilla.org",
50*6236dae4SAndroid Build Coastguard Worker "https://www.cnn.com",
51*6236dae4SAndroid Build Coastguard Worker "https://www.wikipedia.org",
52*6236dae4SAndroid Build Coastguard Worker "https://www.dell.com",
53*6236dae4SAndroid Build Coastguard Worker "https://www.hp.com",
54*6236dae4SAndroid Build Coastguard Worker "https://www.cert.org",
55*6236dae4SAndroid Build Coastguard Worker "https://www.mit.edu",
56*6236dae4SAndroid Build Coastguard Worker "https://www.nist.gov",
57*6236dae4SAndroid Build Coastguard Worker "https://www.ebay.com",
58*6236dae4SAndroid Build Coastguard Worker "https://www.playstation.com",
59*6236dae4SAndroid Build Coastguard Worker "https://www.uefa.com",
60*6236dae4SAndroid Build Coastguard Worker "https://www.ieee.org",
61*6236dae4SAndroid Build Coastguard Worker "https://www.apple.com",
62*6236dae4SAndroid Build Coastguard Worker "https://www.symantec.com",
63*6236dae4SAndroid Build Coastguard Worker "https://www.zdnet.com",
64*6236dae4SAndroid Build Coastguard Worker "https://www.fujitsu.com/global/",
65*6236dae4SAndroid Build Coastguard Worker "https://www.supermicro.com",
66*6236dae4SAndroid Build Coastguard Worker "https://www.hotmail.com",
67*6236dae4SAndroid Build Coastguard Worker "https://www.ietf.org",
68*6236dae4SAndroid Build Coastguard Worker "https://www.bbc.co.uk",
69*6236dae4SAndroid Build Coastguard Worker "https://news.google.com",
70*6236dae4SAndroid Build Coastguard Worker "https://www.foxnews.com",
71*6236dae4SAndroid Build Coastguard Worker "https://www.msn.com",
72*6236dae4SAndroid Build Coastguard Worker "https://www.wired.com",
73*6236dae4SAndroid Build Coastguard Worker "https://www.sky.com",
74*6236dae4SAndroid Build Coastguard Worker "https://www.usatoday.com",
75*6236dae4SAndroid Build Coastguard Worker "https://www.cbs.com",
76*6236dae4SAndroid Build Coastguard Worker "https://www.nbc.com/",
77*6236dae4SAndroid Build Coastguard Worker "https://slashdot.org",
78*6236dae4SAndroid Build Coastguard Worker "https://www.informationweek.com",
79*6236dae4SAndroid Build Coastguard Worker "https://apache.org",
80*6236dae4SAndroid Build Coastguard Worker "https://www.un.org",
81*6236dae4SAndroid Build Coastguard Worker };
82*6236dae4SAndroid Build Coastguard Worker
83*6236dae4SAndroid Build Coastguard Worker #define MAX_PARALLEL 10 /* number of simultaneous transfers */
84*6236dae4SAndroid Build Coastguard Worker #define NUM_URLS sizeof(urls)/sizeof(char *)
85*6236dae4SAndroid Build Coastguard Worker
write_cb(char * data,size_t n,size_t l,void * userp)86*6236dae4SAndroid Build Coastguard Worker static size_t write_cb(char *data, size_t n, size_t l, void *userp)
87*6236dae4SAndroid Build Coastguard Worker {
88*6236dae4SAndroid Build Coastguard Worker /* take care of the data here, ignored in this example */
89*6236dae4SAndroid Build Coastguard Worker (void)data;
90*6236dae4SAndroid Build Coastguard Worker (void)userp;
91*6236dae4SAndroid Build Coastguard Worker return n*l;
92*6236dae4SAndroid Build Coastguard Worker }
93*6236dae4SAndroid Build Coastguard Worker
add_transfer(CURLM * cm,unsigned int i,int * left)94*6236dae4SAndroid Build Coastguard Worker static void add_transfer(CURLM *cm, unsigned int i, int *left)
95*6236dae4SAndroid Build Coastguard Worker {
96*6236dae4SAndroid Build Coastguard Worker CURL *eh = curl_easy_init();
97*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb);
98*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
99*6236dae4SAndroid Build Coastguard Worker curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
100*6236dae4SAndroid Build Coastguard Worker curl_multi_add_handle(cm, eh);
101*6236dae4SAndroid Build Coastguard Worker (*left)++;
102*6236dae4SAndroid Build Coastguard Worker }
103*6236dae4SAndroid Build Coastguard Worker
main(void)104*6236dae4SAndroid Build Coastguard Worker int main(void)
105*6236dae4SAndroid Build Coastguard Worker {
106*6236dae4SAndroid Build Coastguard Worker CURLM *cm;
107*6236dae4SAndroid Build Coastguard Worker CURLMsg *msg;
108*6236dae4SAndroid Build Coastguard Worker unsigned int transfers = 0;
109*6236dae4SAndroid Build Coastguard Worker int msgs_left = -1;
110*6236dae4SAndroid Build Coastguard Worker int left = 0;
111*6236dae4SAndroid Build Coastguard Worker
112*6236dae4SAndroid Build Coastguard Worker curl_global_init(CURL_GLOBAL_ALL);
113*6236dae4SAndroid Build Coastguard Worker cm = curl_multi_init();
114*6236dae4SAndroid Build Coastguard Worker
115*6236dae4SAndroid Build Coastguard Worker /* Limit the amount of simultaneous connections curl should allow: */
116*6236dae4SAndroid Build Coastguard Worker curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
117*6236dae4SAndroid Build Coastguard Worker
118*6236dae4SAndroid Build Coastguard Worker for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS;
119*6236dae4SAndroid Build Coastguard Worker transfers++)
120*6236dae4SAndroid Build Coastguard Worker add_transfer(cm, transfers, &left);
121*6236dae4SAndroid Build Coastguard Worker
122*6236dae4SAndroid Build Coastguard Worker do {
123*6236dae4SAndroid Build Coastguard Worker int still_alive = 1;
124*6236dae4SAndroid Build Coastguard Worker curl_multi_perform(cm, &still_alive);
125*6236dae4SAndroid Build Coastguard Worker
126*6236dae4SAndroid Build Coastguard Worker /* !checksrc! disable EQUALSNULL 1 */
127*6236dae4SAndroid Build Coastguard Worker while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) {
128*6236dae4SAndroid Build Coastguard Worker if(msg->msg == CURLMSG_DONE) {
129*6236dae4SAndroid Build Coastguard Worker char *url;
130*6236dae4SAndroid Build Coastguard Worker CURL *e = msg->easy_handle;
131*6236dae4SAndroid Build Coastguard Worker curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
132*6236dae4SAndroid Build Coastguard Worker fprintf(stderr, "R: %d - %s <%s>\n",
133*6236dae4SAndroid Build Coastguard Worker msg->data.result, curl_easy_strerror(msg->data.result), url);
134*6236dae4SAndroid Build Coastguard Worker curl_multi_remove_handle(cm, e);
135*6236dae4SAndroid Build Coastguard Worker curl_easy_cleanup(e);
136*6236dae4SAndroid Build Coastguard Worker left--;
137*6236dae4SAndroid Build Coastguard Worker }
138*6236dae4SAndroid Build Coastguard Worker else {
139*6236dae4SAndroid Build Coastguard Worker fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
140*6236dae4SAndroid Build Coastguard Worker }
141*6236dae4SAndroid Build Coastguard Worker if(transfers < NUM_URLS)
142*6236dae4SAndroid Build Coastguard Worker add_transfer(cm, transfers++, &left);
143*6236dae4SAndroid Build Coastguard Worker }
144*6236dae4SAndroid Build Coastguard Worker if(left)
145*6236dae4SAndroid Build Coastguard Worker curl_multi_wait(cm, NULL, 0, 1000, NULL);
146*6236dae4SAndroid Build Coastguard Worker
147*6236dae4SAndroid Build Coastguard Worker } while(left);
148*6236dae4SAndroid Build Coastguard Worker
149*6236dae4SAndroid Build Coastguard Worker curl_multi_cleanup(cm);
150*6236dae4SAndroid Build Coastguard Worker curl_global_cleanup();
151*6236dae4SAndroid Build Coastguard Worker
152*6236dae4SAndroid Build Coastguard Worker return EXIT_SUCCESS;
153*6236dae4SAndroid Build Coastguard Worker }
154