/** * 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 (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; }
public function saveToFileCache($text) { global $wgUseFileCache; if (!$wgUseFileCache || strlen($text) < 512) { // Disabled or empty/broken output (OOM and PHP errors) return $text; } wfDebug(__METHOD__ . "()\n", false); $this->checkCacheDirs(); $f = fopen($this->fileCacheName(), 'w'); if ($f) { $now = wfTimestampNow(); if ($this->useGzip()) { $rawtext = str_replace('</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text); $text = gzencode($rawtext); } else { $text = str_replace('</html>', '<!-- Cached ' . $now . " -->\n</html>", $text); } fwrite($f, $text); fclose($f); if ($this->useGzip()) { if (wfClientAcceptsGzip()) { header('Content-Encoding: gzip'); return $text; } else { return $rawtext; } } else { return $text; } } return $text; }
/** * Save this cache object with the given text. * Use this as an ob_start() handler. * @param $text string * @return bool Whether $wgUseFileCache is enabled */ public function saveToFileCache($text) { global $wgUseFileCache; if (!$wgUseFileCache || strlen($text) < 512) { // Disabled or empty/broken output (OOM and PHP errors) return $text; } wfDebug(__METHOD__ . "()\n", 'log'); $now = wfTimestampNow(); if ($this->useGzip()) { $text = str_replace('</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text); } else { $text = str_replace('</html>', '<!-- Cached ' . $now . " -->\n</html>", $text); } // Store text to FS... $compressed = $this->saveText($text); if ($compressed === false) { return $text; // error } // gzip output to buffer as needed and set headers... if ($this->useGzip()) { // @todo Ugly wfClientAcceptsGzip() function - use context! if (wfClientAcceptsGzip()) { header('Content-Encoding: gzip'); return $compressed; } else { return $text; } } else { return $text; } }
/** * @covers ::wfClientAcceptsGzip */ public function testClientAcceptsGzipTest() { $settings = array('gzip' => true, 'bzip' => false, '*' => false, 'compress, gzip' => true, 'gzip;q=1.0' => true, 'foozip' => false, 'foo*zip' => false, 'gzip;q=abcde' => true, 'gzip;q=12345678.9' => true, ' gzip' => true); if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { $old_server_setting = $_SERVER['HTTP_ACCEPT_ENCODING']; } foreach ($settings as $encoding => $expect) { $_SERVER['HTTP_ACCEPT_ENCODING'] = $encoding; $this->assertEquals($expect, wfClientAcceptsGzip(true), "'{$encoding}' => " . wfBoolToStr($expect)); } if (isset($old_server_setting)) { $_SERVER['HTTP_ACCEPT_ENCODING'] = $old_server_setting; } }
/** * 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; }
function saveToFileCache($origtext) { $text = $origtext; if (strcmp($text, '') == 0) { return ''; } wfDebug(" saveToFileCache()\n", false); $this->checkCacheDirs(); $f = fopen($this->fileCacheName(), 'w'); if ($f) { $now = wfTimestampNow(); if ($this->useGzip()) { $rawtext = str_replace('</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text); $text = gzencode($rawtext); } else { $text = str_replace('</html>', '<!-- Cached ' . $now . " -->\n</html>", $text); } fwrite($f, $text); fclose($f); if ($this->useGzip()) { if (wfClientAcceptsGzip()) { header('Content-Encoding: gzip'); return $text; } else { return $rawtext; } } else { return $text; } } return $text; }