[TASK] Add HTTP headers to Frontend Response object 99/58499/10
authorBenni Mack <benni@typo3.org>
Sun, 30 Sep 2018 13:23:28 +0000 (15:23 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Sun, 30 Sep 2018 21:33:06 +0000 (23:33 +0200)
HTTP Headers are added to the PSR-7 response object now
within TSFE.

They are then sent at the very end of the request process.

A bw compat layer is added as well.

Resolves: #86486
Releases: master
Change-Id: Ifdeaab26868d0cf60dd87d52046a2565c4971488
Reviewed-on: https://review.typo3.org/58499
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>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Deprecation-86486-TypoScriptFrontendController-processOutput.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallMatcher.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-86486-TypoScriptFrontendController-processOutput.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-86486-TypoScriptFrontendController-processOutput.rst
new file mode 100644 (file)
index 0000000..0471133
--- /dev/null
@@ -0,0 +1,34 @@
+.. include:: ../../Includes.txt
+
+===================================================================
+Deprecation: #86486 - TypoScriptFrontendController->processOutput()
+===================================================================
+
+See :issue:`86486`
+
+Description
+===========
+
+The method :php:`TypoScriptFrontendController->processOutput()` has been
+marked as deprecated.
+
+
+Impact
+======
+
+Calling this method will trigger a PHP deprecation notice.
+
+
+Affected Installations
+======================
+
+TYPO3 installations with extensions, which use the method.
+
+
+Migration
+=========
+
+Use :php:`TypoScriptFrontendController->applyHttpHeadersToResponse()` and
+:php:`TypoScriptFrontendController->processContentForOutput()` instead, if necessary.
+
+.. index:: Frontend, PHP-API, FullyScanned
index aa60eca..c80c1ba 100644 (file)
@@ -3928,9 +3928,23 @@ class TypoScriptFrontendController implements LoggerAwareInterface
      * This includes substituting the "username" comment, sending additional headers
      * (as defined in the TypoScript "config.additionalHeaders" object), XHTML cleaning content (if configured)
      * Works on $this->content.
+     *
+     * @deprecated since TYPO3 v9.5, will be removed in TYPO3 v10.0.
      */
     public function processOutput()
     {
+        trigger_error('TypoScriptFrontendController->processOutput() will be removed in TYPO3 v10.0. Use streamFile() instead.', E_USER_DEPRECATED);
+        $this->sendHttpHeadersDirectly();
+        $this->processContentForOutput();
+    }
+
+    /**
+     * Runs PHP header() calls. In an ideal world, this should never happen, but we keep it for bw compat.
+     *
+     * @internal
+     */
+    public function sendHttpHeadersDirectly()
+    {
         // Set header for charset-encoding unless disabled
         if (empty($this->config['config']['disableCharsetHeader'])) {
             $headLine = 'Content-Type: ' . $this->contentType . '; charset=' . trim($this->metaCharset);
@@ -3966,6 +3980,16 @@ class TypoScriptFrontendController implements LoggerAwareInterface
                 header($header . ': ' . $value);
             }
         }
+    }
+
+    /**
+     * Process the output before it's actually outputted.
+     *
+     * This includes substituting the "username" comment.
+     * Works on $this->content.
+     */
+    public function processContentForOutput()
+    {
         // Make substitution of eg. username/uid in content only if cache-headers for client/proxy caching is NOT sent!
         if (!$this->isClientCachable) {
             $this->contentStrReplace();
@@ -3978,6 +4002,53 @@ class TypoScriptFrontendController implements LoggerAwareInterface
     }
 
     /**
+     * Add HTTP headers to the response object.
+     *
+     * @param ResponseInterface $response
+     * @return ResponseInterface
+     */
+    public function applyHttpHeadersToResponse(ResponseInterface $response): ResponseInterface
+    {
+        // Set header for charset-encoding unless disabled
+        if (empty($this->config['config']['disableCharsetHeader'])) {
+            $response = $response->withHeader('Content-Type', $this->contentType . '; charset=' . trim($this->metaCharset));
+        }
+        // Set header for content language unless disabled
+        if (empty($this->config['config']['disableLanguageHeader']) && !empty($this->sys_language_isocode)) {
+            $response = $response->withHeader('Content-Language', trim($this->sys_language_isocode));
+        }
+        // Set cache related headers to client (used to enable proxy / client caching!)
+        if (!empty($this->config['config']['sendCacheHeaders'])) {
+            $headers = $this->getCacheHeaders();
+            foreach ($headers as $header => $value) {
+                $response = $response->withHeader($header, $value);
+            }
+        }
+        // Set additional headers if any have been configured via TypoScript
+        $additionalHeaders = $this->getAdditionalHeaders();
+        foreach ($additionalHeaders as $headerConfig) {
+            list($header, $value) = GeneralUtility::trimExplode(':', $headerConfig['header'], false, 2);
+            if ($headerConfig['statusCode']) {
+                $response = $response->withStatus($headerConfig['statusCode']);
+            }
+            if ($headerConfig['replace']) {
+                $response = $response->withHeader($header, $value);
+            } else {
+                $response = $response->withAddedHeader($header, $value);
+            }
+        }
+        // Send appropriate status code in case of temporary content
+        if ($this->tempContent) {
+            $response = $response->withStatus(503, 'Service unavailable');
+            $headers = $this->getHttpHeadersForTemporaryContent();
+            foreach ($headers as $header => $value) {
+                $response = $response->withHeader($header, $value);
+            }
+        }
+        return $response;
+    }
+
+    /**
      * Send cache headers good for client/reverse proxy caching.
      * @see getCacheHeaders() for more details
      * @deprecated since TYPO3 v9.4, will be removed in TYPO3 v10.0. Use $TSFE->processOutput to send headers instead.
index c5fd247..5955590 100644 (file)
@@ -193,7 +193,8 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
         $isOutputting = $controller->isOutputting();
         if ($isOutputting) {
             $this->timeTracker->push('Print Content');
-            $controller->processOutput();
+            $response = $controller->applyHttpHeadersToResponse($response);
+            $controller->processContentForOutput();
             $this->timeTracker->pull();
         }
         // Store session data for fe_users
@@ -202,6 +203,7 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
         // @deprecated since TYPO3 v9.3, will be removed in TYPO3 v10.0.
         $redirectResponse = $controller->redirectToExternalUrl(true);
         if ($redirectResponse instanceof ResponseInterface) {
+            $controller->sendHttpHeadersDirectly();
             return $redirectResponse;
         }
 
index cf796c0..9f4dc35 100644 (file)
@@ -3831,4 +3831,11 @@ return [
             'Deprecation-83793-FALResourceStorage-dumpFileContents.rst'
         ],
     ],
+    'TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController->processOutput' => [
+        'numberOfMandatoryArguments' => 0,
+        'maximumNumberOfArguments' => 0,
+        'restFiles' => [
+            'Deprecation-86486-TypoScriptFrontendController-processOutput.rst'
+        ],
+    ],
 ];