Skip to content

Commit fe4085d

Browse files
Merge pull request #152 from ESP32Async/fix
Urgent fix of previous PR #148: Arduino Core 2 has a different implementation of size()
2 parents 5930f1b + bf9f457 commit fe4085d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/WebResponses.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ AsyncResponseStream::AsyncResponseStream(const char *contentType, size_t bufferS
822822
_contentType = contentType;
823823
// internal buffer will be null on allocation failure
824824
_content = std::unique_ptr<cbuf>(new cbuf(bufferSize));
825-
if (_content->size() != bufferSize) {
825+
if (bufferSize && _content->size() < bufferSize) {
826826
#ifdef ESP32
827827
log_e("Failed to allocate");
828828
#endif
@@ -840,6 +840,14 @@ size_t AsyncResponseStream::write(const uint8_t *data, size_t len) {
840840
if (len > _content->room()) {
841841
size_t needed = len - _content->room();
842842
_content->resizeAdd(needed);
843+
// log a warning if allocation failed, but do not return: keep writing the bytes we can
844+
// with _content->write: if len is more than the available size in the buffer, only
845+
// the available size will be written
846+
if (len > _content->room()) {
847+
#ifdef ESP32
848+
log_e("Failed to allocate");
849+
#endif
850+
}
843851
}
844852
size_t written = _content->write((const char *)data, len);
845853
_contentLength += written;

0 commit comments

Comments
 (0)