[TASK] FE: Move Content-Length header to middleware 27/55627/6
authorBenni Mack <benni@typo3.org>
Fri, 9 Feb 2018 13:06:14 +0000 (14:06 +0100)
committerSusanne Moog <susanne.moog@typo3.org>
Mon, 12 Feb 2018 08:03:05 +0000 (09:03 +0100)
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>
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/frontend/Classes/Middleware/ContentLengthResponseHeader.php [new file with mode: 0644]
typo3/sysext/frontend/Configuration/RequestMiddlewares.php

index 3ffec3c..521ba2b 100644 (file)
@@ -23,7 +23,6 @@ use TYPO3\CMS\Core\Core\Bootstrap;
 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;
@@ -273,21 +272,8 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
         }
 
         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();
     }
diff --git a/typo3/sysext/frontend/Classes/Middleware/ContentLengthResponseHeader.php b/typo3/sysext/frontend/Classes/Middleware/ContentLengthResponseHeader.php
new file mode 100644 (file)
index 0000000..9463f6b
--- /dev/null
@@ -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;
+    }
+}
index a336f66..5d09c58 100644 (file)
@@ -26,5 +26,11 @@ return [
                 'typo3/cms-frontend/preprocessing'
             ]
         ],
+        'typo3/cms-frontend/content-length-headers' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\ContentLengthResponseHeader::class,
+            'after' => [
+                'typo3/cms-frontend/eid'
+            ]
+        ],
     ]
 ];