Skip to content

[HttpKernel] Add skip_response_headers to the HttpCache options #49318

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
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CHANGELOG
* Add `#[WithHttpStatus]` for defining status codes for exceptions
* Use an instance of `Psr\Clock\ClockInterface` to generate the current date time in `DateTimeValueResolver`
* Add `#[WithLogLevel]` for defining log levels for exceptions
* Allow extending the `Autowire` attribute
* Add `skip_response_headers` to the `HttpCache` options

6.2
---
Expand Down
19 changes: 18 additions & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class HttpCache implements HttpKernelInterface, TerminableInterface
* on responses that don't explicitly state whether the response is
* public or private via a Cache-Control directive. (default: Authorization and Cookie)
*
* * skip_response_headers Set of response headers that are never cached even if a response is cacheable (public).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think skipped_response_headers might be a better name for the option

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep it as is, it also makes sense, is shorter and prevents reindenting all the docblock :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docblock was the reason for me to keep it as short as it is now 🙈

* (default: Set-Cookie)
*
* * allow_reload Specifies whether the client can force a cache reload by including a
* Cache-Control "no-cache" directive in the request. Set it to ``true``
* for compliance with RFC 2616. (default: false)
Expand Down Expand Up @@ -97,6 +100,7 @@ public function __construct(HttpKernelInterface $kernel, StoreInterface $store,
'debug' => false,
'default_ttl' => 0,
'private_headers' => ['Authorization', 'Cookie'],
'skip_response_headers' => ['Set-Cookie'],
'allow_reload' => false,
'allow_revalidate' => false,
'stale_while_revalidate' => 2,
Expand Down Expand Up @@ -596,8 +600,17 @@ protected function lock(Request $request, Response $entry): bool
protected function store(Request $request, Response $response)
{
try {
$this->store->write($request, $response);
$restoreHeaders = [];
foreach ($this->options['skip_response_headers'] as $header) {
if (!$response->headers->has($header)) {
continue;
}

$restoreHeaders[$header] = $response->headers->all($header);
$response->headers->remove($header);
}

$this->store->write($request, $response);
$this->record($request, 'store');

$response->headers->set('Age', $response->getAge());
Expand All @@ -607,6 +620,10 @@ protected function store(Request $request, Response $response)
if ($this->options['debug']) {
throw $e;
}
} finally {
foreach ($restoreHeaders as $header => $values) {
$response->headers->set($header, $values);
}
}

// now that the response is cached, release the lock
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,43 @@ public function testResponsesThatMustNotBeUsedStaleIfError($responseHeaders, $sl
$this->assertEquals(500, $this->response->getStatusCode());
}

public function testSkipsConfiguredResponseHeadersForStore()
{
$storeMock = $this->createMock(StoreInterface::class);
$storeMock
->expects($this->once())
->method('write')
->with(
$this->isInstanceOf(Request::class),
$this->callback(function (Response $response) {
$this->assertFalse($response->headers->has('Set-Cookie'));
$this->assertFalse($response->headers->has('Another-One-To-Skip'));
$this->assertTrue($response->headers->has('Cache-Control'));
$this->assertTrue($response->headers->has('Another-One-To-Keep'));

return true;
})
);

$this->setNextResponse(200, [
'Cache-Control' => 'public, s-maxage=20',
'Set-Cookie' => 'foobar=value; path=/',
'Another-One-To-Skip' => 'foobar',
'Another-One-To-Keep' => 'foobar',
]);

$httpCache = new HttpCache($this->kernel, $storeMock, null, [
'skip_response_headers' => ['Set-Cookie', 'Another-One-To-Skip', 'I-do-Not-Exist'],
]);

$response = $httpCache->handle(Request::create('/'));

$this->assertSame('foobar=value; path=/', $response->headers->get('Set-Cookie'));
$this->assertSame('foobar', $response->headers->get('Another-One-To-Skip'));
$this->assertSame('foobar', $response->headers->get('Another-One-To-Keep'));
$this->assertFalse($response->headers->has('I-do-Not-Exist'));
}

public static function getResponseDataThatMustNotBeServedStaleIfError()
{
// All data sets assume that a 10s stale-if-error grace period has been configured
Expand Down