36inline std::vector<uint8_t>
http_download([[maybe_unused]]
const std::string& url,
37 [[maybe_unused]]
size_t start_byte = 0,
38 [[maybe_unused]]
size_t end_byte = 0)
44 size_t proto_end = url.find(
"://");
45 if (proto_end == std::string::npos) {
49 size_t host_start = proto_end + 3;
50 size_t path_start = url.find(
'/', host_start);
51 if (path_start == std::string::npos) {
55 std::string host = url.substr(host_start, path_start - host_start);
56 std::string path = url.substr(path_start);
59 httplib::Client cli((
"http://" + host).c_str());
60 cli.set_follow_location(
true);
61 cli.set_connection_timeout(30);
62 cli.set_read_timeout(60);
65 httplib::Headers headers;
66 if (end_byte > 0 && end_byte >= start_byte) {
71 auto res = cli.Get(path.c_str(), headers);
74 throw_or_abort(
"HTTP request failed for " + url +
": " + httplib::to_string(res.error()));
77 if (res->status != 200 && res->status != 206) {
82 const std::string& body = res->body;
83 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.