Improved WEBServerManager Speed
This commit is contained in:
parent
7e20eb48bb
commit
73441f6b18
@ -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())
|
||||
{
|
||||
@ -116,16 +118,25 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
_httpParserState = ERROR;
|
||||
break;
|
||||
case LINE_BREAK:
|
||||
if(readChar == '\n')
|
||||
_httpParserState = BODY_SECTION;
|
||||
else if(readChar != '\r')
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
_httpParserState = PARAMETER_SECTION;
|
||||
}
|
||||
break;
|
||||
if(readChar == '\n')
|
||||
{
|
||||
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;
|
||||
}
|
||||
break;
|
||||
case HTTP_VERB_SECTION:
|
||||
if(readChar >= 65 && readChar <= 90)
|
||||
{
|
||||
@ -143,16 +154,15 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
_httpParserState = ERROR;
|
||||
break;
|
||||
case HTTP_RESOURCE_SECTION:
|
||||
if(_httpRequestData.HRM == POST)// Need to be changed, using Content-Length is more appropriate
|
||||
activeTimeout = longTimeout;
|
||||
if(_httpRequestData.HRM == POST)// Need to be changed, using Content-Length is more appropriate
|
||||
activeTimeout = longTimeout;
|
||||
if(readChar == '?' )
|
||||
{
|
||||
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
|
||||
_httpRequestData.httpResource = parseBuffer;parseBuffer = NULL;
|
||||
|
||||
if(readChar == '?' )
|
||||
{
|
||||
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
|
||||
_httpRequestData.httpResource = parseBuffer;parseBuffer = NULL;
|
||||
|
||||
_httpParserState = HTTP_RESOURCE_PARAM_SECTION;
|
||||
}
|
||||
_httpParserState = HTTP_RESOURCE_PARAM_SECTION;
|
||||
}
|
||||
else if(readChar == ' ')
|
||||
{
|
||||
free(_httpRequestData.httpResource);_httpRequestData.httpResource = NULL;
|
||||
@ -193,12 +203,12 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
parseKey = NULL;parseValue = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(isKey)
|
||||
parseKey = addChar(parseKey, readChar);
|
||||
else
|
||||
parseValue = addChar(parseValue, readChar);
|
||||
}
|
||||
{
|
||||
if(isKey)
|
||||
parseKey = addChar(parseKey, readChar);
|
||||
else
|
||||
parseValue = addChar(parseValue, readChar);
|
||||
}
|
||||
break;
|
||||
case HTTP_VER_SECTION:
|
||||
if((readChar >= 48 && readChar <= 57) || readChar == '.')
|
||||
@ -219,8 +229,8 @@ boolean WEBServerManager::parseQuery(WiFiClient *wifiClient)
|
||||
Serial.print(readChar);
|
||||
#endif
|
||||
break;
|
||||
case PARAMETER_SECTION: //Here are all the http header params
|
||||
#ifdef DEBUG
|
||||
case PARAMETER_SECTION: //Here are all the http header params
|
||||
#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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user