get / post requests with curl

main
Brett 2023-08-15 02:45:27 -04:00
parent 862766de71
commit 94036bcd3a
5 changed files with 50 additions and 21 deletions

View File

@ -2,7 +2,7 @@
<html>
<body>
<p>Hello {{person}}!</p>
<form action="/req/posta.html" method="post">
<form action="/req/posta.html" method="posts">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>

View File

@ -5,7 +5,7 @@
</head>
<body>
<p>Hello {{person}}!</p>
<form action="/req/posta.html" method="post">
<form action="/req/posta.html" method="posts">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>

View File

@ -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();
};

View File

@ -4,21 +4,29 @@
#include <crowsite/requests/curl.h>
#include <blt/std/logging.h>
#include <blt/std/hashmap.h>
#include <blt/std/memory.h>
#include <cstring>
namespace cs {
namespace cs
{
HASHMAP<std::string, std::string> 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<char> 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));
}
}
void request::post(const std::string& domain)
if (err != CURLE_OK)
{
BLT_ERROR("CURL failed to send POST request '%s'. Error '%s'", domain.c_str(), curl_easy_strerror(err));
}
}
const std::string& request::getResponse(const std::string& domain)
{
return responses[domain];
}
}

View File

@ -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<std::string>(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);