@@ -50,21 +50,13 @@ class AwsChunkedStreamBuf : public std::streambuf {
5050 return traits_type::to_int_type (*gptr ());
5151 }
5252
53- // Compact buffer when consumed data exceeds half buffer size
54- if (m_chunkingBufferPos > m_chunkingBuffer.GetLength () / 2 ) {
55- size_t remaining = m_chunkingBufferSize - m_chunkingBufferPos;
56- if (remaining > 0 ) {
57- std::memmove (m_chunkingBuffer.GetUnderlyingData (),
58- m_chunkingBuffer.GetUnderlyingData () + m_chunkingBufferPos,
59- remaining);
60- }
61- m_chunkingBufferSize = remaining;
62- m_chunkingBufferPos = 0 ;
63- }
64-
6553 // only read and write to chunked stream if the underlying stream
6654 // is still in a valid state and we have buffer space
67- if (m_stream->good ()) {
55+ if (m_stream->good () && m_chunkingBufferPos >= m_chunkingBufferSize) {
56+ // Reset buffer for new data only when buffer is consumed
57+ m_chunkingBufferPos = 0 ;
58+ m_chunkingBufferSize = 0 ;
59+
6860 // Check if we have enough space for worst-case chunk (data + header + footer)
6961 size_t maxChunkSize = m_data.GetLength () + 20 ; // data + hex header + CRLF
7062 if (m_chunkingBufferSize + maxChunkSize <= m_chunkingBuffer.GetLength ()) {
@@ -132,7 +124,8 @@ class AwsChunkedStreamBuf : public std::streambuf {
132124 }
133125 }
134126
135- Aws::Utils::Array<char > m_chunkingBuffer{DataBufferSize * 4 };
127+ // Buffer for chunked data plus overhead for HTTP chunked encoding headers, trailers, and safety margin
128+ Aws::Utils::Array<char > m_chunkingBuffer{DataBufferSize + 128 };
136129 size_t m_chunkingBufferSize{0 };
137130 size_t m_chunkingBufferPos{0 };
138131 Aws::Http::HttpRequest* m_request{nullptr };
0 commit comments