You are here

class CorsResponseEventSubscriber in CORS 8

Response Event Subscriber for adding CORS headers.

Hierarchy

Expanded class hierarchy of CorsResponseEventSubscriber

1 file declares its use of CorsResponseEventSubscriber
CorsResponseEventSubscriberTest.php in tests/src/Unit/EventSubscriber/CorsResponseEventSubscriberTest.php
Contains Drupal\Tests\cors\Unit\EventSubscriber\CorsResponseEventSubscriberTest.
1 string reference to 'CorsResponseEventSubscriber'
cors.services.yml in ./cors.services.yml
cors.services.yml
1 service uses CorsResponseEventSubscriber
cors_event_subscriber in ./cors.services.yml
Drupal\cors\EventSubscriber\CorsResponseEventSubscriber

File

src/EventSubscriber/CorsResponseEventSubscriber.php, line 20
Contains Drupal\cors\EventSubscriber\CorsResponseEventSubscriber

Namespace

Drupal\cors\EventSubscriber
View source
class CorsResponseEventSubscriber implements EventSubscriberInterface {

  /**
   * The CORS config.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The path alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * The path matcher.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  protected $pathMatcher;

  /**
   * Constructs a new CORS response event subscriber.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   *   The alias manager.
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   The path matcher.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, PathMatcherInterface $path_matcher) {
    $this->config = $config_factory
      ->get('cors.settings');
    $this->aliasManager = $alias_manager;
    $this->pathMatcher = $path_matcher;
  }

  /**
   * Adds CORS headers to the response.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The GET response event.
   */
  public function addCorsHeaders(FilterResponseEvent $event) {
    $domains = $this->config
      ->get('domains');
    $request = $event
      ->getRequest();
    $path_info = $request
      ->getPathInfo();
    $current_path = $this->aliasManager
      ->getPathByAlias($path_info);
    $request_headers = $request->headers
      ->all();
    $headers = array(
      'all' => array(
        'Access-Control-Allow-Origin' => array(),
        'Access-Control-Allow-Credentials' => array(),
      ),
      'OPTIONS' => array(
        'Access-Control-Allow-Methods' => array(),
        'Access-Control-Allow-Headers' => array(),
      ),
    );
    foreach ($domains as $path => $settings) {
      $settings = explode("|", $settings);
      $page_match = $this->pathMatcher
        ->matchPath($current_path, $path);
      if ($current_path != $path_info) {
        $page_match = $page_match || $this->pathMatcher
          ->matchPath($path_info, $path);
      }
      if ($page_match) {
        if (!empty($settings[0])) {
          $origins = explode(',', trim($settings[0]));
          foreach ($origins as $origin) {
            if ($origin === '<mirror>') {
              if (!empty($request_headers['Origin'])) {
                $headers['all']['Access-Control-Allow-Origin'][] = $request_headers['Origin'];
              }
            }
            else {
              $headers['all']['Access-Control-Allow-Origin'][] = $origin;
            }
          }
        }
        if (!empty($settings[1])) {
          $headers['OPTIONS']['Access-Control-Allow-Methods'] = explode(',', trim($settings[1]));
        }
        if (!empty($settings[2])) {
          $headers['OPTIONS']['Access-Control-Allow-Headers'] = explode(',', trim($settings[2]));
        }
        if (!empty($settings[3])) {
          $headers['all']['Access-Control-Allow-Credentials'] = explode(',', trim($settings[3]));
        }
      }
    }
    $response = $event
      ->getResponse();
    foreach ($headers as $method => $allowed) {
      $current_method = $request
        ->getMethod();
      if ($method === 'all' || $method === $current_method) {
        foreach ($allowed as $header => $values) {
          if (!empty($values)) {
            foreach ($values as $value) {
              $response->headers
                ->set($header, $value, TRUE);
            }
          }
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = array(
      'addCorsHeaders',
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CorsResponseEventSubscriber::$aliasManager protected property The path alias manager.
CorsResponseEventSubscriber::$config protected property The CORS config.
CorsResponseEventSubscriber::$pathMatcher protected property The path matcher.
CorsResponseEventSubscriber::addCorsHeaders public function Adds CORS headers to the response.
CorsResponseEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CorsResponseEventSubscriber::__construct public function Constructs a new CORS response event subscriber.