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