From 741733774563559c0e96fcaf9f3c755763ffeb83 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Fri, 27 Mar 2020 12:41:45 +0100
Subject: [PATCH] [TASK] Streamline TSFE internal code

Some minor details are changed within TSFE.

- metaCharset is now evaluated at the same time as other TypoScript options making this option available earlier in the process
- getFromCache() and headerNoCache() now optionally receive a PSR-7 request
to evaluate serverParams from the PSR-7 request and not the superglobals

Resolves: #90853
Releases: master
Change-Id: I443925c5d056db10cafb4e81aa30fc7db91c3c71
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63948
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
---
 .../TypoScriptFrontendController.php          | 34 +++++++++++--------
 .../PrepareTypoScriptFrontendRendering.php    |  2 +-
 2 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 2868d40d2f25..486ac96f4471 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -1803,10 +1803,10 @@ class TypoScriptFrontendController implements LoggerAwareInterface
      * See if page is in cache and get it if so
      * Stores the page content in $this->content if something is found.
      *
-     * @throws \InvalidArgumentException
-     * @throws \RuntimeException
+     * @param ServerRequestInterface|null $request if given this is used to determine values in headerNoCache() instead of the superglobal $_SERVER
+     * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
      */
-    public function getFromCache()
+    public function getFromCache(ServerRequestInterface $request = null)
     {
         // clearing the content-variable, which will hold the pagecontent
         $this->content = '';
@@ -1854,7 +1854,7 @@ class TypoScriptFrontendController implements LoggerAwareInterface
 
         // Look for page in cache only if a shift-reload is not sent to the server.
         $lockHash = $this->getLockHash();
-        if (!$this->headerNoCache() && $this->all) {
+        if (!$this->headerNoCache($request) && $this->all) {
             // we got page section information (TypoScript), so lets see if there is also a cached version
             // of this page in the pages cache.
             $this->newHash = $this->getHash();
@@ -1949,13 +1949,19 @@ class TypoScriptFrontendController implements LoggerAwareInterface
      * Will not be called if re-generation of page happens by other reasons (for instance that the page is not in cache yet!)
      * Also, a backend user MUST be logged in for the shift-reload to be detected due to DoS-attack-security reasons.
      *
+     * @param ServerRequestInterface|null $request
      * @return bool If shift-reload in client browser has been clicked, disable getting cached page (and regenerate it).
      */
-    public function headerNoCache()
+    public function headerNoCache(ServerRequestInterface $request = null)
     {
+        if ($request instanceof ServerRequestInterface) {
+            $serverParams = $request->getServerParams();
+        } else {
+            $serverParams = $_SERVER;
+        }
         $disableAcquireCacheData = false;
         if ($this->isBackendUserLoggedIn()) {
-            if (strtolower($_SERVER['HTTP_CACHE_CONTROL']) === 'no-cache' || strtolower($_SERVER['HTTP_PRAGMA']) === 'no-cache') {
+            if (strtolower($serverParams['HTTP_CACHE_CONTROL']) === 'no-cache' || strtolower($serverParams['HTTP_PRAGMA']) === 'no-cache') {
                 $disableAcquireCacheData = true;
             }
         }
@@ -2063,10 +2069,11 @@ class TypoScriptFrontendController implements LoggerAwareInterface
             if ($this->tmpl->loaded) {
                 $timeTracker->push('Setting the config-array');
                 // toplevel - objArrayName
-                $this->sPre = $this->tmpl->setup['types.'][$this->type];
-                $this->pSetup = $this->tmpl->setup[$this->sPre . '.'];
+                $typoScriptPageTypeName = $this->tmpl->setup['types.'][$this->type];
+                $this->sPre = $typoScriptPageTypeName;
+                $this->pSetup = $this->tmpl->setup[$typoScriptPageTypeName . '.'];
                 if (!is_array($this->pSetup)) {
-                    $message = 'The page is not configured! [type=' . $this->type . '][' . $this->sPre . '].';
+                    $message = 'The page is not configured! [type=' . $this->type . '][' . $typoScriptPageTypeName . '].';
                     $this->logger->alert($message);
                     try {
                         $response = GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
@@ -2103,6 +2110,10 @@ class TypoScriptFrontendController implements LoggerAwareInterface
                     if (!isset($this->config['config']['compressJs'])) {
                         $this->config['config']['compressJs'] = 0;
                     }
+                    // Rendering charset of HTML page.
+                    if (isset($this->config['config']['metaCharset']) && $this->config['config']['metaCharset'] !== 'utf-8') {
+                        $this->metaCharset = $this->config['config']['metaCharset'];
+                    }
                     // Setting default cache_timeout
                     if (isset($this->config['config']['cache_period'])) {
                         $this->set_cache_timeout_default((int)$this->config['config']['cache_period']);
@@ -2174,11 +2185,6 @@ class TypoScriptFrontendController implements LoggerAwareInterface
             GeneralUtility::callUserFunction($_funcRef, $_params, $ref);
         }
 
-        // Rendering charset of HTML page.
-        if (isset($this->config['config']['metaCharset']) && $this->config['config']['metaCharset'] !== 'utf-8') {
-            $this->metaCharset = $this->config['config']['metaCharset'];
-        }
-
         // Get values from site language
         $languageAspect = LanguageAspectFactory::createFromSiteLanguage($this->language);
 
diff --git a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
index 3338875e5533..d124374d1e82 100644
--- a/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
+++ b/typo3/sysext/frontend/Classes/Middleware/PrepareTypoScriptFrontendRendering.php
@@ -63,7 +63,7 @@ class PrepareTypoScriptFrontendRendering implements MiddlewareInterface
         // Get from cache
         $this->timeTracker->push('Get Page from cache');
         // Locks may be acquired here
-        $this->controller->getFromCache();
+        $this->controller->getFromCache($request);
         $this->timeTracker->pull();
         // Get config if not already gotten
         // After this, we should have a valid config-array ready
-- 
GitLab