Skip to content

Commit 5f05774

Browse files
committed
bug symfony#25719 [HttpKernel] Uses cookies to track the requests redirection (sroze)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Uses cookies to track the requests redirection | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#25698 | License | MIT | Doc PR | ø In order to track the redirections across requests, we need to have some state. So far, we've been using the session but some users have complained about it (symfony#24774, symfony#24730). The idea is that we don't actually need the session, we can use cookies. It's a tradeoff: using a cookie would mean that both the redirection and the target page will not be cachable (because of the Set-Cookie to set the sf_redirect and the one to clear it). As it's only on dev, it seems fair to say that having no cache (because of `Set-Cookie`s) is a better side effect than starting the session. Commits ------- 83f2579 Uses cookies to track the requests redirection
2 parents f95ac4f + 83f2579 commit 5f05774

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\DataCollector;
1313

14+
use Symfony\Component\HttpFoundation\Cookie;
1415
use Symfony\Component\HttpFoundation\ParameterBag;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
@@ -128,21 +129,24 @@ public function collect(Request $request, Response $response, \Exception $except
128129
unset($this->controllers[$request]);
129130
}
130131

131-
if (null !== $session) {
132-
if ($request->attributes->has('_redirected')) {
133-
$this->data['redirect'] = $session->remove('sf_redirect');
134-
}
132+
if ($request->attributes->has('_redirected') && $redirectCookie = $request->cookies->get('sf_redirect')) {
133+
$this->data['redirect'] = json_decode($redirectCookie, true);
134+
135+
$response->headers->clearCookie('sf_redirect');
136+
}
135137

136-
if ($response->isRedirect()) {
137-
$session->set('sf_redirect', array(
138+
if ($response->isRedirect()) {
139+
$response->headers->setCookie(new Cookie(
140+
'sf_redirect',
141+
json_encode(array(
138142
'token' => $response->headers->get('x-debug-token'),
139143
'route' => $request->attributes->get('_route', 'n/a'),
140144
'method' => $request->getMethod(),
141145
'controller' => $this->parseController($request->attributes->get('_controller')),
142146
'status_code' => $statusCode,
143147
'status_text' => Response::$statusTexts[(int) $statusCode],
144-
));
145-
}
148+
))
149+
));
146150
}
147151

148152
$this->data['identifier'] = $this->data['route'] ?: (is_array($this->data['controller']) ? $this->data['controller']['class'].'::'.$this->data['controller']['method'].'()' : $this->data['controller']);
@@ -312,11 +316,11 @@ public function onKernelController(FilterControllerEvent $event)
312316

313317
public function onKernelResponse(FilterResponseEvent $event)
314318
{
315-
if (!$event->isMasterRequest() || !$event->getRequest()->hasSession()) {
319+
if (!$event->isMasterRequest()) {
316320
return;
317321
}
318322

319-
if ($event->getRequest()->getSession()->has('sf_redirect')) {
323+
if ($event->getRequest()->cookies->has('sf_redirect')) {
320324
$event->getRequest()->attributes->set('_redirected', true);
321325
}
322326
}

0 commit comments

Comments
 (0)