2

There is an Apache server on Solaris that is sending 'Transfer-Encoding: chunked' and not sending the 'Content-Length' header that I have in my PHP (used for downloading files). Do you know a way to prevent this?

Thanks

See: https://stackoverflow.com/questions/1334471/content-length-header-always-zero

I have tried the directive

 SetEnvIfNoCase Request_URI get_file\.php$ no-gzip dont-vary

and now I get a file with the same file size of the original, but the file is corrupted. Here are the headers received from the server:

http://example.com/output_file_download.php?fileID=130

GET /output_file_download.php?fileID=130 HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: user_id=7C%25R; intrauser=kE06Ub%238+2dHT%29U0t%28B%2A; intrakey=rtacconi; PHPSESSID=5a3f8edff822474f3b95b6a6e5c87ad2


HTTP/1.x 200 OK
Date: Thu, 03 Sep 2009 10:02:05 GMT
Server: Apache
Expires: 0
Cache-Control: private
Pragma: public
Content-Description: File Transfer
Content-Disposition: attachment; filename=alfresco-logo.gif
Content-Transfer-Encoding: binary
Content-Length: 2401
Keep-Alive: timeout=15, max=500
Connection: Keep-Alive
Content-Type: application/octet-stream

3
  • 1
    I had this problem just now, luckily I found this question. Sep 23, 2012 at 9:21
  • 1
    ...and I meant to add: Rather than changing the Apache config, I called "apache_setenv('no-gzip', '1');" from PHP and the issue was fixed for me. Not sure why you saw corruption but I'd suspect something in the PHP script. Sep 23, 2012 at 9:27
  • This looks like a duplicate of serverfault.com/questions/183843/….
    – FuePi
    Jul 17, 2018 at 14:15

2 Answers 2

5

Chunked output occurs when Apache doesn't know the total output size before sending, as is the case with compressed transfer (Apache compresses data into chunks when they reach a certain size, then despatches them to the browser/requester while the script is still executing). You could be seeing this because you have mod_deflate or mod_gzip active. You can verify if this is your issue here.

You can disable mod_deflate per file like so (more here)

    SetEnvIfNoCase Request_URI get_file\.php$ no-gzip dont-vary

It's best left on in general as it greatly increases the speed of data transfer.

1
  • Please see my question, I have updated what is going on now.
    – rtacconi
    Sep 3, 2009 at 10:08
3

The Apache 2 output filter will automatically add a content-length header if it sees an end-of-stream marker (EOS) AND nothing has been sent yet. source code:

if (ctx->data_sent == 0 && eos) {
  ap_set_content_length(r, r->bytes_sent);
}

If PHP passes any data down to Apache before it sends EOS, then chunking happens. PHP uses a output buffer of 4096 bytes by default. Any page smaller than that will not get chunked.

Also check if the content is fed via gzip (mod_gzip) and gets compressed apache will then turn to chunked encoding.

2
  • The content is fed via gzip, so probably we can disable that module for the web site. Can this solve the problem? Thanks.
    – rtacconi
    Aug 27, 2009 at 10:43
  • You could try Andy's SetEnvIfNoCase recipe..
    – rkthkr
    Aug 27, 2009 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .