From fea072d4cc2622885e3e6921809446d0353a2394 Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Tue, 12 May 2020 23:01:45 +0200 Subject: [PATCH 1/6] Trigger wsHander onClose from closeConnection() In case closeConnection() is called due to SSL reported error - like session disconnection due to unstable Wifi - onClose() should be called to free up the recorded websocket handlers. --- src/HTTPConnection.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index 0ab739c..60c44bb 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -133,6 +133,7 @@ void HTTPConnection::closeConnection() { if (_httpHeaders != NULL) { HTTPS_LOGD("Free headers"); + _wsHandler->onClose(); delete _httpHeaders; _httpHeaders = NULL; } From 276ec451695a7506af36f1dd585fde6225b9f481 Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Tue, 12 May 2020 23:13:23 +0200 Subject: [PATCH 2/6] Handle error triggered by closed sessions From updateBuffer() a session clean up is initiated via closeConnection() if error is detected when reading from SSL but the already removed session is not handled in other parts of the state machine. --- src/HTTPConnection.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index 60c44bb..c6069b0 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -207,7 +207,7 @@ int HTTPConnection::updateBuffer() { } else { // An error occured _connectionState = STATE_ERROR; - HTTPS_LOGE("An receive error occured, FID=%d", _socket); + HTTPS_LOGE("An receive error occured, FID=%d, SSL_error=%d", _socket, readReturnCode); closeConnection(); return -1; } @@ -255,7 +255,7 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) { size_t HTTPConnection::pendingBufferSize() { updateBuffer(); - + if (isClosed()) return 0; return _bufferUnusedIdx - _bufferProcessed + pendingByteCount(); } @@ -589,11 +589,15 @@ void HTTPConnection::loop() { } // If the handler has terminated the connection, clean up and close the socket too - if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) { - HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket); - delete _wsHandler; - _wsHandler = nullptr; - _connectionState = STATE_CLOSING; + if (_wsHandler != nullptr){ + if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) { + HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket); + delete _wsHandler; + _wsHandler = nullptr; + _connectionState = STATE_CLOSING; + } + } else { + HTTPS_LOGI("WS closed due to SSL level issue and cleanded up"); } break; default:; From 586cdec9ced705b83a3178b5b1cbea5834acd9bf Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Tue, 12 May 2020 23:19:25 +0200 Subject: [PATCH 3/6] Get detailed SSL_read error --- src/HTTPSConnection.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/HTTPSConnection.cpp b/src/HTTPSConnection.cpp index e0e3dd0..5169ee6 100644 --- a/src/HTTPSConnection.cpp +++ b/src/HTTPSConnection.cpp @@ -109,7 +109,11 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) { } size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) { - return SSL_read(_ssl, buffer, length); + int ret = SSL_read(_ssl, buffer, length); + if (ret < 0) { + HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret)); + } + return ret; } size_t HTTPSConnection::pendingByteCount() { From 34b3beba7b5575058e26aa73afcd29e47d2f23b8 Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Mon, 22 Jun 2020 01:02:42 +0200 Subject: [PATCH 4/6] Update src/HTTPSConnection.cpp Co-authored-by: Frank Hessel --- src/HTTPSConnection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTPSConnection.cpp b/src/HTTPSConnection.cpp index 5169ee6..e500ff9 100644 --- a/src/HTTPSConnection.cpp +++ b/src/HTTPSConnection.cpp @@ -111,7 +111,7 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) { size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) { int ret = SSL_read(_ssl, buffer, length); if (ret < 0) { - HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret)); + HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret)); } return ret; } From 985b9020fe7e6d793a6588f6ce282779229d4af2 Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Mon, 22 Jun 2020 01:22:56 +0200 Subject: [PATCH 5/6] Update src/HTTPConnection.cpp Co-authored-by: Frank Hessel --- src/HTTPConnection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index c6069b0..8e496d6 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -207,7 +207,7 @@ int HTTPConnection::updateBuffer() { } else { // An error occured _connectionState = STATE_ERROR; - HTTPS_LOGE("An receive error occured, FID=%d, SSL_error=%d", _socket, readReturnCode); + HTTPS_LOGE("An receive error occurred, FID=%d, code=%d", _socket, readReturnCode); closeConnection(); return -1; } From 2bf7522b0cdb10981b082cf92c607e11d176ba00 Mon Sep 17 00:00:00 2001 From: bligeti <64783210+bligeti@users.noreply.github.com> Date: Mon, 22 Jun 2020 01:25:20 +0200 Subject: [PATCH 6/6] correct the copy-paste error --- src/HTTPConnection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTPConnection.cpp b/src/HTTPConnection.cpp index 8e496d6..e26a846 100644 --- a/src/HTTPConnection.cpp +++ b/src/HTTPConnection.cpp @@ -133,13 +133,13 @@ void HTTPConnection::closeConnection() { if (_httpHeaders != NULL) { HTTPS_LOGD("Free headers"); - _wsHandler->onClose(); delete _httpHeaders; _httpHeaders = NULL; } if (_wsHandler != nullptr) { HTTPS_LOGD("Free WS Handler"); + _wsHandler->onClose(); delete _wsHandler; _wsHandler = NULL; }