Skip to content

Commit d730f87

Browse files
committed
Fix crash and logic issues
1 parent 896f177 commit d730f87

File tree

4 files changed

+15
-33
lines changed

4 files changed

+15
-33
lines changed

src/HTTPConnection.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ HTTPConnection::HTTPConnection(ResourceResolver * resResolver):
1212

1313
_connectionState = STATE_UNDEFINED;
1414
_clientState = CSTATE_UNDEFINED;
15-
_httpHeaders = NULL;
1615
_defaultHeaders = NULL;
1716
_isKeepAlive = false;
1817
_lastTransmissionTS = millis();
@@ -40,7 +39,7 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
4039
if (_socket >= 0) {
4140
HTTPS_LOGI("New connection. Socket FID=%d", _socket);
4241
_connectionState = STATE_INITIAL;
43-
_httpHeaders = new HTTPHeaders();
42+
_httpHeaders.clearAll();
4443
refreshTimeout();
4544
return _socket;
4645
}
@@ -131,11 +130,8 @@ void HTTPConnection::closeConnection() {
131130
_connectionState = STATE_CLOSED;
132131
}
133132

134-
if (_httpHeaders != NULL) {
135-
HTTPS_LOGD("Free headers");
136-
delete _httpHeaders;
137-
_httpHeaders = NULL;
138-
}
133+
HTTPS_LOGD("Free headers");
134+
_httpHeaders.clearAll();
139135

140136
if (_wsHandler != nullptr) {
141137
HTTPS_LOGD("Free WS Handler");
@@ -408,7 +404,7 @@ void HTTPConnection::loop() {
408404
} else {
409405
int idxColon = _parserLine.text.find(':');
410406
if ( (idxColon != std::string::npos) && (_parserLine.text[idxColon+1]==' ') ) {
411-
_httpHeaders->set(new HTTPHeader(
407+
_httpHeaders.set(new HTTPHeader(
412408
_parserLine.text.substr(0, idxColon),
413409
_parserLine.text.substr(idxColon+2)
414410
));
@@ -441,7 +437,7 @@ void HTTPConnection::loop() {
441437
// Check for client's request to keep-alive if we have a handler function.
442438
if (resolvedResource.getMatchingNode()->_nodeType == HANDLER_CALLBACK) {
443439
// Did the client set connection:keep-alive?
444-
HTTPHeader * connectionHeader = _httpHeaders->get("Connection");
440+
HTTPHeader * connectionHeader = _httpHeaders.get("Connection");
445441
std::string connectionHeaderValue = "";
446442
if (connectionHeader != NULL) {
447443
connectionHeaderValue += connectionHeader->_value;
@@ -466,7 +462,7 @@ void HTTPConnection::loop() {
466462
// Create request context
467463
HTTPRequest req = HTTPRequest(
468464
this,
469-
_httpHeaders,
465+
&_httpHeaders,
470466
resolvedResource.getMatchingNode(),
471467
_httpMethod,
472468
resolvedResource.getParams(),
@@ -549,7 +545,7 @@ void HTTPConnection::loop() {
549545
// Refresh the timeout for the new request
550546
refreshTimeout();
551547
// Reset headers for the new connection
552-
_httpHeaders->clearAll();
548+
_httpHeaders.clearAll();
553549
// Go back to initial state
554550
_connectionState = STATE_INITIAL;
555551
}
@@ -604,11 +600,11 @@ void HTTPConnection::loop() {
604600

605601
bool HTTPConnection::checkWebsocket() {
606602
if(_httpMethod == "GET" &&
607-
!_httpHeaders->getValue("Host").empty() &&
608-
_httpHeaders->getValue("Upgrade") == "websocket" &&
609-
_httpHeaders->getValue("Connection").find("Upgrade") != std::string::npos &&
610-
!_httpHeaders->getValue("Sec-WebSocket-Key").empty() &&
611-
_httpHeaders->getValue("Sec-WebSocket-Version") == "13") {
603+
!_httpHeaders.getValue("Host").empty() &&
604+
_httpHeaders.getValue("Upgrade") == "websocket" &&
605+
_httpHeaders.getValue("Connection").find("Upgrade") != std::string::npos &&
606+
!_httpHeaders.getValue("Sec-WebSocket-Key").empty() &&
607+
_httpHeaders.getValue("Sec-WebSocket-Version") == "13") {
612608

613609
HTTPS_LOGI("Upgrading to WS, FID=%d", _socket);
614610
return true;

src/HTTPConnection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class HTTPConnection : private ConnectionContext {
149149
// HTTP properties: Method, Request, Headers
150150
std::string _httpMethod;
151151
std::string _httpResource;
152-
HTTPHeaders * _httpHeaders;
152+
HTTPHeaders _httpHeaders;
153153

154154
// Default headers that are applied to every response
155155
HTTPHeaders * _defaultHeaders;

src/HTTPSConnection.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,8 @@ int HTTPSConnection::initialize(int serverSocketID, mbedtls_ssl_config *sslConfi
8888
return -1;
8989
}
9090

91-
int HTTPSConnection::shutdown() {
92-
int res;
93-
while (true) {
94-
res = mbedtls_ssl_close_notify(&_ssl);
95-
if (res == 0) {
96-
return 1;
97-
}
98-
if (res != MBEDTLS_ERR_SSL_WANT_WRITE) {
99-
return 0;
100-
}
101-
}
102-
}
10391

10492
void HTTPSConnection::closeConnection() {
105-
10693
// FIXME: Copy from HTTPConnection, could be done better probably
10794
if (_connectionState != STATE_ERROR && _connectionState != STATE_CLOSED) {
10895

@@ -118,8 +105,8 @@ void HTTPSConnection::closeConnection() {
118105

119106
// Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
120107
if (_sslCreated) {
121-
if (_connectionState == STATE_ERROR || shutdown() == 0) {
122-
// SSL shutdown will return 1 as soon as the client answered with close notify
108+
if (_connectionState == STATE_ERROR || mbedtls_ssl_close_notify(&_ssl) == 0) {
109+
// SSL shutdown will return 0 as soon as the client answered with close notify
123110
// This means we are safe to close the socket
124111
mbedtls_ssl_free(&_ssl);
125112
_sslCreated = false;

src/HTTPSConnection.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class HTTPSConnection : public HTTPConnection {
3939
virtual bool isSecure();
4040
bool setup(mbedtls_ssl_config *sslConfig);
4141
bool handshake();
42-
int shutdown();
4342

4443
protected:
4544
friend class HTTPRequest;

0 commit comments

Comments
 (0)