Skip to content

[HttpFoundation] Add support for all core response http control directives #35748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* made the Mime component an optional dependency
* added `MarshallingSessionHandler`, `IdentityMarshaller`
* made `Session` accept a callback to report when the session is being used
* Add support for all core cache control directives

5.0.0
-----
Expand Down
36 changes: 30 additions & 6 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ class Response
const HTTP_NOT_EXTENDED = 510; // RFC2774
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585

/**
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
*/
private const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
'must_revalidate' => false,
'no_cache' => false,
'no_store' => false,
'no_transform' => false,
'public' => false,
'private' => false,
'proxy_revalidate' => false,
'max_age' => true,
's_maxage' => true,
'immutable' => false,
'last_modified' => true,
'etag' => true,
];

/**
* @var ResponseHeaderBag
*/
Expand Down Expand Up @@ -921,7 +939,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
/**
* Sets the response's cache headers (validation and/or expiration).
*
* Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable.
* Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
*
* @return $this
*
Expand All @@ -931,7 +949,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
*/
public function setCache(array $options): object
{
if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) {
if ($diff = array_diff(array_keys($options), array_keys(static::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES))) {
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
}

Expand All @@ -951,6 +969,16 @@ public function setCache(array $options): object
$this->setSharedMaxAge($options['s_maxage']);
}

foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
if (!$hasValue && isset($options[$directive])) {
if ($options[$directive]) {
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
} else {
$this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
}
}
}

if (isset($options['public'])) {
if ($options['public']) {
$this->setPublic();
Expand All @@ -967,10 +995,6 @@ public function setCache(array $options): object
}
}

if (isset($options['immutable'])) {
$this->setImmutable((bool) $options['immutable']);
}

return $this;
}

Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ public function testSetCache()

$response->setCache(['immutable' => false]);
$this->assertFalse($response->headers->hasCacheControlDirective('immutable'));

$directives = ['proxy_revalidate', 'must_revalidate', 'no_cache', 'no_store', 'no_transform'];
foreach ($directives as $directive) {
$response->setCache([$directive => true]);

$this->assertTrue($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
}

foreach ($directives as $directive) {
$response->setCache([$directive => false]);

$this->assertFalse($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
}
}

public function testSendContent()
Expand Down