You are here

public function AddHTTPHeaders::onRespond 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::onRespond()
  2. 2.0.x src/EventSubscriber/AddHTTPHeaders.php \Drupal\http_response_headers\EventSubscriber\AddHTTPHeaders::onRespond()

Sets extra HTTP headers.

File

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

Class

AddHTTPHeaders
Provides AddHTTPHeaders.

Namespace

Drupal\http_response_headers\EventSubscriber

Code

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);
      }
    }
  }
}