Beispiel #1
0
/**
 * Handler that compresses data with gzip if allowed by the Accept header.
 * Unlike ob_gzhandler, it works for HEAD requests too.
 * 
 * @param $s string
 *
 * @return string
 */
function wfGzipHandler($s)
{
    if (!function_exists('gzencode') || headers_sent()) {
        return $s;
    }
    $ext = wfRequestExtension();
    if ($ext == '.gz' || $ext == '.tgz') {
        // Don't do gzip compression if the URL path ends in .gz or .tgz
        // This confuses Safari and triggers a download of the page,
        // even though it's pretty clearly labeled as viewable HTML.
        // Bad Safari! Bad!
        return $s;
    }
    if (wfClientAcceptsGzip()) {
        header('Content-Encoding: gzip');
        $s = gzencode($s, 6);
    }
    // Set vary header if it hasn't been set already
    $headers = headers_list();
    $foundVary = false;
    foreach ($headers as $header) {
        if (substr($header, 0, 5) == 'Vary:') {
            $foundVary = true;
            break;
        }
    }
    if (!$foundVary) {
        header('Vary: Accept-Encoding');
        global $wgUseXVO;
        if ($wgUseXVO) {
            header('X-Vary-Options: Accept-Encoding;list-contains=gzip');
        }
    }
    return $s;
}
Beispiel #2
0
/**
 * Handler that compresses data with gzip if allowed by the Accept header.
 * Unlike ob_gzhandler, it works for HEAD requests too.
 */
function wfGzipHandler($s)
{
    if (!function_exists('gzencode') || headers_sent()) {
        return $s;
    }
    $ext = wfRequestExtension();
    if ($ext == '.gz' || $ext == '.tgz') {
        // Don't do gzip compression if the URL path ends in .gz or .tgz
        // This confuses Safari and triggers a download of the page,
        // even though it's pretty clearly labeled as viewable HTML.
        // Bad Safari! Bad!
        return $s;
    }
    if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
        $tokens = preg_split('/[,; ]/', $_SERVER['HTTP_ACCEPT_ENCODING']);
        if (in_array('gzip', $tokens)) {
            header('Content-Encoding: gzip');
            $s = gzencode($s, 3);
        }
    }
    // Set vary header if it hasn't been set already
    $headers = headers_list();
    $foundVary = false;
    foreach ($headers as $header) {
        if (substr($header, 0, 5) == 'Vary:') {
            $foundVary = true;
            break;
        }
    }
    if (!$foundVary) {
        header('Vary: Accept-Encoding');
        header('X-Vary-Options: Accept-Encoding;list-contains=gzip');
    }
    return $s;
}
Beispiel #3
0
/**
 * Handler that compresses data with gzip if allowed by the Accept header.
 * Unlike ob_gzhandler, it works for HEAD requests too.
 *
 * @param string $s
 *
 * @return string
 */
function wfGzipHandler($s)
{
    if (!function_exists('gzencode')) {
        wfDebug(__FUNCTION__ . "() skipping compression (gzencode unavailable)\n");
        return $s;
    }
    if (headers_sent()) {
        wfDebug(__FUNCTION__ . "() skipping compression (headers already sent)\n");
        return $s;
    }
    $ext = wfRequestExtension();
    if ($ext == '.gz' || $ext == '.tgz') {
        // Don't do gzip compression if the URL path ends in .gz or .tgz
        // This confuses Safari and triggers a download of the page,
        // even though it's pretty clearly labeled as viewable HTML.
        // Bad Safari! Bad!
        return $s;
    }
    if (wfClientAcceptsGzip()) {
        wfDebug(__FUNCTION__ . "() is compressing output\n");
        header('Content-Encoding: gzip');
        $s = gzencode($s, 6);
    }
    // Set vary header if it hasn't been set already
    $headers = headers_list();
    $foundVary = false;
    foreach ($headers as $header) {
        $headerName = strtolower(substr($header, 0, 5));
        if ($headerName == 'vary:') {
            $foundVary = true;
            break;
        }
    }
    if (!$foundVary) {
        header('Vary: Accept-Encoding');
        global $wgUseKeyHeader;
        if ($wgUseKeyHeader) {
            header('Key: Accept-Encoding;match=gzip');
        }
    }
    return $s;
}