Skip to content

Commit 1cc6c36

Browse files
author
Benoît Burnichon
committed
Verify edge case where no last-modified header is available
1 parent 9171f8e commit 1cc6c36

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function prepare(Request $request)
217217
$this->maxlen = 0;
218218
} elseif ($request->headers->has('Range')) {
219219
// Process the range headers.
220-
if (!$request->headers->has('If-Range') || $this->getEtag() == $request->headers->get('If-Range') || $this->getLastModified()->format('D, d M Y H:i:s').' GMT' == $request->headers->get('If-Range')) {
220+
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {
221221
$range = $request->headers->get('Range');
222222
$fileSize = $this->file->getSize();
223223

@@ -250,6 +250,19 @@ public function prepare(Request $request)
250250
return $this;
251251
}
252252

253+
private function hasValidIfRangeHeader($header)
254+
{
255+
if ($this->getEtag() == $header) {
256+
return true;
257+
}
258+
259+
if (null === $lastModified = $this->getLastModified()) {
260+
return false;
261+
}
262+
263+
return $lastModified->format('D, d M Y H:i:s').' GMT' == $header;
264+
}
265+
253266
/**
254267
* Sends the file.
255268
*/

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ public function provideRanges()
122122
);
123123
}
124124

125+
public function testRangeRequestsWithoutLastModifiedDate()
126+
{
127+
// prevent auto last modified
128+
$response = BinaryFileResponse::create(__DIR__.'/File/Fixtures/test.gif', 200, array('Content-Type' => 'application/octet-stream'), true, null, false, false);
129+
130+
// prepare a request for a range of the testing file
131+
$request = Request::create('/');
132+
$request->headers->set('If-Range', date('D, d M Y H:i:s').' GMT');
133+
$request->headers->set('Range', 'bytes=1-4');
134+
135+
$this->expectOutputString(file_get_contents(__DIR__.'/File/Fixtures/test.gif'));
136+
$response = clone $response;
137+
$response->prepare($request);
138+
$response->sendContent();
139+
140+
$this->assertEquals(200, $response->getStatusCode());
141+
$this->assertNull($response->headers->get('Content-Range'));
142+
}
143+
125144
/**
126145
* @dataProvider provideFullFileRanges
127146
*/

0 commit comments

Comments
 (0)