34 static size_t curlWriteFunction(
void *contents,
size_t size,
35 size_t nmemb,
void *results) {
36 size_t handled = size * nmemb;
38 ((
string*)results)->append((
char*)contents, handled);
50 static string makeRequest(
const string& url,
const vector<string>&
51 headers,
const string& data =
"") {
53 string returned_headers;
56 curl_global_init(CURL_GLOBAL_ALL);
57 CURL* curl = curl_easy_init();
59 char error_buffer[CURL_ERROR_SIZE];
63 res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
65 throw "curl_easy_setopt failed";
68 res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
70 throw "curl_easy_setopt failed";
73 res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
75 throw "curl_easy_setopt failed";
77 res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
79 throw "curl_easy_setopt failed";
81 res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
83 throw "curl_easy_setopt failed";
85 res = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, curlWriteFunction);
87 res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, &returned_headers);
89 throw "curl_easy_setopt failed";
100 if (data.length() > 0) {
102 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
104 throw "curl_easy_setopt failed";
106 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
108 throw "curl_easy_setopt failed";
110 res = curl_easy_setopt(curl, CURLOPT_POST, 1L);
112 throw "curl_easy_setopt failed";
115 struct curl_slist * curlHeaders =
nullptr;
116 for (
const string& header : headers) {
117 curlHeaders = curl_slist_append(curlHeaders, header.c_str());
119 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaders);
121 throw "curl_easy_setopt failed";
124 res = curl_easy_perform(curl);
126 curl_slist_free_all(curlHeaders);
128 if (res != CURLE_OK) {
130 string footer = string(
"Root cause: ") + string(
"curl_easy_perform() failed.\n")
131 +
"Curl Error Code " + to_string(res) +
"\n" + curl_easy_strerror(res) +
"\n"
132 +
"ErrorBuffer: " + error_buffer +
"\n"
133 +
"Headers: " + returned_headers +
"\n"
134 +
"Results: " + results +
"\n";
138 if (res == CURLE_OK) {
140 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpcode);
142 if (httpcode >= 300) {
143 throw HTTPException(url, httpcode, returned_headers, results);
147 curl_easy_cleanup(curl);
150 throw "curl_easy_init() failed!\nNothing retrieved from server.\n";
153 curl_global_cleanup();
157 static std::string encodeURLPart (
const std::string& s) {
158 std::string returnstr;
160 curl_global_init(CURL_GLOBAL_ALL);
161 CURL* curl = curl_easy_init();
163 char* encodedstr = curl_easy_escape (curl,
s.c_str(), 0);
164 returnstr = encodedstr;
166 curl_free(encodedstr);
168 curl_easy_cleanup(curl);
169 curl_global_cleanup();
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:51
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:69
This is a class for handling calls to the BRIDGES server to transmit JSON to the server and subsequen...
Definition: ServerComm.h:24
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4