annotate core/lib/Drupal/Core/EventSubscriber/Fast404ExceptionHtmlSubscriber.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Symfony\Component\HttpFoundation\Response;
Chris@0 8 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 9 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * High-performance 404 exception subscriber.
Chris@0 13 *
Chris@0 14 * This subscriber will return a minimalist 404 response for HTML requests
Chris@0 15 * without running a full page theming operation.
Chris@0 16 */
Chris@0 17 class Fast404ExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The HTTP kernel.
Chris@0 21 *
Chris@0 22 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
Chris@0 23 */
Chris@0 24 protected $httpKernel;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The config factory.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 30 */
Chris@0 31 protected $configFactory;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructs a new Fast404ExceptionHtmlSubscriber.
Chris@0 35 *
Chris@0 36 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 37 * The configuration factory.
Chris@0 38 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
Chris@0 39 * The HTTP Kernel service.
Chris@0 40 */
Chris@0 41 public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel) {
Chris@0 42 $this->configFactory = $config_factory;
Chris@0 43 $this->httpKernel = $http_kernel;
Chris@0 44 }
Chris@0 45
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 */
Chris@0 50 protected static function getPriority() {
Chris@0 51 // A very high priority so that it can take precedent over anything else,
Chris@0 52 // and thus be fast.
Chris@0 53 return 200;
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * {@inheritdoc}
Chris@0 58 */
Chris@0 59 protected function getHandledFormats() {
Chris@0 60 return ['html'];
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@0 64 * Handles a 404 error for HTML.
Chris@0 65 *
Chris@0 66 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 67 * The event to process.
Chris@0 68 */
Chris@0 69 public function on404(GetResponseForExceptionEvent $event) {
Chris@0 70 $request = $event->getRequest();
Chris@0 71
Chris@0 72 $config = $this->configFactory->get('system.performance');
Chris@0 73 $exclude_paths = $config->get('fast_404.exclude_paths');
Chris@0 74 if ($config->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request->getPathInfo())) {
Chris@0 75 $fast_paths = $config->get('fast_404.paths');
Chris@0 76 if ($fast_paths && preg_match($fast_paths, $request->getPathInfo())) {
Chris@0 77 $fast_404_html = strtr($config->get('fast_404.html'), ['@path' => Html::escape($request->getUri())]);
Chris@0 78 $response = new Response($fast_404_html, Response::HTTP_NOT_FOUND);
Chris@0 79 $event->setResponse($response);
Chris@0 80 }
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 }