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'
1 service uses RemoveHttpHeadersMiddleware
File
- src/
StackMiddleware/ RemoveHttpHeadersMiddleware.php, line 16
Namespace
Drupal\remove_http_headers\StackMiddlewareView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RemoveHttpHeadersMiddleware:: |
protected | property | The config manager service. | |
RemoveHttpHeadersMiddleware:: |
protected | property | The wrapped HTTP kernel. | |
RemoveHttpHeadersMiddleware:: |
public | function | Handles a Request to convert it to a Response. | |
RemoveHttpHeadersMiddleware:: |
protected | function | Remove configured HTTP headers. | |
RemoveHttpHeadersMiddleware:: |
public | function | Constructs a RemoveHttpHeadersMiddleware object. |