I read some articles about setting the content-length of a file that will be served. It has some benefits comparing to chunked downloads. The server serve chunks (don't know what the size of a chunk is anyway) when the content-length is unknown.
In PHP I use the ob_gzhandler to gzip html,js and css files and other text-based content. Setting the raw content-length before gzip output, results in strange side-effects because the length does not match the length of the gzipped output. I notice a delay or the browser reports an encoding error.
I saw a trick (also on stackoverflow) to set the content-length after gzip compression to be sure it reports the correct size. But when I do this, the content is no longer compressed.
The question is, is this behaviour correct? Is gzipped content always send chuncked? Is content-length only needed for files that are not gzipped?
Here some snippets of code:
1 Results in gzipped and chunked file transfer:
ob_start('ob_gzhandler');
echo $sResult;
2 Results in normal file transfer with content-length specified (but expect gzipped and content-length):
ob_start('ob_gzhandler');
echo $sResult;
header('Content-Length: '.ob_get_length());
Here some pictures of http header results: 1
2
ob_end_flush()
it flushes the inner output buffering belonging toob_gzhandler
; then the content length is known ... afterwards, the second call flushes the outer buffer. – Ja͢ck