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

Commit 21feb7b

Browse files
committed
Merge branch 'hotfix/202'
Close #202 Close #188
2 parents 7e8aa6d + 8dc92eb commit 21feb7b

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/Response/SapiStreamEmitter.php

Lines changed: 17 additions & 15 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
{
@@ -56,14 +57,15 @@ public function emit(ResponseInterface $response, $maxBufferLength = 8192)
5657
private function emitBody(ResponseInterface $response, $maxBufferLength)
5758
{
5859
$body = $response->getBody();
59-
if ($body->isSeekable()) {
60-
$body->rewind();
6160

62-
while (! $body->eof()) {
63-
echo $body->read($maxBufferLength);
64-
}
65-
} else {
61+
if (! $body->isSeekable()) {
6662
echo $body;
63+
return;
64+
}
65+
66+
$body->rewind();
67+
while (! $body->eof()) {
68+
echo $body->read($maxBufferLength);
6769
}
6870
}
6971

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

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

84-
if (!$body->isSeekable()) {
85+
if (! $body->isSeekable()) {
8586
$contents = $body->getContents();
86-
echo substr($contents, $first, $last - $first);
87+
echo substr($contents, $first, $last - $first + 1);
8788
return;
8889
}
8990

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);
91+
$body = new RelativeStream($body, $first);
92+
$body->rewind();
93+
$pos = 0;
94+
$length = $last - $first + 1;
95+
while (! $body->eof() && $pos < $length) {
96+
if (($pos + $maxBufferLength) > $length) {
97+
echo $body->read($length - $pos);
9698
break;
9799
}
98100

0 commit comments

Comments
 (0)