Skip to content
API Documentation
BastionGuard™
HttpClient Class Reference

#include <HttpClient.hpp>

Static Public Member Functions

static HttpResponse get (const std::string &url, const std::map< std::string, std::string > &headers={}, long timeoutSeconds=20)
static HttpResponse post (const std::string &url, const std::string &body, const std::map< std::string, std::string > &headers={}, long timeoutSeconds=20)
static std::string urlEncode (const std::string &value)

Static Private Member Functions

static void ensureCurlGlobalInit ()
static HttpResponse perform (const std::string &url, const char *method, const std::string &requestBody, const std::map< std::string, std::string > &headers, long timeoutSeconds)

Detailed Description

Definition at line 32 of file HttpClient.hpp.

Member Function Documentation

◆ ensureCurlGlobalInit()

void HttpClient::ensureCurlGlobalInit ( )
staticprivate

Definition at line 48 of file HttpClient.cpp.

48 {
49 std::call_once(g_curlInitOnce, []() {
50 curl_global_init(CURL_GLOBAL_DEFAULT);
51 });
52}

Referenced by perform(), and urlEncode().

◆ get()

HttpResponse HttpClient::get ( const std::string & url,
const std::map< std::string, std::string > & headers = {},
long timeoutSeconds = 20 )
static

Definition at line 54 of file HttpClient.cpp.

58{
59 return perform(url, "GET", "", headers, timeoutSeconds);
60}
static HttpResponse perform(const std::string &url, const char *method, const std::string &requestBody, const std::map< std::string, std::string > &headers, long timeoutSeconds)

References perform().

Referenced by HibpProvider::checkEmail(), LeakCheckProvider::checkEmail(), and HibpPasswordProvider::checkPassword().

◆ perform()

HttpResponse HttpClient::perform ( const std::string & url,
const char * method,
const std::string & requestBody,
const std::map< std::string, std::string > & headers,
long timeoutSeconds )
staticprivate

Definition at line 71 of file HttpClient.cpp.

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}
static void ensureCurlGlobalInit()
curl_slist * buildHeaderList(const std::map< std::string, std::string > &headers)
std::string error
std::string body

References HttpResponse::body, ensureCurlGlobalInit(), HttpResponse::error, and HttpResponse::status.

Referenced by get(), and post().

◆ post()

HttpResponse HttpClient::post ( const std::string & url,
const std::string & body,
const std::map< std::string, std::string > & headers = {},
long timeoutSeconds = 20 )
static

Definition at line 62 of file HttpClient.cpp.

67{
68 return perform(url, "POST", body, headers, timeoutSeconds);
69}

References perform().

◆ urlEncode()

std::string HttpClient::urlEncode ( const std::string & value)
static

Definition at line 150 of file HttpClient.cpp.

150 {
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}

References ensureCurlGlobalInit().

Referenced by HibpProvider::checkEmail(), and LeakCheckProvider::checkEmail().


The documentation for this class was generated from the following files: