8000 Fix BinaryFileResponse sending wrong Content-Length header for files modified by stream wrappers/filters by TravisCarden · Pull Request #19740 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix BinaryFileResponse sending wrong Content-Length header for files modified by stream wrappers/filters #19740

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
wants to merge 2 commits into from
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
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/File/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,18 @@ protected function getName($name)

return $originalName;
}

/**
* {@inheritdoc}
*/
public function getSize()
{
// Since stream wrappers and stream filters can change the file contents as read, the only
// reliable way to get the effective size of the file is to actually read it.
ob_start();
$size = readfile($this->getPathname());
ob_end_clean();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will load the full response in memory, which means will break for contents that don't fit into memory.
This totally defeat the purpose of using streams imho.
When a stream wrapper/filter is there, we'd better use chunked encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to be correct, of course. I think the code worked in the context of my project on account of incidental configuration details, but considering the matter abstractly it obviously doesn't seem like a solution suitable for generic application. Using chunked encoding in case of a wrapper/filter does seem like a sensible approach. I don't know what that would look like, though.


return $size;
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/File/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Symfony\Component\HttpFoundation\File\Tests\Fixtures\PaddingStreamWrapper;

class FileTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -28,6 +29,21 @@ public function testGetMimeTypeUsesMimeTypeGuessers()
$this->assertEquals('image/gif', $file->getMimeType());
}

public function testGetSizeWithStreamFilter()
{
// @todo Where should these fixture classes go so this isn't necessary?
require_once 'Fixtures/PaddingStreamWrapper.php';
require_once 'Fixtures/PaddingStreamFilter.php';

stream_wrapper_register('pad', '\Symfony\Component\HttpFoundation\File\Tests\Fixtures\PaddingStreamWrapper');
$path = 'pad://test';

$filtered_contents = file_get_contents($path);
$file = new File($path);

$this->assertEquals(strlen($filtered_contents), $file->getSize());
}

public function testGuessExtensionWithoutGuesser()
{
$file = new File(__DIR__.'/Fixtures/directory/.empty');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Symfony\Component\HttpFoundation\File\Tests\Fixtures;

class PaddingStreamFilter extends \php_user_filter
{
const NAME = 'pad';

const PADDING = ' ';

public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in)) {
$bucket->data .= self::PADDING;
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}

return PSFS_PASS_ON;
}
}
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Symfony\Component\HttpFoundation\File\Tests\Fixtures;

class PaddingStreamWrapper
{
protected $file;

public function stream_open($path, $mode, $options, &$opened_path)
{
$this->file = fopen('php://temp', 'r+');

$path_parts = explode('://', $path);
$file_contents = end($path_parts);
fwrite($this->file, $file_contents);
fseek($this->file, 0);

stream_filter_register(PaddingStreamFilter::NAME, '\Symfony\Component\HttpFoundation\File\Tests\Fixtures\PaddingStreamFilter');
stream_filter_append($this->file, PaddingStreamFilter::NAME, STREAM_FILTER_READ);

return (bool) $this->file;
}

public function stream_read($count)
{
return fread($this->file, $count);
}

public function stream_write($data)
{
return fwrite($this->file, $data);
}

public function stream_tell()
{
return ftell($this->file);
}

public function stream_eof()
{
return feof($this->file);
}

public function stream_seek($offset, $whence)
{
return fseek($this->file, $offset, $whence);
}

public function stream_stat()
{
return fstat($this->file);
}

public function url_stat($path, $flags)
{
$file = fopen($path, 'r');
$stat = fstat($file);
fclose($file);

return $stat;
}
}
0