GitHub Viewer
/*
* HTTPSConnection.hpp
*
* Created on: Dec 6, 2017
* Author: frank
*/
#ifndef HTTPS_HTTPSCONNECTION_HPP_
#define HTTPS_HTTPSCONNECTION_HPP_
#include
#include
// Required for SSL
#include "openssl/ssl.h"
#undef read
// Required for sockets
#include "lwip/netdb.h"
#undef read
#include "lwip/sockets.h"
#include "HTTPSServerConstants.hpp"
#include "HTTPHeaders.hpp"
#include "HTTPHeader.hpp"
#include "ResourceResolver.hpp"
#include "ResolvedResource.hpp"
#include "ResourceNode.hpp"
#include "HTTPRequest.hpp"
#include "HTTPResponse.hpp"
namespace httpsserver {
class HTTPSConnection : private ConnectionContext {
public:
HTTPSConnection(ResourceResolver * resResolver);
virtual ~HTTPSConnection();
int initialize(int serverSocketID, SSL_CTX * sslCtx, HTTPHeaders *defaultHeaders);
void loop();
void closeConnection();
bool isClosed();
bool isError();
private:
int updateBuffer();
void serverError();
void clientError();
void readLine(int lengthLimit);
bool isTimeoutExceeded();
void refreshTimeout();
SSL* ssl() {return _ssl;};
void signalClientClose();
void signalRequestError();
size_t getCacheSize();
size_t readBuffer(byte* buffer, size_t length);
size_t pendingBufferSize();
// The receive buffer
char _receiveBuffer[HTTPS_CONNECTION_DATA_CHUNK_SIZE];
// First index on _receive_buffer that has not been processed yet (anything before may be discarded)
int _bufferProcessed;
// The index on the receive_buffer that is the first one which is empty at the end.
int _bufferUnusedIdx;
// Socket address, length etc for the connection
struct sockaddr _sockAddr;
socklen_t _addrLen;
int _socket;
// SSL context for this connection
SSL * _ssl;
// Resource resolver used to resolve resources
ResourceResolver * _resResolver;
// The parser line. The struct is used to read the next line up to the \r\n in readLine()
struct {
std::string text = "";
bool parsingFinished = false;
} _parserLine;
// HTTP properties: Method, Request, Headers
std::string _httpMethod;
std::string _httpResource;
HTTPHeaders * _httpHeaders;
// Default headers that are applied to every response
HTTPHeaders * _defaultHeaders;
// Should we use keep alive
bool _isKeepAlive;
// Timestamp of the last transmission action
unsigned long _lastTransmissionTS;
// Timestamp of when the shutdown was started
unsigned long _shutdownTS;
// Internal state machine of the connection:
//
// O --- > STATE_UNDEFINED -- initialize() --> STATE_INITIAL -- get / http/1.1 --> STATE_REQUEST_FINISHED --.
// | | | |
// | | | | Host: ...\r\n
// STATE_ERROR