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

Commit

Permalink
Used relative stream to simplify emitBodyRange. Removes necessity to …
Browse files Browse the repository at this point in the history
…check lower bound
  • Loading branch information
hannesvdvreken committed Sep 7, 2016
1 parent 8493866 commit 5e38627
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 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 @@ -78,21 +79,20 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
{
list($unit, $first, $last, $length) = $range;

++$last; //zero-based position
$body = $response->getBody();
$body = new RelativeStream($response->getBody(), $first);
$body->rewind();
$pos = 0;
$length = $last - $first + 1;

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

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

while (! $body->eof() && $pos < $last) {
if (($pos + $maxBufferLength) > $last) {
echo $body->read($last - $pos);
while (! $body->eof() && $pos < $length) {
if (($pos + $maxBufferLength) > $length) {
echo $body->read($length - $pos);
break;
}

Expand Down

0 comments on commit 5e38627

Please sign in to comment.