Skip to content

Commit bf2ffdc

Browse files
me21me-no-dev
authored andcommitted
Disabled request body parsing if the handler does nothing. (me-no-dev#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 <[email protected]> * Marked SPIFFSEditor request handler as non-trivial, as it needs to process POST requests. Signed-off-by: Alexandr Zarubkin <[email protected]>
1 parent 8139925 commit bf2ffdc

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

src/ESPAsyncWebServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ class AsyncWebHandler {
322322
virtual void handleRequest(AsyncWebServerRequest *request __attribute__((unused))){}
323323
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))){}
324324
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))){}
325+
virtual bool isRequestHandlerTrivial(){return true;}
325326
};
326327

327328
/*

src/SPIFFSEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class SPIFFSEditor: public AsyncWebHandler {
1818
virtual bool canHandle(AsyncWebServerRequest *request) override final;
1919
virtual void handleRequest(AsyncWebServerRequest *request) override final;
2020
virtual void handleUpload(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) override final;
21+
virtual bool isRequestHandlerTrivial() override final {return false;}
2122
};
2223

2324
#endif

src/WebHandlerImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class AsyncCallbackWebHandler: public AsyncWebHandler {
104104
if(_onBody)
105105
_onBody(request, data, len, index, total);
106106
}
107+
virtual bool isRequestHandlerTrivial() override final {return _onRequest ? false : true;}
107108
};
108109

109110
#endif /* ASYNCWEBSERVERHANDLERIMPL_H_ */

src/WebRequest.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,18 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
127127
}
128128
}
129129
} else if(_parseState == PARSE_REQ_BODY){
130+
// A handler should be already attached at this point in _parseLine function.
131+
// If handler does nothing (_onRequest is NULL), we don't need to really parse the body.
132+
const bool needParse = _handler && !_handler->isRequestHandlerTrivial();
130133
if(_isMultipart){
131-
size_t i;
132-
for(i=0; i<len; i++){
133-
_parseMultipartPostByte(((uint8_t*)buf)[i], i == len - 1);
134-
_parsedLength++;
135-
}
134+
if(needParse){
135+
size_t i;
136+
for(i=0; i<len; i++){
137+
_parseMultipartPostByte(((uint8_t*)buf)[i], i == len - 1);
138+
_parsedLength++;
139+
}
140+
} else
141+
_parsedLength += len;
136142
} else {
137143
if(_parsedLength == 0){
138144
if(_contentType.startsWith("application/x-www-form-urlencoded")){
@@ -149,12 +155,14 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
149155
//check if authenticated before calling the body
150156
if(_handler) _handler->handleBody(this, (uint8_t*)buf, len, _parsedLength, _contentLength);
151157
_parsedLength += len;
152-
} else {
158+
} else if(needParse) {
153159
size_t i;
154160
for(i=0; i<len; i++){
155161
_parsedLength++;
156162
_parsePlainPostChar(((uint8_t*)buf)[i]);
157163
}
164+
} else {
165+
_parsedLength += len;
158166
}
159167
}
160168
if(_parsedLength == _contentLength){

0 commit comments

Comments
 (0)