X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/HTTPConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ HTTPConnection::HTTPConnection(ResourceResolver * resResolver):

_connectionState = STATE_UNDEFINED;
_clientState = CSTATE_UNDEFINED;
_httpHeaders = NULL;
_defaultHeaders = NULL;
_isKeepAlive = false;
_lastTransmissionTS = millis();
Expand Down Expand Up @@ -40,7 +39,7 @@ int HTTPConnection::initialize(int serverSocketID, HTTPHeaders *defaultHeaders)
if (_socket >= 0) {
HTTPS_LOGI("New connection. Socket FID=%d", _socket);
_connectionState = STATE_INITIAL;
_httpHeaders = new HTTPHeaders();
_httpHeaders.clearAll();
refreshTimeout();
return _socket;
}
Expand Down Expand Up @@ -131,11 +130,8 @@ void HTTPConnection::closeConnection() {
_connectionState = STATE_CLOSED;
}

if (_httpHeaders != NULL) {
HTTPS_LOGD("Free headers");
delete _httpHeaders;
_httpHeaders = NULL;
}
HTTPS_LOGD("Free headers");
_httpHeaders.clearAll();

if (_wsHandler != nullptr) {
HTTPS_LOGD("Free WS Handler");
Expand Down Expand Up @@ -408,7 +404,7 @@ void HTTPConnection::loop() {
} else {
int idxColon = _parserLine.text.find(':');
if ( (idxColon != std::string::npos) && (_parserLine.text[idxColon+1]==' ') ) {
_httpHeaders->set(new HTTPHeader(
_httpHeaders.set(new HTTPHeader(
_parserLine.text.substr(0, idxColon),
_parserLine.text.substr(idxColon+2)
));
Expand Down Expand Up @@ -441,7 +437,7 @@ void HTTPConnection::loop() {
// Check for client's request to keep-alive if we have a handler function.
if (resolvedResource.getMatchingNode()->_nodeType == HANDLER_CALLBACK) {
// Did the client set connection:keep-alive?
HTTPHeader * connectionHeader = _httpHeaders->get("Connection");
HTTPHeader * connectionHeader = _httpHeaders.get("Connection");
std::string connectionHeaderValue = "";
if (connectionHeader != NULL) {
connectionHeaderValue += connectionHeader->_value;
Expand All @@ -466,7 +462,7 @@ void HTTPConnection::loop() {
// Create request context
HTTPRequest req = HTTPRequest(
this,
_httpHeaders,
&_httpHeaders,
resolvedResource.getMatchingNode(),
_httpMethod,
resolvedResource.getParams(),
Expand Down Expand Up @@ -549,7 +545,7 @@ void HTTPConnection::loop() {
// Refresh the timeout for the new request
refreshTimeout();
// Reset headers for the new connection
_httpHeaders->clearAll();
_httpHeaders.clearAll();
// Go back to initial state
_connectionState = STATE_INITIAL;
}
Expand Down Expand Up @@ -604,11 +600,11 @@ void HTTPConnection::loop() {

bool HTTPConnection::checkWebsocket() {
if(_httpMethod == "GET" &&
!_httpHeaders->getValue("Host").empty() &&
_httpHeaders->getValue("Upgrade") == "websocket" &&
_httpHeaders->getValue("Connection").find("Upgrade") != std::string::npos &&
!_httpHeaders->getValue("Sec-WebSocket-Key").empty() &&
_httpHeaders->getValue("Sec-WebSocket-Version") == "13") {
!_httpHeaders.getValue("Host").empty() &&
_httpHeaders.getValue("Upgrade") == "websocket" &&
_httpHeaders.getValue("Connection").find("Upgrade") != std::string::npos &&
!_httpHeaders.getValue("Sec-WebSocket-Key").empty() &&
_httpHeaders.getValue("Sec-WebSocket-Version") == "13") {

HTTPS_LOGI("Upgrading to WS, FID=%d", _socket);
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/HTTPConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class HTTPConnection : private ConnectionContext {
// HTTP properties: Method, Request, Headers
std::string _httpMethod;
std::string _httpResource;
HTTPHeaders * _httpHeaders;
HTTPHeaders _httpHeaders;

// Default headers that are applied to every response
HTTPHeaders * _defaultHeaders;
Expand Down
17 changes: 2 additions & 15 deletions src/HTTPSConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,8 @@ int HTTPSConnection::initialize(int serverSocketID, mbedtls_ssl_config *sslConfi
return -1;
}

int HTTPSConnection::shutdown() {
int res;
while (true) {
res = mbedtls_ssl_close_notify(&_ssl);
if (res == 0) {
return 1;
}
if (res != MBEDTLS_ERR_SSL_WANT_WRITE) {
return 0;
}
}
}

void HTTPSConnection::closeConnection() {

// FIXME: Copy from HTTPConnection, could be done better probably
if (_connectionState != STATE_ERROR && _connectionState != STATE_CLOSED) {

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

// Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
if (_sslCreated) {
if (_connectionState == STATE_ERROR || shutdown() == 0) {
// SSL shutdown will return 1 as soon as the client answered with close notify
if (_connectionState == STATE_ERROR || mbedtls_ssl_close_notify(&_ssl) == 0) {
// SSL shutdown will return 0 as soon as the client answered with close notify
// This means we are safe to close the socket
mbedtls_ssl_free(&_ssl);
_sslCreated = false;
Expand Down
1 change: 0 additions & 1 deletion src/HTTPSConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class HTTPSConnection : public HTTPConnection {
virtual bool isSecure();
bool setup(mbedtls_ssl_config *sslConfig);
bool handshake();
int shutdown();

protected:
friend class HTTPRequest;
Expand Down
X Tutup