Converted static HTTP Buffer to dynamic TempBuffer

Saved about 8K in RAM!
This commit is contained in:
Gregg 2023-07-25 05:54:40 -05:00
parent d6f5612f9f
commit 5f9458e625
3 changed files with 31 additions and 22 deletions

View File

@ -154,16 +154,27 @@ void HAPClient::init(){
void HAPClient::processRequest(){ void HAPClient::processRequest(){
int nBytes; int nBytes, messageSize;
messageSize=client.available();
if(messageSize>MAX_HTTP){ // exceeded maximum number of bytes allowed
badRequestError();
LOG0("\n*** ERROR: HTTP message of %d bytes exceeds maximum allowed (%d)\n\n",messageSize,MAX_HTTP);
return;
}
TempBuffer <uint8_t> tempBuffer(messageSize+1); // leave room for null character added below
uint8_t *httpBuf=tempBuffer.buf;
if(cPair){ // expecting encrypted message if(cPair){ // expecting encrypted message
LOG2("<<<< #### "); LOG2("<<<< #### ");
LOG2(client.remoteIP()); LOG2(client.remoteIP());
LOG2(" #### <<<<\n"); LOG2(" #### <<<<\n");
nBytes=receiveEncrypted(); // decrypt and return number of bytes nBytes=receiveEncrypted(httpBuf,messageSize); // decrypt and return number of bytes read
if(!nBytes){ // decryption failed (error message already printed in function) if(!nBytes){ // decryption failed (error message already printed in function)
badRequestError(); badRequestError();
return; return;
} }
@ -173,11 +184,11 @@ void HAPClient::processRequest(){
LOG2(client.remoteIP()); LOG2(client.remoteIP());
LOG2(" <<<<<<<<<\n"); LOG2(" <<<<<<<<<\n");
nBytes=client.read(httpBuf,MAX_HTTP+1); // read all available bytes up to maximum allowed+1 nBytes=client.read(httpBuf,messageSize); // read expected number of bytes
if(nBytes>MAX_HTTP){ // exceeded maximum number of bytes allowed if(nBytes!=messageSize || client.available()!=0){
badRequestError(); badRequestError();
LOG0("\n*** ERROR: Exceeded maximum HTTP message length\n\n"); LOG0("\n*** ERROR: HTTP message not read correctly. Expected %d bytes, read %d bytes, %d bytes remaining\n\n",messageSize,nBytes,client.available());
return; return;
} }
@ -186,7 +197,7 @@ void HAPClient::processRequest(){
httpBuf[nBytes]='\0'; // add null character to enable string functions httpBuf[nBytes]='\0'; // add null character to enable string functions
char *body=(char *)httpBuf; // char pointer to start of HTTP Body char *body=(char *)httpBuf; // char pointer to start of HTTP Body
char *p; // char pointer used for searches char *p; // char pointer used for searches
if(!(p=strstr((char *)httpBuf,"\r\n\r\n"))){ if(!(p=strstr((char *)httpBuf,"\r\n\r\n"))){
badRequestError(); badRequestError();
@ -1443,7 +1454,7 @@ void HAPClient::tlvRespond(){
////////////////////////////////////// //////////////////////////////////////
int HAPClient::receiveEncrypted(){ int HAPClient::receiveEncrypted(uint8_t *httpBuf, int messageSize){
uint8_t buf[1042]; // maximum size of encoded message = 2+1024+16 bytes (HAP Section 6.5.2) uint8_t buf[1042]; // maximum size of encoded message = 2+1024+16 bytes (HAP Section 6.5.2)
int nBytes=0; int nBytes=0;
@ -1452,8 +1463,8 @@ int HAPClient::receiveEncrypted(){
int n=buf[0]+buf[1]*256; // compute number of bytes expected in encoded message int n=buf[0]+buf[1]*256; // compute number of bytes expected in encoded message
if(nBytes+n>MAX_HTTP){ // exceeded maximum number of bytes allowed in plaintext message if(nBytes+n>messageSize){ // exceeded maximum number of bytes allowed in plaintext message
LOG0("\n\n*** ERROR: Exceeded maximum HTTP message length\n\n"); LOG0("\n\n*** ERROR: Decrypted message of %d bytes exceeded maximum expected message length of %d bytes\n\n",nBytes+n,messageSize);
return(0); return(0);
} }
@ -1722,7 +1733,6 @@ void Nonce::inc(){
TLV<kTLVType,10> HAPClient::tlv8; TLV<kTLVType,10> HAPClient::tlv8;
nvs_handle HAPClient::hapNVS; nvs_handle HAPClient::hapNVS;
nvs_handle HAPClient::srpNVS; nvs_handle HAPClient::srpNVS;
uint8_t HAPClient::httpBuf[MAX_HTTP+1];
HKDF HAPClient::hkdf; HKDF HAPClient::hkdf;
pairState HAPClient::pairStatus; pairState HAPClient::pairStatus;
Accessory HAPClient::accessory; Accessory HAPClient::accessory;

View File

@ -73,14 +73,13 @@ struct HAPClient {
// common structures and data shared across all HAP Clients // common structures and data shared across all HAP Clients
static const int MAX_HTTP=8095; // max number of bytes in HTTP message buffer static const int MAX_HTTP=8096; // max number of bytes allowed for HTTP message
static const int MAX_CONTROLLERS=16; // maximum number of paired controllers (HAP requires at least 16) static const int MAX_CONTROLLERS=16; // maximum number of paired controllers (HAP requires at least 16)
static const int MAX_ACCESSORIES=41; // maximum number of allowed Acessories (HAP limit=150, but not enough memory in ESP32 to run that many) static const int MAX_ACCESSORIES=41; // maximum number of allowed Acessories (HAP limit=150, but not enough memory in ESP32 to run that many)
static TLV<kTLVType,10> tlv8; // TLV8 structure (HAP Section 14.1) with space for 10 TLV records of type kTLVType (HAP Table 5-6) static TLV<kTLVType,10> tlv8; // TLV8 structure (HAP Section 14.1) with space for 10 TLV records of type kTLVType (HAP Table 5-6)
static nvs_handle hapNVS; // handle for non-volatile-storage of HAP data static nvs_handle hapNVS; // handle for non-volatile-storage of HAP data
static nvs_handle srpNVS; // handle for non-volatile-storage of SRP data static nvs_handle srpNVS; // handle for non-volatile-storage of SRP data
static uint8_t httpBuf[MAX_HTTP+1]; // buffer to store HTTP messages (+1 to leave room for storing an extra 'overflow' character)
static HKDF hkdf; // generates (and stores) HKDF-SHA-512 32-byte keys derived from an inputKey of arbitrary length, a salt string, and an info string static HKDF hkdf; // generates (and stores) HKDF-SHA-512 32-byte keys derived from an inputKey of arbitrary length, a salt string, and an info string
static pairState pairStatus; // tracks pair-setup status static pairState pairStatus; // tracks pair-setup status
static SRP6A srp; // stores all SRP-6A keys used for Pair-Setup static SRP6A srp; // stores all SRP-6A keys used for Pair-Setup
@ -121,7 +120,7 @@ struct HAPClient {
void tlvRespond(); // respond to client with HTTP OK header and all defined TLV data records (those with length>0) void tlvRespond(); // respond to client with HTTP OK header and all defined TLV data records (those with length>0)
void sendEncrypted(char *body, uint8_t *dataBuf, int dataLen); // send client complete ChaCha20-Poly1305 encrypted HTTP mesage comprising a null-terminated 'body' and 'dataBuf' with 'dataLen' bytes void sendEncrypted(char *body, uint8_t *dataBuf, int dataLen); // send client complete ChaCha20-Poly1305 encrypted HTTP mesage comprising a null-terminated 'body' and 'dataBuf' with 'dataLen' bytes
int receiveEncrypted(); // decrypt HTTP request (HAP Section 6.5) int receiveEncrypted(uint8_t *httpBuf, int messageSize); // decrypt HTTP request (HAP Section 6.5)
int notFoundError(); // return 404 error int notFoundError(); // return 404 error
int badRequestError(); // return 400 error int badRequestError(); // return 400 error

View File

@ -48,7 +48,7 @@ struct TempBuffer {
TempBuffer(size_t len){ TempBuffer(size_t len){
nBytes=len*sizeof(bufType); nBytes=len*sizeof(bufType);
buf=(bufType *)heap_caps_malloc(nBytes,MALLOC_CAP_8BIT); buf=(bufType *)malloc(nBytes);
if(buf==NULL){ if(buf==NULL){
Serial.print("\n\n*** FATAL ERROR: Requested allocation of "); Serial.print("\n\n*** FATAL ERROR: Requested allocation of ");
Serial.print(nBytes); Serial.print(nBytes);
@ -58,7 +58,7 @@ struct TempBuffer {
} }
~TempBuffer(){ ~TempBuffer(){
heap_caps_free(buf); free(buf);
} }
int len(){ int len(){