You are here

public function RedirectRequestSubscriber::onKernelRequestCheckRedirect in Redirect 8

Handles the redirect if any found.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseEvent $event: The event to process.

File

src/EventSubscriber/RedirectRequestSubscriber.php, line 111

Class

RedirectRequestSubscriber
Redirect subscriber for controller requests.

Namespace

Drupal\redirect\EventSubscriber

Code

public function onKernelRequestCheckRedirect(GetResponseEvent $event) {

  // Get a clone of the request. During inbound processing the request
  // can be altered. Allowing this here can lead to unexpected behavior.
  // For example the path_processor.files inbound processor provided by
  // the system module alters both the path and the request; only the
  // changes to the request will be propagated, while the change to the
  // path will be lost.
  $request = clone $event
    ->getRequest();
  if (!$this->checker
    ->canRedirect($request)) {
    return;
  }

  // Get URL info and process it to be used for hash generation.
  parse_str($request
    ->getQueryString(), $request_query);
  if (strpos($request
    ->getPathInfo(), '/system/files/') === 0 && !$request->query
    ->has('file')) {

    // Private files paths are split by the inbound path processor and the
    // relative file path is moved to the 'file' query string parameter. This
    // is because the route system does not allow an arbitrary amount of
    // parameters. We preserve the path as is returned by the request object.
    // @see \Drupal\system\PathProcessor\PathProcessorFiles::processInbound()
    $path = $request
      ->getPathInfo();
  }
  else {

    // Do the inbound processing so that for example language prefixes are
    // removed.
    $path = $this->pathProcessor
      ->processInbound($request
      ->getPathInfo(), $request);
  }
  $path = trim($path, '/');
  $this->context
    ->fromRequest($request);
  try {
    $redirect = $this->redirectRepository
      ->findMatchingRedirect($path, $request_query, $this->languageManager
      ->getCurrentLanguage()
      ->getId());
  } catch (RedirectLoopException $e) {
    \Drupal::logger('redirect')
      ->warning('Redirect loop identified at %path for redirect %rid', [
      '%path' => $e
        ->getPath(),
      '%rid' => $e
        ->getRedirectId(),
    ]);
    $response = new Response();
    $response
      ->setStatusCode(503);
    $response
      ->setContent('Service unavailable');
    $event
      ->setResponse($response);
    return;
  }
  if (!empty($redirect)) {

    // Handle internal path.
    $url = $redirect
      ->getRedirectUrl();
    if ($this->config
      ->get('passthrough_querystring')) {
      $url
        ->setOption('query', (array) $url
        ->getOption('query') + $request_query);
    }
    $headers = [
      'X-Redirect-ID' => $redirect
        ->id(),
    ];
    $response = new TrustedRedirectResponse($url
      ->setAbsolute()
      ->toString(), $redirect
      ->getStatusCode(), $headers);
    $response
      ->addCacheableDependency($redirect);
    $event
      ->setResponse($response);
  }
}