Merge pull request #49964 from Calinou/websocketserver-improve-error-reporting

Improve error reporting in WebSocketServer
This commit is contained in:
Rémi Verschelde 2021-07-06 13:38:43 +02:00 committed by GitHub
commit 8771e910e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -97,26 +97,31 @@ bool WSLServer::PendingPeer::_parse_request(const Vector<String> p_protocols) {
Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uint64_t p_timeout) { Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uint64_t p_timeout) {
if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) { if (OS::get_singleton()->get_ticks_msec() - time > p_timeout) {
print_verbose(vformat("WebSocket handshake timed out after %.3f seconds.", p_timeout * 0.001));
return ERR_TIMEOUT; return ERR_TIMEOUT;
} }
if (use_ssl) { if (use_ssl) {
Ref<StreamPeerSSL> ssl = static_cast<Ref<StreamPeerSSL>>(connection); Ref<StreamPeerSSL> ssl = static_cast<Ref<StreamPeerSSL>>(connection);
if (ssl.is_null()) { if (ssl.is_null()) {
return FAILED; ERR_FAIL_V_MSG(ERR_BUG, "Couldn't get StreamPeerSSL for WebSocket handshake.");
} }
ssl->poll(); ssl->poll();
if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) { if (ssl->get_status() == StreamPeerSSL::STATUS_HANDSHAKING) {
return ERR_BUSY; return ERR_BUSY;
} else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) { } else if (ssl->get_status() != StreamPeerSSL::STATUS_CONNECTED) {
print_verbose(vformat("WebSocket SSL connection error during handshake (StreamPeerSSL status code %d).", ssl->get_status()));
return FAILED; return FAILED;
} }
} }
if (!has_request) { if (!has_request) {
int read = 0; int read = 0;
while (true) { while (true) {
ERR_FAIL_COND_V_MSG(req_pos >= WSL_MAX_HEADER_SIZE, ERR_OUT_OF_MEMORY, "Response headers too big."); ERR_FAIL_COND_V_MSG(req_pos >= WSL_MAX_HEADER_SIZE, ERR_OUT_OF_MEMORY, "WebSocket response headers are too big.");
Error err = connection->get_partial_data(&req_buf[req_pos], 1, read); Error err = connection->get_partial_data(&req_buf[req_pos], 1, read);
if (err != OK) { // Got an error if (err != OK) { // Got an error
print_verbose(vformat("WebSocket error while getting partial data (StreamPeer error code %d).", err));
return FAILED; return FAILED;
} else if (read != 1) { // Busy, wait next poll } else if (read != 1) { // Busy, wait next poll
return ERR_BUSY; return ERR_BUSY;
@ -143,17 +148,21 @@ Error WSLServer::PendingPeer::do_handshake(const Vector<String> p_protocols, uin
req_pos += 1; req_pos += 1;
} }
} }
if (has_request && response_sent < response.size() - 1) { if (has_request && response_sent < response.size() - 1) {
int sent = 0; int sent = 0;
Error err = connection->put_partial_data((const uint8_t *)response.get_data() + response_sent, response.size() - response_sent - 1, sent); Error err = connection->put_partial_data((const uint8_t *)response.get_data() + response_sent, response.size() - response_sent - 1, sent);
if (err != OK) { if (err != OK) {
print_verbose(vformat("WebSocket error while putting partial data (StreamPeer error code %d).", err));
return err; return err;
} }
response_sent += sent; response_sent += sent;
} }
if (response_sent < response.size() - 1) { if (response_sent < response.size() - 1) {
return ERR_BUSY; return ERR_BUSY;
} }
return OK; return OK;
} }