Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c2b1ff

Browse files
committed
bug #23130 Keep s-maxage when expiry and validation are used in combination (mpdude)
This PR was merged into the 2.7 branch. Discussion ---------- Keep s-maxage when expiry and validation are used in combination | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | (Symfony) docs say that [expiration wins over validation](https://symfony.com/doc/current/http_cache/validation.html). So, a) when both the master and embedded response are public with an s-maxage, the result should be public as well and use the lower s-maxage of both, *also* in the case that the embedded response carries validation headers. (The cache may use those for revalidating the embedded response once it has become stale, but that does not impact expiration-based caching of the combined response.) b) when both the master and embedded response are public with an s-maxage, the result should be public as well and use the lower s-maxage of both, *also* in the case that the master response carries validation headers. However, those *must not* be passed on to the client: They do not apply to the combined response, but may only be used by the cache itself to revalidate the (raw) master response. Commits ------- 09bcbc7 Embedding a response that combines expiration and validation, that should not defeat expiration on the combined response
2 parents 551e5ba + 09bcbc7 commit 3c2b1ff

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Symfony/Component/HttpKernel/HttpCache/ResponseCacheStrategy.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
3939
*/
4040
public function add(Response $response)
4141
{
42-
if ($response->isValidateable() || !$response->isCacheable()) {
42+
if (!$response->isFresh() || !$response->isCacheable()) {
4343
$this->cacheable = false;
4444
} else {
4545
$maxAge = $response->getMaxAge();
@@ -70,6 +70,9 @@ public function update(Response $response)
7070
if ($response->isValidateable()) {
7171
$response->setEtag(null);
7272
$response->setLastModified(null);
73+
}
74+
75+
if (!$response->isFresh()) {
7376
$this->cacheable = false;
7477
}
7578

src/Symfony/Component/HttpKernel/Tests/HttpCache/ResponseCacheStrategyTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,45 @@ public function testEmbeddingPrivateResponseMakesMainResponsePrivate()
178178
// Not sure if we should pass "max-age: 60" in this case, as long as the response is private and
179179
// that's the more conservative of both the master and embedded response...?
180180
}
181+
182+
public function testResponseIsExiprableWhenEmbeddedResponseCombinesExpiryAndValidation()
183+
{
184+
/* When "expiration wins over validation" (https://symfony.com/doc/current/http_cache/validation.html)
185+
* and both the main and embedded response provide s-maxage, then the more restricting value of both
186+
* should be fine, regardless of whether the embedded response can be validated later on or must be
187+
* completely regenerated.
188+
*/
189+
$cacheStrategy = new ResponseCacheStrategy();
190+
191+
$masterResponse = new Response();
192+
$masterResponse->setSharedMaxAge(3600);
193+
194+
$embeddedResponse = new Response();
195+
$embeddedResponse->setSharedMaxAge(60);
196+
$embeddedResponse->setEtag('foo');
197+
198+
$cacheStrategy->add($embeddedResponse);
199+
$cacheStrategy->update($masterResponse);
200+
201+
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
202+
}
203+
204+
public function testResponseIsExpirableButNotValidateableWhenMasterResponseCombinesExpirationAndValidation()
205+
{
206+
$cacheStrategy = new ResponseCacheStrategy();
207+
208+
$masterResponse = new Response();
209+
$masterResponse->setSharedMaxAge(3600);
210+
$masterResponse->setEtag('foo');
211+
$masterResponse->setLastModified(new \DateTime());
212+
213+
$embeddedResponse = new Response();
214+
$embeddedResponse->setSharedMaxAge(60);
215+
216+
$cacheStrategy->add($embeddedResponse);
217+
$cacheStrategy->update($masterResponse);
218+
219+
$this->assertSame('60', $masterResponse->headers->getCacheControlDirective('s-maxage'));
220+
$this->assertFalse($masterResponse->isValidateable());
221+
}
181222
}

0 commit comments

Comments
 (0)