32 static size_t curlWriteFunction(
void *contents,
size_t size,
33 size_t nmemb,
void *results) {
34 size_t handled = size * nmemb;
36 ((
string*)results)->append((
char*)contents, handled);
48 static string makeRequest(
const string& url,
const vector<string>&
49 headers,
const string& data =
"") {
53 curl_global_init(CURL_GLOBAL_ALL);
54 CURL* curl = curl_easy_init();
56 char error_buffer[CURL_ERROR_SIZE];
60 res = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
62 throw "curl_easy_setopt failed";
65 res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error_buffer);
67 throw "curl_easy_setopt failed";
70 res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
72 throw "curl_easy_setopt failed";
74 res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &results);
76 throw "curl_easy_setopt failed";
78 res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
80 throw "curl_easy_setopt failed";
82 res = curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
84 throw "curl_easy_setopt failed";
85 if (data.length() > 0) {
87 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
89 throw "curl_easy_setopt failed";
91 res = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
93 throw "curl_easy_setopt failed";
95 res = curl_easy_setopt(curl, CURLOPT_POST, 1L);
97 throw "curl_easy_setopt failed";
100 struct curl_slist* curlHeaders =
nullptr;
101 for (
const string& header : headers) {
102 curlHeaders = curl_slist_append(curlHeaders, header.c_str());
104 res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaders);
106 throw "curl_easy_setopt failed";
109 res = curl_easy_perform(curl);
111 if (res != CURLE_OK) {
112 throw "curl_easy_perform() failed.\nCurl Error Code " 113 + to_string(res) +
"\n" + curl_easy_strerror(res) +
115 + error_buffer +
"\nPossibly Bad BRIDGES Credentials\n";
117 curl_slist_free_all(curlHeaders);
118 curl_easy_cleanup(curl);
121 throw "curl_easy_init() failed!\nNothing retrieved from server.\n";
124 curl_global_cleanup();
128 static std::string encodeURLPart (
const std::string& s) {
129 std::string returnstr;
131 curl_global_init(CURL_GLOBAL_ALL);
132 CURL* curl = curl_easy_init();
134 char* encodedstr = curl_easy_escape (curl, s.c_str(), 0);
135 returnstr = encodedstr;
137 curl_free(encodedstr);
139 curl_easy_cleanup(curl);
140 curl_global_cleanup();
This is a class for handling calls to the BRIDGES server to transmit JSON to the server and subsequen...
Definition: ServerComm.h:22
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:39
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:59