You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/AsyncJson.cpp
+41-18Lines changed: 41 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,11 @@
5
5
6
6
#if ASYNC_JSON_SUPPORT == 1
7
7
8
+
typedefstruct {
9
+
size_t length; // the size we can write into "content", not including null termination
10
+
uint8_t content[1]; // this will be of size "content-length" + 1 byte to guarantee that the content is null terminated. null termination is needed for ArduinoJson 5
// check if body is too large, if it is, don't parse
127
+
// check if json body is too large, if it is, don't deserialize
123
128
if (request->contentLength() > _maxContentLength)
124
129
{
125
130
request->send(413);
126
131
return;
127
132
}
128
133
129
134
// try to parse body as JSON
130
-
if (request->_tempObject != NULL)
135
+
if (request->_tempObject != NULL)// see if we succeeded allocating a buffer earlier
131
136
{
132
-
size_t dataSize = min(request->contentLength(), request->_tempSize); // smaller value of contentLength or the size of the buffer. normally those should match.
if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) { // if request content length is valid size and we have no content buffer yet
167
-
request->_tempObject = malloc(total + 1); // allocate one additional byte so we can null terminate this buffer (needed for ArduinoJson 5)
173
+
if (index == 0) // on first piece
174
+
{
175
+
// check nobody has already allocated the buffer
176
+
if (request->_tempObject != NULL)
177
+
{
178
+
#ifdef ESP32
179
+
log_e("Temp object already in use");
180
+
#endif
181
+
return; // do nothing else here, handleRequest will return a HTTP error
182
+
}
183
+
// check total size is valid
184
+
if (total >= _maxContentLength)
185
+
{
186
+
return; // do nothing else here, handleRequest will return a HTTP error
187
+
}
188
+
// allocate buffer
189
+
request->_tempObject = calloc(1, sizeof(AsyncJsonResponseBuffer) + total); // normally _tempObject will be "free"ed by the destructor of the request, but can release earlier if desired.
168
190
if (request->_tempObject == NULL) { // if allocation failed
169
191
#ifdef ESP32
170
192
log_e("Failed to allocate");
171
193
#endif
172
-
request->abort();
173
-
return;
194
+
return; // do nothing else here, handleRequest will return a HTTP error
174
195
}
175
-
request->_tempSize = total; // store the size of allocation we made into _tempSize
196
+
((AsyncJsonResponseBuffer*)request->_tempObject)->length = total; // store the size of allocation we made into _tempObject
0 commit comments