From 6f832f82fafea2c831f154869888041cd0a6a3e4 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 21 Jun 2022 02:23:51 +0300 Subject: [PATCH 1/4] added message string to websocket error handler --- docs/guides/websockets.md | 2 +- include/crow/routing.h | 2 +- include/crow/websocket.h | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/guides/websockets.md b/docs/guides/websockets.md index 454cee7f9..4782c6322 100644 --- a/docs/guides/websockets.md +++ b/docs/guides/websockets.md @@ -8,7 +8,7 @@ A websocket route differs from a normal route quite a bit. It uses A slightly al - `#!cpp onaccept([&](const crow::request& req, void** userdata){handler code goes here})` - `#!cpp onopen([&](crow::websocket::connection& conn){handler code goes here})` - `#!cpp onmessage([&](crow::websocket::connection& conn, const std::string message, bool is_binary){handler code goes here})` -- `#!cpp onerror([&](crow::websocket::connection& conn){handler code goes here})` +- `#!cpp onerror([&](crow::websocket::connection& conn, const std::string error_message){handler code goes here})` - `#!cpp onclose([&](crow::websocket::connection& conn, const std::string reason){handler code goes here})` !!! note diff --git a/include/crow/routing.h b/include/crow/routing.h index bd305f185..e8465634d 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -508,7 +508,7 @@ namespace crow std::function open_handler_; std::function message_handler_; std::function close_handler_; - std::function error_handler_; + std::function error_handler_; std::function accept_handler_; uint64_t max_payload_; bool max_payload_override_ = false; diff --git a/include/crow/websocket.h b/include/crow/websocket.h index 937290777..f961055a4 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -75,7 +75,7 @@ namespace crow std::function open_handler, std::function message_handler, std::function close_handler, - std::function error_handler, + std::function error_handler, std::function accept_handler): adaptor_(std::move(adaptor)), handler_(handler), @@ -315,7 +315,7 @@ namespace crow adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) - error_handler_(*this); + error_handler_(*this, "Client connection not masked."); check_destroy(); #endif } @@ -341,7 +341,7 @@ namespace crow adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) - error_handler_(*this); + error_handler_(*this, ec.message()); check_destroy(); } }); @@ -379,7 +379,7 @@ namespace crow adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) - error_handler_(*this); + error_handler_(*this, ec.message()); check_destroy(); } }); @@ -414,7 +414,7 @@ namespace crow adaptor_.shutdown_readwrite(); adaptor_.close(); if (error_handler_) - error_handler_(*this); + error_handler_(*this, ec.message()); check_destroy(); } }); @@ -426,7 +426,7 @@ namespace crow close_connection_ = true; adaptor_.close(); if (error_handler_) - error_handler_(*this); + error_handler_(*this, "Message length exceeds maximum paylaod."); check_destroy(); } else if (has_mask_) @@ -455,7 +455,7 @@ namespace crow { close_connection_ = true; if (error_handler_) - error_handler_(*this); + error_handler_(*this, ec.message()); adaptor_.shutdown_readwrite(); adaptor_.close(); check_destroy(); @@ -497,7 +497,7 @@ namespace crow { close_connection_ = true; if (error_handler_) - error_handler_(*this); + error_handler_(*this, ec.message()); adaptor_.shutdown_readwrite(); adaptor_.close(); check_destroy(); @@ -685,7 +685,7 @@ namespace crow std::function open_handler_; std::function message_handler_; std::function close_handler_; - std::function error_handler_; + std::function error_handler_; std::function accept_handler_; }; } // namespace websocket From ab2a132bffaeb7c4370bd37e43fd86f543180e9d Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 21 Jun 2022 02:25:42 +0300 Subject: [PATCH 2/4] fixed error in json float dump (where a pointer was checked against nullptr instead of '\0') --- include/crow/json.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/include/crow/json.h b/include/crow/json.h index 631ca4039..bc43840db 100644 --- a/include/crow/json.h +++ b/include/crow/json.h @@ -1836,12 +1836,12 @@ namespace crow enum { start, - decp, + decp, // Decimal point zero } f_state; char outbuf[128]; MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d); - char *p = &outbuf[0], *o = nullptr; + char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0 f_state = start; while (*p != '\0') { @@ -1849,15 +1849,17 @@ namespace crow char ch = *p; switch (f_state) { - case start: + case start: // Loop and lookahead until a decimal point is found if (ch == '.') { - if (p + 1 && *(p + 1) == '0') p++; + char fch = *(p + 1); + // if the first character is 0, leave it be (this is so that "1.00000" becomes "1.0" and not "1.") + if ( fch != '\0' && fch == '0') p++; f_state = decp; } p++; break; - case decp: + case decp: // Loop until a 0 is found, if found, record its position if (ch == '0') { f_state = zero; @@ -1865,7 +1867,7 @@ namespace crow } p++; break; - case zero: + case zero: // if a non 0 is found (e.g. 1.00004) remove the earlier recorded 0 position and look for more trailing 0s if (ch != '0') { o = nullptr; @@ -1875,7 +1877,7 @@ namespace crow break; } } - if (o != nullptr) + if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin *o = '\0'; out += outbuf; #undef MSC_COMPATIBLE_SPRINTF From ecc06fa0b319a65f566cbb1b99867770c2f1735a Mon Sep 17 00:00:00 2001 From: Farook Al-Sammarraie Date: Tue, 21 Jun 2022 02:32:19 +0300 Subject: [PATCH 3/4] added missing `&` signs in guide --- docs/guides/websockets.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/guides/websockets.md b/docs/guides/websockets.md index 4782c6322..57f568d6d 100644 --- a/docs/guides/websockets.md +++ b/docs/guides/websockets.md @@ -7,9 +7,9 @@ A websocket route differs from a normal route quite a bit. It uses A slightly al - `#!cpp onaccept([&](const crow::request& req, void** userdata){handler code goes here})` - `#!cpp onopen([&](crow::websocket::connection& conn){handler code goes here})` -- `#!cpp onmessage([&](crow::websocket::connection& conn, const std::string message, bool is_binary){handler code goes here})` -- `#!cpp onerror([&](crow::websocket::connection& conn, const std::string error_message){handler code goes here})` -- `#!cpp onclose([&](crow::websocket::connection& conn, const std::string reason){handler code goes here})` +- `#!cpp onmessage([&](crow::websocket::connection& conn, const std::string& message, bool is_binary){handler code goes here})` +- `#!cpp onerror([&](crow::websocket::connection& conn, const std::string& error_message){handler code goes here})` +- `#!cpp onclose([&](crow::websocket::connection& conn, const std::string& reason){handler code goes here})` !!! note From 454dabdba29aa438bd4d98b749989edd580fb6b4 Mon Sep 17 00:00:00 2001 From: Farook Al-Sammarraie Date: Tue, 21 Jun 2022 02:56:07 +0300 Subject: [PATCH 4/4] formatting --- include/crow/json.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/crow/json.h b/include/crow/json.h index bc43840db..dee71261c 100644 --- a/include/crow/json.h +++ b/include/crow/json.h @@ -1852,9 +1852,9 @@ namespace crow case start: // Loop and lookahead until a decimal point is found if (ch == '.') { - char fch = *(p + 1); + char fch = *(p + 1); // if the first character is 0, leave it be (this is so that "1.00000" becomes "1.0" and not "1.") - if ( fch != '\0' && fch == '0') p++; + if (fch != '\0' && fch == '0') p++; f_state = decp; } p++;