You are here

class RemoveHttpHeadersMiddleware in Remove HTTP headers 8

Executes removal of HTTP response headers.

Runs after the page caching middleware took over the request. Because it adds an additional HTTP header.

Hierarchy

  • class \Drupal\remove_http_headers\StackMiddleware\RemoveHttpHeadersMiddleware implements \Symfony\Component\HttpKernel\HttpKernelInterface

Expanded class hierarchy of RemoveHttpHeadersMiddleware

1 string reference to 'RemoveHttpHeadersMiddleware'
remove_http_headers.services.yml in ./remove_http_headers.services.yml
remove_http_headers.services.yml
1 service uses RemoveHttpHeadersMiddleware
http_middleware.remove_http_headers in ./remove_http_headers.services.yml
Drupal\remove_http_headers\StackMiddleware\RemoveHttpHeadersMiddleware

File

src/StackMiddleware/RemoveHttpHeadersMiddleware.php, line 16

Namespace

Drupal\remove_http_headers\StackMiddleware
View source
class RemoveHttpHeadersMiddleware implements HttpKernelInterface {

  /**
   * The wrapped HTTP kernel.
   *
   * @var \Symfony\Component\HttpKernel\HttpKernelInterface
   */
  protected $httpKernel;

  /**
   * The config manager service.
   *
   * @var \Drupal\remove_http_headers\Config\ConfigManager
   */
  protected $configManager;

  /**
   * Constructs a RemoveHttpHeadersMiddleware object.
   *
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $httpKernel
   *   The decorated kernel.
   * @param \Drupal\remove_http_headers\Config\ConfigManager $configManager
   *   The config manager service.
   */
  public function __construct(HttpKernelInterface $httpKernel, ConfigManager $configManager) {
    $this->httpKernel = $httpKernel;
    $this->configManager = $configManager;
  }

  /**
   * {@inheritdoc}
   */
  public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
    $response = $this->httpKernel
      ->handle($request, $type, $catch);

    // Only allow removal of HTTP headers on master request.
    if ($type === static::MASTER_REQUEST) {
      $response = $this
        ->removeConfiguredHttpHeaders($response);
    }
    return $response;
  }

  /**
   * Remove configured HTTP headers.
   *
   * @param \Symfony\Component\HttpFoundation\Response $response
   *   The response object.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The given response object
   *   without the HTTP response headers that should be removed.
   */
  protected function removeConfiguredHttpHeaders(Response $response) : Response {
    $headersToRemove = $this->configManager
      ->getHeadersToRemove();
    foreach ($headersToRemove as $httpHeaderToRemove) {
      $response->headers
        ->remove($httpHeaderToRemove);
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RemoveHttpHeadersMiddleware::$configManager protected property The config manager service.
RemoveHttpHeadersMiddleware::$httpKernel protected property The wrapped HTTP kernel.
RemoveHttpHeadersMiddleware::handle public function Handles a Request to convert it to a Response.
RemoveHttpHeadersMiddleware::removeConfiguredHttpHeaders protected function Remove configured HTTP headers.
RemoveHttpHeadersMiddleware::__construct public function Constructs a RemoveHttpHeadersMiddleware object.