From 385d802064bf85b6b46f91c899101e8880acee05 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sat, 23 Sep 2023 17:50:01 +0100 Subject: [PATCH 01/19] refactor: changed user-agent, added more logging, stopped an infinite loop --- src/dpp/discordvoiceclient.cpp | 20 ++++++++++++++++++++ src/dpp/sslclient.cpp | 9 +++++++++ src/dpp/wsclient.cpp | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 2d0d766349..828d071413 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -397,11 +397,31 @@ bool discord_voice_client::is_playing() { void discord_voice_client::thread_run() { utility::set_thread_name(std::string("vc/") + std::to_string(server_id)); + + int times_looped; + time_t time_since_loop; + do { bool error = false; ssl_client::read_loop(); ssl_client::close(); + + /* This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ + time_t current_time = time(nullptr); + if(current_time - time_since_loop >= 5000) { + times_looped = 0; + } + /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ + times_looped += 1; + /* If we've looped 5 or more times, abort the loop. */ + if(times_looped >= 5) { + log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket."); + break; + } + time_since_loop = current_time; + if (!terminating) { + log(dpp::ll_debug, "Attempting to reconnect the websocket..."); do { try { ssl_client::connect(); diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index 3bf3f948bf..97828e2838 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -67,6 +67,7 @@ #include #include #include +#include #include #include #include @@ -504,6 +505,14 @@ void ssl_client::read_loop() case SSL_ERROR_NONE: /* Data received, add it to the buffer */ if (r > 0) { + std::string data(server_to_client_buffer, r); + std::smatch matches; + + if(std::regex_search(data, matches, std::regex(R"(^(HTTP\/1\.1 .+))"))) { + if(matches.str(1).find("204") == std::string::npos && matches.str(1).find("101") == std::string::npos && matches.str(1).find("200") == std::string::npos) + log(ll_warning, "Received unhandled code: " + matches.str(1)); + } + buffer.append(server_to_client_buffer, r); if (!this->handle_buffer(buffer)) { return; diff --git a/src/dpp/wsclient.cpp b/src/dpp/wsclient.cpp index 38465cf2d1..d94abca54d 100644 --- a/src/dpp/wsclient.cpp +++ b/src/dpp/wsclient.cpp @@ -52,7 +52,7 @@ void websocket_client::connect() "GET " + this->path + " HTTP/1.1\r\n" "Host: " + this->hostname + "\r\n" "pragma: no-cache\r\n" - "User-Agent: DPP/0.1\r\n" + "User-Agent: " + "DiscordBot (https://github.com/brainboxdotcc/DPP, 16.0.38)" + "\r\n" "Upgrade: WebSocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Key: " + this->key + "\r\n" From 82d30a3b7cc59fa834c02e362f94e107d80d2697 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sat, 23 Sep 2023 17:59:33 +0100 Subject: [PATCH 02/19] refactor: changed int to size_t for times_looped --- src/dpp/discordvoiceclient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 828d071413..3edd04f850 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -398,7 +398,7 @@ void discord_voice_client::thread_run() { utility::set_thread_name(std::string("vc/") + std::to_string(server_id)); - int times_looped; + size_t times_looped; time_t time_since_loop; do { From 900ca4311b107573150a72a530a8f18fda0e6ab8 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sat, 23 Sep 2023 20:43:09 +0100 Subject: [PATCH 03/19] fix: changed 5000 to 3 as time_t is seconds --- src/dpp/discordvoiceclient.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 3edd04f850..bd90f5d3be 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -406,9 +406,14 @@ void discord_voice_client::thread_run() ssl_client::read_loop(); ssl_client::close(); - /* This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ time_t current_time = time(nullptr); - if(current_time - time_since_loop >= 5000) { + /* Here, we check if it's been longer than 3 seconds since the previous loop, + * this gives us time to see if it's an actual disconnect, or an error. + * This will prevent us from looping too much, meaning error codes do not cause an infinite loop. + */ + log(dpp::ll_debug, "current time: " + std::string(current_time)); + log(dpp::ll_debug, "time_since_loop: " + std::string(time_since_loop)); + if(current_time - time_since_loop >= 3000) { times_looped = 0; } /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ From 29f36303b824730d44acc12c6dc3f5caecea0107 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sat, 23 Sep 2023 20:56:13 +0100 Subject: [PATCH 04/19] fix: removed some debug messages that I left in --- src/dpp/discordvoiceclient.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index bd90f5d3be..6843ba7b13 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -407,13 +407,11 @@ void discord_voice_client::thread_run() ssl_client::close(); time_t current_time = time(nullptr); - /* Here, we check if it's been longer than 3 seconds since the previous loop, + /* Here, we check if it's been longer than 3 seconds since the previous loop, * this gives us time to see if it's an actual disconnect, or an error. * This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ - log(dpp::ll_debug, "current time: " + std::string(current_time)); - log(dpp::ll_debug, "time_since_loop: " + std::string(time_since_loop)); - if(current_time - time_since_loop >= 3000) { + if(current_time - time_since_loop >= 3) { times_looped = 0; } /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ From 59ee7d72c53f900de59e1051223e6180929132f8 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sat, 23 Sep 2023 21:31:21 +0100 Subject: [PATCH 05/19] fix: renamed and initalised variables --- src/dpp/discordvoiceclient.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 6843ba7b13..3b6a7f06ed 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -398,8 +398,8 @@ void discord_voice_client::thread_run() { utility::set_thread_name(std::string("vc/") + std::to_string(server_id)); - size_t times_looped; - time_t time_since_loop; + size_t times_looped = 0; + time_t last_loop_time = time(nullptr); do { bool error = false; @@ -411,7 +411,7 @@ void discord_voice_client::thread_run() * this gives us time to see if it's an actual disconnect, or an error. * This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ - if(current_time - time_since_loop >= 3) { + if(current_time - last_loop_time >= 3) { times_looped = 0; } /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ @@ -421,7 +421,7 @@ void discord_voice_client::thread_run() log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket."); break; } - time_since_loop = current_time; + last_loop_time = current_time; if (!terminating) { log(dpp::ll_debug, "Attempting to reconnect the websocket..."); From 18479183fb03034f560bddab456d7cd42073b3f0 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 08:07:59 +0700 Subject: [PATCH 06/19] feat: add utility::hex_to_str --- include/dpp/utility.h | 2 ++ src/dpp/utility.cpp | 7 ++++++- src/unittest/test.cpp | 7 +++++++ src/unittest/test.h | 2 ++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/include/dpp/utility.h b/include/dpp/utility.h index ad2e632da0..bbd764dd88 100644 --- a/include/dpp/utility.h +++ b/include/dpp/utility.h @@ -741,5 +741,7 @@ namespace dpp { std::array data; }; + std::string hex_to_str(long h); + } // namespace utility } // namespace dpp diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 8a28201c33..54687db653 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -772,6 +771,12 @@ namespace dpp { #endif #endif } + + std::string hex_to_str(long h) { + std::stringstream s; + s << std::hex << h; + return s.str(); + } } // namespace utility } // namespace dpp diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index f3adb662f9..1643c8bb4e 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -613,6 +613,13 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b dpp::utility::thread_url(1,0) == "" && dpp::utility::thread_url(0,0) == "" ); + + set_test(UTILITY_HEX_TO_STR, false); + set_test(UTILITY_HEX_TO_STR, + dpp::utility::hex_to_str(0x10) == "10" && + dpp::utility::hex_to_str(0x00) == "0" && + dpp::utility::hex_to_str(0x26) == "26" && + dpp::utility::hex_to_str(0x02) == "2"); } #ifndef _WIN32 diff --git a/src/unittest/test.h b/src/unittest/test.h index 96d531e0a4..239266907f 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -192,6 +192,8 @@ DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline); DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline); DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline); DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline); +DPP_TEST(UTILITY_HEX_TO_STR, "utility::hex_to_str", tf_offline); + DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline); DPP_TEST(EMOJI_GET_URL, "emoji::get_url", tf_offline); DPP_TEST(ROLE_COMPARE, "role::operator<", tf_offline); From 7ef50c0bdf3d0f38798b220e00c8255f7be4166d Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 08:09:23 +0700 Subject: [PATCH 07/19] fix: test segfault --- src/unittest/test.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 1643c8bb4e..8082b9a921 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -169,10 +169,14 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b set_test(READFILE, false); std::string rf_test = dpp::utility::read_file(SHARED_OBJECT); FILE* fp = fopen(SHARED_OBJECT, "rb"); - fseek(fp, 0, SEEK_END); - size_t off = (size_t)ftell(fp); - fclose(fp); - set_test(READFILE, off == rf_test.length()); + if (!fp) + set_test(READFILE, false); + else { + fseek(fp, 0, SEEK_END); + size_t off = (size_t)ftell(fp); + fclose(fp); + set_test(READFILE, off == rf_test.length()); + } set_test(TIMESTAMPTOSTRING, false); set_test(TIMESTAMPTOSTRING, dpp::ts_to_string(1642611864) == "2022-01-19T17:04:24Z"); From 2d928a394e0c4d22cf9da61e45583efd38fdb0d8 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 08:10:31 +0700 Subject: [PATCH 08/19] fix: http_version header is in hex --- include/dpp/httpsclient.h | 8 ++++++++ src/dpp/queues.cpp | 4 ---- src/dpp/wsclient.cpp | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index 09bbd8453d..e12a2ac344 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -27,9 +27,17 @@ #include #include #include +#include +#include namespace dpp { +static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " + + utility::hex_to_str(DPP_VERSION_MAJOR) + "." + + utility::hex_to_str(DPP_VERSION_MINOR) + "." + + utility::hex_to_str(DPP_VERSION_PATCH) + ")"; + +static inline constexpr const char* DISCORD_HOST = "https://discord.com"; /** * @brief HTTP connection status diff --git a/src/dpp/queues.cpp b/src/dpp/queues.cpp index 2c7fed5af2..4625020a46 100644 --- a/src/dpp/queues.cpp +++ b/src/dpp/queues.cpp @@ -29,13 +29,9 @@ #include #include #include -#include namespace dpp { -static std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " + std::to_string(DPP_VERSION_MAJOR) + "." + std::to_string(DPP_VERSION_MINOR) + "." + std::to_string(DPP_VERSION_PATCH) + ")"; -static const char* DISCORD_HOST = "https://discord.com"; - http_request::http_request(const std::string &_endpoint, const std::string &_parameters, http_completion_event completion, const std::string &_postdata, http_method _method, const std::string &audit_reason, const std::string &filename, const std::string &filecontent, const std::string &filemimetype) : complete_handler(completion), completed(false), non_discord(false), endpoint(_endpoint), parameters(_parameters), postdata(_postdata), method(_method), reason(audit_reason), mimetype("application/json"), waiting(false) { diff --git a/src/dpp/wsclient.cpp b/src/dpp/wsclient.cpp index d94abca54d..d23f6742af 100644 --- a/src/dpp/wsclient.cpp +++ b/src/dpp/wsclient.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace dpp { @@ -52,7 +53,7 @@ void websocket_client::connect() "GET " + this->path + " HTTP/1.1\r\n" "Host: " + this->hostname + "\r\n" "pragma: no-cache\r\n" - "User-Agent: " + "DiscordBot (https://github.com/brainboxdotcc/DPP, 16.0.38)" + "\r\n" + "User-Agent: " + http_version + "\r\n" "Upgrade: WebSocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Key: " + this->key + "\r\n" From a336a655be5baacecb7439ed7a27ec84cc475396 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 08:26:23 +0700 Subject: [PATCH 09/19] add READFILE log --- src/unittest/test.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 8082b9a921..8cfe9462f8 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -169,8 +169,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b set_test(READFILE, false); std::string rf_test = dpp::utility::read_file(SHARED_OBJECT); FILE* fp = fopen(SHARED_OBJECT, "rb"); - if (!fp) + if (!fp) { + std::cout << "READFILE: Failed to open file: " << SHARED_OBJECT << "\n"; set_test(READFILE, false); + } else { fseek(fp, 0, SEEK_END); size_t off = (size_t)ftell(fp); From c8b0e6f8ddf6b8152d32fd2b52fe890ab527e987 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 16:11:55 +0700 Subject: [PATCH 10/19] remove unneeded utility --- include/dpp/httpsclient.h | 8 +++++--- include/dpp/utility.h | 2 -- src/dpp/utility.cpp | 5 ----- src/unittest/test.cpp | 6 ------ src/unittest/test.h | 1 - 5 files changed, 5 insertions(+), 17 deletions(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index e12a2ac344..10dbee321b 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -26,16 +26,18 @@ #include #include #include +#include #include #include #include +#include namespace dpp { static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " - + utility::hex_to_str(DPP_VERSION_MAJOR) + "." - + utility::hex_to_str(DPP_VERSION_MINOR) + "." - + utility::hex_to_str(DPP_VERSION_PATCH) + ")"; + + std::to_string(from_string(std::to_string(DPP_VERSION_MAJOR), std::hex)) + "." + + std::to_string(from_string(std::to_string(DPP_VERSION_MINOR), std::hex)) + "." + + std::to_string(from_string(std::to_string(DPP_VERSION_PATCH), std::hex)) + ")"; static inline constexpr const char* DISCORD_HOST = "https://discord.com"; diff --git a/include/dpp/utility.h b/include/dpp/utility.h index bbd764dd88..ad2e632da0 100644 --- a/include/dpp/utility.h +++ b/include/dpp/utility.h @@ -741,7 +741,5 @@ namespace dpp { std::array data; }; - std::string hex_to_str(long h); - } // namespace utility } // namespace dpp diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 54687db653..1f866ba363 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -772,11 +772,6 @@ namespace dpp { #endif } - std::string hex_to_str(long h) { - std::stringstream s; - s << std::hex << h; - return s.str(); - } } // namespace utility } // namespace dpp diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 8cfe9462f8..4d082c6c2d 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -620,12 +620,6 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b dpp::utility::thread_url(0,0) == "" ); - set_test(UTILITY_HEX_TO_STR, false); - set_test(UTILITY_HEX_TO_STR, - dpp::utility::hex_to_str(0x10) == "10" && - dpp::utility::hex_to_str(0x00) == "0" && - dpp::utility::hex_to_str(0x26) == "26" && - dpp::utility::hex_to_str(0x02) == "2"); } #ifndef _WIN32 diff --git a/src/unittest/test.h b/src/unittest/test.h index 239266907f..08de933345 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -192,7 +192,6 @@ DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline); DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline); DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline); DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline); -DPP_TEST(UTILITY_HEX_TO_STR, "utility::hex_to_str", tf_offline); DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline); DPP_TEST(EMOJI_GET_URL, "emoji::get_url", tf_offline); From ba7a779c9979683a068d63bfb913a50bdc8d50da Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 16:18:41 +0700 Subject: [PATCH 11/19] use to_hex --- include/dpp/httpsclient.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index 10dbee321b..bae4f58d81 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -35,9 +34,9 @@ namespace dpp { static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " - + std::to_string(from_string(std::to_string(DPP_VERSION_MAJOR), std::hex)) + "." - + std::to_string(from_string(std::to_string(DPP_VERSION_MINOR), std::hex)) + "." - + std::to_string(from_string(std::to_string(DPP_VERSION_PATCH), std::hex)) + ")"; + + to_hex(DPP_VERSION_MAJOR) + "." + + to_hex(DPP_VERSION_MINOR) + "." + + to_hex(DPP_VERSION_PATCH) + ")"; static inline constexpr const char* DISCORD_HOST = "https://discord.com"; From ef5c591209bd607a3793247b08d6b90f7b03cd56 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 16:27:39 +0700 Subject: [PATCH 12/19] remove unused include --- include/dpp/httpsclient.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index bae4f58d81..0d6c5057aa 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -28,7 +28,6 @@ #include #include #include -#include #include namespace dpp { From 454c7595d791fe910f2514aa5f749e752fa0115e Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 16:36:58 +0700 Subject: [PATCH 13/19] remove blank line --- src/unittest/test.cpp | 1 - src/unittest/test.h | 1 - 2 files changed, 2 deletions(-) diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 4d082c6c2d..f08b300366 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -619,7 +619,6 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b dpp::utility::thread_url(1,0) == "" && dpp::utility::thread_url(0,0) == "" ); - } #ifndef _WIN32 diff --git a/src/unittest/test.h b/src/unittest/test.h index 08de933345..96d531e0a4 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -192,7 +192,6 @@ DPP_TEST(UTILITY_CHANNEL_URL, "utility::channel_url", tf_offline); DPP_TEST(UTILITY_THREAD_URL, "utility::thread_url", tf_offline); DPP_TEST(UTILITY_AVATAR_SIZE, "utility::avatar_size", tf_offline); DPP_TEST(UTILITY_CDN_ENDPOINT_URL_HASH, "utility::cdn_endpoint_url_hash", tf_offline); - DPP_TEST(STICKER_GET_URL, "sticker::get_url aka utility::cdn_endpoint_url_sticker", tf_offline); DPP_TEST(EMOJI_GET_URL, "emoji::get_url", tf_offline); DPP_TEST(ROLE_COMPARE, "role::operator<", tf_offline); From 2917587d1a64b8d87e72ed2c45b0232550be9085 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 16:40:38 +0700 Subject: [PATCH 14/19] remove blank line --- src/dpp/utility.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 1f866ba363..04db275526 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -771,7 +771,6 @@ namespace dpp { #endif #endif } - } // namespace utility } // namespace dpp From d96d961500d75c6d18a678b08c942b9e777c4915 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sun, 24 Sep 2023 10:59:44 +0100 Subject: [PATCH 15/19] fix: implemented correct http_version. fixed a couple issues. Co-authored-by: Neko-Life --- include/dpp/httpsclient.h | 6 +++--- src/dpp/discordvoiceclient.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/dpp/httpsclient.h b/include/dpp/httpsclient.h index 0d6c5057aa..c2733b0b04 100644 --- a/include/dpp/httpsclient.h +++ b/include/dpp/httpsclient.h @@ -33,9 +33,9 @@ namespace dpp { static inline const std::string http_version = "DiscordBot (https://github.com/brainboxdotcc/DPP, " - + to_hex(DPP_VERSION_MAJOR) + "." - + to_hex(DPP_VERSION_MINOR) + "." - + to_hex(DPP_VERSION_PATCH) + ")"; + + to_hex(DPP_VERSION_MAJOR, false) + "." + + to_hex(DPP_VERSION_MINOR, false) + "." + + to_hex(DPP_VERSION_PATCH, false) + ")"; static inline constexpr const char* DISCORD_HOST = "https://discord.com"; diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 3b6a7f06ed..9ce2958938 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -415,7 +415,7 @@ void discord_voice_client::thread_run() times_looped = 0; } /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ - times_looped += 1; + times_looped++; /* If we've looped 5 or more times, abort the loop. */ if(times_looped >= 5) { log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket."); From 0276716977fc4f02f3a7a6136da8453ca91918f9 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sun, 24 Sep 2023 12:54:01 +0100 Subject: [PATCH 16/19] refactor: replaced regex for string splitting --- src/dpp/discordvoiceclient.cpp | 5 +++-- src/dpp/sslclient.cpp | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 9ce2958938..7d17a63d73 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -411,9 +411,9 @@ void discord_voice_client::thread_run() * this gives us time to see if it's an actual disconnect, or an error. * This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ - if(current_time - last_loop_time >= 3) { + if(current_time - last_loop_time >= 3) times_looped = 0; - } + /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ times_looped++; /* If we've looped 5 or more times, abort the loop. */ @@ -421,6 +421,7 @@ void discord_voice_client::thread_run() log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket."); break; } + last_loop_time = current_time; if (!terminating) { diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index 97828e2838..c851b35cf2 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -67,7 +67,6 @@ #include #include #include -#include #include #include #include @@ -505,12 +504,13 @@ void ssl_client::read_loop() case SSL_ERROR_NONE: /* Data received, add it to the buffer */ if (r > 0) { - std::string data(server_to_client_buffer, r); - std::smatch matches; + const std::string data(server_to_client_buffer, r); + const std::vector& data_lines = utility::tokenize(data); + const std::string& http_reponse(data_lines[0]); - if(std::regex_search(data, matches, std::regex(R"(^(HTTP\/1\.1 .+))"))) { - if(matches.str(1).find("204") == std::string::npos && matches.str(1).find("101") == std::string::npos && matches.str(1).find("200") == std::string::npos) - log(ll_warning, "Received unhandled code: " + matches.str(1)); + /* Does the first line begin with a http code and does it not contain either 204, 101 or 200? */ + if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos && http_reponse.find("204") == std::string::npos && http_reponse.find("101") == std::string::npos && http_reponse.find("200") == std::string::npos) { + log(ll_warning, "Received unhandled code: " + http_reponse); } buffer.append(server_to_client_buffer, r); From ce06081b531ef1247691bef98fe4da0d1d3cf59d Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sun, 24 Sep 2023 12:56:48 +0100 Subject: [PATCH 17/19] fix: reverted test.cpp changes --- src/unittest/test.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index f08b300366..f3adb662f9 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -169,16 +169,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b set_test(READFILE, false); std::string rf_test = dpp::utility::read_file(SHARED_OBJECT); FILE* fp = fopen(SHARED_OBJECT, "rb"); - if (!fp) { - std::cout << "READFILE: Failed to open file: " << SHARED_OBJECT << "\n"; - set_test(READFILE, false); - } - else { - fseek(fp, 0, SEEK_END); - size_t off = (size_t)ftell(fp); - fclose(fp); - set_test(READFILE, off == rf_test.length()); - } + fseek(fp, 0, SEEK_END); + size_t off = (size_t)ftell(fp); + fclose(fp); + set_test(READFILE, off == rf_test.length()); set_test(TIMESTAMPTOSTRING, false); set_test(TIMESTAMPTOSTRING, dpp::ts_to_string(1642611864) == "2022-01-19T17:04:24Z"); From bf2e364f99ea92903880953ec60556ecd12e7d62 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Sun, 24 Sep 2023 14:30:14 +0100 Subject: [PATCH 18/19] fix: corrected a possible fail with checking http code --- src/dpp/sslclient.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index c851b35cf2..7ab352e0e4 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -505,12 +505,18 @@ void ssl_client::read_loop() /* Data received, add it to the buffer */ if (r > 0) { const std::string data(server_to_client_buffer, r); - const std::vector& data_lines = utility::tokenize(data); + const std::vector data_lines = utility::tokenize(data); const std::string& http_reponse(data_lines[0]); - /* Does the first line begin with a http code and does it not contain either 204, 101 or 200? */ - if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos && http_reponse.find("204") == std::string::npos && http_reponse.find("101") == std::string::npos && http_reponse.find("200") == std::string::npos) { - log(ll_warning, "Received unhandled code: " + http_reponse); + /* Does the first line begin with a http code? */ + if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos) { + /* Now let's split the first line by every space, meaning we can check the actual HTTP code. */ + const std::vector line_split_by_space = utility::tokenize(data_lines[0], " "); + const std::string& http_code(line_split_by_space[1]); + + /* Does the http code not contain either 204, 101 or 200? If it doesn't, log it. */ + if(http_code.find("204") == std::string::npos && http_code.find("101") == std::string::npos && http_code.find("200") == std::string::npos) + log(ll_warning, "Received unhandled code: " + http_reponse); } buffer.append(server_to_client_buffer, r); From ac5b21cde1da13418d3d0370a700fc3ca300a189 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Mon, 25 Sep 2023 08:59:18 +0100 Subject: [PATCH 19/19] fix: applied requested changes --- src/dpp/sslclient.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index 7ab352e0e4..a3dc1c40be 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -505,18 +505,24 @@ void ssl_client::read_loop() /* Data received, add it to the buffer */ if (r > 0) { const std::string data(server_to_client_buffer, r); + /* Split the data into an array for every line. */ const std::vector data_lines = utility::tokenize(data); - const std::string& http_reponse(data_lines[0]); + /* Get the first line as we always know that's the HTTP response. */ + const std::string http_reponse(data_lines[0]); /* Does the first line begin with a http code? */ if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos) { /* Now let's split the first line by every space, meaning we can check the actual HTTP code. */ const std::vector line_split_by_space = utility::tokenize(data_lines[0], " "); - const std::string& http_code(line_split_by_space[1]); - /* Does the http code not contain either 204, 101 or 200? If it doesn't, log it. */ - if(http_code.find("204") == std::string::npos && http_code.find("101") == std::string::npos && http_code.find("200") == std::string::npos) - log(ll_warning, "Received unhandled code: " + http_reponse); + /* We need to make sure there's at least 3 elements in line_split_by_space. */ + if(line_split_by_space.size() >= 3) { + const int http_code = std::stoi(line_split_by_space[1]); + + /* If the http_code isn't 204, 101, or 200, log it. */ + if(http_code != 204 && http_code != 101 && http_code != 200) + log(ll_warning, "Received unhandled code: " + http_reponse); + } } buffer.append(server_to_client_buffer, r);