8#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
9#define CPPHTTPLIB_OPENSSL_SUPPORT
29 std::string& path)
const {
31 std::regex url_regex(R
"(^(https?):\/\/([^:\/\s]+)(?::(\d+))?(\/.*)?$)");
34 if (!std::regex_match(url, matches, url_regex)) {
35 return absl::InvalidArgumentError(
"Invalid URL format: " + url);
38 scheme = matches[1].str();
39 host = matches[2].str();
42 if (matches[3].matched) {
43 port = std::stoi(matches[3].str());
45 port = (scheme ==
"https") ? 443 : 80;
49 path = matches[4].matched ? matches[4].str() :
"/";
51 return absl::OkStatus();
55 const std::string& scheme,
56 const std::string& host,
60 std::string cache_key = scheme +
"://" + host +
":" + std::to_string(port);
69 std::shared_ptr<httplib::Client> client;
71 if (scheme ==
"https") {
72#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
73 client = std::make_shared<httplib::Client>(host, port);
74 client->enable_server_certificate_verification(
false);
76 return absl::UnimplementedError(
77 "HTTPS not supported: OpenSSL support not compiled in");
79 }
else if (scheme ==
"http") {
80 client = std::make_shared<httplib::Client>(host, port);
82 return absl::InvalidArgumentError(
"Unsupported URL scheme: " + scheme);
86 return absl::InternalError(
"Failed to create HTTP client");
103 if (httplib_headers) {
104 const auto& headers = *
static_cast<const httplib::Headers*
>(httplib_headers);
105 for (
const auto& header : headers) {
106 result[header.first] = header.second;
114 const std::string& url,
117 std::string scheme, host, path;
126 httplib::Headers httplib_headers;
127 for (
const auto& [key, value] : headers) {
128 httplib_headers.emplace(key, value);
132 auto res = client->Get(path.c_str(), httplib_headers);
135 return absl::UnavailableError(
"HTTP GET request failed: " + url);
140 response.
body = res->body;
147 const std::string& url,
148 const std::string& body,
151 std::string scheme, host, path;
160 httplib::Headers httplib_headers;
161 for (
const auto& [key, value] : headers) {
162 httplib_headers.emplace(key, value);
166 if (httplib_headers.find(
"Content-Type") == httplib_headers.end()) {
167 httplib_headers.emplace(
"Content-Type",
"application/json");
171 auto res = client->Post(path.c_str(), httplib_headers, body,
172 httplib_headers.find(
"Content-Type")->second);
175 return absl::UnavailableError(
"HTTP POST request failed: " + url);
180 response.
body = res->body;
187 const std::string& url,
188 const std::string& body,
191 std::string scheme, host, path;
200 httplib::Headers httplib_headers;
201 for (
const auto& [key, value] : headers) {
202 httplib_headers.emplace(key, value);
206 if (httplib_headers.find(
"Content-Type") == httplib_headers.end()) {
207 httplib_headers.emplace(
"Content-Type",
"application/json");
211 auto res = client->Put(path.c_str(), httplib_headers, body,
212 httplib_headers.find(
"Content-Type")->second);
215 return absl::UnavailableError(
"HTTP PUT request failed: " + url);
220 response.
body = res->body;
227 const std::string& url,
230 std::string scheme, host, path;
239 httplib::Headers httplib_headers;
240 for (
const auto& [key, value] : headers) {
241 httplib_headers.emplace(key, value);
245 auto res = client->Delete(path.c_str(), httplib_headers);
248 return absl::UnavailableError(
"HTTP DELETE request failed: " + url);
253 response.
body = res->body;
absl::StatusOr< HttpResponse > Get(const std::string &url, const Headers &headers={}) override
Perform an HTTP GET request.
absl::StatusOr< std::shared_ptr< httplib::Client > > GetOrCreateClient(const std::string &scheme, const std::string &host, int port)
Create or get cached httplib client for a host.
void SetTimeout(int timeout_seconds) override
Set a timeout for HTTP requests.
Headers ConvertHeaders(const void *httplib_headers) const
Convert httplib headers to our Headers type.
absl::StatusOr< HttpResponse > Post(const std::string &url, const std::string &body, const Headers &headers={}) override
Perform an HTTP POST request.
absl::Status ParseUrl(const std::string &url, std::string &scheme, std::string &host, int &port, std::string &path) const
Parse URL into components.
absl::StatusOr< HttpResponse > Delete(const std::string &url, const Headers &headers={}) override
Perform an HTTP DELETE request.
~HttpLibClient() override
std::map< std::string, std::shared_ptr< httplib::Client > > client_cache_
absl::StatusOr< HttpResponse > Put(const std::string &url, const std::string &body, const Headers &headers={}) override
Perform an HTTP PUT request.
virtual void SetTimeout(int timeout_seconds)
Set a timeout for HTTP requests.
#define ASSIGN_OR_RETURN(type_variable_name, expression)
std::map< std::string, std::string > Headers
HTTP headers type definition.
#define RETURN_IF_ERROR(expr)
HTTP response structure containing status, body, and headers.