-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhttps_fetch.cpp
37 lines (34 loc) · 1.17 KB
/
https_fetch.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
#include <co_async/co_async.hpp>
#include <co_async/std.hpp>
using namespace co_async;
using namespace std::literals;
static Task<Expected<>> amain() {
HTTPConnectionPool pool;
std::vector<Task<Expected<>>> res;
for (std::string path: {"index.html", "style.css", "koru-icon.png", "mtk_2021_200.png"}) {
res.push_back(co_bind([&, path] () -> Task<Expected<>> {
auto conn = co_await co_await pool.connect("https://man7.org");
HTTPRequest req = {
.method = "GET",
.uri = URI::parse("/" + path),
};
debug(), "requesting", req;
auto [res, body] = co_await co_await conn->request_streamed(req, {});
auto content = co_await co_await body.getall();
co_await co_await file_write(make_path("/tmp", path), content);
co_return {};
}));
}
co_await co_await when_all(res);
Pid pid = co_await co_await ProcessBuilder()
.path("display")
.arg("/tmp/koru-icon.png")
.spawn();
co_await co_await wait_process(pid);
co_await co_await co_sleep(1s);
co_return {};
}
int main() {
co_main(amain());
return 0;
}