Search code examples
httpdelphigzipindy

How to decode gzip encoded html?


I got data from web server:

data := '
HTTP/1.1 200 OK
Content-Encoding: gzip
Vary: Accept-Encoding
Content-type: text/html
Transfer-Encoding: chunked
Server: Apache

3d5
????????????????????????????????????
????????????????????????????????????
????????????????????????????????????
';

The size of data is: 3d5 (in hex) all is stored to TIdBytes variable "data".

How to decode gziped data, change something in it, and encode back and edit the length 3d5 to new.


Solution

  • The Transfer-Encoding response header is set to chunked. That means the server sends the body data in chunks, where each chunk indicates its own size, where a 0-length chunk indicates the end of the data. The 3d5 refers to the size of the first chunk. That would be the full size of the HTML only if there is just 1 chunk of data in the response.

    TIdHTTP internally handles chunked data for you. If the de-chunked data has been gzip'ped, TIdHTTP can decompress it for you if you assign a TIdZLibCompressorBase-derived component, such as TIdCompressorZLib, to the TIdHTTP.Compressor property beforehand.