39inline std::vector<uint8_t>
http_download([[maybe_unused]]
const std::string& url,
40 [[maybe_unused]]
size_t start_byte = 0,
41 [[maybe_unused]]
size_t end_byte = 0)
47 size_t proto_end = url.find(
"://");
48 if (proto_end == std::string::npos) {
52 size_t host_start = proto_end + 3;
53 size_t path_start = url.find(
'/', host_start);
54 if (path_start == std::string::npos) {
58 std::string host = url.substr(host_start, path_start - host_start);
59 std::string path = url.substr(path_start);
62 httplib::Client cli(
"http://" + host);
63 cli.set_follow_location(
true);
64 cli.set_connection_timeout(30);
65 cli.set_read_timeout(60);
68 httplib::Headers headers;
69 if (end_byte > 0 && end_byte >= start_byte) {
74 auto res = cli.Get(path.c_str(), headers);
77 throw_or_abort(
"HTTP request failed for " + url +
": " + httplib::to_string(res.error()));
80 if (res->status != 200 && res->status != 206) {
85 const std::string& body = res->body;
86 return std::vector<uint8_t>(body.begin(), body.end());
std::vector< uint8_t > http_download(const std::string &url, size_t start_byte=0, size_t end_byte=0)
Download data from a URL with optional Range header support.