Bug #96294
open
PageContentErrorHandler ignores configured additionalHeaders
Added by Marc Willmann over 2 years ago.
Updated over 1 year ago.
Description
When using a page e.g. as 404 error page the in TypoScript configured additionalHeaders are ignored and not set. Most likely these headers are lost when fetching the internal error page via HTTP and deliver the result to the client.
This behaviour was detected in TYPO3 10LTS, but is the very same in 11LTS.
TS:
page.config.additionalHeaders.10.header = X-Powered-By: nothing
works fine on every page, except the error pages.
Interesting. Is this gone in v11 when you enable 'Subrequest page errors' feature toggle? This may not work out if extensions are not sub request ready (which will be enforced in v12), but it could be a start.
The source of trouble is in PageContentErrorHandler, which delegates the "content-type" header only.
See: https://github.com/TYPO3/typo3/blob/fc51ccbf2bb8a8c959aa74cbceca124971e6e7fd/typo3/sysext/core/Classes/Error/PageErrorHandler/PageContentErrorHandler.php#L122
Would propose to delegate all headers from sub-request response.
There is the temporary solution:
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace ......\PageErrorHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler;
/**
* Class PageNotFoundHandler, extends core functionality of {@link PageContentErrorHandler}.
* It reuses the headers of response on sub-request.
*
* NOTE: Can be removed if https://forge.typo3.org/issues/96294 is solved.
*/
class PageNotFoundHandler extends PageContentErrorHandler
{
/**
* @var ResponseInterface|null
*/
protected ?ResponseInterface $responseOfSubRequest = null;
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
{
$response = parent::handlePageError($request, $message, $reasons);
if ($this->responseOfSubRequest instanceof ResponseInterface) {
foreach ($this->responseOfSubRequest->getHeaders() as $headerName => $headerValue) {
$response = $response->withHeader($headerName, $headerValue);
}
}
return $response;
}
/**
* @inheritDoc
*/
protected function stashEnvironment(callable $fetcher): ResponseInterface
{
$this->responseOfSubRequest = parent::stashEnvironment($fetcher);
return $this->responseOfSubRequest;
}
}
Also available in: Atom
PDF