vendor/symfony/security-http/Firewall/ChannelListener.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Firewall;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  13. use Symfony\Component\Security\Http\AccessMapInterface;
  14. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  15. /**
  16.  * ChannelListener switches the HTTP protocol based on the access control
  17.  * configuration.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. class ChannelListener implements ListenerInterface
  22. {
  23.     private $map;
  24.     private $authenticationEntryPoint;
  25.     private $logger;
  26.     public function __construct(AccessMapInterface $mapAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null)
  27.     {
  28.         $this->map $map;
  29.         $this->authenticationEntryPoint $authenticationEntryPoint;
  30.         $this->logger $logger;
  31.     }
  32.     /**
  33.      * Handles channel management.
  34.      */
  35.     public function handle(GetResponseEvent $event)
  36.     {
  37.         $request $event->getRequest();
  38.         list(, $channel) = $this->map->getPatterns($request);
  39.         if ('https' === $channel && !$request->isSecure()) {
  40.             if (null !== $this->logger) {
  41.                 if ('https' === $request->headers->get('X-Forwarded-Proto')) {
  42.                     $this->logger->info('Redirecting to HTTPS. ("X-Forwarded-Proto" header is set to "https" - did you set "trusted_proxies" correctly?)');
  43.                 } elseif (false !== strpos($request->headers->get('Forwarded'), 'proto=https')) {
  44.                     $this->logger->info('Redirecting to HTTPS. ("Forwarded" header is set to "proto=https" - did you set "trusted_proxies" correctly?)');
  45.                 } else {
  46.                     $this->logger->info('Redirecting to HTTPS.');
  47.                 }
  48.             }
  49.             $response $this->authenticationEntryPoint->start($request);
  50.             $event->setResponse($response);
  51.             return;
  52.         }
  53.         if ('http' === $channel && $request->isSecure()) {
  54.             if (null !== $this->logger) {
  55.                 $this->logger->info('Redirecting to HTTP.');
  56.             }
  57.             $response $this->authenticationEntryPoint->start($request);
  58.             $event->setResponse($response);
  59.         }
  60.     }
  61. }