Skip to content

Commit

Permalink
bug #17602 [HttpFoundation] Fix BinaryFileResponse incorrect behavior…
Browse files Browse the repository at this point in the history
… with if-range header (bburnichon)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpFoundation] Fix BinaryFileResponse incorrect behavior with if-range header

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16540
| License       | MIT
| Doc PR        | -

Commits
-------

aaad5bd Add check on If-Range header
  • Loading branch information
fabpot committed Feb 26, 2016
2 parents 8ae5dc3 + aaad5bd commit be30748
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Expand Up @@ -220,7 +220,7 @@ public function prepare(Request $request)
$this->maxlen = 0;
} elseif ($request->headers->has('Range')) {
// Process the range headers.
if (!$request->headers->has('If-Range') || $this->getEtag() === $request->headers->get('If-Range')) {
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
$range = $request->headers->get('Range');
$fileSize = $this->file->getSize();

Expand Down Expand Up @@ -253,6 +253,19 @@ public function prepare(Request $request)
return $this;
}

private function hasValidIfRangeHeader($header)
{
if ($this->getEtag() === $header) {
return true;
}

if (null === $lastModified = $this->getLastModified()) {
return false;
}

return $lastModified->format('D, d M Y H:i:s').' GMT' === $header;
}

/**
* Sends the file.
*
Expand Down
Expand Up @@ -80,6 +80,37 @@ public function testRequests($requestRange, $offset, $length, $responseRange)
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
}

/**
* @dataProvider provideRanges
*/
public function testRequestsWithoutEtag($requestRange, $offset, $length, $responseRange)
{
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'));

// do a request to get the LastModified
$request = Request::create('/');
$response->prepare($request);
$lastModified = $response->headers->get('Last-Modified');

// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('If-Range', $lastModified);
$request->headers->set('Range', $requestRange);

$file = fopen(__DIR__.'/File/Fixtures/test.gif', 'r');
fseek($file, $offset);
$data = fread($file, $length);
fclose($file);

$this->expectOutputString($data);
$response = clone $response;
$response->prepare($request);
$response->sendContent();

$this->assertEquals(206, $response->getStatusCode());
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
}

public function provideRanges()
{
return array(
Expand All @@ -91,6 +122,25 @@ public function provideRanges()
);
}

public function testRangeRequestsWithoutLastModifiedDate()
{
// prevent auto last modified
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);

// prepare a request for a range of the testing file
$request = Request::create('/');
$request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
$request->headers->set('Range', 'bytes=1-4');

$this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
$response = clone $response;
$response->prepare($request);
$response->sendContent();

$this->assertEquals(200, $response->getStatusCode());
$this->assertNull($response->headers->get('Content-Range'));
}

/**
* @dataProvider provideFullFileRanges
*/
Expand Down

0 comments on commit be30748

Please sign in to comment.