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