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

Commit 962a839

Browse files
committed
Merge pull request #202 from hannesvdvreken/use-relative-stream
Used relative stream to simplify emitBodyRange. Removes necessity to check lower bound
2 parents 7e8aa6d + 5e38627 commit 962a839

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Response/SapiStreamEmitter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Psr\Http\Message\ResponseInterface;
1313
use RuntimeException;
14+
use Zend\Diactoros\RelativeStream;
1415

1516
class SapiStreamEmitter implements EmitterInterface
1617
{
@@ -78,21 +79,20 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
7879
{
7980
list($unit, $first, $last, $length) = $range;
8081

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

8487
if (!$body->isSeekable()) {
8588
$contents = $body->getContents();
8689
echo substr($contents, $first, $last - $first);
8790
return;
8891
}
8992

90-
$body->seek($first);
91-
$pos = $first;
92-
93-
while (! $body->eof() && $pos < $last) {
94-
if (($pos + $maxBufferLength) > $last) {
95-
echo $body->read($last - $pos);
93+
while (! $body->eof() && $pos < $length) {
94+
if (($pos + $maxBufferLength) > $length) {
95+
echo $body->read($length - $pos);
9696
break;
9797
}
9898

0 commit comments

Comments
 (0)