protected function _deliverMedia(CakeResponse $response, $mediaFile, $mediaInfo) { $response->sharable(true, 2592000); //$response->mustRevalidate(true); $response->expires('+30 days'); $modTime = filemtime($mediaFile); $response->modified($modTime); $response->etag(md5($mediaFile . $modTime)); //$response->header("Pragma", "cache"); $response->type($mediaInfo['ext']); $response->file($mediaFile); $response->send(); }
protected function _deliver(CakeResponse $response, Asset $asset) { ob_start(); $compressionEnabled = Configure::read('Asset.compress') && $response->compress(); if ($response->type($asset->extension()) == $asset->extension()) { $contentType = 'application/octet-stream'; $agent = 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', $asset->size()); } $response->cache(filemtime($asset->file)); $response->send(); ob_clean(); echo $asset->content(); if ($compressionEnabled) { ob_end_flush(); } }
/** * testRenderWithView method * * @return void */ public function testRenderWithView() { App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $Controller->name = $Controller->viewPath = 'Posts'; $data = array('User' => array('username' => 'fake'), 'Item' => array(array('name' => 'item1'), array('name' => 'item2'))); $Controller->set('user', $data); $View = new JsonView($Controller); $output = $View->render('index'); $expected = json_encode(array('user' => 'fake', 'list' => array('item1', 'item2'))); $this->assertIdentical($expected, $output); $this->assertIdentical('application/json', $Response->type()); }
/** * testRenderWithView method * * @return void */ public function testRenderWithView() { App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $Controller->name = $Controller->viewPath = 'Posts'; $data = array(array('User' => array('username' => 'user1')), array('User' => array('username' => 'user2'))); $Controller->set('users', $data); $View = new XmlView($Controller); $output = $View->render('index'); $expected = '<?xml version="1.0" encoding="UTF-8"?><users><user>user1</user><user>user2</user></users>'; $this->assertIdentical($expected, str_replace(array("\r", "\n"), '', $output)); $this->assertIdentical('application/xml', $Response->type()); $this->assertInstanceOf('HelperCollection', $View->Helpers); }
/** * Sends an asset file to the client * * @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($assetFile, $ext) { ob_start(); $compressionEnabled = Configure::read('Asset.compress') && $this->response->compress(); if ($this->response->type($ext) == $ext) { $contentType = 'application/octet-stream'; $agent = 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'; } $this->response->type($contentType); } $this->response->cache(filemtime($assetFile)); $this->response->send(); ob_clean(); if ($ext === 'css' || $ext === 'js') { include $assetFile; } else { readfile($assetFile); } if ($compressionEnabled) { ob_end_flush(); } }
/** * testRenderWithView method * * @return void */ public function testRenderWithView() { App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $Controller->name = $Controller->viewPath = 'Posts'; $data = array(array('User' => array('username' => 'user1')), array('User' => array('username' => 'user2'))); $Controller->set('users', $data); $View = new XmlView($Controller); $output = $View->render('index'); $expected = array('users' => array('user' => array('user1', 'user2'))); $expected = Xml::build($expected)->asXML(); $this->assertSame($expected, $output); $this->assertSame('application/xml', $Response->type()); $this->assertInstanceOf('HelperCollection', $View->Helpers); }
/** * Tests the type method * */ public function testType() { $response = new CakeResponse(); $this->assertEquals('text/html', $response->type()); $response->type('pdf'); $this->assertEquals('application/pdf', $response->type()); $this->assertEquals('application/crazy-mime', $response->type('application/crazy-mime')); $this->assertEquals('application/json', $response->type('json')); $this->assertEquals('text/vnd.wap.wml', $response->type('wap')); $this->assertEquals('application/vnd.wap.xhtml+xml', $response->type('xhtml-mobile')); $this->assertEquals('text/csv', $response->type('csv')); $response->type(array('keynote' => 'application/keynote', 'bat' => 'application/bat')); $this->assertEquals('application/keynote', $response->type('keynote')); $this->assertEquals('application/bat', $response->type('bat')); $this->assertFalse($response->type('wackytype')); }
/** * Returns the current response type (Content-type header), or null if not alias exists * * @return mixed A string content type alias, or raw content type if no alias map exists, * otherwise null */ public function responseType() { return $this->mapType($this->response->type()); }
/** * AjaxViewTest::testWithoutSubdir() * * @return void */ public function testWithoutSubdir() { $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $View = new AjaxView($Controller); $View->viewPath = 'Items'; $View->subDir = false; $result = $View->render('index'); $this->assertSame('application/json', $Response->type()); $expected = array('error' => null, 'content' => 'My Index Test ctp'); $expected = json_encode($expected); $this->assertTextEquals($expected, $result); }
/** * testRenderWithView method * * @return void */ public function testRenderWithView() { App::build(array('View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS))); $Request = new CakeRequest(null, false); $Request->params['named'] = array('page' => 2); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $Controller->name = $Controller->viewPath = 'Posts'; $data = array('User' => array('username' => 'fake'), 'Item' => array(array('name' => 'item1'), array('name' => 'item2'))); $Controller->set('user', $data); $View = new JsonView($Controller); $View->helpers = array('Paginator'); $output = $View->render('index'); $expected = array('user' => 'fake', 'list' => array('item1', 'item2'), 'paging' => array('page' => 2)); $this->assertSame(json_encode($expected), $output); $this->assertSame('application/json', $Response->type()); $View->request->query = array('jsonCallback' => 'jfunc'); $Controller->set('_jsonp', 'jsonCallback'); $View = new JsonView($Controller); $View->helpers = array('Paginator'); $output = $View->render('index'); $expected['paging']['?']['jsonCallback'] = 'jfunc'; $expected = 'jfunc(' . json_encode($expected) . ')'; $this->assertSame($expected, $output); $this->assertSame('application/javascript', $Response->type()); }
/** * Display or download the given file * * @param string $view Not used * @param string $layout Not used * @return mixed * @throws NotFoundException */ public function render($view = null, $layout = null) { $name = $download = $extension = $id = $modified = $path = $cache = $mimeType = $compress = null; extract($this->viewVars, EXTR_OVERWRITE); if (is_dir($path)) { $path = $path . $id; } else { $path = APP . $path . $id; } if (!is_file($path)) { if (Configure::read('debug')) { throw new NotFoundException(sprintf('The requested file %s was not found', $path)); } throw new NotFoundException('The requested file was not found'); } if (is_array($mimeType)) { $this->response->type($mimeType); } if (isset($extension) && $this->_isActive()) { $extension = strtolower($extension); $chunkSize = 8192; $buffer = ''; $fileSize = @filesize($path); $handle = fopen($path, 'rb'); if ($handle === false) { return false; } if (!empty($modified) && !is_numeric($modified)) { $modified = strtotime($modified, time()); } else { $modified = time(); } if ($this->response->type($extension) === false) { $download = true; } if ($cache) { $this->response->cache($modified, $cache); } else { $this->response->header(array('Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT', 'Expires' => '0', 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0', 'Pragma' => 'no-cache')); } if ($download) { $agent = env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) { $contentType = 'application/octetstream'; } else { if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/force-download'; } } if (!empty($contentType)) { $this->response->type($contentType); } if (is_null($name)) { $name = $id; } $this->response->download($name); $this->response->header(array('Accept-Ranges' => 'bytes')); $httpRange = env('HTTP_RANGE'); if (isset($httpRange)) { list($toss, $range) = explode('=', $httpRange); $size = $fileSize - 1; $length = $fileSize - $range; $this->response->header(array('Content-Length' => $length, 'Content-Range' => 'bytes ' . $range . $size . '/' . $fileSize)); $this->response->statusCode(206); fseek($handle, $range); } else { $this->response->header('Content-Length', $fileSize); } } else { $this->response->header(array('Content-Length' => $fileSize)); } $this->_clearBuffer(); if ($compress) { $this->_compressionEnabled = $this->response->compress(); } $this->response->send(); return $this->_sendFile($handle); } return false; }
/** * RssViewTest::testSerializeWithArrayLinks() * * `'atom:link' => array('@href' => array(...)` becomes * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link * * @return void */ public function testSerializeWithArrayLinks() { $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = array('channel' => array('title' => 'Channel title', 'link' => 'http://channel.example.org', 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')), 'description' => 'Channel description'), 'items' => array(array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one'), array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two'))); $Controller->set(array('channel' => $data, '_serialize' => 'channel')); $View = new RssView($Controller); $result = $View->render(false); $expected = <<<RSS <?xml version="1.0" encoding="UTF-8"?> <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"> <channel> <title>Channel title</title> <link>http://channel.example.org</link> <atom:link href="{$this->baseUrl}/foo/bar" rel="self" type="application/rss+xml"/> <description>Channel description</description> <item> <title>Title One</title> <link>{$this->baseUrl}/foo/bar</link> <description>Content one</description> </item> <item> <title>Title Two</title> <link>{$this->baseUrl}/foo/bar</link> <description>Content two</description> </item> </channel> </rss> RSS; //debug($result); $this->assertSame('application/rss+xml', $Response->type()); $this->assertTextEquals($expected, $result); }
/** * CsvViewTest::testRenderWithSpecialCharacters() * * @return void */ public function testRenderWithSpecialCharacters() { App::build(array('View' => realpath(dirname(__FILE__) . DS . '..' . DS . '..' . DS . 'test_app' . DS . 'View' . DS) . DS)); $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $Controller->name = $Controller->viewPath = 'Posts'; $data = array(array('User' => array('username' => 'José'), 'Item' => array('type' => 'äöü')), array('User' => array('username' => 'Including,Comma'), 'Item' => array('name' => 'Containing"char', 'type' => 'Containing\'char')), array('User' => array('username' => 'Some Space'), 'Item' => array('name' => "A\nNewline", 'type' => "A\tTab"))); $_extract = array('User.username', 'Item.name', 'Item.type'); $Controller->set(array('user' => $data, '_extract' => $_extract)); $Controller->set(array('_serialize' => 'user')); $View = new CsvView($Controller); $output = $View->render(false); $expected = <<<CSV José,NULL,äöü "Including,Comma","Containing""char",Containing'char "Some Space","A Newline","A\tTab" CSV; $this->assertTextEquals($expected, $output); $this->assertSame('text/csv', $Response->type()); }
/** * ダウンロード * * @param string $filename ダウンロード時のファイル名 * @return CakeResponse */ public function download($filename) { $response = new CakeResponse(); $response->type('text/csv'); $response->file($this->path, ['name' => $filename, 'download' => 'true']); return $response; }
/** * Download * * @param string $filename download時のファイル名 * @return CakeResponse ダウンロードレスポンス */ public function download($filename) { // closeされてなかったらcloseする if ($this->_open) { $this->close(); } $response = new CakeResponse(); $response->type('application/zip'); $response->file($this->path, ['name' => $filename, 'download' => 'true']); return $response; }