Skip to content

Add BinaryFileResponse::streamsEntireFile method #38280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,13 @@ public function deleteFileAfterSend($shouldDelete = true)

return $this;
}

/**
* Returns true if the entire file is streamed when calling sendContent().
* Usually this will return false when the client requests a specific range of the file with the Range header.
*/
public function streamsEntireFile(): bool
{
return -1 === $this->maxlen && 0 === $this->offset;
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.14
------

* [BC BREAK] added `streamsEntireFile()` to `BinaryFileResponse`.

4.4.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
/**
* @dataProvider provideRanges
*/
public function testRequests($requestRange, $offset, $length, $responseRange)
public function testRequests($statusCode, $requestRange, $offset, $length, $responseRange, $streamsEntireFile)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();

Expand All @@ -105,15 +105,16 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
$response->prepare($request);
$response->sendContent();

$this->assertEquals(206, $response->getStatusCode());
$this->assertEquals($statusCode, $response->getStatusCode());
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
$this->assertSame((string) $length, $response->headers->get('Content-Length'));
$this->assertEquals($streamsEntireFile, $response->streamsEntireFile());
}

/**
* @dataProvider provideRanges
*/
public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
public function testRequestsWithoutEtag($statusCode, $requestRange, $offset, $length, $responseRange, $streamsEntireFile)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream']);

Expand All @@ -137,18 +138,21 @@ public function testRequestsWithoutEtag($requestRange, $offset, $length, $respon
$response->prepare($request);
$response->sendContent();

$this->assertEquals(206, $response->getStatusCode());
$this->assertEquals($statusCode, $response->getStatusCode());
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
$this->assertEquals($streamsEntireFile, $response->streamsEntireFile());
}

public function provideRanges()
{
return [
['bytes=1-4', 1, 4, 'bytes 1-4/35'],
['bytes=-5', 30, 5, 'bytes 30-34/35'],
['bytes=30-', 30, 5, 'bytes 30-34/35'],
['bytes=30-30', 30, 1, 'bytes 30-30/35'],
['bytes=30-34', 30, 5, 'bytes 30-34/35'],
[206, 'bytes=1-4', 1, 4, 'bytes 1-4/35', false],
[206, 'bytes=-5', 30, 5, 'bytes 30-34/35', false],
[206, 'bytes=30-', 30, 5, 'bytes 30-34/35', false],
[206, 'bytes=30-30', 30, 1, 'bytes 30-30/35', false],
[206, 'bytes=30-34', 30, 5, 'bytes 30-34/35', false],
[200, 'bytes=0-', 0, 35, null, true],
[200, 'bytes=0-34', 0, 35, null, true],
];
}

Expand Down