Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Always send Content-Length in Apache?

I'm loading a particularly large JSON string that is dynamically generated by PHP. To provide some feedback to the user, I want to show download progress.

I have the code figured out, and it works fine for static content such as images, JS files, etc. However, it doesn't seem to work for dynamic files.

This makes sense, since the dynamic files don't have predictable content length, but even if I add this in PHP:

ob_start(function($c) {
    header("Content-Length: ".strlen($c));
    return $c;
});

It still does not send the header (but if I add any other header it works fine).

Is there any way to force Apache to send the Content-Length header? Currently my only alternative is to save the output to a temporary file and redirect to it instead. This would work, but it's kind of messy so I'd rather avoid it if possible.

like image 545
Niet the Dark Absol Avatar asked Jan 22 '13 17:01

Niet the Dark Absol


People also ask

Is content-length set automatically?

When making a POST request, HTTP clients typically automatically add a Content-Length header based on the size of the data supplied and a Content-Type header based on the type of data being transferred.

Is content-length mandatory?

The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.

Is content-length header required request?

The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.


1 Answers

I had similar problem but in my case Content-Length header wasn't sent by Apache because the response was gzip compressed. When I disable compression the Content-Length was calculated and sent properly.

Below is htaccess setting to disable gzip compression for swf file only

<FilesMatch "\.swf$">
  SetEnv no-gzip 1
</FilesMatch>
like image 167
gerrytan Avatar answered Oct 03 '22 14:10

gerrytan