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#endif
12#ifdef __GNUC__
13#pragma GCC diagnostic push
14#pragma GCC diagnostic ignored "-Wunused-parameter"
15#endif
16#ifndef __wasm__
17#include <httplib.h>
18#endif
19#ifdef __GNUC__
20#pragma GCC diagnostic pop
21#endif
22#ifdef __clang__
23#pragma clang diagnostic pop
24#endif
25
26#include <string>
27#include <vector>
28
29namespace bb::srs {
30
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)
41{
42#ifdef __wasm__
43 throw_or_abort("HTTP download not supported in WASM");
44#else
45 // Parse URL into host and path
46 size_t proto_end = url.find("://");
47 if (proto_end == std::string::npos) {
48 throw_or_abort("Invalid URL format: " + url);
49 }
50
51 size_t host_start = proto_end + 3;
52 size_t path_start = url.find('/', host_start);
53 if (path_start == std::string::npos) {
54 throw_or_abort("Invalid URL format: " + url);
55 }
56
57 std::string host = url.substr(host_start, path_start - host_start);
58 std::string path = url.substr(path_start);
59
60 // Create HTTP client (non-SSL)
61 httplib::Client cli("http://" + host);
62 cli.set_follow_location(true);
63 cli.set_connection_timeout(30);
64 cli.set_read_timeout(60);
65
66 // Prepare headers
67 httplib::Headers headers;
68 if (end_byte > 0 && end_byte >= start_byte) {
69 headers.emplace("Range", "bytes=" + std::to_string(start_byte) + "-" + std::to_string(end_byte));
70 }
71
72 // Download
73 auto res = cli.Get(path.c_str(), headers);
74
75 if (!res) {
76 throw_or_abort("HTTP request failed for " + url + ": " + httplib::to_string(res.error()));
77 }
78
79 if (res->status != 200 && res->status != 206) {
80 throw_or_abort("HTTP request failed for " + url + " with status " + std::to_string(res->status));
81 }
82
83 // Convert string body to vector<uint8_t>
84 const std::string& body = res->body;
85 return std::vector<uint8_t>(body.begin(), body.end());
86#endif
87}
88} // 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)