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

Commit 8dc92eb

Browse files
committed
Reinstate fix from #200
Order of operations was critical here; always check to see if the body is seekable before attempting to rewind it.
1 parent 962a839 commit 8dc92eb

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/Response/SapiStreamEmitter.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ public function emit(ResponseInterface $response, $maxBufferLength = 8192)
5757
private function emitBody(ResponseInterface $response, $maxBufferLength)
5858
{
5959
$body = $response->getBody();
60-
if ($body->isSeekable()) {
61-
$body->rewind();
6260

63-
while (! $body->eof()) {
64-
echo $body->read($maxBufferLength);
65-
}
66-
} else {
61+
if (! $body->isSeekable()) {
6762
echo $body;
63+
return;
64+
}
65+
66+
$body->rewind();
67+
while (! $body->eof()) {
68+
echo $body->read($maxBufferLength);
6869
}
6970
}
7071

@@ -79,17 +80,18 @@ private function emitBodyRange(array $range, ResponseInterface $response, $maxBu
7980
{
8081
list($unit, $first, $last, $length) = $range;
8182

82-
$body = new RelativeStream($response->getBody(), $first);
83-
$body->rewind();
84-
$pos = 0;
85-
$length = $last - $first + 1;
83+
$body = $response->getBody();
8684

87-
if (!$body->isSeekable()) {
85+
if (! $body->isSeekable()) {
8886
$contents = $body->getContents();
89-
echo substr($contents, $first, $last - $first);
87+
echo substr($contents, $first, $last - $first + 1);
9088
return;
9189
}
9290

91+
$body = new RelativeStream($body, $first);
92+
$body->rewind();
93+
$pos = 0;
94+
$length = $last - $first + 1;
9395
while (! $body->eof() && $pos < $length) {
9496
if (($pos + $maxBufferLength) > $length) {
9597
echo $body->read($length - $pos);

0 commit comments

Comments
 (0)