[FEATURE] Allow array of handlers in RequestFactory 07/61407/8
authorFrank Naegler <frank.naegler@typo3.org>
Fri, 2 Aug 2019 19:18:48 +0000 (21:18 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Fri, 23 Aug 2019 13:40:19 +0000 (15:40 +0200)
Guzzle has the possibility to push custom middleware handlers during
client initialization. With this patch it is now possible to define own
handlers as an array of callables in $GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'].
The RequestFactory builds a HandlerStack from that array
and injects it into the created client.

Resolves: #88871
Releases: master
Change-Id: I62cb9c58c9ef7f8ce66255eb34b3a0953666cbc2
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61407
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Classes/Http/RequestFactory.php
typo3/sysext/core/Configuration/DefaultConfiguration.php
typo3/sysext/core/Documentation/Changelog/master/Feature-88871-RequestFactoryRespectsGuzzleMiddlewareHandlerConfigurationFromTYPO3_CONF_VARS.rst [new file with mode: 0644]

index 5bfe197..d421d1d 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\Http;
 
 use GuzzleHttp\Client;
 use GuzzleHttp\ClientInterface;
+use GuzzleHttp\HandlerStack;
 use Psr\Http\Message\ResponseInterface;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 
@@ -48,6 +49,14 @@ class RequestFactory
         $httpOptions = $GLOBALS['TYPO3_CONF_VARS']['HTTP'];
         $httpOptions['verify'] = filter_var($httpOptions['verify'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? $httpOptions['verify'];
 
+        if (isset($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']) && is_array($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'])) {
+            $stack = HandlerStack::create();
+            foreach ($GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'] ?? [] as $handler) {
+                $stack->push($handler);
+            }
+            $httpOptions['handler'] = $stack;
+        }
+
         return GeneralUtility::makeInstance(Client::class, $httpOptions);
     }
 }
index 11cc118..f37dc86 100644 (file)
@@ -1323,6 +1323,7 @@ return [
         'timeout' => 0,
         'verify' => true,
         'version' => '1.1',
+        'handler' => [], // Array of callables
         'headers' => [ // Additional HTTP headers sent by every request TYPO3 executes.
             'User-Agent' => 'TYPO3' // String: Default user agent. Defaults to TYPO3.
         ]
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-88871-RequestFactoryRespectsGuzzleMiddlewareHandlerConfigurationFromTYPO3_CONF_VARS.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-88871-RequestFactoryRespectsGuzzleMiddlewareHandlerConfigurationFromTYPO3_CONF_VARS.rst
new file mode 100644 (file)
index 0000000..dd88bac
--- /dev/null
@@ -0,0 +1,32 @@
+.. include:: ../../Includes.txt
+
+=======================================================================
+Feature: #88871 - Handle middleware handler in RequestFactory correctly
+=======================================================================
+
+See :issue:`88871`
+
+Description
+===========
+
+Guzzle offers the possibility to register custom middleware handlers during the client initialization.
+With this feature it is now possible to define those custom handlers in :php:`$GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']` as an array.
+The RequestFactory builds a handler stack based on the :php:`$GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler']` array and injects it into the created client.
+
+Impact
+======
+
+The default handler stack (guzzle defaults) will be extended and not overwritten.
+
+Example:
+--------
+
+.. code-block:: php
+
+   # Add custom middleware to default Guzzle handler stack
+   $GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'][] =
+      (\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\ACME\Middleware\Guzzle\CustomMiddleware::class))->handler();
+   $GLOBALS['TYPO3_CONF_VARS']['HTTP']['handler'][] =
+      (\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\ACME\Middleware\Guzzle\SecondCustomMiddleware::class))->handler();
+
+.. index:: PHP-API, ext:core