Improved WEBServerManager responsiveness
This commit is contained in:
parent
fced2942ab
commit
8cae58706f
@ -1,8 +1,9 @@
|
||||
#include "WEBServerManager.h"
|
||||
|
||||
#define DEBUG
|
||||
#define DEBUG_BODY
|
||||
#define DEBUG_PARAMETER
|
||||
//#define DEBUG
|
||||
//#define DEBUG_BODY
|
||||
//#define DEBUG_PARAMETER
|
||||
//#define DEBUG_CONTENT_LENGTH
|
||||
//#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)
|
||||
@ -84,9 +85,10 @@ boolean WEBServerManager::runServer()
|
||||
|
||||
boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
{
|
||||
char readChar(0), *parseBuffer(NULL), *parseKey(NULL), *parseValue(NULL);
|
||||
boolean smallTimeout(false), isKey(true), receivingDone(false);
|
||||
unsigned int shortTimeout = 100, longTimeout = 5000, activeTimeout = shortTimeout;
|
||||
char readChar(0), *parseBuffer(NULL), *parseKey(NULL), *parseValue(NULL), *parseParameter(NULL), *contentLength(NULL);
|
||||
boolean isKey(true), receivingDone(false);
|
||||
unsigned int activeTimeout = 10000;
|
||||
unsigned long dataBytesCounter = 0, dataBytes = 0;
|
||||
|
||||
/* Better way to read data
|
||||
char temp[2048];
|
||||
@ -96,7 +98,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 < activeTimeout))
|
||||
{
|
||||
if(wifiClient->available())
|
||||
{
|
||||
@ -131,9 +133,23 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
}
|
||||
else if(readChar != '\r')
|
||||
{
|
||||
#ifdef DEBUG_BODY
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
if(parseParameter != NULL)
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
#ifdef DEBUG_PARAMETER
|
||||
Serial.println(parseParameter);
|
||||
#endif
|
||||
free(parseParameter);parseParameter = NULL;
|
||||
}
|
||||
parseParameter = addChar(parseParameter, readChar);
|
||||
_httpParserState = PARAMETER_SECTION;
|
||||
}
|
||||
break;
|
||||
@ -153,9 +169,7 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
else
|
||||
_httpParserState = ERROR;
|
||||
break;
|
||||
case HTTP_RESOURCE_SECTION:
|
||||
if(_httpRequestData.HRM == POST)// Need to be changed, using Content-Length is more appropriate
|
||||
activeTimeout = longTimeout;
|
||||
case HTTP_RESOURCE_SECTION:
|
||||
if(readChar == '?' )
|
||||
{
|
||||
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
|
||||
@ -224,19 +238,18 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
}
|
||||
break;
|
||||
case BODY_SECTION:
|
||||
//parseBuffer = addChar(parseBuffer, readChar);
|
||||
#ifdef DEBUG
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
//parseBuffer = addChar(parseBuffer, readChar);
|
||||
if(_httpRequestData.HRM != GET)dataBytesCounter++;//Should be always true
|
||||
#ifdef DEBUG_BODY
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
break;
|
||||
case PARAMETER_SECTION: //Here are all the http header params
|
||||
#ifdef DEBUG_PARAMETER
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
case PARAMETER_SECTION: //Here are all the http header params
|
||||
if(readChar == '\n')
|
||||
{
|
||||
_httpParserState = LINE_BREAK;
|
||||
}
|
||||
}else
|
||||
parseParameter = addChar(parseParameter, readChar);
|
||||
break;
|
||||
case IGNORED:
|
||||
break;
|
||||
@ -248,9 +261,9 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
}
|
||||
|
||||
if(receivingDone) break;
|
||||
if(_httpRequestData.HRM == POST && dataBytes != 0 && dataBytes == dataBytesCounter) break;
|
||||
|
||||
_clientTimeout = millis();
|
||||
smallTimeout = true;
|
||||
}
|
||||
ESP.wdtFeed();
|
||||
}
|
||||
@ -264,6 +277,8 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
}
|
||||
}
|
||||
|
||||
free(parseParameter);parseParameter = NULL;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("HTTP VERB : ");
|
||||
Serial.println(_httpRequestData.HRM);
|
||||
|
Loading…
Reference in New Issue
Block a user