10000 bug #85 Fix BinaryFileResponse with range to psr response conversion … · symfony/symfony@cffb3a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit cffb3a8

Browse files
bug #85 Fix BinaryFileResponse with range to psr response conversion (iluuu1994)
This PR was merged into the 2.0-dev branch. Discussion ---------- Fix BinaryFileResponse with range to psr response conversion Closes #84 As requested by [Fabien](#38280 (comment)). I think using the slightly less optimal version of checking for a `Content-Range` header is better than relying on reflection. In theory, this could be slightly sub optimal when streaming whole files and setting the `Content-Range` manually but I'm assuming that's very rare in practice. Commits ------- 5d5932d Fix BinaryFileResponse with range to psr response conversion
2 parents e44f249 + 5d5932d commit cffb3a8

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Factory/PsrHttpFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ public function createResponse(Response $symfonyResponse)
127127
{
128128
$response = $this->responseFactory->createResponse($symfonyResponse->getStatusCode(), Response::$statusTexts[$symfonyResponse->getStatusCode()] ?? '');
129129

130-
if ($symfonyResponse instanceof BinaryFileResponse) {
130+
if ($symfonyResponse instanceof BinaryFileResponse && !$symfonyResponse->headers->has('Content-Range')) {
131131
$stream = $this->streamFactory->createStreamFromFile(
132132
$symfonyResponse->getFile()->getPathname()
133133
);
134134
} else {
135135
$stream = $this->streamFactory->createStreamFromFile('php://temp', 'wb+');
136-
if ($symfonyResponse instanceof StreamedResponse) {
136+
if ($symfonyResponse instanceof StreamedResponse || $symfonyResponse instanceof BinaryFileResponse) {
137137
ob_start(function ($buffer) use ($stream) {
138138
$stream->write($buffer);
139139

Tests/Factory/AbstractHttpMessageFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,23 @@ public function testCreateResponseFromBinaryFile()
182182
$this->assertEquals('Binary', $psrResponse->getBody()->__toString());
183183
}
184184

185+
public function testCreateResponseFromBinaryFileWithRange()
186+
{
187+
$path = tempnam($this->tmpDir, uniqid());
188+
file_put_contents($path, 'Binary');
189+
190+
$request = new Request();
191+
$request->headers->set('Range', 'bytes=1-4');
192+
193+
$response = new BinaryFileResponse($path, 200, ['Content-Type' => 'plain/text']);
194+
$response->prepare($request);
195+
196+
$psrResponse = $this->factory->createResponse($response);
197+
198+
$this->assertEquals('inar', $psrResponse->getBody()->__toString());
199+
$this->assertSame('bytes 1-4/6', $psrResponse->getHeaderLine('Content-Range'));
200+
}
201+
185202
public function testUploadErrNoFile()
186203
{
187204
$file = new UploadedFile('', '', null, UPLOAD_ERR_NO_FILE, true);

0 commit comments

Comments
 (0)
0