// Copyright (c) 2026 Juhapekka Piiroinen // All Rights Reserved. // AmoosedWeb project: https://amoosed.com #pragma once #include "AmoosedWeb/Reader.hpp" #include #include #include namespace amoosed { // Synchronous libcurl transport for Linux and other non-Windows targets. // The origin is fixed at construction; redirects and credentials are disabled. // Call initialize during product startup before threads on libcurl < 7.84; // fetch also initializes once as a fallback. Run fetches on a worker thread // and call cancel from another thread. Initialization lasts for the process. class CurlHttpHost final : public HttpHost { public: static bool initialize() noexcept; explicit CurlHttpHost(std::string_view origin, std::string& error); bool ready() const noexcept { return ready_; } void cancel() noexcept override { cancelled_.store(true); } void reset_cancel() noexcept override { cancelled_.store(false); } bool cancelled() const override { return cancelled_.load(); } bool fetch_range(std::string_view relative_path, std::size_t first, std::size_t last, std::size_t max_body, FetchResponse& response, std::string& error) override; bool fetch_suffix(std::string_view relative_path, std::size_t length, std::size_t max_body, FetchResponse& response, std::string& error) override; bool fetch_complete(std::string_view relative_path, std::size_t max_body, FetchResponse& response, std::string& error) override; private: bool fetch(std::string_view relative_path, const std::string& range, std::size_t max_body, FetchResponse& response, std::string& error); std::string origin_; std::atomic_bool cancelled_{false}; bool ready_ = false, loopback_ = false; }; }