From 94036bcd3afdee54e5801a4fefbdbe22b3af1f02 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 15 Aug 2023 02:45:27 -0400 Subject: [PATCH] get / post requests with curl --- crow_test/index.html | 2 +- crow_test/webcontent/index.html | 2 +- include/crowsite/requests/curl.h | 6 ++-- src/crowsite/requests/curl.cpp | 56 +++++++++++++++++++++++--------- src/main.cpp | 5 ++- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/crow_test/index.html b/crow_test/index.html index 449db29..f2d4439 100644 --- a/crow_test/index.html +++ b/crow_test/index.html @@ -2,7 +2,7 @@

Hello {{person}}!

-
+

diff --git a/crow_test/webcontent/index.html b/crow_test/webcontent/index.html index a3940cf..04d6416 100644 --- a/crow_test/webcontent/index.html +++ b/crow_test/webcontent/index.html @@ -5,7 +5,7 @@

Hello {{person}}!

- +

diff --git a/include/crowsite/requests/curl.h b/include/crowsite/requests/curl.h index f97884e..0abb4a5 100644 --- a/include/crowsite/requests/curl.h +++ b/include/crowsite/requests/curl.h @@ -26,11 +26,13 @@ namespace cs public: request(); + static const std::string& getResponse(const std::string& domain); + void setAuthHeader(const std::string& header); - void get(const std::string& domain); + void get(const std::string& domain, const std::string& data = ""); - void post(const std::string& domain); + void post(const std::string& domain, const std::string& data = ""); ~request(); }; diff --git a/src/crowsite/requests/curl.cpp b/src/crowsite/requests/curl.cpp index 448cf1b..1467e49 100644 --- a/src/crowsite/requests/curl.cpp +++ b/src/crowsite/requests/curl.cpp @@ -4,21 +4,29 @@ #include #include #include +#include +#include -namespace cs { +namespace cs +{ HASHMAP responses; - void writeData(char *ptr, size_t size, size_t nmemb, void *userdata){ - BLT_INFO("Data %p, %u %u", ptr, size, nmemb); - const char* name = (const char*) userdata; + size_t writeData(char* ptr, size_t size, size_t nmemb, void* userdata) + { + auto* name = (const char*) userdata; std::string site{name}; - std::string response; - response.reserve(nmemb); - for (size_t i = 0; i < nmemb; i++) - response += ptr[i]; - BLT_TRACE("%s", response.c_str()); - responses[site] = response; + + blt::scoped_buffer response{size * nmemb}; + memcpy(response.ptr(), ptr, size * nmemb); + + if (responses.find(site) != responses.end()){ + std::string res{response.ptr()}; + responses[site].append(res); + } else + responses[site] = std::string(response.ptr()); + + return size * nmemb; } void requests::init() @@ -56,21 +64,37 @@ namespace cs { curl_easy_setopt(handler, CURLOPT_HTTPHEADER, headers); } - void request::get(const std::string& domain) + void request::get(const std::string& domain, const std::string& data) { + BLT_WARN("Domain: %s", domain.c_str()); + auto full = domain + data; + curl_easy_setopt(handler, CURLOPT_URL, full.c_str()); + curl_easy_setopt(handler, CURLOPT_WRITEDATA, domain.c_str()); + curl_easy_setopt(handler, CURLOPT_WRITEFUNCTION, writeData); + + auto err = curl_easy_perform(handler); + if (err != CURLE_OK) + { + BLT_ERROR("CURL failed to send GET request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err)); + } + } + + void request::post(const std::string& domain, const std::string& data) + { + curl_easy_setopt(handler, CURLOPT_POSTFIELDS, data.c_str()); curl_easy_setopt(handler, CURLOPT_URL, domain.c_str()); curl_easy_setopt(handler, CURLOPT_WRITEDATA, domain.c_str()); curl_easy_setopt(handler, CURLOPT_WRITEFUNCTION, writeData); - // TODO Error decode auto err = curl_easy_perform(handler); - if (err){ - BLT_ERROR("CURL failed to send request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err)); + if (err != CURLE_OK) + { + BLT_ERROR("CURL failed to send POST request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err)); } } - void request::post(const std::string& domain) + const std::string& request::getResponse(const std::string& domain) { - + return responses[domain]; } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4edb600..0624d07 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,7 +103,10 @@ int main(int argc, const char** argv) cs::request get; get.setAuthHeader("MediaBrowser Client=Crowsite, Device=YourMom, Token=" + blt::arg_parse::get(args["token"])); - get.get("https://media.tpgc.me/Auth/Keys"); + get.get("https://media.tpgc.me/Users"); + + const auto& f = cs::request::getResponse("https://media.tpgc.me/Users"); + BLT_TRACE(f); BLT_INFO("Starting site %s.", SITE_NAME); crow::mustache::set_global_base(SITE_FILES_PATH);