@@ -113,53 +113,77 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons
113
113
114
114
void AsyncCallbackJsonWebHandler::handleRequest (AsyncWebServerRequest *request) {
115
115
if (_onRequest) {
116
+ // GET request:
116
117
if (request->method () == HTTP_GET) {
117
118
JsonVariant json;
118
119
_onRequest (request, json);
119
120
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
+ }
121
138
122
139
#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 ()) {
126
143
#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>();
131
148
#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>();
136
153
#endif
137
154
138
- _onRequest (request, json);
139
- return ;
140
- }
155
+ _onRequest (request, json);
156
+ } else {
157
+ // error parsing the body
158
+ request->send (400 );
141
159
}
142
- request->send (_contentLength > _maxContentLength ? 413 : 400 );
143
- } else {
144
- request->send (500 );
145
160
}
146
161
}
147
162
148
163
void AsyncCallbackJsonWebHandler::handleBody (AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
149
164
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
153
172
if (request->_tempObject == NULL ) {
173
+ request->_tempObject = calloc (total + 1 , sizeof (uint8_t )); // null-terminated string
174
+ if (request->_tempObject == NULL ) {
154
175
#ifdef ESP32
155
- log_e (" Failed to allocate" );
176
+ log_e (" Failed to allocate" );
156
177
#endif
157
- request->abort ();
158
- return ;
178
+ request->abort ();
179
+ return ;
180
+ }
159
181
}
160
182
}
183
+
161
184
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);
163
187
}
164
188
}
165
189
}
0 commit comments