Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
http_download.hpp
Go to the documentation of this file.
1#pragma once
3#include <cstdint>
4
5#ifdef __clang__
6#pragma clang diagnostic push
7// -Wdeprecated-literal-operator is only available in Clang 18+, ignore unknown warnings for Apple Clang
8#pragma clang diagnostic ignored "-Wunknown-warning-option"
9#pragma clang diagnostic ignored "-Wdeprecated-literal-operator"
10#pragma clang diagnostic ignored "-Wunused-parameter"
11#pragma clang diagnostic ignored "-Wsign-conversion"
12#endif
13#ifdef __GNUC__
14#pragma GCC diagnostic push
15#pragma GCC diagnostic ignored "-Wunused-parameter"
16#endif
17#ifndef __wasm__
18#include <httplib.h>
19#endif
20#ifdef __GNUC__
21#pragma GCC diagnostic pop
22#endif
23#ifdef __clang__
24#pragma clang diagnostic pop
25#endif
26
27#include <string>
28#include <vector>
29
30namespace bb::srs {
31
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)
42{
43#ifdef __wasm__
44 throw_or_abort("HTTP download not supported in WASM");
45#else
46 // Parse URL into host and path
47 size_t proto_end = url.find("://");
48 if (proto_end == std::string::npos) {
49 throw_or_abort("Invalid URL format: " + url);
50 }
51
52 size_t host_start = proto_end + 3;
53 size_t path_start = url.find('/', host_start);
54 if (path_start == std::string::npos) {
55 throw_or_abort("Invalid URL format: " + url);
56 }
57
58 std::string host = url.substr(host_start, path_start - host_start);
59 std::string path = url.substr(path_start);
60
61 // Create HTTP client (non-SSL)
62 httplib::Client cli("http://" + host);
63 cli.set_follow_location(true);
64 cli.set_connection_timeout(30);
65 cli.set_read_timeout(60);
66
67 // Prepare headers
68 httplib::Headers headers;
69 if (end_byte > 0 && end_byte >= start_byte) {
70 headers.emplace("Range", "bytes=" + std::to_string(start_byte) + "-" + std::to_string(end_byte));
71 }
72
73 // Download
74 auto res = cli.Get(path.c_str(), headers);
75
76 if (!res) {
77 throw_or_abort("HTTP request failed for " + url + ": " + httplib::to_string(res.error()));
78 }
79
80 if (res->status != 200 && res->status != 206) {
81 throw_or_abort("HTTP request failed for " + url + " with status " + std::to_string(res->status));
82 }
83
84 // Convert string body to vector<uint8_t>
85 const std::string& body = res->body;
86 return std::vector<uint8_t>(body.begin(), body.end());
87#endif
88}
89} // namespace bb::srs
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.
std::string to_string(bb::avm2::ValueTag tag)
void throw_or_abort(std::string const &err)