-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
59 lines (45 loc) · 1.49 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <curl/curl.h>
#include <string>
#include <sstream>
#include <thread>
#include <vector>
#include <iostream>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *stream) {
size_t realsize = size * nmemb;
std::string data((const char*) contents, realsize);
*((std::stringstream*) stream) << data << std::endl;
return realsize;
}
static void *pull_one_url(void *url) {
CURL *curl;
std::stringstream out;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &out);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << long(out.tellp()) << " bytes received from " << (char *) url << std::endl;
//std::cout << out.str() << std::endl;
}
curl_easy_cleanup(curl);
return NULL;
}
int main(int argc, char **argv) {
size_t num_urls = (size_t) (argc - 1);
std::vector<std::string> urls;
for (int i = 0; i < argc - 1; i++) {
urls.push_back(std::move(argv[i + 1]));
}
std::vector<std::thread> pool_thread;
curl_global_init(CURL_GLOBAL_ALL);
for (int i = 0; i < num_urls; i++) {
pool_thread.emplace_back(pull_one_url, (void*)urls[i].c_str());
}
for (auto &thr:pool_thread) {
thr.join();
}
return 0;
}