@@ -113,53 +113,77 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons
113113
114114void AsyncCallbackJsonWebHandler::handleRequest (AsyncWebServerRequest *request) {
115115 if (_onRequest) {
116+ // GET request:
116117 if (request->method () == HTTP_GET) {
117118 JsonVariant json;
118119 _onRequest (request, json);
119120 return ;
120- } else if (request->_tempObject != NULL ) {
121+ }
122+
123+ // POST / PUT / ... requests:
124+ // check if JSON body is too large, if it is, don't deserialize
125+ if (request->contentLength () > _maxContentLength) {
126+ #ifdef ESP32
127+ log_e (" Content length exceeds maximum allowed" );
128+ #endif
129+ request->send (413 );
130+ return ;
131+ }
132+
133+ if (request->_tempObject == NULL ) {
134+ // there is no body
135+ request->send (400 );
136+ return ;
137+ }
121138
122139#if ARDUINOJSON_VERSION_MAJOR == 5
123- DynamicJsonBuffer jsonBuffer;
124- JsonVariant json = jsonBuffer.parse ((uint8_t *)( request->_tempObject ) );
125- if (json.success ()) {
140+ DynamicJsonBuffer jsonBuffer;
141+ JsonVariant json = jsonBuffer.parse ((const char *) request->_tempObject );
142+ if (json.success ()) {
126143#elif ARDUINOJSON_VERSION_MAJOR == 6
127- DynamicJsonDocument jsonBuffer (this ->maxJsonBufferSize );
128- DeserializationError error = deserializeJson (jsonBuffer, (uint8_t *)( request->_tempObject ) );
129- if (!error) {
130- JsonVariant json = jsonBuffer.as <JsonVariant>();
144+ DynamicJsonDocument jsonBuffer (this ->maxJsonBufferSize );
145+ DeserializationError error = deserializeJson (jsonBuffer, (const char *) request->_tempObject );
146+ if (!error) {
147+ JsonVariant json = jsonBuffer.as <JsonVariant>();
131148#else
132- JsonDocument jsonBuffer;
133- DeserializationError error = deserializeJson (jsonBuffer, (uint8_t *)( request->_tempObject ) );
134- if (!error) {
135- JsonVariant json = jsonBuffer.as <JsonVariant>();
149+ JsonDocument jsonBuffer;
150+ DeserializationError error = deserializeJson (jsonBuffer, (const char *) request->_tempObject );
151+ if (!error) {
152+ JsonVariant json = jsonBuffer.as <JsonVariant>();
136153#endif
137154
138- _onRequest (request, json);
139- return ;
140- }
155+ _onRequest (request, json);
156+ } else {
157+ // error parsing the body
158+ request->send (400 );
141159 }
142- request->send (_contentLength > _maxContentLength ? 413 : 400 );
143- } else {
144- request->send (500 );
145160 }
146161}
147162
148163void AsyncCallbackJsonWebHandler::handleBody (AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
149164 if (_onRequest) {
150- _contentLength = total;
151- if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) {
152- request->_tempObject = malloc (total);
165+ // ignore callback if size is larger than maxContentLength
166+ if (total > _maxContentLength) {
167+ return ;
168+ }
169+
170+ if (index == 0 ) {
171+ // this check allows request->_tempObject to be initialized from a middleware
153172 if (request->_tempObject == NULL ) {
173+ request->_tempObject = calloc (total + 1 , sizeof (uint8_t )); // null-terminated string
174+ if (request->_tempObject == NULL ) {
154175#ifdef ESP32
155- log_e (" Failed to allocate" );
176+ log_e (" Failed to allocate" );
156177#endif
157- request->abort ();
158- return ;
178+ request->abort ();
179+ return ;
180+ }
159181 }
160182 }
183+
161184 if (request->_tempObject != NULL ) {
162- memcpy ((uint8_t *)(request->_tempObject ) + index, data, len);
185+ uint8_t *buffer = (uint8_t *)request->_tempObject ;
186+ memcpy (buffer + index, data, len);
163187 }
164188 }
165189}
0 commit comments