| 1 |
<?php |
| 2 |
|
| 3 |
namespace YoastSEO_Vendor\GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface; |
| 6 |
use YoastSEO_Vendor\GuzzleHttp\TransportSharing; |
| 7 |
use YoastSEO_Vendor\Psr\Http\Message\RequestInterface; |
| 8 |
/** |
| 9 |
* HTTP handler that uses cURL easy handles as a transport layer. |
| 10 |
* |
| 11 |
* When using the CurlHandler, custom curl options can be specified as an |
| 12 |
* associative array of curl option constants mapping to values in the |
| 13 |
* **curl** key of the "client" key of the request. |
| 14 |
* |
| 15 |
* @final |
| 16 |
*/ |
| 17 |
class CurlHandler |
| 18 |
{ |
| 19 |
private const KNOWN_CONSTRUCTOR_OPTIONS = ['handle_factory' => \true, 'transport_sharing' => \true]; |
| 20 |
/** |
| 21 |
* @var CurlFactoryInterface |
| 22 |
*/ |
| 23 |
private $factory; |
| 24 |
/** |
| 25 |
* @var CurlShareHandleState|null |
| 26 |
*/ |
| 27 |
private $shareHandleState; |
| 28 |
/** |
| 29 |
* Accepts an associative array of options: |
| 30 |
* |
| 31 |
* - handle_factory: Optional curl factory used to create cURL handles. |
| 32 |
* - transport_sharing: Optional transport sharing mode. |
| 33 |
* |
| 34 |
* @param array{handle_factory?: ?CurlFactoryInterface, transport_sharing?: mixed} $options Array of options to use with the handler |
| 35 |
*/ |
| 36 |
public function __construct(array $options = []) |
| 37 |
{ |
| 38 |
foreach ($options as $name => $_) { |
| 39 |
if (!isset(self::KNOWN_CONSTRUCTOR_OPTIONS[$name])) { |
| 40 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/guzzle', '7.14', \sprintf('The "%s" CurlHandler constructor option is unknown; guzzlehttp/guzzle 8.0 will reject unknown constructor options.', (string) $name)); |
| 41 |
} |
| 42 |
} |
| 43 |
\YoastSEO_Vendor\GuzzleHttp\Handler\CurlShareHandleState::assertNoRequiredSharingCustomFactoryConflict($options, 'CurlHandler'); |
| 44 |
$transportSharing = $options['transport_sharing'] ?? null; |
| 45 |
$sharingMode = \YoastSEO_Vendor\GuzzleHttp\Handler\CurlShareHandleState::normalizeMode($transportSharing, 'transport_sharing'); |
| 46 |
if (\array_key_exists('handle_factory', $options) && $options['handle_factory'] !== null) { |
| 47 |
$this->shareHandleState = null; |
| 48 |
$this->factory = $options['handle_factory']; |
| 49 |
return; |
| 50 |
} |
| 51 |
$this->shareHandleState = $sharingMode !== \YoastSEO_Vendor\GuzzleHttp\TransportSharing::NONE ? \YoastSEO_Vendor\GuzzleHttp\Handler\CurlShareHandleState::fromOption($transportSharing) : null; |
| 52 |
$this->factory = $this->shareHandleState !== null ? new \YoastSEO_Vendor\GuzzleHttp\Handler\CurlFactory(3, $this->shareHandleState->mode, $this->shareHandleState) : new \YoastSEO_Vendor\GuzzleHttp\Handler\CurlFactory(3); |
| 53 |
} |
| 54 |
public function __invoke(\YoastSEO_Vendor\Psr\Http\Message\RequestInterface $request, array $options) : \YoastSEO_Vendor\GuzzleHttp\Promise\PromiseInterface |
| 55 |
{ |
| 56 |
\YoastSEO_Vendor\GuzzleHttp\Handler\HostValidator::assertRequestHost($request); |
| 57 |
if (isset($options['delay'])) { |
| 58 |
\usleep($options['delay'] * 1000); |
| 59 |
} |
| 60 |
// A Multiplexing::NONE request option holds unconditionally here: |
| 61 |
// transport sharing never shares the connection cache on this |
| 62 |
// branch, and nothing else executes during the blocking curl_exec(), |
| 63 |
// so the transfer cannot share its connection with a concurrent |
| 64 |
// transfer. |
| 65 |
$easy = $this->factory->create($request, $options); |
| 66 |
\curl_exec($easy->handle); |
| 67 |
$easy->errno = \curl_errno($easy->handle); |
| 68 |
return \YoastSEO_Vendor\GuzzleHttp\Handler\CurlFactory::finish($this, $easy, $this->factory); |
| 69 |
} |
| 70 |
} |
| 71 |
|