-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetter.cpp
More file actions
305 lines (234 loc) · 9.93 KB
/
getter.cpp
File metadata and controls
305 lines (234 loc) · 9.93 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <algorithm>
#include <iostream>
#include <fstream>
#include <functional>
#include <thread>
#include <variant>
#include <boost/asio/connect.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/asio/use_future.hpp>
#include <boost/beast/version.hpp>
#include <fmt/core.h>
#include "generic_exception_handling.hpp"
#include "debug_print.hpp"
#include "string_functions.hpp"
#include "getter.hpp"
namespace http = boost::beast::http;
namespace ssl = boost::asio::ssl;
using tcp = boost::asio::ip::tcp;
getter::getter() :
ioc(),
resolver(ioc),
guard(boost::asio::make_work_guard(ioc)),
networking_threads(std::make_unique<std::thread[]>(number_of_threads))
{
for(size_t i = 0; i < number_of_threads; ++i)
networking_threads[i] = std::thread([this](){this->ioc.run();});
}
getter::~getter() {
guard.reset();
for(size_t i = 0; i < number_of_threads; ++i)
networking_threads[i].join();
}
auto get_time_stamp() {
return std::chrono::high_resolution_clock::now();
}
std::unique_ptr<beastly_connection> getter::make_connection(parsed_url uri) {
const auto protocols = {ssl::context::tlsv11, ssl::context::tlsv12, ssl::context::tlsv13};
std::unique_ptr<beastly_connection> network_resources;
if(uri.protocol == "https") {
for(auto p : protocols) {
network_resources = std::make_unique<beastly_connection>(uri, ioc, resolver);
auto ec = network_resources->set_up_ssl(p, ssl::verify_peer);
if(!ec) {
break;
}
else {
network_resources.release();
debug_print(fmt::format("Algorithm ID {}: An error occurred attempting to set up SSL: {}\n", p, ec.message()));
}
}
}
else {
network_resources = std::make_unique<beastly_connection>(uri, ioc, resolver);
}
return network_resources;
}
void getter::coro_download(std::string url,
std::function<void (size_t, size_t)> progress_handler,
std::function<void (const boost::beast::error_code &, size_t, beastly_connection &)> completion_handler,
std::promise<bool> promise,
boost::asio::yield_context yield_ctx) {
boost::beast::error_code ec;
//auto protocols = {ssl::context::tlsv11, ssl::context::tlsv12, ssl::context::tlsv13};
constexpr size_t max_redirects = 5;
auto parsed = parse(url);
if(parsed.host.empty()) {
promise.set_value(false);
return;
}
size_t length_hint = 0;
std::unique_ptr<beastly_connection> network_resources;
network_resources = make_connection(parsed);
if(!network_resources) {
fmt::print("Was not able to set up an SSL connection.\n");
return;
}
parsed_url old;
// Find the file + get a length hint to use later.
for(size_t i = 0; i < max_redirects; ++i) {
debug_print("Attempting to get header, redirect {}\n", i);
http::request<http::string_body> hdr_req {http::verb::head, parsed.total_path, 11};
std::string cleaned_host_name = parsed.host + ":" + parsed.port;
hdr_req.set(http::field::host, cleaned_host_name.c_str());
hdr_req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
network_resources->write_request(hdr_req, yield_ctx[ec]);
if(ec) {
fmt::print("write_request failed: {}\n", ec.message());
return;
}
network_resources->read_header(yield_ctx[ec]);
if(ec) {
fmt::print("read_header failed: {}\n", ec.message());
}
auto status = network_resources->get_status();
old = parsed;
switch(status) {
case http::status::ok: {
fmt::print("\tok\n");
auto s = network_resources->parser().base()["Content-Length"].to_string();
length_hint = s.empty()? beastly_connection::default_download_limit : std::max(beastly_connection::default_download_limit,static_cast<size_t>(std::stoul(s)));
goto try_to_get;
}
case http::status::found: {
parsed = parse(network_resources->parser().base()["Location"].to_string());
if(parsed == old)
goto try_to_get;
continue;
}
case http::status::moved_permanently: {
parsed = parse(network_resources->parser().base()["Location"].to_string());
if(old == parsed)
goto try_to_get;
continue;
}
default: {
fmt::print("Received unexpected HTTP status: {}\n", status);
return;
}
}
}
try_to_get:
network_resources.release();
network_resources = make_connection(parsed);
if(!network_resources) {
fmt::print("Was not able to set up an SSL connection.\n");
return;
}
length_hint = std::max(beastly_connection::default_download_limit, length_hint);
network_resources->set_parser_body_limit(length_hint);
debug_print("Length hint: {}\n", length_hint);
auto print_time_taken_since = [](auto start_time) {
auto end_time = get_time_stamp();
std::chrono::duration<double> dur = end_time - start_time;
fmt::print("Download took {}\n", dur.count());
};
// At this point we've found the true url and (possibly) gotten a size hint. We just need to download the file!
for(size_t i = 0; i < max_redirects; ++i) {
debug_print("Attempting with redirect {}\n", i);
auto const start_time = get_time_stamp();
std::string cleaned_host_name = parsed.host + ":" + parsed.port;
http::request<http::string_body> file_req {http::verb::get, parsed.total_path, 11};
file_req.set(http::field::host, cleaned_host_name.c_str());
file_req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
network_resources->write_request(std::move(file_req), yield_ctx[ec]);
if(ec) {
fmt::print("write_request failed: {}\n", ec.message());
return;
}
size_t completed = 0;
while(1) {
size_t bytes_read = network_resources->async_read_some(yield_ctx[ec]);
if(ec) {
fmt::print("error.\n");
}
if(network_resources->parser_is_done()) {
auto status = network_resources->get_status();
parsed_url old = parsed;
switch(status) {
case http::status::ok: {
completion_handler(ec, bytes_read, *network_resources);
print_time_taken_since(start_time);
promise.set_value(true);
return;
}
case http::status::found: {
parsed = parse(network_resources->parser().base()["Location"].to_string());
if(old == parsed) {
goto final_attempt;
}
goto break_read_loop;
}
case http::status::temporary_redirect: {
parsed = parse(network_resources->parser().base()["Location"].to_string());
goto break_read_loop;
}
case http::status::moved_permanently: {
parsed = parse(network_resources->parser().base()["Location"].to_string());
goto break_read_loop;
}
default: {
fmt::print("Received unexpected HTTP status: {}\n", status);
return;
}
}
}
completed += bytes_read;
progress_handler(completed, length_hint);
}
break_read_loop:;
network_resources.release();
network_resources = make_connection(parsed);
network_resources->set_parser_body_limit(length_hint);
if(!network_resources) {
fmt::print("Was not able to set up an SSL connection.\n");
return;
}
}
final_attempt:
debug_print("Final attempt: {}://{}:{}{}\n", parsed.protocol, parsed.host, parsed.port, parsed.path);
auto const start_time = get_time_stamp();
http::request<http::string_body> file_req {http::verb::get, parsed.total_path, 11};
std::string cleaned_host_name = parsed.host + ":" + parsed.port;
file_req.set(http::field::host, cleaned_host_name.c_str());
file_req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
network_resources->write_request(std::move(file_req), yield_ctx[ec]);
if(ec) {
debug_print("write_request failed: {}\n", ec.message());
return;
}
size_t completed = 0;
while(1) {
size_t bytes_read = network_resources->async_read_some(yield_ctx[ec]);
if(ec) {
fmt::print("error\n");
}
if(network_resources->parser_is_done()) {
completion_handler(ec, bytes_read, *network_resources);
print_time_taken_since(start_time);
promise.set_value(true);
return;
}
completed += bytes_read;
progress_handler(completed, length_hint);
}
}
std::future<bool> getter::get(std::string url,
std::function<void (size_t, size_t)> progress_handler,
std::function<void (const boost::beast::error_code &, size_t, beastly_connection &)> completion_handler) {
std::promise<bool> promise;
auto future = promise.get_future();
boost::asio::spawn(this->ioc,
[this, url, ph=std::move(progress_handler), ch=std::move(completion_handler), promise = std::move(promise)](boost::asio::yield_context yield_ctx) mutable { coro_download(url, std::move(ph), std::move(ch), std::move(promise), yield_ctx);});
return future;
}