2016-07-05 00:51:53 +01:00
|
|
|
#include "HTTPHelper.hpp"
|
|
|
|
|
2016-07-10 01:18:13 +01:00
|
|
|
/*
|
|
|
|
* Warning: (Awful) C Code
|
|
|
|
*/
|
2016-07-05 00:51:53 +01:00
|
|
|
|
2016-07-10 01:18:13 +01:00
|
|
|
std::string HTTPHelper::post_request(std::string url, std::string content_type, std::string data) {
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res;
|
|
|
|
std::string read_buffer;
|
|
|
|
struct curl_slist *headers = nullptr;
|
2016-07-05 00:51:53 +01:00
|
|
|
|
2016-07-10 01:18:13 +01:00
|
|
|
curl = curl_easy_init();
|
2016-07-05 00:51:53 +01:00
|
|
|
if (curl) {
|
2016-07-10 01:18:13 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
|
2016-07-10 19:16:42 +01:00
|
|
|
// Now with real HTTPS!
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt");
|
2016-07-10 01:18:13 +01:00
|
|
|
|
|
|
|
std::string content_header = "Content-Type: " + content_type;
|
|
|
|
headers = curl_slist_append(headers, content_header.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &read_buffer);
|
|
|
|
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
|
|
|
if (res != CURLE_OK) {
|
|
|
|
return "ERROR";
|
2016-07-05 00:51:53 +01:00
|
|
|
}
|
2016-07-10 01:18:13 +01:00
|
|
|
|
|
|
|
/* always cleanup */
|
2016-07-05 00:51:53 +01:00
|
|
|
curl_easy_cleanup(curl);
|
2016-07-10 01:18:13 +01:00
|
|
|
curl_slist_free_all(headers);
|
2016-07-05 00:51:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-10 01:18:13 +01:00
|
|
|
return read_buffer;
|
|
|
|
}
|
2016-07-05 00:51:53 +01:00
|
|
|
|
2016-07-10 01:18:13 +01:00
|
|
|
size_t HTTPHelper::write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
|
|
|
|
((std::string *) userp)->append((char *)contents, size * nmemb);
|
|
|
|
return size * nmemb;
|
2016-07-05 00:51:53 +01:00
|
|
|
}
|