Skip to content

Commit 73b0edf

Browse files
Merge pull request #349 from ESP32Async/issue-346
Several fixes around Chunk processing (Json) of a large Json document and available length passed to fill send buffer
2 parents 079139a + 19a957c commit 73b0edf

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/AsyncJson.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ size_t AsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
6161
#else
6262
serializeJson(_root, dest);
6363
#endif
64-
return len;
64+
return dest.written();
6565
}
6666

6767
#if ARDUINOJSON_VERSION_MAJOR == 6
@@ -89,7 +89,7 @@ size_t PrettyAsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
8989
#else
9090
serializeJsonPretty(_root, dest);
9191
#endif
92-
return len;
92+
return dest.written();
9393
}
9494

9595
// MessagePack content type response
@@ -106,7 +106,7 @@ size_t AsyncMessagePackResponse::setLength() {
106106
size_t AsyncMessagePackResponse::_fillBuffer(uint8_t *data, size_t len) {
107107
ChunkPrint dest(data, _sentLength, len);
108108
serializeMsgPack(_root, dest);
109-
return len;
109+
return dest.written();
110110
}
111111

112112
#endif

src/ChunkPrint.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33

44
#include <ChunkPrint.h>
55

6-
ChunkPrint::ChunkPrint(uint8_t *destination, size_t from, size_t len) : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {}
7-
86
size_t ChunkPrint::write(uint8_t c) {
9-
if (_to_skip > 0) {
10-
_to_skip--;
7+
// handle case where len is zero
8+
if (!_len) {
9+
return 0;
10+
}
11+
// skip first bytes until from is zero (bytes were already sent by previous chunk)
12+
if (_from) {
13+
_from--;
1114
return 1;
12-
} else if (_to_write > 0) {
13-
_to_write--;
14-
_destination[_pos++] = c;
15+
}
16+
// write a maximum of len bytes
17+
if (_len - _index) {
18+
_destination[_index++] = c;
1519
return 1;
1620
}
21+
// we have finished writing len bytes, ignore the rest
1722
return 0;
1823
}

src/ChunkPrint.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
class ChunkPrint : public Print {
99
private:
1010
uint8_t *_destination;
11-
size_t _to_skip;
12-
size_t _to_write;
13-
size_t _pos;
11+
size_t _from;
12+
size_t _len;
13+
size_t _index;
1414

1515
public:
16-
ChunkPrint(uint8_t *destination, size_t from, size_t len);
16+
ChunkPrint(uint8_t *destination, size_t from, size_t len) : _destination(destination), _from(from), _len(len), _index(0) {}
1717
size_t write(uint8_t c);
1818
size_t write(const uint8_t *buffer, size_t size) {
1919
return this->Print::write(buffer, size);
2020
}
21+
size_t written() const {
22+
return _index;
23+
}
2124
};

src/WebResponses.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s
470470
}
471471
}
472472
} else {
473-
size_t const readLen = _fillBufferAndProcessTemplates(_send_buffer->data(), std::min(_send_buffer->size(), tcp_win));
473+
size_t const readLen =
474+
_fillBufferAndProcessTemplates(_send_buffer->data(), std::min(std::min(_send_buffer->size(), tcp_win), _contentLength - _sentLength));
474475
if (readLen == 0) {
475476
// no more data to send
476477
_state = RESPONSE_END;

0 commit comments

Comments
 (0)