diff --git a/core/core.services.yml b/core/core.services.yml index 788be4a..ef9ccd8 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -377,13 +377,7 @@ services: http_handler_stack: class: GuzzleHttp\HandlerStack public: false - tags: - - { name: http_client_handler } - calls: - - [setHandler, ['@http_handler_curl']] - http_handler_curl: - class: GuzzleHttp\Handler\CurlHandler - public: false + factory: [GuzzleHttp\HandlerStack, create] http_client: class: Drupal\Core\Http\Client arguments: ['@http_handler_stack'] diff --git a/core/lib/Drupal/Core/Http/Client.php b/core/lib/Drupal/Core/Http/Client.php index 326281f..afcd8ce 100644 --- a/core/lib/Drupal/Core/Http/Client.php +++ b/core/lib/Drupal/Core/Http/Client.php @@ -10,17 +10,30 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Site\Settings; use GuzzleHttp\Client as GuzzleClient; -use GuzzleHttp\Event\SubscriberInterface; +use GuzzleHttp\Handler\Proxy; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * Drupal default HTTP client class. */ class Client extends GuzzleClient { + protected $effectiveUrl; + /** * {@inheritdoc} */ - public function __construct(callable $handler, array $config = []) { + public function __construct(HandlerStack $stack, array $config = []) { + $stack->after('allow_redirects', Middleware::mapRequest(function (RequestInterface $request) { + $this->effectiveUrl = $request->getUri(); + return $request; + }), 'check_redirect'); + $stack->after('allow_redirects', Middleware::mapResponse(function (ResponseInterface $response) { + return $response->withHeader('X-Effective-Url', (string) $this->effectiveUrl); + }), 'store_redirect'); $default_config = array( // Security consideration: we must not use the certificate authority // file shipped with Guzzle because it can easily get outdated if a @@ -33,7 +46,7 @@ public function __construct(callable $handler, array $config = []) { 'headers' => array( 'User-Agent' => 'Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(), ), - 'handler' => $handler, + 'handler' => $stack, ); // The entire config array is merged/configurable to allow Guzzle client @@ -41,7 +54,7 @@ public function __construct(callable $handler, array $config = []) { // 'message_factory'. $config = NestedArray::mergeDeep(array('defaults' => $default_config), $config, Settings::get('http_client_config', array())); - parent::__construct($config); + parent::__construct($config['defaults']); } } diff --git a/core/modules/aggregator/src/Plugin/aggregator/fetcher/DefaultFetcher.php b/core/modules/aggregator/src/Plugin/aggregator/fetcher/DefaultFetcher.php index 1fb54d6..2434b74 100644 --- a/core/modules/aggregator/src/Plugin/aggregator/fetcher/DefaultFetcher.php +++ b/core/modules/aggregator/src/Plugin/aggregator/fetcher/DefaultFetcher.php @@ -97,9 +97,9 @@ public function fetch(FeedInterface $feed) { $feed->http_headers = $response->getHeaders(); // Update the feed URL in case of a 301 redirect. -// if ($response->getEffectiveUrl() != $feed->getUrl()) { -// $feed->setUrl($response->getEffectiveUrl()); -// } + if ($response->getHeader('X-Effective-Url')[0] != $feed->getUrl()) { + $feed->setUrl($response->getHeader('X-Effective-Url')[0]); + } return TRUE; } catch (RequestException $e) {