bug #11079 Response::isNotModified returns true when If-Modified-Since is later than Last-Modified (skolodyazhnyy)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #11079).

Discussion
----------

Response::isNotModified returns true when If-Modified-Since is later than Last-Modified

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

Patch for #10501. I have reworked a bit `Response::isNotModified` method to make it more readable. Now it's comparing If-Modified-Since and Last-Modified as dates.

Commits
-------

42ec76e Response::isNotModified returns true when If-Modified-Since is later than Last-Modified
This commit is contained in:
Fabien Potencier 2014-09-23 12:19:54 +02:00
commit 3d11433950
2 changed files with 105 additions and 5 deletions

View File

@ -1033,12 +1033,16 @@ class Response
return false;
}
$lastModified = $request->headers->get('If-Modified-Since');
$notModified = false;
$notModified = false;
$lastModified = $this->headers->get('Last-Modified');
$modifiedSince = $request->headers->get('If-Modified-Since');
if ($etags = $request->getEtags()) {
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
} elseif ($lastModified) {
$notModified = $lastModified == $this->headers->get('Last-Modified');
$notModified = in_array($this->getEtag(), $etags) || in_array('*', $etags);
}
if ($modifiedSince && $lastModified) {
$notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified);
}
if ($notModified) {

View File

@ -118,6 +118,102 @@ class ResponseTest extends ResponseTestCase
$this->assertFalse($modified);
}
public function testIsNotModifiedNotSafe()
{
$request = Request::create('/homepage', 'POST');
$response = new Response();
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedLastModified()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';
$request = new Request();
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('Last-Modified', $before);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('Last-Modified', '');
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedEtag()
{
$etagOne = 'randomly_generated_etag';
$etagTwo = 'randomly_generated_etag_2';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
$response = new Response();
$response->headers->set('ETag', $etagOne);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', $etagTwo);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', '');
$this->assertFalse($response->isNotModified($request));
}
public function testIsNotModifiedLastModifiedAndEtag()
{
$before = 'Sun, 25 Aug 2013 18:32:31 GMT';
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$after = 'Sun, 25 Aug 2013 19:33:31 GMT';
$etag = 'randomly_generated_etag';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $after);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('ETag', 'non-existent-etag');
$response->headers->set('Last-Modified', $before);
$this->assertFalse($response->isNotModified($request));
$response->headers->set('ETag', $etag);
$response->headers->set('Last-Modified', $modified);
$this->assertTrue($response->isNotModified($request));
}
public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
{
$modified = 'Sun, 25 Aug 2013 18:33:31 GMT';
$etag = 'randomly_generated_etag';
$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);
$response = new Response();
$response->headers->set('ETag', $etag);
$this->assertTrue($response->isNotModified($request));
$response->headers->set('ETag', 'non-existent-etag');
$this->assertFalse($response->isNotModified($request));
}
public function testIsValidateable()
{
$response = new Response('', 200, array('Last-Modified' => $this->createDateTimeOneHourAgo()->format(DATE_RFC2822)));