File size: 4,738 Bytes
ef28ed2 070294b ef28ed2 d78e3fd ef28ed2 d78e3fd 070294b ef28ed2 070294b 6023bcd 070294b ef28ed2 070294b d78e3fd ef28ed2 070294b ef28ed2 070294b ef28ed2 6567a38 d78e3fd 070294b 6023bcd 070294b ef28ed2 070294b d78e3fd ef28ed2 d78e3fd ef28ed2 070294b 6023bcd 070294b d78e3fd 070294b ef28ed2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | #pragma once
#include "net/http_cache.hpp"
#include "privacy/shield.hpp"
#include <atomic>
#include <cstdint>
#include <functional>
#include <map>
#include <mutex>
#include <string>
#include <vector>
namespace astra {
struct HttpResponse {
int status_code = 0;
std::string final_url;
std::string content_type;
std::map<std::string, std::string> headers;
std::string body;
std::string error;
std::string tls_version; // e.g. "TLSv1.3"
std::string alpn_protocol; // e.g. "h2" or "http/1.1"
bool blocked_by_privacy = false;
bool blocked_by_extension = false;
bool from_cache = false;
bool tls_verified = false;
bool used_http2 = false;
bool multiplexed_h2 = false;
bool reused_connection = false;
PrivacyDecision privacy_decision;
};
struct DnrHookResult {
bool block = false;
std::string redirect_url;
std::vector<std::pair<std::string, std::string>> add_headers;
std::string rule_source;
};
// AstraNet v4: Native Binary HTTP/2 (RFC 7540 + HPACK/Huffman) + HTTP/1.1 Keep-Alive Pool
// + Strict-Verified TLS 1.3 + ALPN (h2, http/1.1 via dlopen libssl.so.3)
// + gzip/deflate (dlopen libz.so.1) + bounded RAM/Disk HttpCache + AstraShield + DNR
// + bounded multi-threaded parallel subresource batch fetcher.
class HttpClient {
public:
explicit HttpClient(AstraShield& shield, HttpCache* cache = nullptr);
~HttpClient();
using DnrCallback = std::function<DnrHookResult(const std::string& url, const std::string& top_site)>;
void set_dnr_hook(DnrCallback cb) { dnr_hook_ = std::move(cb); }
void set_strict_tls(bool enabled) { strict_tls_ = enabled; }
void set_http2_enabled(bool enabled) { http2_enabled_ = enabled; }
void set_cache(HttpCache* cache) { cache_ = cache; }
HttpResponse get(const std::string& url,
const std::string& top_level_site = "",
bool is_top_level_navigation = true,
int max_redirects = 5);
HttpResponse request(const std::string& method,
const std::string& url,
const std::string& body = "",
const std::vector<std::pair<std::string, std::string>>& extra_headers = {},
const std::string& content_type = "application/json",
const std::string& top_level_site = "");
HttpResponse post(const std::string& url,
const std::string& body,
const std::string& content_type = "application/json",
const std::string& top_level_site = "");
// Fetch multiple subresources concurrently using a bounded worker pool (default max 4 threads).
std::vector<HttpResponse> fetch_batch(const std::vector<std::string>& urls,
const std::string& top_level_site = "",
size_t max_threads = 4);
size_t http2_requests() const { return http2_requests_.load(); }
size_t http2_multiplexed_streams() const { return http2_multiplexed_streams_.load(); }
size_t keepalive_reuses() const { return keepalive_reuses_.load(); }
static bool decompress_gzip_or_zlib(const uint8_t* data, size_t len, std::string& out);
static bool tls_available();
static std::string resolve_relative_url(const std::string& base_url, const std::string& rel);
private:
struct PooledConn {
std::string key; // host:port:tls
int fd = -1;
void* ctx = nullptr;
void* ssl = nullptr;
std::string tls_version;
std::string alpn;
bool tls_verified = false;
int64_t last_used_sec = 0;
};
bool take_pooled(const std::string& key, PooledConn& out);
void return_pooled(PooledConn&& conn);
void close_conn(PooledConn& conn);
HttpResponse perform_single_request(const std::string& method,
const ParsedUrl& parsed,
const std::string& top_level_site,
const std::vector<std::pair<std::string, std::string>>& extra_headers,
const std::string& req_body,
const std::string& req_content_type);
AstraShield& shield_;
HttpCache* cache_ = nullptr;
DnrCallback dnr_hook_;
bool strict_tls_ = true;
bool http2_enabled_ = true;
std::atomic<size_t> http2_requests_{0};
std::atomic<size_t> http2_multiplexed_streams_{0};
std::atomic<size_t> keepalive_reuses_{0};
mutable std::mutex state_mu_;
std::mutex pool_mu_;
std::vector<PooledConn> conn_pool_;
};
} // namespace astra
|