72 const std::string& url,
74 const std::string& requestBody,
75 const std::map<std::string, std::string>& headers,
82 CURL* curl = curl_easy_init();
84 resp.
error =
"curl_easy_init() failed";
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())
94 merged[
"User-Agent"] =
"BastionGuard/IdentityLeak (GTKMM4)";
97 if (merged.find(
"Accept") == merged.end() &&
98 merged.find(
"accept") == merged.end())
100 merged[
"Accept"] =
"application/json";
103 headerList = buildHeaderList(merged);
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);
109 curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeoutSeconds);
110 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
112 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
113 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
115 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
116 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
119 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerList);
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());
127 curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
130 CURLcode code = curl_easy_perform(curl);
132 if (code != CURLE_OK) {
135 resp.
error = curl_easy_strerror(code);
138 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
139 resp.
status = (int)httpCode;
140 resp.
body = std::move(responseBody);
144 if (headerList) curl_slist_free_all(headerList);
145 curl_easy_cleanup(curl);
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)