compress() 공개 메소드

Sets the correct output buffering handler to send a compressed response. Responses will be compressed with zlib, if the extension is available.
public compress ( ) : boolean
리턴 boolean false if client does not accept compressed responses or no handler is available, true otherwise
 /**
  * Sends an asset file to the client
  *
  * @param \Cake\Network\Request $request The request object to use.
  * @param \Cake\Network\Response $response The response object to use.
  * @param string $assetFile Path to the asset file in the file system
  * @param string $ext The extension of the file to determine its mime type
  * @return void
  */
 protected function _deliverAsset(Request $request, Response $response, $assetFile, $ext)
 {
     $compressionEnabled = $response->compress();
     if ($response->type($ext) === $ext) {
         $contentType = 'application/octet-stream';
         $agent = $request->env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             $contentType = 'application/octetstream';
         }
         $response->type($contentType);
     }
     if (!$compressionEnabled) {
         $response->header('Content-Length', filesize($assetFile));
     }
     // $response->cache(filemtime($assetFile), $this->_cacheTime);
     $response->sendHeaders();
     readfile($assetFile);
     if ($compressionEnabled) {
         ob_end_flush();
     }
 }
예제 #2
0
 /**
  * Tests the compress method
  *
  * @return void
  */
 public function testCompress()
 {
     $this->skipIf(defined('HHVM_VERSION'), 'HHVM does not implement ob_gzhandler');
     $response = new Response();
     if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
         $this->assertFalse($response->compress());
         $this->markTestSkipped('Is not possible to test output compression');
     }
     $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
     $result = $response->compress();
     $this->assertFalse($result);
     $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
     $result = $response->compress();
     $this->assertTrue($result);
     $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
     ob_get_clean();
 }
예제 #3
0
 /**
  * Sends an asset file to the client
  *
  * @param \Cake\Network\Request $request The request object to use.
  * @param \Cake\Network\Response $response The response object to use.
  * @param string $assetFile Path to the asset file in the file system
  * @param string $ext The extension of the file to determine its mime type
  * @return void
  */
 protected function _deliverCacheFile(Request $request, Response $response, $file, $ext)
 {
     $compressionEnabled = $response->compress();
     if ($response->type($ext) === $ext) {
         $contentType = 'application/octet-stream';
         $agent = $request->env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             $contentType = 'application/octetstream';
         }
         $response->type($contentType);
     }
     if (!$compressionEnabled) {
         $response->header('Content-Length', filesize($file));
     }
     $content = file_get_contents($file);
     $cacheInfo = $this->extractCacheInfo($content);
     $modifiedTime = filemtime($file);
     $cacheTime = $cacheInfo['time'];
     if (!$cacheTime) {
         $cacheTime = $this->_cacheTime;
     }
     $response->cache($modifiedTime, $cacheTime);
     $response->type($cacheInfo['ext']);
     if (Configure::read('debug') || $this->config('debug')) {
         if ($cacheInfo['ext'] === 'html') {
             $content = '<!--created:' . $modifiedTime . '-->' . $content;
         }
     }
     $response->body($content);
 }
예제 #4
0
 /**
  * Tests the compress method
  *
  * @return void
  */
 public function testCompress()
 {
     if (php_sapi_name() !== 'cli') {
         $this->markTestSkipped('The response compression can only be tested in cli.');
     }
     $response = new Response();
     if (ini_get("zlib.output_compression") === '1' || !extension_loaded("zlib")) {
         $this->assertFalse($response->compress());
         $this->markTestSkipped('Is not possible to test output compression');
     }
     $_SERVER['HTTP_ACCEPT_ENCODING'] = '';
     $result = $response->compress();
     $this->assertFalse($result);
     $_SERVER['HTTP_ACCEPT_ENCODING'] = 'gzip';
     $result = $response->compress();
     $this->assertTrue($result);
     $this->assertTrue(in_array('ob_gzhandler', ob_list_handlers()));
     ob_get_clean();
 }