Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 0034c38

Browse files
authored
Merge many PRs
Merge some upstream PRs - Handling of errors - like unstable network - coming via SSL fhessel#89 - WIP: Prevent crash on WebSocket request to non-WebSocket node. fhessel#106 - Fix infinite loop when the buffer ends with \r fhessel#123 - Fixed memory leak in the Websocket example fhessel#157
1 parent c3dcebd commit 0034c38

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/HTTPConnection.cpp

+21-11
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ void HTTPConnection::closeConnection() {
137137
_httpHeaders = NULL;
138138
}
139139

140-
if (_wsHandler != nullptr) {
140+
if (_wsHandler != nullptr)
141+
{
141142
HTTPS_LOGD("Free WS Handler");
143+
_wsHandler->onClose();
142144
delete _wsHandler;
143145
_wsHandler = NULL;
144146
}
@@ -206,7 +208,7 @@ int HTTPConnection::updateBuffer() {
206208
} else {
207209
// An error occured
208210
_connectionState = STATE_ERROR;
209-
HTTPS_LOGE("An receive error occured, FID=%d", _socket);
211+
HTTPS_LOGE("A receive error occurred, FID=%d, code=%d", _socket, readReturnCode);
210212
closeConnection();
211213
return -1;
212214
}
@@ -252,9 +254,13 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) {
252254
return length;
253255
}
254256

255-
size_t HTTPConnection::pendingBufferSize() {
257+
size_t HTTPConnection::pendingBufferSize()
258+
{
256259
updateBuffer();
257-
260+
261+
if (isClosed())
262+
return 0;
263+
258264
return _bufferUnusedIdx - _bufferProcessed + pendingByteCount();
259265
}
260266

@@ -588,17 +594,21 @@ void HTTPConnection::loop() {
588594
}
589595

590596
// If the handler has terminated the connection, clean up and close the socket too
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;
597+
if (_wsHandler != nullptr){
598+
if (_wsHandler->closed() || _clientState == CSTATE_CLOSED) {
599+
HTTPS_LOGI("WS closed, freeing Handler, FID=%d", _socket);
600+
delete _wsHandler;
601+
_wsHandler = nullptr;
602+
_connectionState = STATE_CLOSING;
603+
}
604+
} else {
605+
HTTPS_LOGI("WS closed due to SSL level issue and cleanded up");
596606
}
597607
break;
598-
default:;
608+
default:
609+
break;
599610
}
600611
}
601-
602612
}
603613

604614

src/HTTPSCallbackFunction.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace httpsserver {
88
/**
99
* \brief A callback function that will be called by the server to handle a request
1010
*/
11-
typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res);
11+
typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res);
1212
}
1313

1414
#endif /* SRC_HTTPSCALLBACKFUNCTION_HPP_ */

src/HTTPSConnection.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,16 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) {
108108
return SSL_write(_ssl, buffer, length);
109109
}
110110

111-
size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) {
112-
return SSL_read(_ssl, buffer, length);
111+
size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length)
112+
{
113+
int ret = SSL_read(_ssl, buffer, length);
114+
115+
if (ret < 0)
116+
{
117+
HTTPS_LOGD("SSL_read error: %d", SSL_get_error(_ssl, ret));
118+
}
119+
120+
return ret;
113121
}
114122

115123
size_t HTTPSConnection::pendingByteCount() {

src/ResourceNode.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace httpsserver {
1010

1111
/**
1212
* \brief This HTTPNode represents a route that maps to a regular HTTP request for a resource (static or dynamic)
13-
*
13+
*
1414
* It therefore contrasts to the WebsocketNode, which handles requests for Websockets.
1515
*/
1616
class ResourceNode : public HTTPNode {

0 commit comments

Comments
 (0)