38inline std::vector<uint8_t>
http_download([[maybe_unused]]
const std::string& url,
39 [[maybe_unused]]
size_t start_byte = 0,
40 [[maybe_unused]]
size_t end_byte = 0)
46 size_t proto_end = url.find(
"://");
47 if (proto_end == std::string::npos) {
51 size_t host_start = proto_end + 3;
52 size_t path_start = url.find(
'/', host_start);
53 if (path_start == std::string::npos) {
57 std::string host = url.substr(host_start, path_start - host_start);
58 std::string path = url.substr(path_start);
61 httplib::Client cli(
"http://" + host);
62 cli.set_follow_location(
true);
63 cli.set_connection_timeout(30);
64 cli.set_read_timeout(60);
67 httplib::Headers headers;
68 if (end_byte > 0 && end_byte >= start_byte) {
73 auto res = cli.Get(path.c_str(), headers);
76 throw_or_abort(
"HTTP request failed for " + url +
": " + httplib::to_string(res.error()));
79 if (res->status != 200 && res->status != 206) {
84 const std::string& body = res->body;
85 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.