Skip to content

Commit 5f3f951

Browse files
committed
Correctly merge mix of public and private responses
If a response is flagged as public and others are flagged private, the merged response can be considered private-cacheable.
1 parent 480f56b commit 5f3f951

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
3636
/**
3737
* Cache-Control headers that are sent to the final response if the appear in ALL of the responses.
3838
*/
39-
private static $inheritDirectives = array('public', 'private', 'immutable');
39+
private static $inheritDirectives = array('immutable');
4040

4141
private $embeddedResponses = 0;
4242
private $isNotCacheableResponseEmbedded = false;
@@ -76,6 +76,14 @@ public function add(Response $response)
7676
}
7777
}
7878

79+
if (false !== $this->flagDirectives['public']) {
80+
$this->flagDirectives['public'] = $response->headers->hasCacheControlDirective('public');
81+
}
82+
83+
if (false !== $this->flagDirectives['private']) {
84+
$this->flagDirectives['private'] = $response->headers->hasCacheControlDirective('private') || $response->headers->hasCacheControlDirective('public');
85+
}
86+
7987
$age = $response->getAge();
8088
$this->age = max($this->age, $age);
8189

@@ -124,7 +132,13 @@ public function update(Response $response)
124132
return;
125133
}
126134

127-
$response->headers->set('Cache-Control', implode(', ', array_keys(array_filter($this->flagDirectives))));
135+
$flags = array_filter($this->flagDirectives);
136+
137+
if (isset($flags['public'], $flags['private'])) {
138+
unset($flags['private']);
139+
}
140+
141+
$response->headers->set('Cache-Control', implode(', ', array_keys($flags)));
128142

129143
if ($this->ageDirectives['max-age']) {
130144
$response->headers->addCacheControlDirective('max-age', $this->ageDirectives['max-age'] + $this->age);

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ public function testValidationOnMasterResponseIsNotPossibleWhenItContainsEmbedde
112112
$this->assertFalse($masterResponse->isValidateable());
113113
$this->assertFalse($masterResponse->headers->has('Last-Modified'));
114114
$this->assertFalse($masterResponse->headers->has('ETag'));
115-
// $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
116-
// $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
117115
}
118116

119117
public function testMasterResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()

0 commit comments

Comments
 (0)