Skip to content

Commit 28d9f0c

Browse files
committed
also clean away the NO_AUTO_CACHE_CONTROL_HEADER if we have no session
1 parent f1795c0 commit 28d9f0c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,24 @@ public function onKernelResponse(FilterResponseEvent $event)
7171
return;
7272
}
7373

74+
$response = $event->getResponse();
75+
$autoCacheControl = !$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER);
76+
// Always remove the internal header if present
77+
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
78+
7479
if (!$session = $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
7580
return;
7681
}
7782

78-
$response = $event->getResponse();
79-
8083
if ($session instanceof Session ? $session->getUsageIndex() !== end($this->sessionUsageStack) : $session->isStarted()) {
81-
if (!$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER)) {
84+
if ($autoCacheControl) {
8285
$response
8386
->setPrivate()
8487
->setMaxAge(0)
8588
->headers->addCacheControlDirective('must-revalidate');
8689
}
8790
}
8891

89-
// Always remove the internal header if present
90-
$response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
91-
9292
if ($session->isStarted()) {
9393
/*
9494
* Saves the session, in case it is still open, before sending the response/headers.

src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,24 @@ public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
106106
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
107107
}
108108

109-
public function testUninitilizedSession()
109+
public function testUninitializedSession()
110110
{
111-
$event = $this->getMockBuilder(FilterResponseEvent::class)->disableOriginalConstructor()->getMock();
112-
$event->expects($this->once())->method('isMasterRequest')->willReturn(true);
111+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock();
112+
$response = new Response();
113+
$response->setSharedMaxAge(60);
114+
$response->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
113115

114116
$container = new ServiceLocator(array(
115117
'initialized_session' => function () {},
116118
));
117119

118120
$listener = new SessionListener($container);
119-
$listener->onKernelResponse($event);
121+
$listener->onKernelResponse(new FilterResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response));
122+
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
123+
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
124+
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
125+
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
126+
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
120127
}
121128

122129
public function testSurrogateMasterRequestIsPublic()

0 commit comments

Comments
 (0)