Skip to content
API Documentation
BastionGuard™
HttpClient.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
21#include "HttpClient.hpp"
22
23#include <curl/curl.h>
24#include <mutex>
25#include <sstream>
26
27namespace {
28 std::once_flag g_curlInitOnce;
29
30 size_t writeCallback(char* ptr, size_t size, size_t nmemb, void* userdata) {
31 if (!userdata) return 0;
32 const size_t total = size * nmemb;
33 auto* out = static_cast<std::string*>(userdata);
34 out->append(ptr, total);
35 return total;
36 }
37
38 curl_slist* buildHeaderList(const std::map<std::string, std::string>& headers) {
39 curl_slist* list = nullptr;
40 for (const auto& kv : headers) {
41 std::string h = kv.first + ": " + kv.second;
42 list = curl_slist_append(list, h.c_str());
43 }
44 return list;
45 }
46}
47
49 std::call_once(g_curlInitOnce, []() {
50 curl_global_init(CURL_GLOBAL_DEFAULT);
51 });
52}
53
55 const std::string& url,
56 const std::map<std::string, std::string>& headers,
57 long timeoutSeconds)
58{
59 return perform(url, "GET", "", headers, timeoutSeconds);
60}
61
63 const std::string& url,
64 const std::string& body,
65 const std::map<std::string, std::string>& headers,
66 long timeoutSeconds)
67{
68 return perform(url, "POST", body, headers, timeoutSeconds);
69}
70
72 const std::string& url,
73 const char* method,
74 const std::string& requestBody,
75 const std::map<std::string, std::string>& headers,
76 long timeoutSeconds)
77{
79
80 HttpResponse resp;
81
82 CURL* curl = curl_easy_init();
83 if (!curl) {
84 resp.error = "curl_easy_init() failed";
85 return resp;
86 }
87
88 std::string responseBody;
89 curl_slist* headerList = nullptr;
90 std::map<std::string, std::string> merged = headers;
91 if (merged.find("User-Agent") == merged.end() &&
92 merged.find("user-agent") == merged.end())
93 {
94 merged["User-Agent"] = "BastionGuard/IdentityLeak (GTKMM4)";
95 }
96
97 if (merged.find("Accept") == merged.end() &&
98 merged.find("accept") == merged.end())
99 {
100 merged["Accept"] = "application/json";
101 }
102
103 headerList = buildHeaderList(merged);
104
105 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
106 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
107 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5L);
108
109 curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSeconds);
110 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
111
112 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
113 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
114
115 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
116 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
117
118 if (headerList) {
119 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList);
120 }
121
122 if (std::string(method) == "POST") {
123 curl_easy_setopt(curl, CURLOPT_POST, 1L);
124 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, requestBody.c_str());
125 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)requestBody.size());
126 } else {
127 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
128 }
129
130 CURLcode code = curl_easy_perform(curl);
131
132 if (code != CURLE_OK) {
133 resp.status = 0;
134 resp.body = "";
135 resp.error = curl_easy_strerror(code);
136 } else {
137 long httpCode = 0;
138 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
139 resp.status = (int)httpCode;
140 resp.body = std::move(responseBody);
141 resp.error.clear();
142 }
143
144 if (headerList) curl_slist_free_all(headerList);
145 curl_easy_cleanup(curl);
146
147 return resp;
148}
149
150std::string HttpClient::urlEncode(const std::string& value) {
152
153 CURL* curl = curl_easy_init();
154 if (!curl) return "";
155
156 char* enc = curl_easy_escape(curl, value.c_str(), (int)value.size());
157 std::string out = enc ? enc : "";
158 if (enc) curl_free(enc);
159
160 curl_easy_cleanup(curl);
161 return out;
162}
static HttpResponse get(const std::string &url, const std::map< std::string, std::string > &headers={}, long timeoutSeconds=20)
static void ensureCurlGlobalInit()
static std::string urlEncode(const std::string &value)
static HttpResponse post(const std::string &url, const std::string &body, const std::map< std::string, std::string > &headers={}, long timeoutSeconds=20)
static HttpResponse perform(const std::string &url, const char *method, const std::string &requestBody, const std::map< std::string, std::string > &headers, long timeoutSeconds)
size_t writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata)
curl_slist * buildHeaderList(const std::map< std::string, std::string > &headers)
std::string error
std::string body