Need to get the Content-Encoding header before headers are sent.

Hi - when a text document is sent, depending upon the capabilities of the requesting client and the server configuration, it is either sent un-compressed, gzip compressed, or deflate compressed.

I need to detect what is going to happen.

The reason - to be cache friendly I am generating Etag header and evaluating the if-none-match header if a client sends it with a request.

Because of proxy servers, even if the content is identical, the Etag header needs to be different for no-compression, gzip, and deflate.

If my code knows for a fact the server is using zlib.output_compression I can accurately guess based upon the Accept-Encoding header from the client but zlib.output_compression isn’t the only scheme out there.

Using headers_list() unfortunately does not show what Content-Encoding will be sent until after the headers are sent, at which point it is too late to adjust the Etag header.

Is there a reliable way to ask php what it is going to use for the Content-Encoding ??

I’m guessing the reason why headers_list() doesn’t show it before sending headers is because until the point, the code can do things like enable/disable compression that would change what is going to get used - but surely there must be some way to ask php what it is planning to use for the request before the headers are sent.

Thanks for suggestions.

Maybe this will help:

[PHP]

<?php /****************************** * HTTP Compression Self-Test * Source: http://sammitch.ca * Version: 1.0 * Date: Jan 24, 2013 ******************************/ // make sure cURL is available if( ! function_exists('curl_init') ) { die('You must have the cURL extension installed/enabled.'); // make sure the script isn't being called via command line } else if( php_sapi_name() == 'cli' ) { die('This script must be invoked via a web browser.'); } // use a $_GET var to prevent infinite looping through HTTP requests if( ! isset($_GET['self']) ) { // common compression encodings we want to check for in order of desirability // 'identity' means uncompressed $encodings = array( 'gzip' => '1.0', 'deflate' => '0.5', 'compress' => '0.5', 'identity' => '0.1' ); // create the header string $temp = array(); foreach( $encodings as $key => $value ) { $temp[] = sprintf('%s;q=%s', $key, $value); } $accept_header = 'Accept-Encoding: ' . implode(', ', $temp); echo "
Requesting the following encodings:\n\t" . $accept_header . "
"; } else { // if $_GET['self'] is present print out an itty-bitty test page echo <<<_EOF_ Test page

Test Page

_EOF_; } [/PHP] Source: http://sammitch.ca/2013/01/http-compression-self-test-in-php/ Tested output: [QUOTE] Requesting the following encodings: Accept-Encoding: gzip;q=1.0, deflate;q=0.5, compress;q=0.5, identity;q=0.1 [/QUOTE]
Sponsor our Newsletter | Privacy Policy | Terms of Service