vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php

90 lines · 3138 bytes

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