bug #28960 also clean away the NO_AUTO_CACHE_CONTROL_HEADER if we have no session (dbu)

This PR was merged into the 4.1 branch.

Discussion
----------

also clean away the NO_AUTO_CACHE_CONTROL_HEADER if we have no session

| Q             | A
| ------------- | ---
| Branch?       | 4.1 (feature added in this branch)
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This header is not supposed to ever be exposed to clients of the application, but a hack to transfer information within the symfony application. This bugfix makes it so that we also remove the header when no session is started and thus no decision has to be taken whether to overwrite the caching instructions or not.

Commits
-------

28d9f0c84c also clean away the NO_AUTO_CACHE_CONTROL_HEADER if we have no session
This commit is contained in:
Fabien Potencier 2018-10-24 05:36:46 +02:00
commit 3dda3cc5b3
2 changed files with 17 additions and 10 deletions

View File

@ -71,14 +71,17 @@ abstract class AbstractSessionListener implements EventSubscriberInterface
return;
}
$response = $event->getResponse();
$autoCacheControl = !$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER);
// Always remove the internal header if present
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
return;
}
$response = $event->getResponse();
if ($session instanceof Session ? $session->getUsageIndex() !== end($this->sessionUsageStack) : $session->isStarted()) {
if (!$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER)) {
if ($autoCacheControl) {
$response
->setPrivate()
->setMaxAge(0)
@ -86,9 +89,6 @@ abstract class AbstractSessionListener implements EventSubscriberInterface
}
}
// Always remove the internal header if present
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
if ($session->isStarted()) {
/*
* Saves the session, in case it is still open, before sending the response/headers.

View File

@ -106,17 +106,24 @@ class SessionListenerTest extends TestCase
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
}
public function testUninitilizedSession()
public function testUninitializedSession()
{
$event = $this->getMockBuilder(FilterResponseEvent::class)->disableOriginalConstructor()->getMock();
$event->expects($this->once())->method('isMasterRequest')->willReturn(true);
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock();
$response = new Response();
$response->setSharedMaxAge(60);
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
$container = new ServiceLocator(array(
'initialized_session' => function () {},
));
$listener = new SessionListener($container);
$listener->onKernelResponse($event);
$listener->onKernelResponse(new FilterResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response));
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
}
public function testSurrogateMasterRequestIsPublic()