Commit d7cb4ce9 by Qiang Xue

Fixes issue #276: support range request for sending content as a file.

parent 1a7f5f58
...@@ -204,17 +204,12 @@ class Response extends \yii\base\Response ...@@ -204,17 +204,12 @@ class Response extends \yii\base\Response
{ {
$this->sendHeaders(); $this->sendHeaders();
$this->sendContent(); $this->sendContent();
for ($level = ob_get_level(); $level > 0; --$level) {
if (function_exists('fastcgi_finish_request')) { if (!@ob_end_flush()) {
fastcgi_finish_request(); ob_clean();
} else {
for ($level = ob_get_level(); $level > 0; --$level) {
if (!@ob_end_flush()) {
ob_clean();
}
} }
flush();
} }
flush();
} }
public function reset() public function reset()
...@@ -306,8 +301,15 @@ class Response extends \yii\base\Response ...@@ -306,8 +301,15 @@ class Response extends \yii\base\Response
*/ */
public function sendContentAsFile($content, $attachmentName, $mimeType = 'application/octet-stream') public function sendContentAsFile($content, $attachmentName, $mimeType = 'application/octet-stream')
{ {
$this->getHeaders() $headers = $this->getHeaders();
->addDefault('Pragma', 'public') $contentLength = StringHelper::strlen($content);
$range = $this->getHttpRange($contentLength);
if ($range === false) {
$headers->set('Content-Range', "bytes */$contentLength");
throw new HttpException(416, Yii::t('yii', 'Requested range not satisfiable'));
}
$headers->addDefault('Pragma', 'public')
->addDefault('Accept-Ranges', 'bytes') ->addDefault('Accept-Ranges', 'bytes')
->addDefault('Expires', '0') ->addDefault('Expires', '0')
->addDefault('Content-Type', $mimeType) ->addDefault('Content-Type', $mimeType)
...@@ -316,7 +318,16 @@ class Response extends \yii\base\Response ...@@ -316,7 +318,16 @@ class Response extends \yii\base\Response
->addDefault('Content-Length', StringHelper::strlen($content)) ->addDefault('Content-Length', StringHelper::strlen($content))
->addDefault('Content-Disposition', "attachment; filename=\"$attachmentName\""); ->addDefault('Content-Disposition', "attachment; filename=\"$attachmentName\"");
$this->content = $content; list($begin, $end) = $range;
if ($begin !=0 || $end != $contentLength - 1) {
$this->setStatusCode(206);
$headers->set('Content-Range', "bytes $begin-$end/$contentLength");
$this->content = StringHelper::substr($content, $begin, $end - $begin + 1);
} else {
$this->setStatusCode(200);
$this->content = $content;
}
$this->send(); $this->send();
} }
...@@ -347,10 +358,6 @@ class Response extends \yii\base\Response ...@@ -347,10 +358,6 @@ class Response extends \yii\base\Response
$this->setStatusCode(200); $this->setStatusCode(200);
} }
if (isset($options['mimeType'])) {
$headers->set('Content-Type', $options['mimeType']);
}
$length = $end - $begin + 1; $length = $end - $begin + 1;
$headers->addDefault('Pragma', 'public') $headers->addDefault('Pragma', 'public')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment