Adding methods for setting headers and handling header output, will make testing easier on the class

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8099 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2009-03-15 00:08:56 +00:00
parent f1d25bcc20
commit ed43e95cf3

View file

@ -79,6 +79,13 @@ class MediaView extends View {
'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh', 'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml', 'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb'); 'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb');
/**
* Holds headers sent to browser before rendering media
*
* @var array
* @access protected
*/
var $_headers = array();
/** /**
* Constructor * Constructor
* *
@ -105,7 +112,7 @@ class MediaView extends View {
} else { } else {
$path = APP . $path . $id; $path = APP . $path . $id;
} }
if (!file_exists($path)) { if (!file_exists($path)) {
header('Content-Type: text/html'); header('Content-Type: text/html');
$this->cakeError('error404'); $this->cakeError('error404');
@ -118,7 +125,7 @@ class MediaView extends View {
if (is_array($mimeType)) { if (is_array($mimeType)) {
$this->mimeType = array_merge($this->mimeType, $mimeType); $this->mimeType = array_merge($this->mimeType, $mimeType);
} }
if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) { if (isset($extension) && isset($this->mimeType[$extension]) && connection_status() == 0) {
$chunkSize = 8192; $chunkSize = 8192;
$buffer = ''; $buffer = '';
@ -148,13 +155,14 @@ class MediaView extends View {
)); ));
} }
foreach($contentTypes as $contentType) { foreach($contentTypes as $contentType) {
header('Content-Type: ' . $contentType); $this->_header('Content-Type: ' . $contentType);
} }
header('Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";'); $this->_header(array(
header('Expires: 0'); 'Content-Disposition: attachment; filename="' . $name . '.' . $extension . '";',
header('Accept-Ranges: bytes'); 'Expires: 0',
header('Cache-Control: private', false); 'Accept-Ranges: bytes',
header('Pragma: private'); 'Cache-Control: private' => false,
'Pragma: private'));
$httpRange = env('HTTP_RANGE'); $httpRange = env('HTTP_RANGE');
if (isset($httpRange)) { if (isset($httpRange)) {
@ -163,30 +171,36 @@ class MediaView extends View {
$size = $fileSize - 1; $size = $fileSize - 1;
$length = $fileSize - $range; $length = $fileSize - $range;
header('HTTP/1.1 206 Partial Content'); $this->_header(array(
header('Content-Length: ' . $length); 'HTTP/1.1 206 Partial Content',
header('Content-Range: bytes ' . $range . $size . '/' . $fileSize); 'Content-Length: ' . $length,
'Content-Range: bytes ' . $range . $size . '/' . $fileSize));
fseek($handle, $range); fseek($handle, $range);
} else { } else {
header('Content-Length: ' . $fileSize); $this->_header('Content-Length: ' . $fileSize);
} }
} else { } else {
header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT'); $this->_header('Date: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
if ($cache) { if ($cache) {
if (!is_numeric($cache)) { if (!is_numeric($cache)) {
$cache = strtotime($cache) - time(); $cache = strtotime($cache) - time();
} }
header('Cache-Control: max-age=' . $cache); $this->_header(array(
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT'); 'Cache-Control: max-age=' . $cache,
header('Pragma: cache'); 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache) . ' GMT',
'Pragma: cache'));
} else { } else {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); $this->_header(array(
header('Pragma: no-cache'); 'Cache-Control: must-revalidate, post-check=0, pre-check=0',
'Pragma: no-cache'));
} }
header('Last-Modified: ' . $modified); $this->_header(array(
header('Content-Type: ' . $this->mimeType[$extension]); 'Last-Modified: ' . $modified,
header('Content-Length: ' . $fileSize); 'Content-Type: ' . $this->mimeType[$extension],
'Content-Length: ' . $fileSize));
} }
$this->_output();
@ob_end_clean(); @ob_end_clean();
while (!feof($handle) && connection_status() == 0 && !connection_aborted()) { while (!feof($handle) && connection_status() == 0 && !connection_aborted()) {
@ -201,5 +215,35 @@ class MediaView extends View {
} }
return false; return false;
} }
/**
* Method to set headers
* @param mixed $key
* @param boolean $boolean
* @access protected
*/
function _header($header, $boolean = true) {
if (is_array($header)) {
foreach ($header as $string => $boolean) {
if (is_numeric($string)) {
$this->_headers[] = array($boolean => true);
} else {
$this->_headers[] = array($string => $boolean);
}
}
return;
}
$this->_headers[] = array($header => $boolean);
return;
}
/**
* Method to output headers
* @access protected
*/
function _output() {
foreach ($this->_headers as $header => $value) {
$string = key($header);
header($string, $value[$string]);
}
}
} }
?> ?>