[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/m4k3r-org/esp32_https_server/master/https/HTTPSConnection.cpp [Back]  [Original]

/*
 * HTTPSConnection.cpp
 *
 *  Created on: Dec 6, 2017
 *      Author: frank
 */

#include "HTTPSConnection.hpp"

namespace httpsserver {


HTTPSConnection::HTTPSConnection(ResourceResolver * resResolver):
	_resResolver(resResolver) {
	_socket = -1;
	_addrLen = 0;
	_ssl = NULL;

	_bufferProcessed = 0;
	_bufferUnusedIdx = 0;

	_connectionState = STATE_UNDEFINED;
	_clientState = CSTATE_UNDEFINED;
	_httpHeaders = NULL;
	_defaultHeaders = NULL;
	_isKeepAlive = false;
	_lastTransmissionTS = millis();
}

HTTPSConnection::~HTTPSConnection() {
	// Close the socket
	closeConnection();
}

/**
 * Initializes the connection from a server socket.
 *
 * The call WILL BLOCK if accept(serverSocketID) blocks. So use select() to check for that in advance.
 */
int HTTPSConnection::initialize(int serverSocketID, SSL_CTX * sslCtx, HTTPHeaders *defaultHeaders) {
	if (_connectionState == STATE_UNDEFINED) {
		_defaultHeaders = defaultHeaders;
		_socket = accept(serverSocketID, (struct sockaddr * )&_sockAddr, &_addrLen);

		// Build up SSL Connection context if the socket has been created successfully
		if (_socket >= 0) {
			HTTPS_DLOGHEX("[-->] New connection. Socket fid is: ", _socket);

			_ssl = SSL_new(sslCtx);

			if (_ssl) {
				// Bind SSL to the socket
				int success = SSL_set_fd(_ssl, _socket);
				if (success) {

					// Perform the handshake
					success = SSL_accept(_ssl);
					if (success) {
						_connectionState = STATE_INITIAL;
						_httpHeaders = new HTTPHeaders();
						refreshTimeout();
						return _socket;
					} else {
						HTTPS_DLOG("[ERR] SSL_accept failed. Aborting handshake.");
					}
				} else {
					HTTPS_DLOG("[ERR] SSL_set_fd failed. Aborting handshake.");
				}
			} else {
				HTTPS_DLOG("[ERR] SSL_new failed. Aborting handshake.");
			}
		}
		_connectionState = STATE_ERROR;
		_clientState = CSTATE_ACTIVE;

		// This will only be called if the connection could not be established and cleanup
		// variables like _ssl etc.
		closeConnection();
	}
	// Error: The connection has already been established or could not be established
	return -1;
}


/**
 * True if the connection is timed out.
 *
 * (Should be checkd in the loop and transition should go to CONNECTION_CLOSE if exceeded)
 */
bool HTTPSConnection::isTimeoutExceeded() {
	return _lastTransmissionTS + HTTPS_CONNECTION_TIMEOUT < millis();
}

/**
 * Resets the timeout to allow again the full HTTPS_CONNECTION_TIMEOUT milliseconds
 */
void HTTPSConnection::refreshTimeout() {
	_lastTransmissionTS = millis();
}

/**
 * Returns true, if the connection has been closed.
 */
bool HTTPSConnection::isClosed() {
	return (_connectionState == STATE_ERROR || _connectionState == STATE_CLOSED);
}

/**
 * Returns true, if the connection has been closed due to error
 */
bool HTTPSConnection::isError() {
	return (_connectionState == STATE_ERROR);
}

void HTTPSConnection::closeConnection() {
	// TODO: Call an event handler here, maybe?

	// Tear down SSL
	if (_ssl) {
		// wait here so that SSL_shutdown returns 1
		while(SSL_shutdown(_ssl) == 0) delay(1);
		SSL_free(_ssl);
		_ssl = NULL;
	}

	// Tear down the socket
	if (_socket >= 0) {
		HTTPS_DLOGHEX("[

Web Proxy Viewer  |  New URL  |  Original Page