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.     private $map;
  23.     private $authenticationEntryPoint;
  24.     private $logger;
  25.     public function __construct(AccessMapInterface $mapAuthenticationEntryPointInterface $authenticationEntryPointLoggerInterface $logger null) {
  26.         $this->map $map;
  27.         $this->authenticationEntryPoint $authenticationEntryPoint;
  28.         $this->logger $logger;
  29.     }
  30.     /**
  31.      * Handles channel management.
  32.      */
  33.     public function handle(GetResponseEvent $event) {
  34.         $request $event->getRequest();
  35.         list(, $channel) = $this->map->getPatterns($request);
  36.         if ('https' === $channel && !$request->isSecure()) {
  37.             if (null !== $this->logger) {
  38.                 if ('https' === $request->headers->get('X-Forwarded-Proto')) {
  39.                     $this->logger->info('Redirecting to HTTPS. ("X-Forwarded-Proto" header is set to "https" - did you set "trusted_proxies" correctly?)');
  40.                 } elseif (false !== strpos($request->headers->get('Forwarded'), 'proto=https')) {
  41.                     $this->logger->info('Redirecting to HTTPS. ("Forwarded" header is set to "proto=https" - did you set "trusted_proxies" correctly?)');
  42.                 } else {
  43.                     $this->logger->info('Redirecting to HTTPS.');
  44.                 }
  45.             }
  46.             $response $this->authenticationEntryPoint->start($request);
  47.             $event->setResponse($response);
  48.             return;
  49.         }
  50.         if ('http' === $channel && $request->isSecure()) {
  51.             if (null !== $this->logger) {
  52.                 $this->logger->info('Redirecting to HTTP.');
  53.             }
  54.             $response $this->authenticationEntryPoint->start($request);
  55.             $event->setResponse($response);
  56.         }
  57.     }
  58. }