Merge pull request #476 from CrowCpp/ws-error-reason

Add error message to Websocket `onerror` handler
This commit is contained in:
Farook Al-Sammarraie 2022-06-21 14:29:57 +03:00 committed by GitHub
commit 8d7e53d40e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 20 deletions

View File

@ -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 onaccept([&](const crow::request& req, void** userdata){handler code goes here})`
- `#!cpp onopen([&](crow::websocket::connection& conn){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 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})` - `#!cpp onclose([&](crow::websocket::connection& conn, const std::string& reason){handler code goes here})`
!!! note !!! note

View File

@ -1836,12 +1836,12 @@ namespace crow
enum enum
{ {
start, start,
decp, decp, // Decimal point
zero zero
} f_state; } f_state;
char outbuf[128]; char outbuf[128];
MSC_COMPATIBLE_SPRINTF(outbuf, "%f", v.num.d); 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; f_state = start;
while (*p != '\0') while (*p != '\0')
{ {
@ -1849,15 +1849,17 @@ namespace crow
char ch = *p; char ch = *p;
switch (f_state) switch (f_state)
{ {
case start: case start: // Loop and lookahead until a decimal point is found
if (ch == '.') 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; f_state = decp;
} }
p++; p++;
break; break;
case decp: case decp: // Loop until a 0 is found, if found, record its position
if (ch == '0') if (ch == '0')
{ {
f_state = zero; f_state = zero;
@ -1865,7 +1867,7 @@ namespace crow
} }
p++; p++;
break; 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') if (ch != '0')
{ {
o = nullptr; o = nullptr;
@ -1875,7 +1877,7 @@ namespace crow
break; break;
} }
} }
if (o != nullptr) if (o != nullptr) // if any trailing 0s are found, terminate the string where they begin
*o = '\0'; *o = '\0';
out += outbuf; out += outbuf;
#undef MSC_COMPATIBLE_SPRINTF #undef MSC_COMPATIBLE_SPRINTF

View File

@ -508,7 +508,7 @@ namespace crow
std::function<void(crow::websocket::connection&)> open_handler_; std::function<void(crow::websocket::connection&)> open_handler_;
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_; std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_; std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
std::function<void(crow::websocket::connection&)> error_handler_; std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
std::function<bool(const crow::request&, void**)> accept_handler_; std::function<bool(const crow::request&, void**)> accept_handler_;
uint64_t max_payload_; uint64_t max_payload_;
bool max_payload_override_ = false; bool max_payload_override_ = false;

View File

@ -75,7 +75,7 @@ namespace crow
std::function<void(crow::websocket::connection&)> open_handler, std::function<void(crow::websocket::connection&)> open_handler,
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler, std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler,
std::function<void(crow::websocket::connection&, const std::string&)> close_handler, std::function<void(crow::websocket::connection&, const std::string&)> close_handler,
std::function<void(crow::websocket::connection&)> error_handler, std::function<void(crow::websocket::connection&, const std::string&)> error_handler,
std::function<bool(const crow::request&, void**)> accept_handler): std::function<bool(const crow::request&, void**)> accept_handler):
adaptor_(std::move(adaptor)), adaptor_(std::move(adaptor)),
handler_(handler), handler_(handler),
@ -315,7 +315,7 @@ namespace crow
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, "Client connection not masked.");
check_destroy(); check_destroy();
#endif #endif
} }
@ -341,7 +341,7 @@ namespace crow
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, ec.message());
check_destroy(); check_destroy();
} }
}); });
@ -379,7 +379,7 @@ namespace crow
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, ec.message());
check_destroy(); check_destroy();
} }
}); });
@ -414,7 +414,7 @@ namespace crow
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, ec.message());
check_destroy(); check_destroy();
} }
}); });
@ -426,7 +426,7 @@ namespace crow
close_connection_ = true; close_connection_ = true;
adaptor_.close(); adaptor_.close();
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, "Message length exceeds maximum paylaod.");
check_destroy(); check_destroy();
} }
else if (has_mask_) else if (has_mask_)
@ -455,7 +455,7 @@ namespace crow
{ {
close_connection_ = true; close_connection_ = true;
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, ec.message());
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
check_destroy(); check_destroy();
@ -497,7 +497,7 @@ namespace crow
{ {
close_connection_ = true; close_connection_ = true;
if (error_handler_) if (error_handler_)
error_handler_(*this); error_handler_(*this, ec.message());
adaptor_.shutdown_readwrite(); adaptor_.shutdown_readwrite();
adaptor_.close(); adaptor_.close();
check_destroy(); check_destroy();
@ -685,7 +685,7 @@ namespace crow
std::function<void(crow::websocket::connection&)> open_handler_; std::function<void(crow::websocket::connection&)> open_handler_;
std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_; std::function<void(crow::websocket::connection&, const std::string&, bool)> message_handler_;
std::function<void(crow::websocket::connection&, const std::string&)> close_handler_; std::function<void(crow::websocket::connection&, const std::string&)> close_handler_;
std::function<void(crow::websocket::connection&)> error_handler_; std::function<void(crow::websocket::connection&, const std::string&)> error_handler_;
std::function<bool(const crow::request&, void**)> accept_handler_; std::function<bool(const crow::request&, void**)> accept_handler_;
}; };
} // namespace websocket } // namespace websocket