113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci/* <DESC> 2513498266Sopenharmony_ci * Download many files in parallel, in the same thread. 2613498266Sopenharmony_ci * </DESC> 2713498266Sopenharmony_ci */ 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include <errno.h> 3013498266Sopenharmony_ci#include <stdlib.h> 3113498266Sopenharmony_ci#include <string.h> 3213498266Sopenharmony_ci#ifndef _WIN32 3313498266Sopenharmony_ci# include <unistd.h> 3413498266Sopenharmony_ci#endif 3513498266Sopenharmony_ci#include <curl/curl.h> 3613498266Sopenharmony_ci 3713498266Sopenharmony_cistatic const char *urls[] = { 3813498266Sopenharmony_ci "https://www.microsoft.com", 3913498266Sopenharmony_ci "https://opensource.org", 4013498266Sopenharmony_ci "https://www.google.com", 4113498266Sopenharmony_ci "https://www.yahoo.com", 4213498266Sopenharmony_ci "https://www.ibm.com", 4313498266Sopenharmony_ci "https://www.mysql.com", 4413498266Sopenharmony_ci "https://www.oracle.com", 4513498266Sopenharmony_ci "https://www.ripe.net", 4613498266Sopenharmony_ci "https://www.iana.org", 4713498266Sopenharmony_ci "https://www.amazon.com", 4813498266Sopenharmony_ci "https://www.netcraft.com", 4913498266Sopenharmony_ci "https://www.heise.de", 5013498266Sopenharmony_ci "https://www.chip.de", 5113498266Sopenharmony_ci "https://www.ca.com", 5213498266Sopenharmony_ci "https://www.cnet.com", 5313498266Sopenharmony_ci "https://www.mozilla.org", 5413498266Sopenharmony_ci "https://www.cnn.com", 5513498266Sopenharmony_ci "https://www.wikipedia.org", 5613498266Sopenharmony_ci "https://www.dell.com", 5713498266Sopenharmony_ci "https://www.hp.com", 5813498266Sopenharmony_ci "https://www.cert.org", 5913498266Sopenharmony_ci "https://www.mit.edu", 6013498266Sopenharmony_ci "https://www.nist.gov", 6113498266Sopenharmony_ci "https://www.ebay.com", 6213498266Sopenharmony_ci "https://www.playstation.com", 6313498266Sopenharmony_ci "https://www.uefa.com", 6413498266Sopenharmony_ci "https://www.ieee.org", 6513498266Sopenharmony_ci "https://www.apple.com", 6613498266Sopenharmony_ci "https://www.symantec.com", 6713498266Sopenharmony_ci "https://www.zdnet.com", 6813498266Sopenharmony_ci "https://www.fujitsu.com/global/", 6913498266Sopenharmony_ci "https://www.supermicro.com", 7013498266Sopenharmony_ci "https://www.hotmail.com", 7113498266Sopenharmony_ci "https://www.ietf.org", 7213498266Sopenharmony_ci "https://www.bbc.co.uk", 7313498266Sopenharmony_ci "https://news.google.com", 7413498266Sopenharmony_ci "https://www.foxnews.com", 7513498266Sopenharmony_ci "https://www.msn.com", 7613498266Sopenharmony_ci "https://www.wired.com", 7713498266Sopenharmony_ci "https://www.sky.com", 7813498266Sopenharmony_ci "https://www.usatoday.com", 7913498266Sopenharmony_ci "https://www.cbs.com", 8013498266Sopenharmony_ci "https://www.nbc.com/", 8113498266Sopenharmony_ci "https://slashdot.org", 8213498266Sopenharmony_ci "https://www.informationweek.com", 8313498266Sopenharmony_ci "https://apache.org", 8413498266Sopenharmony_ci "https://www.un.org", 8513498266Sopenharmony_ci}; 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci#define MAX_PARALLEL 10 /* number of simultaneous transfers */ 8813498266Sopenharmony_ci#define NUM_URLS sizeof(urls)/sizeof(char *) 8913498266Sopenharmony_ci 9013498266Sopenharmony_cistatic size_t write_cb(char *data, size_t n, size_t l, void *userp) 9113498266Sopenharmony_ci{ 9213498266Sopenharmony_ci /* take care of the data here, ignored in this example */ 9313498266Sopenharmony_ci (void)data; 9413498266Sopenharmony_ci (void)userp; 9513498266Sopenharmony_ci return n*l; 9613498266Sopenharmony_ci} 9713498266Sopenharmony_ci 9813498266Sopenharmony_cistatic void add_transfer(CURLM *cm, unsigned int i, int *left) 9913498266Sopenharmony_ci{ 10013498266Sopenharmony_ci CURL *eh = curl_easy_init(); 10113498266Sopenharmony_ci curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb); 10213498266Sopenharmony_ci curl_easy_setopt(eh, CURLOPT_URL, urls[i]); 10313498266Sopenharmony_ci curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]); 10413498266Sopenharmony_ci curl_multi_add_handle(cm, eh); 10513498266Sopenharmony_ci (*left)++; 10613498266Sopenharmony_ci} 10713498266Sopenharmony_ci 10813498266Sopenharmony_ciint main(void) 10913498266Sopenharmony_ci{ 11013498266Sopenharmony_ci CURLM *cm; 11113498266Sopenharmony_ci CURLMsg *msg; 11213498266Sopenharmony_ci unsigned int transfers = 0; 11313498266Sopenharmony_ci int msgs_left = -1; 11413498266Sopenharmony_ci int left = 0; 11513498266Sopenharmony_ci 11613498266Sopenharmony_ci curl_global_init(CURL_GLOBAL_ALL); 11713498266Sopenharmony_ci cm = curl_multi_init(); 11813498266Sopenharmony_ci 11913498266Sopenharmony_ci /* Limit the amount of simultaneous connections curl should allow: */ 12013498266Sopenharmony_ci curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL); 12113498266Sopenharmony_ci 12213498266Sopenharmony_ci for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS; 12313498266Sopenharmony_ci transfers++) 12413498266Sopenharmony_ci add_transfer(cm, transfers, &left); 12513498266Sopenharmony_ci 12613498266Sopenharmony_ci do { 12713498266Sopenharmony_ci int still_alive = 1; 12813498266Sopenharmony_ci curl_multi_perform(cm, &still_alive); 12913498266Sopenharmony_ci 13013498266Sopenharmony_ci while((msg = curl_multi_info_read(cm, &msgs_left))) { 13113498266Sopenharmony_ci if(msg->msg == CURLMSG_DONE) { 13213498266Sopenharmony_ci char *url; 13313498266Sopenharmony_ci CURL *e = msg->easy_handle; 13413498266Sopenharmony_ci curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); 13513498266Sopenharmony_ci fprintf(stderr, "R: %d - %s <%s>\n", 13613498266Sopenharmony_ci msg->data.result, curl_easy_strerror(msg->data.result), url); 13713498266Sopenharmony_ci curl_multi_remove_handle(cm, e); 13813498266Sopenharmony_ci curl_easy_cleanup(e); 13913498266Sopenharmony_ci left--; 14013498266Sopenharmony_ci } 14113498266Sopenharmony_ci else { 14213498266Sopenharmony_ci fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); 14313498266Sopenharmony_ci } 14413498266Sopenharmony_ci if(transfers < NUM_URLS) 14513498266Sopenharmony_ci add_transfer(cm, transfers++, &left); 14613498266Sopenharmony_ci } 14713498266Sopenharmony_ci if(left) 14813498266Sopenharmony_ci curl_multi_wait(cm, NULL, 0, 1000, NULL); 14913498266Sopenharmony_ci 15013498266Sopenharmony_ci } while(left); 15113498266Sopenharmony_ci 15213498266Sopenharmony_ci curl_multi_cleanup(cm); 15313498266Sopenharmony_ci curl_global_cleanup(); 15413498266Sopenharmony_ci 15513498266Sopenharmony_ci return EXIT_SUCCESS; 15613498266Sopenharmony_ci} 157