Json parser update
This commit is contained in:
parent
8b35de51fc
commit
e9efb4b12c
@ -68,31 +68,22 @@ private:
|
||||
createSSLConnection("discord.com", "443");
|
||||
}
|
||||
}
|
||||
std::string parseJson(const std::string& response) {
|
||||
unsigned long jsonStart = response.find("\r\n\r\n");
|
||||
if (jsonStart == std::string::npos) {
|
||||
return "";
|
||||
}
|
||||
jsonStart += 4;
|
||||
std::string jsonString = response.substr(jsonStart);
|
||||
unsigned long jsonBegin = jsonString.find('['), jsonEnd = jsonString.rfind(']');
|
||||
if (jsonBegin == std::string::npos || jsonEnd == std::string::npos || jsonEnd < jsonBegin) {
|
||||
return "";
|
||||
}
|
||||
jsonString = jsonString.substr(jsonBegin, jsonEnd - jsonBegin + 1);
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\r'), jsonString.end());
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\t'), jsonString.end());
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\n'), jsonString.end());
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\\'), jsonString.end());
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), '\''), jsonString.end());
|
||||
jsonString.erase(std::remove(jsonString.begin(), jsonString.end(), ' '), jsonString.end());
|
||||
try {
|
||||
nlohmann::json jsonData = nlohmann::json::parse(jsonString);
|
||||
return jsonData.dump();
|
||||
}
|
||||
catch (const nlohmann::json::parse_error& e) {
|
||||
return "";
|
||||
std::string extract(const std::string& httpResponse) {
|
||||
unsigned long pos = httpResponse.find("\r\n\r\n");
|
||||
if (pos != std::string::npos) {
|
||||
std::string json, chunkSizeStr;
|
||||
std::istringstream stream(httpResponse.substr(pos + 4));
|
||||
while (std::getline(stream, chunkSizeStr)) {
|
||||
unsigned long chunkSize = std::stoul(chunkSizeStr, nullptr, 16);
|
||||
if (chunkSize == 0) break;
|
||||
std::string chunk(chunkSize, '\0');
|
||||
stream.read(&chunk[0], chunkSize);
|
||||
json.append(chunk);
|
||||
stream.ignore(2);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
public:
|
||||
static NetworkManager& getInstance() {
|
||||
@ -104,37 +95,38 @@ public:
|
||||
if (ctx) SSL_CTX_free(ctx);
|
||||
close(sock);
|
||||
}
|
||||
std::string request(const std::string& method, const std::string& path, const std::string& data = "") {
|
||||
#ifdef DEBUG
|
||||
createSSLConnection("discord.com", "443");
|
||||
#endif
|
||||
auto net = [this, method, path, data, result = std::string("")]() mutable -> std::string {
|
||||
std::string request = method + " " + path + " HTTP/1.1\r\nHost: discord.com\r\nAccept: application/json\r\nContent-Type: application/json\r\n";
|
||||
request += "Authorization: " + web.getToken() + "\r\nContent-Length: " + std::to_string(data.length()) + "\r\n";
|
||||
#ifdef DEBUG
|
||||
request += "Connection: close\r\n\r\n";
|
||||
#elif defined(RELEASE)
|
||||
request += "Connection: keep-alive\r\n\r\n";
|
||||
#endif
|
||||
std::string request(const std::string& method, const std::string& path, const std::string& data = "", bool closeConnection = true) {
|
||||
if (closeConnection) createSSLConnection("discord.com", "443");
|
||||
auto net = [this, method, path, data, closeConnection, result = std::string("")]() mutable -> std::string {
|
||||
std::string request = method + " " + path + " HTTP/1.1\r\n";
|
||||
request += "Host: discord.com\r\n";
|
||||
request += "Accept: application/json\r\n";
|
||||
request += "Content-Type: application/json\r\n";
|
||||
request += "Authorization: " + web.getToken() + "\r\n";
|
||||
request += "Content-Length: " + std::to_string(data.length()) + "\r\n";
|
||||
if (closeConnection) {
|
||||
request += "Connection: close\r\n\r\n";
|
||||
} else {
|
||||
request += "Connection: keep-alive\r\n\r\n";
|
||||
}
|
||||
request += data;
|
||||
if (SSL_write(ssl.get(), request.c_str(), request.length()) <= 0) {
|
||||
Logs::create(ERROR, NETWORK, "Failed to send request");
|
||||
handleSSLInitErrors();
|
||||
return "";
|
||||
}
|
||||
Logs::create(INFO, NETWORK, "Request " + method + " " + path);
|
||||
#ifdef DEBUG
|
||||
std::vector<char> buffer(1024);
|
||||
int bytesRead;
|
||||
while ((bytesRead = SSL_read(ssl.get(), buffer.data(), buffer.size())) > 0) {
|
||||
result.append(buffer.data(), bytesRead);
|
||||
if (bytesRead == buffer.size()) {
|
||||
buffer.resize(buffer.size() * 2);
|
||||
if (closeConnection) {
|
||||
std::vector<char> buffer(1024);
|
||||
int bytesRead;
|
||||
while ((bytesRead = SSL_read(ssl.get(), buffer.data(), buffer.size())) > 0) {
|
||||
result.append(buffer.data(), bytesRead);
|
||||
if (bytesRead == buffer.size()) {
|
||||
buffer.resize(buffer.size() * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return parseJson(result);
|
||||
#elif defined(RELEASE)
|
||||
return result;
|
||||
#endif
|
||||
return extract(result);
|
||||
};
|
||||
return net();
|
||||
}
|
||||
|
@ -98,7 +98,8 @@ enum GatewayIntents {
|
||||
GuildScheduledEvents = 65536,
|
||||
GuildVoiceStates = 128,
|
||||
GuildWebhooks = 32,
|
||||
MessageContent = 32768
|
||||
MessageContent = 32768,
|
||||
AllIntents = 131071
|
||||
};
|
||||
enum UserStatus {
|
||||
Offline,
|
||||
|
@ -11,6 +11,7 @@ enum type { WEBSOCKET, NETWORK };
|
||||
class Logs {
|
||||
public:
|
||||
static void create(level lvl, type t, const std::string& message) {
|
||||
#ifdef DEBUG
|
||||
std::string color;
|
||||
switch (lvl) {
|
||||
case INFO:
|
||||
@ -27,6 +28,7 @@ public:
|
||||
break;
|
||||
}
|
||||
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[37m" << message << "\033[0m" << std::endl;
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
static std::string getCurrentTime() {
|
||||
|
@ -1,17 +1,16 @@
|
||||
#include <api.h>
|
||||
#include <includes.h>
|
||||
#include <vector>
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) return -1;
|
||||
WebSocket* bot = &WebSocket::getInstance(argv[2], ALL_INTENTS);
|
||||
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Bot<Message, User, Author>& msg) {
|
||||
WebSocket* bot = &WebSocket::getInstance(argv[2], GatewayIntents::AllIntents);
|
||||
bot->on(GatewayEvents::READY, [](const Bot<Message, User, Author>& msg) {
|
||||
if (!g(2, msg.net)->isBot()) {
|
||||
g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content()));
|
||||
cout << g(0, msg.net)->send("939957962972229634", j("content", "asd")) << endl;
|
||||
}
|
||||
});
|
||||
bot->on(GatewayEvents::MESSAGE_CREATE, [](const Bot<Message, User, Author>& msg) {
|
||||
if (!g(2, msg.net)->isBot()) {
|
||||
g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content()));
|
||||
cout << g(0, msg.net)->send("939957962972229634", j("content", g(2, msg.net)->content())) << endl;
|
||||
}
|
||||
});
|
||||
bot->start();
|
||||
|
Loading…
x
Reference in New Issue
Block a user