Skip to content

Commit

Permalink
Don't add Accept-Range header on unsafe HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph authored and fabpot committed Jan 3, 2015
1 parent cb79d91 commit 24a287f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Expand Up @@ -169,7 +169,11 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
public function prepare(Request $request)
{
$this->headers->set('Content-Length', $this->file->getSize());
$this->headers->set('Accept-Ranges', 'bytes');

if (!$this->headers->has('Accept-Ranges')) {
// Only accept ranges on safe HTTP methods
$this->headers->set('Accept-Ranges', $request->isMethodSafe() ? 'bytes' : 'none');
}

if (!$this->headers->has('Content-Type')) {
$this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream');
Expand Down
Expand Up @@ -200,6 +200,25 @@ public function testSplFileObject()
$this->assertEquals(realpath($response->getFile()->getPathname()), realpath($filePath));
}

public function testAcceptRangeOnUnsafeMethods()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif');
$response->prepare($request);

$this->assertEquals('none', $response->headers->get('Accept-Ranges'));
}

public function testAcceptRangeNotOverriden()
{
$request = Request::create('/', 'POST');
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif');
$response->headers->set('Accept-Ranges', 'foo');
$response->prepare($request);

$this->assertEquals('foo', $response->headers->get('Accept-Ranges'));
}

public function getSampleXAccelMappings()
{
return array(
Expand Down

0 comments on commit 24a287f

Please sign in to comment.