HTTP_Encoder::getAcceptedEncoding PHP 메소드

getAcceptedEncoding() 공개 정적인 메소드

If no Accept-Encoding header is set, or the browser is IE before v6 SP2, this will return ('', ''), the "identity" encoding. A syntax-aware scan is done of the Accept-Encoding, so the method must be non 0. The methods are favored in order of gzip, deflate, then compress. Deflate is always smallest and generally faster, but is rarely sent by servers, so client support could be buggier.
public static getAcceptedEncoding ( boolean $allowCompress = true, boolean $allowDeflate = true ) : array
$allowCompress boolean allow the older compress encoding
$allowDeflate boolean allow the more recent deflate encoding
리턴 array two values, 1st is the actual encoding method, 2nd is the alias of that method to use in the Content-Encoding header (some browsers call gzip "x-gzip" etc.)
    public static function getAcceptedEncoding($allowCompress = true, $allowDeflate = true)
    {
        // @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
        if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) || self::isBuggyIe()) {
            return array('', '');
        }
        $ae = $_SERVER['HTTP_ACCEPT_ENCODING'];
        // gzip checks (quick)
        if (0 === strpos($ae, 'gzip,') || 0 === strpos($ae, 'deflate, gzip,')) {
            return array('gzip', 'gzip');
        }
        // gzip checks (slow)
        if (preg_match('@(?:^|,)\\s*((?:x-)?gzip)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@', $ae, $m)) {
            return array('gzip', $m[1]);
        }
        if ($allowDeflate) {
            // deflate checks
            $aeRev = strrev($ae);
            if (0 === strpos($aeRev, 'etalfed ,') || 0 === strpos($aeRev, 'etalfed,') || 0 === strpos($ae, 'deflate,') || preg_match('@(?:^|,)\\s*deflate\\s*(?:$|,|;\\s*q=(?:0\\.|1))@', $ae)) {
                return array('deflate', 'deflate');
            }
        }
        if ($allowCompress && preg_match('@(?:^|,)\\s*((?:x-)?compress)\\s*(?:$|,|;\\s*q=(?:0\\.|1))@', $ae, $m)) {
            return array('compress', $m[1]);
        }
        return array('', '');
    }

Usage Example

예제 #1
0
function test_HTTP_Encoder()
{
    global $thisDir;
    HTTP_Encoder::$encodeToIe6 = true;
    $methodTests = array(array('ua' => 'Any browser', 'ae' => 'compress, x-gzip', 'exp' => array('gzip', 'x-gzip'), 'desc' => 'recognize "x-gzip" as gzip'), array('ua' => 'Any browser', 'ae' => 'compress, x-gzip;q=0.5', 'exp' => array('gzip', 'x-gzip'), 'desc' => 'gzip w/ non-zero q'), array('ua' => 'Any browser', 'ae' => 'compress, x-gzip;q=0', 'exp' => array('compress', 'compress'), 'desc' => 'gzip w/ zero q'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE6 w/o "enhanced security"'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'ae' => 'gzip, deflate', 'exp' => array('deflate', 'deflate'), 'desc' => 'IE6 w/ "enhanced security"'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE5.5'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.25', 'ae' => 'gzip,deflate', 'exp' => array('deflate', 'deflate'), 'desc' => 'Opera identifying as IE6'));
    foreach ($methodTests as $test) {
        $_SERVER['HTTP_USER_AGENT'] = $test['ua'];
        $_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae'];
        $exp = $test['exp'];
        $ret = HTTP_Encoder::getAcceptedEncoding();
        $passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']);
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n";
            echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
            echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
        }
    }
    HTTP_Encoder::$encodeToIe6 = false;
    $methodTests = array(array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE6 w/ "enhanced security"'));
    foreach ($methodTests as $test) {
        $_SERVER['HTTP_USER_AGENT'] = $test['ua'];
        $_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae'];
        $exp = $test['exp'];
        $ret = HTTP_Encoder::getAcceptedEncoding();
        $passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']);
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n";
            echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n";
            echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n";
        }
    }
    if (!function_exists('gzdeflate')) {
        echo "!WARN: HTTP_Encoder : Zlib support is not present in PHP. Encoding cannot be performed/tested.\n";
        return;
    }
    // test compression of varied content (HTML,JS, & CSS)
    $variedContent = file_get_contents($thisDir . '/_test_files/html/before.html') . file_get_contents($thisDir . '/_test_files/css/subsilver.css') . file_get_contents($thisDir . '/_test_files/js/jquery-1.2.3.js');
    $variedLength = strlen($variedContent);
    $encodingTests = array(array('method' => 'deflate', 'inv' => 'gzinflate', 'exp' => 32157), array('method' => 'gzip', 'inv' => '_gzdecode', 'exp' => 32175), array('method' => 'compress', 'inv' => 'gzuncompress', 'exp' => 32211));
    foreach ($encodingTests as $test) {
        $e = new HTTP_Encoder(array('content' => $variedContent, 'method' => $test['method']));
        $e->encode(9);
        $ret = strlen($e->getContent());
        // test uncompression
        $roundTrip = @call_user_func($test['inv'], $e->getContent());
        $desc = "HTTP_Encoder : {$test['method']} : uncompress possible";
        $passed = assertTrue($variedContent == $roundTrip, $desc);
        // test expected compressed size
        $desc = "HTTP_Encoder : {$test['method']} : compressed to " . sprintf('%4.2f%% of original', $ret / $variedLength * 100);
        $passed = assertTrue(abs($ret - $test['exp']) < 100, $desc);
        if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) {
            echo "\n--- {$test['method']}: expected bytes: ", "{$test['exp']}. Returned: {$ret} ", "(off by " . abs($ret - $test['exp']) . " bytes)\n\n";
        }
    }
}
All Usage Examples Of HTTP_Encoder::getAcceptedEncoding