cpp-netlib/cpp-netlib — pull request #132 by Mike-ECT (2012-07-27 12:15:05+00:00)
Title: Error processing a HTTP response with chunked body

When reading a chunked HTTP body, read_body_transfer_chunk_encoding() does the following in a loop:
* determine size of the next chunk
* transfer_at_least(size of the next chunk)

However, before reading the first chunk, data for it may already have been received and stored in the response_buffer. Therefore, it is wrong to expect chunk_size bytes from the socket. When persistent TCP connections are used, the thread will forever block on the read operation, because it tries to read too many bytes. With non-persistent TCP connections, the bug has no visible effect, because reading simply finishes with less than the expected number of bytes.

The fix is to transfer the bytes already available in the response_buffer to the body_stream. This is already done *after* each read operation, but should also be done *before* the first read operation. Then, the correct number of bytes are expected from the socket.

Please review my fix and integrate.