7 #include "nlohmann/json.hpp"
9 #include <boost/uuid/uuid.hpp>
10 #include <boost/uuid/uuid_generators.hpp>
11 #include <boost/uuid/uuid_io.hpp>
12 #include <boost/asio.hpp>
24 curl_global_init(CURL_GLOBAL_ALL);
30 curl_global_cleanup();
60 size_t total_size =
size * nmemb;
67 size_t newLength =
size * nmemb;
73 catch (std::bad_alloc &
e)
75 std::cout <<
"Not enough memory to allocate "<<newLength<<
" bytes "<< std::endl;
90 input->erase(0, toCopy);
97 const char *method_name,
const std::string& header_params)
104 std::string sanitisedPath(current_path);
105 auto caretPosition = sanitisedPath.find(
'^');
106 if (caretPosition != std::string::npos)
107 sanitisedPath.replace(caretPosition, 1,
"%5E");
109 curl = curl_easy_init();
111 struct curl_slist *
headers = NULL;
116 std::cout <<
"cURL request to " <<
url << std::endl;
121 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
127 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
131 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"DELETE");
132 if (header_params !=
"")
136 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
143 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"PUT");
145 headers = curl_slist_append(
headers,
"Content-Type: application/json");
147 if (header_params !=
"")
151 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
152 curl_easy_setopt(curl, CURLOPT_POST, 1L);
155 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, stt.c_str());
159 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
s);
162 res = curl_easy_perform(curl);
166 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
169 curl_easy_cleanup(curl);
172 curl_global_cleanup();
180 throw CrestException(std::string(
"ERROR in ") + std::string(method_name) +
" | CURL not init");
189 curl_global_init(CURL_GLOBAL_DEFAULT);
190 curl = curl_easy_init();
197 std::string content_type =
"application/octet-stream";
202 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
210 std::cerr <<
"SOCKS_PROXY environment variable not found." << std::endl;
216 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
219 std::vector<char> response_data;
220 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
WriteCallback);
221 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
223 res = curl_easy_perform(curl);
227 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
229 curl_easy_cleanup(curl);
232 const char *method_name =
"CrestRequest::getPayloadRequest";
235 return response_data;
237 throw CrestException(std::string(
"ERROR in ") +
"getPayloadRequest CURL not init");
242 const std::string&
objectType,
const std::string& compressionType,
const std::string&
version,
243 const std::vector<std::string> &
files)
248 curl_global_init(CURL_GLOBAL_DEFAULT);
249 curl = curl_easy_init();
251 struct curl_slist *
headers = NULL;
258 std::string content_type =
"application/octet-stream";
263 curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
273 curl_easy_setopt(curl, CURLOPT_URL,
url.c_str());
274 std::string js_str = js.dump();
279 std::string
format =
"X-Crest-PayloadFormat: ";
280 if (
files.size() > 0)
282 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"POST");
284 content_type =
"application/form-data";
288 curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
"PUT");
293 headers = curl_slist_append(
headers,
"Content-Type: multipart/form-data");
296 #if LIBCURL_VERSION_MAJOR < 8
297 struct curl_httppost *formpost = NULL;
298 struct curl_httppost *lastptr = NULL;
299 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"tag", CURLFORM_COPYCONTENTS,
tag.c_str(), CURLFORM_END);
300 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"endtime", CURLFORM_COPYCONTENTS,
std::to_string(endtime).c_str(), CURLFORM_END);
303 curl_formadd(&formpost, &lastptr,
304 CURLFORM_COPYNAME,
"storeset",
305 CURLFORM_BUFFER,
"json_data",
306 CURLFORM_BUFFERPTR, js_str.c_str(),
307 CURLFORM_BUFFERLENGTH, js_str.size(),
308 CURLFORM_CONTENTTYPE, content_type.c_str(),
311 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"version", CURLFORM_COPYCONTENTS,
version.c_str(), CURLFORM_END);
312 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"objectType", CURLFORM_COPYCONTENTS,
objectType.c_str(), CURLFORM_END);
313 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"compressionType", CURLFORM_COPYCONTENTS, compressionType.c_str(), CURLFORM_END);
317 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME,
"files", CURLFORM_FILE,
file.c_str(), CURLFORM_END);
319 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
323 curl_mime *mime = curl_mime_init(curl);
325 curl_mimepart *
part = curl_mime_addpart(mime);
326 curl_mime_name(
part,
"tag");
327 curl_mime_data(
part,
tag.c_str(),
tag.size());
329 part = curl_mime_addpart(mime);
331 curl_mime_name(
part,
"endtime");
332 curl_mime_data(
part, endtime_s.c_str(), endtime_s.size());
334 part = curl_mime_addpart(mime);
335 curl_mime_name(
part,
"version");
338 part = curl_mime_addpart(mime);
339 curl_mime_name(
part,
"objectType");
342 part = curl_mime_addpart(mime);
343 curl_mime_name(
part,
"compressionType");
344 curl_mime_data(
part, compressionType.c_str(), compressionType.size());
346 part = curl_mime_addpart(mime);
347 curl_mime_name(
part,
"storeset");
348 curl_mime_type(
part, content_type.c_str());
349 curl_mime_data(
part, js.dump().c_str(), js.dump().size());
353 curl_mimepart *
part = curl_mime_addpart(mime);
354 curl_mime_name(
part,
"files");
355 curl_mime_filedata(
part,
file.c_str());
358 curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
360 curl_easy_setopt(curl, CURLOPT_HTTPHEADER,
headers);
363 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &
response);
366 CURLcode
res = curl_easy_perform(curl);
370 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
371 const char *method_name =
"CrestRequest::uploadPayload";
374 curl_easy_cleanup(curl);
375 #if LIBCURL_VERSION_MAJOR < 8
376 curl_formfree(formpost);
378 curl_mime_free(mime);
381 curl_global_cleanup();
388 std::string mes =
"ERROR in CrestRequest::uploadPayload";
399 std::string plainString =
"";
402 for (
long unsigned int i = 0;
i < xmlBuffer.length();
i++)
404 char convertc = xmlBuffer[
i];
408 else if (convertc ==
'>')
415 plainString += convertc;
423 std::string str2 =
str;
427 while ((
pos = str2.find(needle)) != str2.npos)
441 catch (nlohmann::json::parse_error &
e)
448 const char *method_name)
454 std::string mes =
"ERROR in ";
470 if (response_code >= 400 || response_code == 303)
474 std::string mes =
"ERROR in ";
488 catch (nlohmann::json::parse_error &
e)
494 std::string
wh =
e.what();
514 auto res = js.find(
"type");
520 std::string
type = js.value(
"type",
" unknown type ");
521 if (
type ==
"error" ||
type ==
"info")
523 std::string
message = js.value(
"message",
" unknown message ");
530 auto r1 = js.find(
"error");
531 auto r2 = js.find(
"message");
535 std::string
error = js.value(
"error",
" unknown error ");
538 std::string
message = js.value(
"message",
"");