Add check on If-Range header
Also verify edge case where no last-modified header is available
This commit is contained in:
parent
d8ad5a2cd4
commit
aaad5bd3d1
@ -220,7 +220,7 @@ class BinaryFileResponse extends Response
|
|||||||
$this->maxlen = 0;
|
$this->maxlen = 0;
|
||||||
} elseif ($request->headers->has('Range')) {
|
} elseif ($request->headers->has('Range')) {
|
||||||
// Process the range headers.
|
// 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');
|
$range = $request->headers->get('Range');
|
||||||
$fileSize = $this->file->getSize();
|
$fileSize = $this->file->getSize();
|
||||||
|
|
||||||
@ -253,6 +253,19 @@ class BinaryFileResponse extends Response
|
|||||||
return $this;
|
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.
|
* Sends the file.
|
||||||
*
|
*
|
||||||
|
@ -80,6 +80,37 @@ class BinaryFileResponseTest extends ResponseTestCase
|
|||||||
$this->assertEquals($responseRange, $response->headers->get('Content-Range'));
|
$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()
|
public function provideRanges()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
@ -91,6 +122,25 @@ class BinaryFileResponseTest extends ResponseTestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
* @dataProvider provideFullFileRanges
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user