[HttpFoundation] Add File\Stream for size-unknown BinaryFileResponse
This commit is contained in:
parent
6f6100a8e1
commit
8011209a18
@ -186,13 +186,6 @@ class BinaryFileResponse extends Response
|
||||
*/
|
||||
public function prepare(Request $request)
|
||||
{
|
||||
$this->headers->set('Content-Length', $this->file->getSize());
|
||||
|
||||
if (!$this->headers->has('Accept-Ranges')) {
|
||||
// Only accept ranges on safe HTTP methods
|
||||
$this->headers->set('Accept-Ranges', $request->isMethodSafe(false) ? 'bytes' : 'none');
|
||||
}
|
||||
|
||||
if (!$this->headers->has('Content-Type')) {
|
||||
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
|
||||
}
|
||||
@ -206,6 +199,16 @@ class BinaryFileResponse extends Response
|
||||
$this->offset = 0;
|
||||
$this->maxlen = -1;
|
||||
|
||||
if (false === $fileSize = $this->file->getSize()) {
|
||||
return $this;
|
||||
}
|
||||
$this->headers->set('Content-Length', $fileSize);
|
||||
|
||||
if (!$this->headers->has('Accept-Ranges')) {
|
||||
// Only accept ranges on safe HTTP methods
|
||||
$this->headers->set('Accept-Ranges', $request->isMethodSafe(false) ? 'bytes' : 'none');
|
||||
}
|
||||
|
||||
if (self::$trustXSendfileTypeHeader && $request->headers->has('X-Sendfile-Type')) {
|
||||
// Use X-Sendfile, do not send any content.
|
||||
$type = $request->headers->get('X-Sendfile-Type');
|
||||
@ -237,7 +240,6 @@ class BinaryFileResponse extends Response
|
||||
// Process the range headers.
|
||||
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
|
||||
$range = $request->headers->get('Range');
|
||||
$fileSize = $this->file->getSize();
|
||||
|
||||
list($start, $end) = explode('-', substr($range, 6), 2) + array(0);
|
||||
|
||||
|
@ -4,6 +4,8 @@ CHANGELOG
|
||||
3.3.0
|
||||
-----
|
||||
|
||||
* added `File\Stream`, to be passed to `BinaryFileResponse` when the size of the served file is unknown,
|
||||
disabling `Range` and `Content-Length` handling, switching to chunked encoding instead
|
||||
* added the `Cookie::fromString()` method that allows to create a cookie from a
|
||||
raw header string
|
||||
|
||||
|
28
src/Symfony/Component/HttpFoundation/File/Stream.php
Normal file
28
src/Symfony/Component/HttpFoundation/File/Stream.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\HttpFoundation\File;
|
||||
|
||||
/**
|
||||
* A PHP stream of unknown size.
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class Stream extends File
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSize()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\HttpFoundation\Tests;
|
||||
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\File\Stream;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
use Symfony\Component\HttpFoundation\Tests\File\FakeFile;
|
||||
@ -97,6 +98,7 @@ class BinaryFileResponseTest extends ResponseTestCase
|
||||
|
||||
$this->assertEquals(206, $response->getStatusCode());
|
||||
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
|
||||
$this->assertSame($length, $response->headers->get('Content-Length'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -315,6 +317,15 @@ class BinaryFileResponseTest extends ResponseTestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testStream()
|
||||
{
|
||||
$request = Request::create('/');
|
||||
$response = new BinaryFileResponse(new Stream(__DIR__.'/../README.md'), 200, array('Content-Type' => 'text/plain'));
|
||||
$response->prepare($request);
|
||||
|
||||
$this->assertNull($response->headers->get('Content-Length'));
|
||||
}
|
||||
|
||||
protected function provideResponse()
|
||||
{
|
||||
return new BinaryFileResponse(__DIR__.'/../README.md', 200, array('Content-Type' => 'application/octet-stream'));
|
||||
|
Reference in New Issue
Block a user