diff --git a/core/lib/Drupal/Core/Http/Client.php b/core/lib/Drupal/Core/Http/Client.php index feab56a..60b7b23 100644 --- a/core/lib/Drupal/Core/Http/Client.php +++ b/core/lib/Drupal/Core/Http/Client.php @@ -7,8 +7,9 @@ namespace Drupal\Core\Http; -use GuzzleHttp\Client as GuzzleClient; +use Drupal\Core\Http\Plugin\SimpletestHttpRequestSubscriber; use Drupal\Component\Utility\NestedArray; +use GuzzleHttp\Client as GuzzleClient; /** * Drupal default HTTP client class. @@ -19,28 +20,11 @@ class Client extends GuzzleClient { * {@inheritdoc} */ public function __construct(array $config = []) { - // If the database prefix is being used by SimpleTest to run the tests in a - // copied database then set the user-agent header to the database prefix so - // that any calls to other Drupal pages will run the SimpleTest prefixed - // database. The user-agent is used to ensure that multiple testing sessions - // running at the same time won't interfere with each other as they would if - // the database prefix were stored statically in a file or database - // variable. - if ($test_prefix = drupal_valid_test_ua()) { - $default_user_agent = drupal_generate_test_ua($test_prefix); - } - else { - $default_user_agent = 'Drupal (+http://drupal.org/)'; - } - $defaults = array( - 'curl' => array( - CURLOPT_TIMEOUT => 30, - CURLOPT_MAXREDIRS => 3, - ), - 'defaults' => array( - 'headers' => array( - 'User-Agent' => $default_user_agent, + 'config' => array( + 'curl' => array( + CURLOPT_TIMEOUT => 30, + CURLOPT_MAXREDIRS => 3, ), // Security consideration: we must not use the certificate authority file // shipped with Guzzle because it can easily get outdated if a certificate @@ -51,10 +35,17 @@ public function __construct(array $config = []) { 'certificate_authority' => 'system', ), ), + 'defaults' => array( + 'headers' => array( + 'User-Agent' => 'Drupal (+http://drupal.org/)', + ), + ), ); $config = NestedArray::mergeDeep($defaults, $config); parent::__construct($config); + + $this->getEmitter()->attach(new SimpletestHttpRequestSubscriber()); } diff --git a/core/lib/Drupal/Core/Http/Plugin/SimpletestHttpRequestSubscriber.php b/core/lib/Drupal/Core/Http/Plugin/SimpletestHttpRequestSubscriber.php new file mode 100644 index 0000000..825588c --- /dev/null +++ b/core/lib/Drupal/Core/Http/Plugin/SimpletestHttpRequestSubscriber.php @@ -0,0 +1,42 @@ + array('onBeforeSendRequest'), + ); + } + + /** + * Event callback for the 'before' event + */ + public function onBeforeSendRequest(BeforeEvent $event) { + // If the database prefix is being used by SimpleTest to run the tests in a copied + // database then set the user-agent header to the database prefix so that any + // calls to other Drupal pages will run the SimpleTest prefixed database. The + // user-agent is used to ensure that multiple testing sessions running at the + // same time won't interfere with each other as they would if the database + // prefix were stored statically in a file or database variable. + if ($test_prefix = drupal_valid_test_ua()) { + $event->getRequest()->setHeader('User-Agent', drupal_generate_test_ua($test_prefix)); + } + } +}