Improved WEBServerManager responsiveness

This commit is contained in:
anschrammh 2019-04-17 20:26:37 +02:00
parent fced2942ab
commit 8cae58706f

View File

@ -1,8 +1,9 @@
#include "WEBServerManager.h" #include "WEBServerManager.h"
#define DEBUG //#define DEBUG
#define DEBUG_BODY //#define DEBUG_BODY
#define DEBUG_PARAMETER //#define DEBUG_PARAMETER
//#define DEBUG_CONTENT_LENGTH
//#define DEBUG_RAW //#define DEBUG_RAW
WEBServerManager::WEBServerManager(unsigned int port, SDCardManager *sdCardManager) : _wifiServer(port), _sdCardManager(sdCardManager), _httpRequestData({UNDEFINED, UNKNOWN, UNKNOWN_MIME, Dictionary<DictionaryHelper::StringEntity>(), Dictionary<DictionaryHelper::StringEntity>(), NULL,NULL}), _httpParserState(INIT), _clientState(WAITING_FOR_CLIENT), _port(port), _clientTimeout(0) WEBServerManager::WEBServerManager(unsigned int port, SDCardManager *sdCardManager) : _wifiServer(port), _sdCardManager(sdCardManager), _httpRequestData({UNDEFINED, UNKNOWN, UNKNOWN_MIME, Dictionary<DictionaryHelper::StringEntity>(), Dictionary<DictionaryHelper::StringEntity>(), NULL,NULL}), _httpParserState(INIT), _clientState(WAITING_FOR_CLIENT), _port(port), _clientTimeout(0)
@ -84,9 +85,10 @@ boolean WEBServerManager::runServer()
boolean WEBServerManager::parseQuery(WiFiClient *wifiClient) boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
{ {
char readChar(0), *parseBuffer(NULL), *parseKey(NULL), *parseValue(NULL); char readChar(0), *parseBuffer(NULL), *parseKey(NULL), *parseValue(NULL), *parseParameter(NULL), *contentLength(NULL);
boolean smallTimeout(false), isKey(true), receivingDone(false); boolean isKey(true), receivingDone(false);
unsigned int shortTimeout = 100, longTimeout = 5000, activeTimeout = shortTimeout; unsigned int activeTimeout = 10000;
unsigned long dataBytesCounter = 0, dataBytes = 0;
/* Better way to read data /* Better way to read data
char temp[2048]; char temp[2048];
@ -96,7 +98,7 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
_httpParserState = INIT; _httpParserState = INIT;
_clientTimeout = millis(); _clientTimeout = millis();
boolean slashesOrAntiSlashesOnly(true); boolean slashesOrAntiSlashesOnly(true);
while(wifiClient->available() || ( millis() - _clientTimeout < (smallTimeout ? activeTimeout : 10000))) while(wifiClient->available() || ( millis() - _clientTimeout < activeTimeout))
{ {
if(wifiClient->available()) if(wifiClient->available())
{ {
@ -131,9 +133,23 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
} }
else if(readChar != '\r') else if(readChar != '\r')
{ {
#ifdef DEBUG_BODY if(parseParameter != NULL)
Serial.print(readChar); {
contentLength = strstr(parseParameter, "ent-Len");//Matches Content-Length short to save some RAM
if(contentLength != NULL)
{
dataBytes = strtol(contentLength+11,NULL,10);
#ifdef DEBUG_CONTENT_LENGTH
Serial.print("Data length : ");Serial.println(dataBytes);
#endif #endif
}
#ifdef DEBUG_PARAMETER
Serial.println(parseParameter);
#endif
free(parseParameter);parseParameter = NULL;
}
parseParameter = addChar(parseParameter, readChar);
_httpParserState = PARAMETER_SECTION; _httpParserState = PARAMETER_SECTION;
} }
break; break;
@ -154,8 +170,6 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
_httpParserState = ERROR; _httpParserState = ERROR;
break; break;
case HTTP_RESOURCE_SECTION: case HTTP_RESOURCE_SECTION:
if(_httpRequestData.HRM == POST)// Need to be changed, using Content-Length is more appropriate
activeTimeout = longTimeout;
if(readChar == '?' ) if(readChar == '?' )
{ {
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL; free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
@ -225,18 +239,17 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
break; break;
case BODY_SECTION: case BODY_SECTION:
//parseBuffer = addChar(parseBuffer, readChar); //parseBuffer = addChar(parseBuffer, readChar);
#ifdef DEBUG if(_httpRequestData.HRM != GET)dataBytesCounter++;//Should be always true
#ifdef DEBUG_BODY
Serial.print(readChar); Serial.print(readChar);
#endif #endif
break; break;
case PARAMETER_SECTION: //Here are all the http header params case PARAMETER_SECTION: //Here are all the http header params
#ifdef DEBUG_PARAMETER
Serial.print(readChar);
#endif
if(readChar == '\n') if(readChar == '\n')
{ {
_httpParserState = LINE_BREAK; _httpParserState = LINE_BREAK;
} }else
parseParameter = addChar(parseParameter, readChar);
break; break;
case IGNORED: case IGNORED:
break; break;
@ -248,9 +261,9 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
} }
if(receivingDone) break; if(receivingDone) break;
if(_httpRequestData.HRM == POST && dataBytes != 0 && dataBytes == dataBytesCounter) break;
_clientTimeout = millis(); _clientTimeout = millis();
smallTimeout = true;
} }
ESP.wdtFeed(); ESP.wdtFeed();
} }
@ -264,6 +277,8 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
} }
} }
free(parseParameter);parseParameter = NULL;
#ifdef DEBUG #ifdef DEBUG
Serial.print("HTTP VERB : "); Serial.print("HTTP VERB : ");
Serial.println(_httpRequestData.HRM); Serial.println(_httpRequestData.HRM);