1
votes

The data comes from here, I take tcp socket to get it.

the response like:

HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Tue, 06 Aug 2013 08:25:48 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Content-Encoding: gzip

2e2
?

And then I decompress used the zlib function "uncompress", but get a Z_DATA_ERROR returned code. It looks like the data started position "2e2" was not a validate gzip stream data?

1

1 Answers

1
votes

The transfer encoding is chunked. Each chunk of data is preceded by the chunk size specified in hexadecimal followed by a line terminator. Then, that many bytes should be read in for the content. The chunk data is followed by another line terminator. The next chunk has the same format (size followed by data) until a 0 sized chunk is sent.

You need to decode each chunk and append it to the decompress buffer. Leaving the chunk size in your data stream would not be treated as valid input by zlib.