diff --git a/core/core.services.yml b/core/core.services.yml index 0aec7ac..567e757 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -378,12 +378,17 @@ services: class: GuzzleHttp\HandlerStack public: false factory: GuzzleHttp\HandlerStack::create + configurator: ['@http_handler_stack_configurator', configure] + http_handler_stack_configurator: + class: Drupal\Core\Http\HandlerStackConfigurator + public: false + arguments: ['@service_container'] http_client: class: Drupal\Core\Http\Client factory: http_client_factory:fromOptions http_client_factory: class: Drupal\Core\Http\ClientFactory - arguments: ['@handler_stack'] + arguments: ['@http_handler_stack'] theme.negotiator: class: Drupal\Core\Theme\ThemeNegotiator arguments: ['@access_check.theme'] diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index cac83a6..c9eea1f 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -10,7 +10,7 @@ use Drupal\Core\Cache\Context\CacheContextsPass; use Drupal\Core\Cache\ListCacheBinsPass; use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass; -use Drupal\Core\DependencyInjection\Compiler\GuzzleHandlerPass; +use Drupal\Core\DependencyInjection\Compiler\GuzzleMiddlewarePass; use Drupal\Core\DependencyInjection\Compiler\ContextProvidersPass; use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass; use Drupal\Core\DependencyInjection\Compiler\RegisterLazyRouteEnhancers; @@ -76,7 +76,7 @@ public function register(ContainerBuilder $container) { // Collect tagged handler services as method calls on consumer services. $container->addCompilerPass(new TaggedHandlersPass()); $container->addCompilerPass(new RegisterStreamWrappersPass()); - $container->addCompilerPass(new GuzzleHandlerPass()); + $container->addCompilerPass(new GuzzleMiddlewarePass()); // Add a compiler pass for registering event subscribers. $container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING); @@ -134,10 +134,10 @@ protected function registerTest(ContainerBuilder $container) { if (!drupal_valid_test_ua()) { return; } - // Add the HTTP request subscriber to Guzzle. + // Add the HTTP request middleware to Guzzle. $container ->register('test.http_client.handler', 'Drupal\Core\Test\EventSubscriber\HttpRequestSubscriber') - ->addTag('http_client_handler'); + ->addTag('http_client_middleware'); } } diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleHandlerPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php similarity index 51% rename from core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleHandlerPass.php rename to core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php index 009e8b2..d2523cf 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleHandlerPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/GuzzleMiddlewarePass.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\DependencyInjection\Compiler\GuzzleHandlerPass. + * Contains \Drupal\Core\DependencyInjection\Compiler\GuzzleMiddlewarePass. */ namespace Drupal\Core\DependencyInjection\Compiler; @@ -11,15 +11,15 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; -class GuzzleHandlerPass implements CompilerPassInterface { +class GuzzleMiddlewarePass implements CompilerPassInterface { /** * {@inheritdoc} */ public function process(ContainerBuilder $container) { - $service_ids = array_keys($container->findTaggedServiceIds('http_client_handler')); - $container->getDefinition('http_client') - ->addArgument($service_ids); + $middleware_ids = array_keys($container->findTaggedServiceIds('http_client_middleware')); + $container->getDefinition('http_handler_stack_configurator') + ->addArgument($middleware_ids); } } diff --git a/core/lib/Drupal/Core/Http/ClientFactory.php b/core/lib/Drupal/Core/Http/ClientFactory.php index 9c2eee2..b9d597a 100644 --- a/core/lib/Drupal/Core/Http/ClientFactory.php +++ b/core/lib/Drupal/Core/Http/ClientFactory.php @@ -62,7 +62,7 @@ public function fromOptions(array $config = []) { // The entire config array is merged/configurable to allow Guzzle client // options outside of 'defaults' to be changed, such as 'adapter', or // 'message_factory'. - $config = NestedArray::mergeDeep(array($default_config), Settings::get('http_client_config', array())); + $config = NestedArray::mergeDeep($config, $default_config, Settings::get('http_client_config', array())); return new Client($config); } diff --git a/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php b/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php new file mode 100644 index 0000000..d5aa3c5 --- /dev/null +++ b/core/lib/Drupal/Core/Http/HandlerStackConfigurator.php @@ -0,0 +1,62 @@ +get($middleware_id); + if (method_exists($middleware, '__invoke')) { + $class = get_class($middleware); + $this->middlewares[$middleware_id] = $class(); + } + } + } + + /** + * Configures the handler stack using services tagged as http_middleware. + * + * @param \GuzzleHttp\HandlerStack $handler_stack + * The handler stack + */ + public function configure(HandlerStack $handler_stack) { + foreach ($this->middlewares as $middleware_id => $middleware) { + $handler_stack->push($middleware, $middleware_id); + } + } + +} diff --git a/core/lib/Drupal/Core/Http/StackHandlerRegistrationInterface.php b/core/lib/Drupal/Core/Http/StackHandlerRegistrationInterface.php deleted file mode 100644 index 948bf02..0000000 --- a/core/lib/Drupal/Core/Http/StackHandlerRegistrationInterface.php +++ /dev/null @@ -1,20 +0,0 @@ -httpClientFactory->fromOptions(['on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $uri) use (&$actual_uri) { - $actual_uri = $uri; - }])->send($request); + $response = $this->httpClientFactory->fromOptions(['allow_redirects' => [ + 'on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $uri) use (&$actual_uri) { + $actual_uri = (string) $uri; + }]])->send($request); // In case of a 304 Not Modified, there is no new content, so return // FALSE. @@ -109,8 +110,7 @@ public function fetch(FeedInterface $feed) { $feed->http_headers = $response->getHeaders(); // Update the feed URL in case of a 301 redirect. - $actual_uri = (string) $actual_uri; - if ($actual_uri != $feed->getUrl()) { + if ($actual_uri && $actual_uri !== $feed->getUrl()) { $feed->setUrl($actual_uri); } return TRUE; diff --git a/core/modules/locale/locale.batch.inc b/core/modules/locale/locale.batch.inc index 6f2ad73..f549e51 100644 --- a/core/modules/locale/locale.batch.inc +++ b/core/modules/locale/locale.batch.inc @@ -238,14 +238,14 @@ function locale_translation_http_check($uri) { $logger = \Drupal::logger('locale'); try { $actual_uri = NULL; - $response = \Drupal::httpClientFactory()->fromOptions(['on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $uri) { - $actual_uri = $uri; - }])->head($uri); + $response = \Drupal::httpClientFactory()->fromOptions(['allow_redirects' => [ + 'on_redirect' => function(RequestInterface $request, ResponseInterface $response, UriInterface $uri) use (&$actual_uri) { + $actual_uri = (string) $uri; + }]])->head($uri); $result = array(); - $actual_uri = (string) $actual_uri; // Return the effective URL if it differs from the requested. - if ($actual_uri != $uri) { + if ($actual_uri && $actual_uri !== $uri) { $result['location'] = $actual_uri; }