Disabled request body parsing if the handler does nothing. (#266)

* Disabled request body parsing if the handler does nothing. This will save memory and prevent crashes on large POST requests.

Signed-off-by: Alexandr Zarubkin <me21@yandex.ru>

* Marked SPIFFSEditor request handler as non-trivial, as it needs to process POST requests.

Signed-off-by: Alexandr Zarubkin <me21@yandex.ru>
This commit is contained in:
Alexandr Zarubkin
2017-11-06 16:48:47 +03:00
committed by Me No Dev
parent 8139925eb9
commit bf2ffdc51c
4 changed files with 17 additions and 6 deletions

View File

@ -322,6 +322,7 @@ class AsyncWebHandler {
virtual void handleRequest(AsyncWebServerRequest *request __attribute__((unused))){}
virtual void handleUpload(AsyncWebServerRequest *request __attribute__((unused)), const String& filename __attribute__((unused)), size_t index __attribute__((unused)), uint8_t *data __attribute__((unused)), size_t len __attribute__((unused)), bool final __attribute__((unused))){}
virtual void handleBody(AsyncWebServerRequest *request __attribute__((unused)), uint8_t *data __attribute__((unused)), size_t len __attribute__((unused)), size_t index __attribute__((unused)), size_t total __attribute__((unused))){}
virtual bool isRequestHandlerTrivial(){return true;}
};
/*

View File

@ -18,6 +18,7 @@ class SPIFFSEditor: public AsyncWebHandler {
virtual bool canHandle(AsyncWebServerRequest *request) override final;
virtual void handleRequest(AsyncWebServerRequest *request) override final;
virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final;
virtual bool isRequestHandlerTrivial() override final {return false;}
};
#endif

View File

@ -104,6 +104,7 @@ class AsyncCallbackWebHandler: public AsyncWebHandler {
if(_onBody)
_onBody(request, data, len, index, total);
}
virtual bool isRequestHandlerTrivial() override final {return _onRequest ? false : true;}
};
#endif /* ASYNCWEBSERVERHANDLERIMPL_H_ */

View File

@ -127,12 +127,18 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
}
}
} else if(_parseState == PARSE_REQ_BODY){
// A handler should be already attached at this point in _parseLine function.
// If handler does nothing (_onRequest is NULL), we don't need to really parse the body.
const bool needParse = _handler && !_handler->isRequestHandlerTrivial();
if(_isMultipart){
size_t i;
for(i=0; i<len; i++){
_parseMultipartPostByte(((uint8_t*)buf)[i], i == len - 1);
_parsedLength++;
}
if(needParse){
size_t i;
for(i=0; i<len; i++){
_parseMultipartPostByte(((uint8_t*)buf)[i], i == len - 1);
_parsedLength++;
}
} else
_parsedLength += len;
} else {
if(_parsedLength == 0){
if(_contentType.startsWith("application/x-www-form-urlencoded")){
@ -149,12 +155,14 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
//check if authenticated before calling the body
if(_handler) _handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
_parsedLength += len;
} else {
} else if(needParse) {
size_t i;
for(i=0; i<len; i++){
_parsedLength++;
_parsePlainPostChar(((uint8_t*)buf)[i]);
}
} else {
_parsedLength += len;
}
}
if(_parsedLength == _contentLength){