Skip to content

Commit

Permalink
[TASK] FE: Move Content-Length header to middleware
Browse files Browse the repository at this point in the history
Resolves: #83828
Releases: master
Change-Id: Id5f85a5ae38260cc1a1bf730b365a9b1c425bc7e
Reviewed-on: https://review.typo3.org/55627
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Benjamin Franzke <bfr@qbus.de>
Tested-by: Benjamin Franzke <bfr@qbus.de>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
  • Loading branch information
bmack authored and susannemoog committed Feb 12, 2018
1 parent 437015c commit f6a34fe
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
14 changes: 0 additions & 14 deletions typo3/sysext/frontend/Classes/Http/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Core\Http\RequestHandlerInterface;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
Expand Down Expand Up @@ -273,21 +272,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

if ($sendTSFEContent) {
// Send content-length header.
// Notice that all HTML content outside the length of the content-length header will be cut off!
// Therefore content of unknown length from included PHP-scripts and if admin users are logged
// in (admin panel might show...) or if debug mode is turned on, we disable it!
if (
(!isset($this->controller->config['config']['enableContentLengthHeader']) || $this->controller->config['config']['enableContentLengthHeader'])
&& !$this->controller->isBackendUserLoggedIn() && !$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
&& !$this->controller->config['config']['debug'] && !$this->controller->doWorkspacePreview()
) {
header('Content-Length: ' . strlen($this->controller->content));
}
$response->getBody()->write($this->controller->content);
}
GeneralUtility::makeInstance(LogManager::class)
->getLogger(get_class())->debug('END of FRONTEND session', ['_FLUSH' => true]);

return $response ?: new NullResponse();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Frontend\Middleware;

/*
* 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!
*/

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\NullResponse;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Send content-length header.
* Notice that all HTML content outside the length of the content-length header will be cut off!
* Therefore content of unknown length from included PHP-scripts and if admin users are logged
* in (admin panel might show...) or if debug mode is turned on, we disable it!
*
* @internal
*/
class ContentLengthResponseHeader implements MiddlewareInterface
{

/**
* Adds the content length
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (
!($response instanceof NullResponse)
&& $GLOBALS['TSFE'] instanceof TypoScriptFrontendController
&& $GLOBALS['TSFE']->isOutputting()) {
if (
(!isset($GLOBALS['TSFE']->config['config']['enableContentLengthHeader']) || $GLOBALS['TSFE']->config['config']['enableContentLengthHeader'])
&& !$GLOBALS['TSFE']->isBackendUserLoggedIn() && !$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
&& !$GLOBALS['TSFE']->config['config']['debug'] && !$GLOBALS['TSFE']->doWorkspacePreview()
) {
$response = $response->withHeader('Content-Length', (string)$response->getBody()->getSize());
}
}
return $response;
}
}
6 changes: 6 additions & 0 deletions typo3/sysext/frontend/Configuration/RequestMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@
'typo3/cms-frontend/preprocessing'
]
],
'typo3/cms-frontend/content-length-headers' => [
'target' => \TYPO3\CMS\Frontend\Middleware\ContentLengthResponseHeader::class,
'after' => [
'typo3/cms-frontend/eid'
]
],
]
];

0 comments on commit f6a34fe

Please sign in to comment.