Skip to content

Commit b1fa23e

Browse files
committed
Add BinaryFileResponse::streamsEntireFile method
This method will return false when the client requests only a specific range of a file. See https://github.com/symfony/psr-http-message-bridge/issues/84
1 parent 50f37f0 commit b1fa23e

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,13 @@ public function deleteFileAfterSend($shouldDelete = true)
356356

357357
return $this;
358358
}
359+
360+
/**
361+
* Returns true if the entire file is streamed when calling sendContent().
362+
* Usually this will return false when the client requests a specific range of the file with the Range header.
363+
*/
364+
public function streamsEntireFile(): bool
365+
{
366+
return $this->maxlen === -1 && $this->offset === 0;
367+
}
359368
}

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.14
5+
------
6+
7+
* [BC BREAK] added `streamsEntireFile()` to `BinaryFileResponse`.
8+
49
4.4.0
510
-----
611

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function testSetContentDispositionGeneratesSafeFallbackFilenameForWrongly
8181
/**
8282
* @dataProvider provideRanges
8383
*/
84-
public function testRequests($requestRange, $offset, $length, $responseRange)
84+
public function testRequests($statusCode, $requestRange, $offset, $length, $responseRange, $streamsEntireFile)
8585
{
8686
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, ['Content-Type' => 'application/octet-stream'])->setAutoEtag();
8787

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

108-
$this->assertEquals(206, $response->getStatusCode());
108+
$this->assertEquals($statusCode, $response->getStatusCode());
109109
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
110110
$this->assertSame((string) $length, $response->headers->get('Content-Length'));
111+
$this->assertEquals($streamsEntireFile, $response->streamsEntireFile());
111112
}
112113

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

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

140-
$this->assertEquals(206, $response->getStatusCode());
141+
$this->assertEquals($statusCode, $response->getStatusCode());
141142
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
143+
$this->assertEquals($streamsEntireFile, $response->streamsEntireFile());
142144
}
143145

144146
public function provideRanges()
145147
{
146148
return [
147-
['bytes=1-4', 1, 4, 'bytes 1-4/35'],
148-
['bytes=-5', 30, 5, 'bytes 30-34/35'],
149-
['bytes=30-', 30, 5, 'bytes 30-34/35'],
150-
['bytes=30-30', 30, 1, 'bytes 30-30/35'],
151-
['bytes=30-34', 30, 5, 'bytes 30-34/35'],
149+
[206, 'bytes=1-4', 1, 4, 'bytes 1-4/35', false],
150+
[206, 'bytes=-5', 30, 5, 'bytes 30-34/35', false],
151+
[206, 'bytes=30-', 30, 5, 'bytes 30-34/35', false],
152+
[206, 'bytes=30-30', 30, 1, 'bytes 30-30/35', false],
153+
[206, 'bytes=30-34', 30, 5, 'bytes 30-34/35', false],
154+
[200, 'bytes=0-', 0, 35, null, true],
155+
[200, 'bytes=0-34', 0, 35, null, true],
152156
];
153157
}
154158

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy