Skip to content

Commit

Permalink
Merge branch '4.4' into 5.3
Browse files Browse the repository at this point in the history
* 4.4:
  Fix composer.json versions
  Fix composer.json versions
  Remove redundant license info
  [HttpFoundation] Fix isNotModified determination logic
  Fix Url Validator false positives
  [Translation] Reverse fallback locales
  [FrameworkBundle] Fall back to default configuration in debug:config and consistently resolve parameter values
  allow null for framework.translator.default_path
  improve failure messages of the CrawlerSelectorTextContains constraint
  • Loading branch information
nicolas-grekas committed Aug 26, 2021
2 parents abeecbf + 69f0b8c commit 8180363
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ public function toArray()
*/
public function getETags()
{
return preg_split('/\s*,\s*/', $this->headers->get('if_none_match', ''), -1, \PREG_SPLIT_NO_EMPTY);
return preg_split('/\s*,\s*/', $this->headers->get('If-None-Match', ''), -1, \PREG_SPLIT_NO_EMPTY);
}

/**
Expand Down
25 changes: 20 additions & 5 deletions Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,12 +1090,27 @@ public function isNotModified(Request $request): bool
$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);
}
if ($ifNoneMatchEtags = $request->getETags()) {
$etag = $this->getEtag();
if (0 == strncmp($etag, 'W/', 2)) {
$etag = substr($etag, 2);
}

// Use weak comparison as per https://tools.ietf.org/html/rfc7232#section-3.2.
foreach ($ifNoneMatchEtags as $ifNoneMatchEtag) {
if (0 == strncmp($ifNoneMatchEtag, 'W/', 2)) {
$ifNoneMatchEtag = substr($ifNoneMatchEtag, 2);
}

if ($modifiedSince && $lastModified) {
$notModified = strtotime($modifiedSince) >= strtotime($lastModified) && (!$etags || $notModified);
if ($ifNoneMatchEtag === $etag || '*' === $ifNoneMatchEtag) {
$notModified = true;
break;
}
}
}
// Only do If-Modified-Since date comparison when If-None-Match is not present as per https://tools.ietf.org/html/rfc7232#section-3.3.
elseif ($modifiedSince && $lastModified) {
$notModified = strtotime($modifiedSince) >= strtotime($lastModified);
}

if ($notModified) {
Expand Down
40 changes: 36 additions & 4 deletions Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testIsNotModifiedEtag()
$etagTwo = 'randomly_generated_etag_2';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));
$request->headers->set('If-None-Match', sprintf('%s, %s, %s', $etagOne, $etagTwo, 'etagThree'));

$response = new Response();

Expand All @@ -209,6 +209,38 @@ public function testIsNotModifiedEtag()

$response->headers->set('ETag', '');
$this->assertFalse($response->isNotModified($request));

// Test wildcard
$request = new Request();
$request->headers->set('If-None-Match', '*');

$response->headers->set('ETag', $etagOne);
$this->assertTrue($response->isNotModified($request));
}

public function testIsNotModifiedWeakEtag()
{
$etag = 'randomly_generated_etag';
$weakEtag = 'W/randomly_generated_etag';

$request = new Request();
$request->headers->set('If-None-Match', $etag);
$response = new Response();

$response->headers->set('ETag', $etag);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', $weakEtag);
$this->assertTrue($response->isNotModified($request));

$request->headers->set('If-None-Match', $weakEtag);
$response = new Response();

$response->headers->set('ETag', $etag);
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', $weakEtag);
$this->assertTrue($response->isNotModified($request));
}

public function testIsNotModifiedLastModifiedAndEtag()
Expand All @@ -219,14 +251,14 @@ public function testIsNotModifiedLastModifiedAndEtag()
$etag = 'randomly_generated_etag';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$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));
$this->assertTrue($response->isNotModified($request));

$response->headers->set('ETag', 'non-existent-etag');
$response->headers->set('Last-Modified', $before);
Expand All @@ -243,7 +275,7 @@ public function testIsNotModifiedIfModifiedSinceAndEtagWithoutLastModified()
$etag = 'randomly_generated_etag';

$request = new Request();
$request->headers->set('if_none_match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-None-Match', sprintf('%s, %s', $etag, 'etagThree'));
$request->headers->set('If-Modified-Since', $modified);

$response = new Response();
Expand Down

0 comments on commit 8180363

Please sign in to comment.