Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/202' into develop
Browse files Browse the repository at this point in the history
Forward port #202
  • Loading branch information
weierophinney committed Sep 7, 2016
2 parents 5612186 + 8dc92eb commit 84ccec6
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/Response/SapiStreamEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Zend\Diactoros\RelativeStream;

class SapiStreamEmitter implements EmitterInterface
{
Expand Down Expand Up @@ -56,14 +57,15 @@ public function emit(ResponseInterface $response, $maxBufferLength = 8192)
private function emitBody(ResponseInterface $response, $maxBufferLength)
{
$body = $response->getBody();
if ($body->isSeekable()) {
$body->rewind();

while (! $body->eof()) {
echo $body->read($maxBufferLength);
}
} else {
if (! $body->isSeekable()) {
echo $body;
return;
}

$body->rewind();
while (! $body->eof()) {
echo $body->read($maxBufferLength);
}
}

Expand All @@ -78,21 +80,21 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
{
list($unit, $first, $last, $length) = $range;

++$last; //zero-based position
$body = $response->getBody();

if (!$body->isSeekable()) {
if (! $body->isSeekable()) {
$contents = $body->getContents();
echo substr($contents, $first, $last - $first);
echo substr($contents, $first, $last - $first + 1);
return;
}

$body->seek($first);
$pos = $first;

while (! $body->eof() && $pos < $last) {
if (($pos + $maxBufferLength) > $last) {
echo $body->read($last - $pos);
$body = new RelativeStream($body, $first);
$body->rewind();
$pos = 0;
$length = $last - $first + 1;
while (! $body->eof() && $pos < $length) {
if (($pos + $maxBufferLength) > $length) {
echo $body->read($length - $pos);
break;
}

Expand Down

0 comments on commit 84ccec6

Please sign in to comment.