Skip to content

Commit

Permalink
Revert "AsyncAbstractResponse: Fix packet buffer lifetime"
Browse files Browse the repository at this point in the history
This reverts commit ca1262c.
  • Loading branch information
willmmiles committed Jan 28, 2024
1 parent 9531e61 commit e558c14
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 41 deletions.
4 changes: 1 addition & 3 deletions src/WebResponseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#undef max
#endif
#include <vector>
#include <default_init_allocator.h>
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.

class AsyncBasicResponse: public AsyncWebServerResponse {
Expand All @@ -47,8 +46,7 @@ class AsyncAbstractResponse: public AsyncWebServerResponse {
// This is inefficient with vector, but if we use some other container,
// we won't be able to access it as contiguous array of bytes when reading from it,
// so by gaining performance in one place, we'll lose it in another.
std::vector<uint8_t, default_init_allocator<uint8_t>> _pkt_buf;
std::vector<uint8_t, default_init_allocator<uint8_t>> _cache;
std::vector<uint8_t> _cache;
size_t _readDataFromCacheOrContent(uint8_t* data, const size_t len);
size_t _fillBufferAndProcessTemplates(uint8_t* buf, size_t maxLen);
protected:
Expand Down
12 changes: 8 additions & 4 deletions src/WebResponses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,11 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
outLen = ((_contentLength - _sentLength) > space)?space:(_contentLength - _sentLength);
}

if (_pkt_buf.size() < (outLen + headLen)) {
// Make it bigger without preserving the existing contents
_pkt_buf = decltype(_pkt_buf) ( (size_t) (outLen+headLen) );
uint8_t *buf = (uint8_t *)malloc(outLen+headLen);
if (!buf) {
// os_printf("_ack malloc %d failed\n", outLen+headLen);
return 0;
}
uint8_t *buf = _pkt_buf.data();

if(headLen){
memcpy(buf, _head.c_str(), _head.length());
Expand All @@ -338,6 +338,7 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
// See RFC2616 sections 2, 3.6.1.
readLen = _fillBufferAndProcessTemplates(buf+headLen+6, outLen - 8);
if(readLen == RESPONSE_TRY_AGAIN){
free(buf);
return 0;
}
outLen = sprintf((char*)buf+headLen, "%x", readLen) + headLen;
Expand All @@ -350,6 +351,7 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
} else {
readLen = _fillBufferAndProcessTemplates(buf+headLen, outLen);
if(readLen == RESPONSE_TRY_AGAIN){
free(buf);
return 0;
}
outLen = readLen + headLen;
Expand All @@ -369,6 +371,8 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
_sentLength += outLen - headLen;
}

free(buf);

if((_chunked && readLen == 0) || (!_sendContentLength && outLen == 0) || (!_chunked && _sentLength == _contentLength)){
_state = RESPONSE_WAIT_ACK;
}
Expand Down
34 changes: 0 additions & 34 deletions src/default_init_allocator.h

This file was deleted.

0 comments on commit e558c14

Please sign in to comment.