Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/AsyncJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ size_t AsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
#else
serializeJson(_root, dest);
#endif
return len;
return dest.written();
}

#if ARDUINOJSON_VERSION_MAJOR == 6
Expand Down Expand Up @@ -89,7 +89,7 @@ size_t PrettyAsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
#else
serializeJsonPretty(_root, dest);
#endif
return len;
return dest.written();
}

// MessagePack content type response
Expand All @@ -106,7 +106,7 @@ size_t AsyncMessagePackResponse::setLength() {
size_t AsyncMessagePackResponse::_fillBuffer(uint8_t *data, size_t len) {
ChunkPrint dest(data, _sentLength, len);
serializeMsgPack(_root, dest);
return len;
return dest.written();
}

#endif
Expand Down
19 changes: 12 additions & 7 deletions src/ChunkPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

#include <ChunkPrint.h>

ChunkPrint::ChunkPrint(uint8_t *destination, size_t from, size_t len) : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {}

size_t ChunkPrint::write(uint8_t c) {
if (_to_skip > 0) {
_to_skip--;
// handle case where len is zero
if (!_len) {
return 0;
}
// skip first bytes until from is zero (bytes were already sent by previous chunk)
if (_from) {
_from--;
return 1;
} else if (_to_write > 0) {
_to_write--;
_destination[_pos++] = c;
}
// write a maximum of len bytes
if (_len - _index) {
_destination[_index++] = c;
return 1;
}
// we have finished writing len bytes, ignore the rest
return 0;
}
11 changes: 7 additions & 4 deletions src/ChunkPrint.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
class ChunkPrint : public Print {
private:
uint8_t *_destination;
size_t _to_skip;
size_t _to_write;
size_t _pos;
size_t _from;
size_t _len;
size_t _index;

public:
ChunkPrint(uint8_t *destination, size_t from, size_t len);
ChunkPrint(uint8_t *destination, size_t from, size_t len) : _destination(destination), _from(from), _len(len), _index(0) {}
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size) {
return this->Print::write(buffer, size);
}
size_t written() const {
return _index;
}
};
3 changes: 2 additions & 1 deletion src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s
}
}
} else {
size_t const readLen = _fillBufferAndProcessTemplates(_send_buffer->data(), std::min(_send_buffer->size(), tcp_win));
size_t const readLen =
_fillBufferAndProcessTemplates(_send_buffer->data(), std::min(std::min(_send_buffer->size(), tcp_win), _contentLength - _sentLength));
if (readLen == 0) {
// no more data to send
_state = RESPONSE_END;
Expand Down