Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80494dd

Browse files
committed
automatically generate safe fallback filename
1 parent 8d0ec00 commit 80494dd

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
157157
$filename = $this->file->getFilename();
158158
}
159159

160+
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
161+
$encoding = mb_detect_encoding($filename);
162+
163+
for ($i = 0; $i < mb_strlen($filename, $encoding); $i++) {
164+
$char = mb_substr($filename, $i, 1, $encoding);
165+
166+
if (ord($char) < 32 || ord($char) > 126) {
167+
$filenameFallback .= '_';
168+
} else {
169+
$filenameFallback .= $char;
170+
}
171+
}
172+
}
173+
160174
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
161175
$this->headers->set('Content-Disposition', $dispositionHeader);
162176

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function testConstruction()
3434
$this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
3535
}
3636

37+
public function testConstructWithNonAsciiFilename()
38+
{
39+
new BinaryFileResponse(__DIR__.'/Fixtures/föö.html', 200, array(), true, 'attachment');
40+
}
41+
3742
/**
3843
* @expectedException \LogicException
3944
*/
@@ -49,6 +54,14 @@ public function testGetContent()
4954
$this->assertFalse($response->getContent());
5055
}
5156

57+
public function testSetContentDispositionGeneratesSafeFallbackFilename()
58+
{
59+
$response = new BinaryFileResponse(__FILE__);
60+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
61+
62+
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
63+
}
64+
5265
/**
5366
* @dataProvider provideRanges
5467
*/

src/Symfony/Component/HttpFoundation/Tests/Fixtures/föö.html

Whitespace-only changes.

0 commit comments

Comments
 (0)