You are here

class AddHTTPHeaders in HTTP Response Headers 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders
  2. 2.0.x src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders

Provides AddHTTPHeaders.

Hierarchy

  • class \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AddHTTPHeaders

1 string reference to 'AddHTTPHeaders'
http_response_headers.services.yml in ./http_response_headers.services.yml
http_response_headers.services.yml
1 service uses AddHTTPHeaders
http_response_headers in ./http_response_headers.services.yml
\Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders

File

src/EventSubscriber/AddHTTPHeaders.php, line 17
Contains \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders.

Namespace

Drupal\http_response_headers\EventSubscriber
View source
class AddHTTPHeaders implements EventSubscriberInterface {

  /**
   * Sets extra HTTP headers.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $response = $event
      ->getResponse();
    $config = \Drupal::config('http_response_headers.settings');

    // Security HTTP headers.
    $security = $config
      ->get('security');
    foreach ($security as $param => $value) {
      if (!empty($value)) {
        $response->headers
          ->set($param, $value);
      }
    }
    $authenticated_only = $config
      ->get('performance_authenticated_only');
    $current_user = \Drupal::currentUser();

    // Performance HTTP headers.
    if ($authenticated_only && !$current_user
      ->isAnonymous()) {
      $performance = $config
        ->get('performance');
      foreach ($performance as $param => $value) {
        if (!empty($value)) {
          $response->headers
            ->set($param, $value);
        }
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AddHTTPHeaders::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AddHTTPHeaders::onRespond public function Sets extra HTTP headers.