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

Commit

Permalink
Merge many PRs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
khoih-prog authored Sep 27, 2022
1 parent c3dcebd commit 0034c38
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
32 changes: 21 additions & 11 deletions src/HTTPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ void HTTPConnection::closeConnection() {
_httpHeaders = NULL;
}

if (_wsHandler != nullptr) {
if (_wsHandler != nullptr)
{
HTTPS_LOGD("Free WS Handler");
_wsHandler->onClose();
delete _wsHandler;
_wsHandler = NULL;
}
Expand Down Expand Up @@ -206,7 +208,7 @@ int HTTPConnection::updateBuffer() {
} else {
// An error occured
_connectionState = STATE_ERROR;
HTTPS_LOGE("An receive error occured, FID=%d", _socket);
HTTPS_LOGE("A receive error occurred, FID=%d, code=%d", _socket, readReturnCode);
closeConnection();
return -1;
}
Expand Down Expand Up @@ -252,9 +254,13 @@ size_t HTTPConnection::readBuffer(byte* buffer, size_t length) {
return length;
}

size_t HTTPConnection::pendingBufferSize() {
size_t HTTPConnection::pendingBufferSize()
{
updateBuffer();


if (isClosed())
return 0;

return _bufferUnusedIdx - _bufferProcessed + pendingByteCount();
}

Expand Down Expand Up @@ -588,17 +594,21 @@ 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:;
default:
break;
}
}

}


Expand Down
2 changes: 1 addition & 1 deletion src/HTTPSCallbackFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace httpsserver {
/**
* \brief A callback function that will be called by the server to handle a request
*/
typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res);
typedef void (HTTPSCallbackFunction)(HTTPRequest * req, HTTPResponse * res);
}

#endif /* SRC_HTTPSCALLBACKFUNCTION_HPP_ */
12 changes: 10 additions & 2 deletions src/HTTPSConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,16 @@ size_t HTTPSConnection::writeBuffer(byte* buffer, size_t length) {
return SSL_write(_ssl, buffer, length);
}

size_t HTTPSConnection::readBytesToBuffer(byte* buffer, size_t length) {
return SSL_read(_ssl, buffer, 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));
}

return ret;
}

size_t HTTPSConnection::pendingByteCount() {
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace httpsserver {

/**
* \brief This HTTPNode represents a route that maps to a regular HTTP request for a resource (static or dynamic)
*
*
* It therefore contrasts to the WebsocketNode, which handles requests for Websockets.
*/
class ResourceNode : public HTTPNode {
Expand Down

0 comments on commit 0034c38

Please sign in to comment.