Skip to content

Commit f843e4e

Browse files
committed
Option for ignoring the Content-Range header
When the response body has already been limited to the requested range, the emitter should ignore the Content-Range header and emit the full response body. Signed-off-by: Stadly <magnar@myrtveit.com>
1 parent 65db2eb commit f843e4e

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/Emitter/SapiStreamEmitter.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ class SapiStreamEmitter implements EmitterInterface
2121
/** @var int Maximum output buffering size for each iteration. */
2222
private $maxBufferLength;
2323

24-
public function __construct(int $maxBufferLength = 8192)
24+
/** @var bool Emit the full response body regardless of the Content-Range header. */
25+
private $ignoreContentRange;
26+
27+
public function __construct(int $maxBufferLength = 8192, bool $ignoreContentRange = false)
2528
{
26-
$this->maxBufferLength = $maxBufferLength;
29+
$this->maxBufferLength = $maxBufferLength;
30+
$this->ignoreContentRange = $ignoreContentRange;
2731
}
2832

2933
/**
@@ -40,7 +44,7 @@ public function emit(ResponseInterface $response): bool
4044

4145
flush();
4246

43-
$range = $this->parseContentRange($response->getHeaderLine('Content-Range'));
47+
$range = $this->ignoreContentRange ? null : $this->parseContentRange($response->getHeaderLine('Content-Range'));
4448

4549
if (null === $range || 'bytes' !== $range[0]) {
4650
$this->emitBody($response);

test/Emitter/SapiStreamEmitterTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
*/
3636
class SapiStreamEmitterTest extends AbstractEmitterTest
3737
{
38+
/** @var SapiStreamEmitter */
39+
private $emitterIgnoreContentRange;
40+
3841
public function setUp(): void
3942
{
4043
HeaderStack::reset();
41-
$this->emitter = new SapiStreamEmitter();
44+
$this->emitter = new SapiStreamEmitter();
45+
$this->emitterIgnoreContentRange = new SapiStreamEmitter(8192, true);
4246
}
4347

4448
public function testEmitCallbackStreamResponse(): void
@@ -593,6 +597,18 @@ public function contentRangeProvider(): array
593597
];
594598
}
595599

600+
/**
601+
* @psalm-return array<array-key, array{0: string, 1: string, 2: string}>
602+
*/
603+
public function ignoreContentRangeProvider(): array
604+
{
605+
return [
606+
['bytes 0-2/*', 'Hel', 'Hel'],
607+
['bytes 3-6/*', 'lo w', 'lo w'],
608+
['items 0-0/1', 'Hello world', 'Hello world'],
609+
];
610+
}
611+
596612
/**
597613
* @dataProvider contentRangeProvider
598614
*/
@@ -608,6 +624,21 @@ public function testContentRange(string $header, string $body, string $expected)
608624
self::assertSame($expected, ob_get_clean());
609625
}
610626

627+
/**
628+
* @dataProvider ignoreContentRangeProvider
629+
*/
630+
public function testIgnoreContentRange(string $header, string $body, string $expected): void
631+
{
632+
$response = (new Response())
633+
->withHeader('Content-Range', $header);
634+
635+
$response->getBody()->write($body);
636+
637+
ob_start();
638+
$this->emitterIgnoreContentRange->emit($response);
639+
self::assertSame($expected, ob_get_clean());
640+
}
641+
611642
public function testContentRangeUnseekableBody(): void
612643
{
613644
$body = new CallbackStream(function () {

0 commit comments

Comments
 (0)