28 : url(url), httpcode(httpcode), headers(headers), data(data) {
29 what_str = std::string(
"HTTPException raised when hitting ") + url +
"\n" +
30 "HTTP code: " + to_string(httpcode) +
"\n" +
35 virtual const char*
what() const noexcept {
36 return what_str.c_str();
55 static size_t curlWriteFunction(
void *contents,
size_t size,
56 size_t nmemb,
void *results) {
57 size_t handled = size * nmemb;
59 ((
string*)results)->append((
char*)contents, handled);
71 static string makeRequest(
const string& url,
const vector<string>&
72 headers,
const string& data =
"") {
74 string returned_headers;
77 curl_global_init(CURL_GLOBAL_ALL);
78 CURL* curl = curl_easy_init();
80 char error_buffer[CURL_ERROR_SIZE];
84 res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
86 throw "curl_easy_setopt failed";
89 res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
91 throw "curl_easy_setopt failed";
94 res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
96 throw "curl_easy_setopt failed";
98 res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
100 throw "curl_easy_setopt failed";
102 res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
104 throw "curl_easy_setopt failed";
106 res = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curlWriteFunction);
108 res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, &returned_headers);
110 throw "curl_easy_setopt failed";
121 if (data.length() > 0) {
123 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
125 throw "curl_easy_setopt failed";
127 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
129 throw "curl_easy_setopt failed";
131 res = curl_easy_setopt(curl, CURLOPT_POST, 1L);
133 throw "curl_easy_setopt failed";
136 struct curl_slist * curlHeaders =
nullptr;
137 for (
const string& header : headers) {
138 curlHeaders = curl_slist_append(curlHeaders, header.c_str());
140 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaders);
142 throw "curl_easy_setopt failed";
145 res = curl_easy_perform(curl);
147 curl_slist_free_all(curlHeaders);
149 if (res != CURLE_OK) {
151 string footer = string(
"Root cause: ") + string(
"curl_easy_perform() failed.\n")
152 +
"Curl Error Code " + to_string(res) +
"\n" + curl_easy_strerror(res) +
"\n"
153 +
"ErrorBuffer: " + error_buffer +
"\n"
154 +
"Headers: " + returned_headers +
"\n"
155 +
"Results: " + results +
"\n";
159 if (res == CURLE_OK) {
161 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
163 if (httpcode >= 300) {
164 throw HTTPException(url, httpcode, returned_headers, results);
168 curl_easy_cleanup(curl);
171 throw "curl_easy_init() failed!\nNothing retrieved from server.\n";
174 curl_global_cleanup();
178 static std::string encodeURLPart (
const std::string& s) {
179 std::string returnstr;
181 curl_global_init(CURL_GLOBAL_ALL);
182 CURL* curl = curl_easy_init();
184 char* encodedstr = curl_easy_escape (curl,
s.c_str(), 0);
185 returnstr = encodedstr;
187 curl_free(encodedstr);
189 curl_easy_cleanup(curl);
190 curl_global_cleanup();
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:50
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:64
This is a class for handling calls to the BRIDGES server to transmit JSON to the server and subsequen...
Definition: ServerComm.h:45
Support for drawing Bar charts.
Definition: alltypes.h:4
Definition: ServerComm.h:17
virtual const char * what() const noexcept
Definition: ServerComm.h:35
std::string url
Definition: ServerComm.h:18
HTTPException(std::string url, long httpcode, std::string headers, std::string data)
Definition: ServerComm.h:24
std::string what_str
Definition: ServerComm.h:23
std::string data
Definition: ServerComm.h:21
std::string headers
Definition: ServerComm.h:20
long httpcode
Definition: ServerComm.h:19