Skip to content

Commit 92a4ddf

Browse files
KaloNKbligetifhessel
authored
Merge fhessel#89 from fhessel/esp32_https_server (#7)
* 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. * 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. * Get detailed SSL_read error * Update src/HTTPSConnection.cpp Co-authored-by: Frank Hessel <[email protected]> * Update src/HTTPConnection.cpp Co-authored-by: Frank Hessel <[email protected]> * correct the copy-paste error Co-authored-by: bligeti <[email protected]> Co-authored-by: Frank Hessel <[email protected]>
1 parent 3003bda commit 92a4ddf

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/HTTPConnection.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ void HTTPConnection::closeConnection() {
139139

140140
if (_wsHandler != nullptr) {
141141
HTTPS_LOGD("Free WS Handler");
142+
_wsHandler->onClose();
142143
delete _wsHandler;
143144
_wsHandler = NULL;
144145
}
@@ -206,7 +207,7 @@ int HTTPConnection::updateBuffer() {
206207
} else {
207208
// An error occured
208209
_connectionState = STATE_ERROR;
209-
HTTPS_LOGE("An receive error occured, FID=%d", _socket);
210+
HTTPS_LOGE("An receive error occurred, FID=%d, code=%d", _socket, readReturnCode);
210211
closeConnection();
211212
return -1;
212213
}
@@ -254,7 +255,7 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) {
254255

255256
size_t HTTPConnection::pendingBufferSize() {
256257
updateBuffer();
257-
258+
if (isClosed()) return 0;
258259
return _bufferUnusedIdx - _bufferProcessed + pendingByteCount();
259260
}
260261

@@ -586,11 +587,15 @@ void HTTPConnection::loop() {
586587
}
587588

588589
// If the handler has terminated the connection, clean up and close the socket too
589-
if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) {
590-
HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket);
591-
delete _wsHandler;
592-
_wsHandler = nullptr;
593-
_connectionState = STATE_CLOSING;
590+
if (_wsHandler != nullptr){
591+
if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) {
592+
HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket);
593+
delete _wsHandler;
594+
_wsHandler = nullptr;
595+
_connectionState = STATE_CLOSING;
596+
}
597+
} else {
598+
HTTPS_LOGI("WS closed due to SSL level issue and cleanded up");
594599
}
595600
break;
596601
default:;

src/HTTPSConnection.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) {
109109
}
110110

111111
size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) {
112-
return SSL_read(_ssl, buffer, length);
112+
int ret = SSL_read(_ssl, buffer, length);
113+
if (ret < 0) {
114+
HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret));
115+
}
116+
return ret;
113117
}
114118

115119
size_t HTTPSConnection::pendingByteCount() {

0 commit comments

Comments
 (0)