-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex008-parallel_http_get.cpp
72 lines (56 loc) · 2.27 KB
/
ex008-parallel_http_get.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
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <vector>
#include "coke/wait.h"
#include "coke/http/http_client.h"
#include "coke/http/http_utils.h"
/**
* This example send multiple http requests, waits for the results
* and outputs them to stdout, showing how to start a group of tasks
* in parallel.
*/
coke::Task<> parallel_http_get(std::vector<std::string> urls) {
coke::HttpClient cli;
std::vector<coke::HttpResult> results;
std::vector<coke::HttpAwaiter> tasks;
tasks.reserve(urls.size());
for (const std::string &url : urls)
tasks.push_back(cli.request(url));
results = co_await coke::async_wait(std::move(tasks));
for (std::size_t i = 0; i < urls.size(); i++) {
auto &res = results[i];
std::cout << "URL: " << urls[i] << "\n";
if (res.state == coke::STATE_SUCCESS) {
coke::HttpResponse &resp = res.resp;
std::cout << resp.get_http_version() << ' '
<< resp.get_status_code() << ' '
<< resp.get_reason_phrase() << "\n\n";
for (coke::HttpHeaderView h : coke::HttpHeaderCursor(resp))
std::cout << h.name << ": " << h.value << "\n";
// In order to ensure a more brief display,
// the http body is not output to the console.
std::cout << "\nBody chunked: " << resp.is_chunked() << std::endl;
if (resp.is_chunked()) {
for (std::string_view v : coke::HttpChunkCursor(resp))
std::cout << "Body chunk size: " << v.size() << std::endl;
}
else {
std::string_view body = coke::http_body_view(resp);
std::cout << "Body size: " << body.size() << "\n";
}
}
else {
std::cout << "ERROR: state:" << res.state << " error:" << res.error << std::endl;
std::cout << coke::get_error_string(res.state, res.error) << std::endl;
}
std::cout << std::string(80, '-') << std::endl;
}
}
// Example: ./parallel_http_get http://example.com/ http://example.com/notfound
int main(int argc, char *argv[]) {
std::vector<std::string> urls;
for (int i = 1; i < argc; i++)
urls.emplace_back(argv[i]);
coke::sync_wait(parallel_http_get(urls));
return 0;
}