|
6 | 6 | #if ASYNC_JSON_SUPPORT == 1
|
7 | 7 |
|
8 | 8 | typedef struct {
|
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 |
| 9 | + size_t length; // the size we can write into "content", not including null termination |
| 10 | + uint8_t content |
| 11 | + [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 |
11 | 12 | } AsyncJsonResponseBuffer;
|
12 | 13 |
|
13 | 14 | #if ARDUINOJSON_VERSION_MAJOR == 5
|
@@ -125,24 +126,23 @@ void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request)
|
125 | 126 | }
|
126 | 127 | // this is not a GET
|
127 | 128 | // check if json body is too large, if it is, don't deserialize
|
128 |
| - if (request->contentLength() > _maxContentLength) |
129 |
| - { |
| 129 | + if (request->contentLength() > _maxContentLength) { |
130 | 130 | request->send(413);
|
131 | 131 | return;
|
132 | 132 | }
|
133 | 133 |
|
134 | 134 | // try to parse body as JSON
|
135 |
| - if (request->_tempObject != NULL) // see if we succeeded allocating a buffer earlier |
| 135 | + if (request->_tempObject != NULL) // see if we succeeded allocating a buffer earlier |
136 | 136 | {
|
137 |
| - AsyncJsonResponseBuffer * buffer = (AsyncJsonResponseBuffer*)request->_tempObject; |
| 137 | + AsyncJsonResponseBuffer *buffer = (AsyncJsonResponseBuffer *)request->_tempObject; |
138 | 138 | #if ARDUINOJSON_VERSION_MAJOR == 5
|
139 | 139 | DynamicJsonBuffer jsonBuffer;
|
140 |
| - buffer->content[buffer->length] = '\0'; // null terminate, assume we allocated one extra char |
| 140 | + buffer->content[buffer->length] = '\0'; // null terminate, assume we allocated one extra char |
141 | 141 | // parse can only get null terminated strings as parameters
|
142 | 142 | JsonVariant json = jsonBuffer.parse(buffer->content);
|
143 | 143 | if (json.success()) {
|
144 | 144 | #elif ARDUINOJSON_VERSION_MAJOR == 6
|
145 |
| - DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); // content with length > this->maxJsonBufferSize might not get deserialized |
| 145 | + DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); // content with length > this->maxJsonBufferSize might not get deserialized |
146 | 146 | DeserializationError error = deserializeJson(jsonBuffer, buffer->content, buffer->length);
|
147 | 147 | if (!error) {
|
148 | 148 | JsonVariant json = jsonBuffer.as<JsonVariant>();
|
@@ -170,46 +170,43 @@ void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request)
|
170 | 170 |
|
171 | 171 | void AsyncCallbackJsonWebHandler::handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
|
172 | 172 | if (_onRequest) {
|
173 |
| - if (index == 0) // on first piece |
| 173 | + if (index == 0) // on first piece |
174 | 174 | {
|
175 | 175 | // check nobody has already allocated the buffer
|
176 |
| - if (request->_tempObject != NULL) |
177 |
| - { |
| 176 | + if (request->_tempObject != NULL) { |
178 | 177 | #ifdef ESP32
|
179 | 178 | log_e("Temp object already in use");
|
180 | 179 | #endif
|
181 |
| - return; // do nothing else here, handleRequest will return a HTTP error |
| 180 | + return; // do nothing else here, handleRequest will return a HTTP error |
182 | 181 | }
|
183 | 182 | // check total size is valid
|
184 |
| - if (total >= _maxContentLength) |
185 |
| - { |
186 |
| - return; // do nothing else here, handleRequest will return a HTTP error |
| 183 | + if (total >= _maxContentLength) { |
| 184 | + return; // do nothing else here, handleRequest will return a HTTP error |
187 | 185 | }
|
188 | 186 | // 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. |
190 |
| - if (request->_tempObject == NULL) { // if allocation failed |
| 187 | + request->_tempObject = calloc( |
| 188 | + 1, sizeof(AsyncJsonResponseBuffer) + total |
| 189 | + ); // normally _tempObject will be "free"ed by the destructor of the request, but can release earlier if desired. |
| 190 | + if (request->_tempObject == NULL) { // if allocation failed |
191 | 191 | #ifdef ESP32
|
192 | 192 | log_e("Failed to allocate");
|
193 | 193 | #endif
|
194 |
| - return; // do nothing else here, handleRequest will return a HTTP error |
| 194 | + return; // do nothing else here, handleRequest will return a HTTP error |
195 | 195 | }
|
196 |
| - ((AsyncJsonResponseBuffer*)request->_tempObject)->length = total; // store the size of allocation we made into _tempObject |
| 196 | + ((AsyncJsonResponseBuffer *)request->_tempObject)->length = total; // store the size of allocation we made into _tempObject |
197 | 197 | }
|
198 | 198 |
|
199 | 199 | // add data to the buffer if the buffer exists
|
200 | 200 | if (request->_tempObject != NULL) {
|
201 |
| - AsyncJsonResponseBuffer * buffer = (AsyncJsonResponseBuffer*)request->_tempObject; |
| 201 | + AsyncJsonResponseBuffer *buffer = (AsyncJsonResponseBuffer *)request->_tempObject; |
202 | 202 | // check if the buffer is the right size so we don't write out of bounds
|
203 |
| - if (buffer->length >= total && buffer->length >= index + len) |
204 |
| - { |
| 203 | + if (buffer->length >= total && buffer->length >= index + len) { |
205 | 204 | memcpy(buffer->content + index, data, len);
|
206 |
| - } |
207 |
| - else |
208 |
| - { |
| 205 | + } else { |
209 | 206 | #ifdef ESP32
|
210 | 207 | log_e("Bad size of temp buffer");
|
211 | 208 | #endif
|
212 |
| - return; // do nothing else here |
| 209 | + return; // do nothing else here |
213 | 210 | }
|
214 | 211 | }
|
215 | 212 | }
|
|
0 commit comments