From 18479183fb03034f560bddab456d7cd42073b3f0 Mon Sep 17 00:00:00 2001 From: Neko-Life Date: Sun, 24 Sep 2023 08:07:59 +0700 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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