Improved WEBServerManager Speed

This commit is contained in:
Anatole SCHRAMM 2019-04-17 14:55:42 +02:00
parent 7e20eb48bb
commit 73441f6b18

View File

@ -1,6 +1,8 @@
#include "WEBServerManager.h"
#define DEBUG
#define DEBUG_BODY
#define DEBUG_PARAMETER
//#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)
@ -83,7 +85,7 @@ boolean WEBServerManager::runServer()
boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
{
char readChar(0), *parseBuffer(NULL), *parseKey(NULL), *parseValue(NULL);
boolean smallTimeout(false), isKey(true);
boolean smallTimeout(false), isKey(true), receivingDone(false);
unsigned int shortTimeout = 100, longTimeout = 5000, activeTimeout = shortTimeout;
/* Better way to read data
@ -94,7 +96,7 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
_httpParserState = INIT;
_clientTimeout = millis();
boolean slashesOrAntiSlashesOnly(true);
while(wifiClient->available() || millis() - _clientTimeout < (smallTimeout ? activeTimeout : 10000))
while(wifiClient->available() || ( millis() - _clientTimeout < (smallTimeout ? activeTimeout : 10000)))
{
if(wifiClient->available())
{
@ -117,10 +119,19 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
break;
case LINE_BREAK:
if(readChar == '\n')
_httpParserState = BODY_SECTION;
else if(readChar != '\r')
{
if(_httpRequestData.HRM == GET)
{
#ifdef DEBUG
Serial.println("GET DONE");
#endif
receivingDone = true;
}
_httpParserState = BODY_SECTION;
}
else if(readChar != '\r')
{
#ifdef DEBUG_BODY
Serial.print(readChar);
#endif
_httpParserState = PARAMETER_SECTION;
@ -145,7 +156,6 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
case HTTP_RESOURCE_SECTION:
if(_httpRequestData.HRM == POST)// Need to be changed, using Content-Length is more appropriate
activeTimeout = longTimeout;
if(readChar == '?' )
{
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
@ -220,7 +230,7 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
#endif
break;
case PARAMETER_SECTION: //Here are all the http header params
#ifdef DEBUG
#ifdef DEBUG_PARAMETER
Serial.print(readChar);
#endif
if(readChar == '\n')
@ -236,6 +246,9 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
default :
break;
}
if(receivingDone) break;
_clientTimeout = millis();
smallTimeout = true;
}