Skip to content

Commit

Permalink
Disabled request body parsing if the handler does nothing. (homieiot#266
Browse files Browse the repository at this point in the history
)

* 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 <[email protected]>

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

Signed-off-by: Alexandr Zarubkin <[email protected]>
  • Loading branch information
me21 authored and me-no-dev committed Nov 6, 2017
1 parent 8139925 commit bf2ffdc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
};

/*
Expand Down
1 change: 1 addition & 0 deletions src/SPIFFSEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/WebHandlerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
20 changes: 14 additions & 6 deletions src/WebRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")){
Expand All @@ -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){
Expand Down

0 comments on commit bf2ffdc

Please sign in to comment.