Lucas-Bot/dependencies/Http/library.cpp

106 lines
3.4 KiB
C++
Raw Permalink Normal View History

2022-10-08 22:47:18 +08:00
/*
* This file is part of Lucas' Bot.
*
* Lucas' Bot is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
Lucas' Bot is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
for more details.
You should have received a copy of the GNU Affero General Public License along
with Lucas' Bot. If not, see <https://www.gnu.org/licenses/>.
*/
2022-10-20 13:15:42 +08:00
#define EXPORT_HTTP
#include "http.h"
2022-10-08 22:47:18 +08:00
#include "curl/curl.h"
#include <random>
#include <fstream>
#include <sstream>
namespace Http {
2022-10-20 13:15:42 +08:00
HTTP_LIB Request::Request() {
2022-10-08 22:47:18 +08:00
this->url = "";
this->method = "GET";
this->body = "";
this->header = "";
this->response = "";
this->verbose = false;
}
2022-10-20 13:15:42 +08:00
HTTP_LIB Request::~Request() = default;
2022-10-08 22:47:18 +08:00
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::setUrl(const std::string& theUrl) {
2022-10-08 22:47:18 +08:00
this->url = theUrl;
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::setMethod(const std::string& theMethod) {
2022-10-08 22:47:18 +08:00
this->method = theMethod;
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::setBody(const std::string& theBody) {
2022-10-08 22:47:18 +08:00
this->body = theBody;
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::setHeader(const std::string& theHeader) {
2022-10-08 22:47:18 +08:00
this->header = theHeader;
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::setVerbose(bool theVerbose) {
2022-10-08 22:47:18 +08:00
this->verbose = theVerbose;
}
2022-10-20 13:15:42 +08:00
std::string HTTP_LIB Request::getResponse() {
2022-10-08 22:47:18 +08:00
return this->response;
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB Request::send() {
2022-10-08 22:47:18 +08:00
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, this->method.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, this->body.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Request::writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &this->response);
curl_easy_setopt(curl, CURLOPT_VERBOSE, this->verbose);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
}
2022-10-20 13:15:42 +08:00
size_t HTTP_LIB Request::writeCallback(char *ptr, size_t size, size_t nmemb, void *userdata) {
2022-10-08 22:47:18 +08:00
auto *response = static_cast<std::string *>(userdata);
response->append(ptr, size * nmemb);
return size * nmemb;
}
2022-10-20 13:15:42 +08:00
std::string HTTP_LIB FileUtilities::readFile(const std::string &path) {
2022-10-08 22:47:18 +08:00
std::ifstream file(path, std::ios::in | std::ios::binary | std::ios::ate);
std::stringstream buffer;
// read binary from path to the string
buffer << file.rdbuf();
return buffer.str();
}
2022-10-20 13:15:42 +08:00
void HTTP_LIB FileUtilities::writeFile(const std::string &path, const std::string &content) {
2022-10-08 22:47:18 +08:00
std::ofstream file(path, std::ios::out | std::ios::binary);
// write content to path in binary format
file.write(content.c_str(), content.size());
// Close
file.close();
}
}