Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2c9f7a

Browse files
committed
Fixed inconsistent cache headers depending on used session storage
1 parent 21dca38 commit d2c9f7a

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class NativeSessionStorage implements SessionStorageInterface
6060
* ("auto_start", is not supported as it tells PHP to start a session before
6161
* PHP starts to execute user-land code. Setting during runtime has no effect).
6262
*
63-
* cache_limiter, "" (use "0" to prevent headers from being sent entirely).
63+
* cache_limiter, "0"
6464
* cache_expire, "0"
6565
* cookie_domain, ""
6666
* cookie_httponly, ""
@@ -101,7 +101,7 @@ class NativeSessionStorage implements SessionStorageInterface
101101
public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
102102
{
103103
$options += array(
104-
'cache_limiter' => '',
104+
'cache_limiter' => 0,
105105
'cache_expire' => 0,
106106
'use_cookies' => 1,
107107
'lazy_write' => 1,

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/Fixtures/storage.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ $_SESSION is not empty
1515
Array
1616
(
1717
[0] => Content-Type: text/plain; charset=utf-8
18-
[1] => Cache-Control: max-age=0, private, must-revalidate
1918
)
2019
shutdown

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function testDefaultSessionCacheLimiter()
150150
$this->iniSet('session.cache_limiter', 'nocache');
151151

152152
$storage = new NativeSessionStorage();
153-
$this->assertEquals('', ini_get('session.cache_limiter'));
153+
$this->assertEquals('0', ini_get('session.cache_limiter'));
154154
}
155155

156156
public function testExplicitSessionCacheLimiter()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function onKernelResponse(FilterResponseEvent $event)
5353
$session = $event->getRequest()->getSession();
5454
if ($session && $session->isStarted()) {
5555
$session->save();
56+
57+
$event->getResponse()->setPrivate();
5658
}
5759
}
5860

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19+
use Symfony\Component\HttpKernel\EventListener\SaveSessionListener;
20+
use Symfony\Component\HttpKernel\HttpKernelInterface;
21+
22+
class SaveSessionListenerTest extends TestCase
23+
{
24+
public function testOnlyTriggeredOnMasterRequest()
25+
{
26+
$listener = new SaveSessionListener();
27+
$event = $this->getMockBuilder(FilterResponseEvent::class)->getMock();
28+
$event->expects($this->once())->method('isMasterRequest')->willReturn(false);
29+
$event->expects($this->never())->method('getRequest');
30+
31+
// sub request
32+
$listener->onKernelResponse($event);
33+
}
34+
35+
public function testSessionSavedAndResponsePrivate()
36+
{
37+
$listener = new SaveSessionListener();
38+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
39+
40+
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
41+
$session->expects($this->once())->method('isStarted')->willReturn(true);
42+
$session->expects($this->once())->method('save');
43+
44+
$request = new Request();
45+
$request->setSession($session);
46+
$response = new Response();
47+
$listener->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response));
48+
49+
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
50+
}
51+
}

0 commit comments

Comments
 (0)