4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16 use Symfony\Component\HttpKernel\KernelEvents;
17 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
18 use Symfony\Component\HttpKernel\UriSigner;
19 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22 * Handles content fragments represented by special URIs.
24 * All URL paths starting with /_fragment are handled as
25 * content fragments by this listener.
27 * If throws an AccessDeniedHttpException exception if the request
28 * is not signed or if it is not an internal sub-request.
30 * @author Fabien Potencier <fabien@symfony.com>
32 class FragmentListener implements EventSubscriberInterface
35 private $fragmentPath;
40 * @param UriSigner $signer A UriSigner instance
41 * @param string $fragmentPath The path that triggers this listener
43 public function __construct(UriSigner $signer, $fragmentPath = '/_fragment')
45 $this->signer = $signer;
46 $this->fragmentPath = $fragmentPath;
50 * Fixes request attributes when the path is '/_fragment'.
52 * @param GetResponseEvent $event A GetResponseEvent instance
54 * @throws AccessDeniedHttpException if the request does not come from a trusted IP.
56 public function onKernelRequest(GetResponseEvent $event)
58 $request = $event->getRequest();
60 if ($this->fragmentPath !== rawurldecode($request->getPathInfo())) {
64 if ($request->attributes->has('_controller')) {
65 // Is a sub-request: no need to parse _path but it should still be removed from query parameters as below.
66 $request->query->remove('_path');
71 if ($event->isMasterRequest()) {
72 $this->validateRequest($request);
75 parse_str($request->query->get('_path', ''), $attributes);
76 $request->attributes->add($attributes);
77 $request->attributes->set('_route_params', array_replace($request->attributes->get('_route_params', array()), $attributes));
78 $request->query->remove('_path');
81 protected function validateRequest(Request $request)
83 // is the Request safe?
84 if (!$request->isMethodSafe(false)) {
85 throw new AccessDeniedHttpException();
88 // is the Request signed?
89 // we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
90 if ($this->signer->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().(null !== ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : ''))) {
94 throw new AccessDeniedHttpException();
98 * @deprecated since version 2.3.19, to be removed in 3.0.
102 protected function getLocalIpAddresses()
104 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3.19 and will be removed in 3.0.', E_USER_DEPRECATED);
106 return array('127.0.0.1', 'fe80::1', '::1');
109 public static function getSubscribedEvents()
112 KernelEvents::REQUEST => array(array('onKernelRequest', 48)),