From 86e48c043a8f086bc7507c590b35ab3fe84f99e0 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Sun, 5 Nov 2023 00:49:26 +0000 Subject: [PATCH] Remove requester --- src/requester.cpp | 57 ----------------------------------------------- src/requester.hpp | 33 --------------------------- 2 files changed, 90 deletions(-) delete mode 100644 src/requester.cpp delete mode 100644 src/requester.hpp diff --git a/src/requester.cpp b/src/requester.cpp deleted file mode 100644 index 36cd20d..0000000 --- a/src/requester.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "requester.hpp" -#include -#include -#include -#include -#include - -extern sc::logger logger; - -size_t requester::write_data(char *buf, size_t sz, size_t nmemb, void *user_data) { - size_t realsize = sz * nmemb; - SCInfo(logger, "received ", realsize, " bytes"); - std::string* text {reinterpret_cast(user_data)}; - text->append(buf, realsize); - return realsize; -} - -requester::requester() { - curl_global_init(CURL_GLOBAL_ALL); - _h.reset(curl_easy_init()); - curl_easy_setopt(_h.get(), CURLOPT_WRITEFUNCTION, write_data); -} - -requester::~requester() { - curl_global_cleanup(); -} - -std::string requester::get(const std::string &url) { - std::string text; - curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str()); - curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &text); - auto success = curl_easy_perform(_h.get()); - if (success != CURLE_OK) throw std::runtime_error("could not get remote data"); - long status; - success = curl_easy_getinfo(_h.get(), CURLINFO_RESPONSE_CODE, &status); - if (success != CURLE_OK || status != 200) throw std::runtime_error("could not get remote data"); - return text; -} - -std::string requester::post(const std::string& url, const std::string& json) { - curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str()); - curl_easy_setopt(_h.get(), CURLOPT_POST, 1L); - curl_easy_setopt(_h.get(), CURLOPT_POSTFIELDS, json.c_str()); - curl_easy_setopt(_h.get(), CURLOPT_POSTFIELDSIZE, json.size()); - - struct curl_slist* headers {nullptr}; - headers = curl_slist_append(headers, "Content-Type: application/json"); - std::unique_ptr hdrs {headers, curl_slist_free_all}; - curl_easy_setopt(_h.get(), CURLOPT_HTTPHEADER, headers); - - std::string response; - curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &response); - - auto success = curl_easy_perform(_h.get()); - if (success != CURLE_OK) throw std::runtime_error("could not post data"); - return response; -} diff --git a/src/requester.hpp b/src/requester.hpp deleted file mode 100644 index ce735bc..0000000 --- a/src/requester.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef _requester_H_ -#define _requester_H_ - -#include -#include -#include - -class requester { - public: - // callback for data receiving - static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data); - - // this class is a RAII class for a curl handle - requester(); - ~requester(); - - // forbid copying and moving - requester(const requester&) = delete; - requester& operator=(const requester&) = delete; - requester(requester&&) = delete; - requester& operator=(requester&&) = delete; - - // perform a http get request - std::string get(const std::string& url); - - // perform a http post request - std::string post(const std::string& url, const std::string& json); - - private: - std::unique_ptr _h {nullptr, curl_easy_cleanup}; -}; - -#endif // _requester_H_