Skip to content

Commit

Permalink
Correctly merge mix of public and private responses
Browse files Browse the repository at this point in the history
If a response is flagged as public and others are flagged private,
the merged response can be considered private-cacheable.
  • Loading branch information
aschempp committed Jun 20, 2018
1 parent b594310 commit b98e25a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Expand Up @@ -36,7 +36,7 @@ class ResponseCacheStrategy implements ResponseCacheStrategyInterface
/**
* Cache-Control headers that are sent to the final response if the appear in ALL of the responses.
*/
private static $inheritDirectives = array('public', 'private', 'immutable');
private static $inheritDirectives = array('immutable');

private $embeddedResponses = 0;
private $isNotCacheableResponseEmbedded = false;
Expand Down Expand Up @@ -76,6 +76,14 @@ public function add(Response $response)
}
}

if (false !== $this->flagDirectives['public']) {
$this->flagDirectives['public'] = $response->headers->hasCacheControlDirective('public');
}

if (false !== $this->flagDirectives['private']) {
$this->flagDirectives['private'] = $response->headers->hasCacheControlDirective('private') || $response->headers->hasCacheControlDirective('public');
}

$age = $response->getAge();
$this->age = max($this->age, $age);

Expand Down Expand Up @@ -124,7 +132,13 @@ public function update(Response $response)
return;
}

$response->headers->set('Cache-Control', implode(', ', array_keys(array_filter($this->flagDirectives))));
$flags = array_filter($this->flagDirectives);

if (isset($flags['public'], $flags['private'])) {
unset($flags['private']);
}

$response->headers->set('Cache-Control', implode(', ', array_keys($flags)));

if ($this->ageDirectives['max-age']) {
$response->headers->addCacheControlDirective('max-age', $this->ageDirectives['max-age'] + $this->age);
Expand Down
Expand Up @@ -112,8 +112,6 @@ public function testValidationOnMasterResponseIsNotPossibleWhenItContainsEmbedde
$this->assertFalse($masterResponse->isValidateable());
$this->assertFalse($masterResponse->headers->has('Last-Modified'));
$this->assertFalse($masterResponse->headers->has('ETag'));
// $this->assertTrue($masterResponse->headers->hasCacheControlDirective('no-cache'));
// $this->assertTrue($masterResponse->headers->hasCacheControlDirective('must-revalidate'));
}

public function testMasterResponseWithValidationIsUnchangedWhenThereIsNoEmbeddedResponse()
Expand Down

0 comments on commit b98e25a

Please sign in to comment.