diff --git a/src/Emitter/SapiStreamEmitter.php b/src/Emitter/SapiStreamEmitter.php index 445560d..b4cbf6a 100644 --- a/src/Emitter/SapiStreamEmitter.php +++ b/src/Emitter/SapiStreamEmitter.php @@ -86,7 +86,7 @@ private function emitBodyRange(array $range, ResponseInterface $response): void $length = $last - $first + 1; if ($body->isSeekable()) { - $body->seek($first); + $body->seek($body->getSize() === $length ? 0 : $first); $first = 0; } diff --git a/test/Emitter/SapiStreamEmitterTest.php b/test/Emitter/SapiStreamEmitterTest.php index 0eb606f..ba3938f 100644 --- a/test/Emitter/SapiStreamEmitterTest.php +++ b/test/Emitter/SapiStreamEmitterTest.php @@ -608,6 +608,33 @@ public function testContentRange(string $header, string $body, string $expected) self::assertSame($expected, ob_get_clean()); } + /** + * @psalm-return array + */ + public function ignoreContentRangeProvider(): array + { + return [ + ['bytes 0-2/*', 'Hel', 'Hel'], + ['bytes 3-6/*', 'lo w', 'lo w'], + ['items 0-0/1', 'Hello world', 'Hello world'], + ]; + } + + /** + * @dataProvider ignoreContentRangeProvider + */ + public function testIgnoreContentRange(string $header, string $body, string $expected): void + { + $response = (new Response()) + ->withHeader('Content-Range', $header); + + $response->getBody()->write($body); + + ob_start(); + $this->emitter->emit($response); + self::assertSame($expected, ob_get_clean()); + } + public function testContentRangeUnseekableBody(): void { $body = new CallbackStream(function () {