From 0a2faccdf1afdfca04248bb79e37c3d8c0a5bce5 Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Sun, 12 Feb 2023 16:48:23 -0700 Subject: [PATCH] Added initial CurlConnection class in anticipation of downloading motor data from ThrustCurve.org --- CMakeLists.txt | 1 + src/Utils/CMakeLists.txt | 6 +++ src/Utils/CurlConnection.cpp | 71 ++++++++++++++++++++++++++++++++++++ src/Utils/CurlConnection.h | 29 +++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 src/Utils/CMakeLists.txt create mode 100644 src/Utils/CurlConnection.cpp create mode 100644 src/Utils/CurlConnection.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d89694d..82212ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ enable_testing() add_subdirectory(src/gui) add_subdirectory(src/sim) +add_subdirectory(src/Utils) #find_package(wxWidgets REQUIRED gl core base) #include(${wxWidgets_USE_FILE}) diff --git a/src/Utils/CMakeLists.txt b/src/Utils/CMakeLists.txt new file mode 100644 index 0000000..880bbe8 --- /dev/null +++ b/src/Utils/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package(CURL) + +add_library(utils + CurlConnection.cpp) + +target_link_libraries(utils curl jsoncpp ssl crypto) diff --git a/src/Utils/CurlConnection.cpp b/src/Utils/CurlConnection.cpp new file mode 100644 index 0000000..50d819e --- /dev/null +++ b/src/Utils/CurlConnection.cpp @@ -0,0 +1,71 @@ +#include "CurlConnection.h" + +#include +#include + +#include + +#include + +namespace +{ + size_t curlCallback(void* content, size_t size, size_t nmemb, std::string* buffer) +{ + buffer->append(static_cast(content), size*nmemb); + return size*nmemb; +} +} + +namespace Utils +{ + +CurlConnection::CurlConnection() +{ + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); +} + +std::string CurlConnection::get(const std::string& url, + const std::vector& extraHttpHeaders) +{ + std::string str_result; + std::string post_data = ""; + std::string action = "GET"; + if(curl) + { + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str_result); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); + curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip"); + + if(!extraHttpHeaders.empty()) + { + struct curl_slist *chunk = NULL; + for(int i = 0; i < extraHttpHeaders.size(); ++i) + { + chunk = curl_slist_append(chunk, extraHttpHeaders[i].c_str()); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); + } + + if(post_data.size() > 0 || action == "POST" || action == "PUT" || action == "DELETE") + { + if(action == "PUT" || action == "DELETE") + { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, action.c_str()); + } + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str()); + } + res = curl_easy_perform(curl); + + if(res != CURLE_OK) + { + std::cout << "There was an error: " << curl_easy_strerror(res) << "\n"; + } + } + + return str_result; +} + +} // namespace Utils \ No newline at end of file diff --git a/src/Utils/CurlConnection.h b/src/Utils/CurlConnection.h new file mode 100644 index 0000000..4527b38 --- /dev/null +++ b/src/Utils/CurlConnection.h @@ -0,0 +1,29 @@ +#ifndef _APPRESETAPI_H_ +#define _APPRESETAPI_H_ + +#include +#include +#include + +namespace Utils +{ + +class CurlConnection +{ +public: + CurlConnection(); + + std::string get(const std::string& url, const std::vector& extraHttpHeaders); + + +private: + CURL* curl; + CURLcode res; + +}; + +void initCurl(const std::string& host); + +} // namespace utils + +#endif // _APPRESETAPI_H_ \ No newline at end of file