Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/Internal/CurlClientState.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient\Internal;
13
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\HttpClient\Response\CurlResponse;
16
17 /**
18 * Internal representation of the cURL client's state.
19 *
20 * @author Alexander M. Turek <[email protected]>
21 *
22 * @internal
23 */
24 final class CurlClientState extends ClientState
25 {
26 public ?\CurlMultiHandle $handle;
27 public ?\CurlShareHandle $share;
28 public bool $performing = false;
29
30 /** @var PushedResponse[] */
31 public array $pushedResponses = [];
32 public DnsCache $dnsCache;
33 /** @var float[] */
34 public array $pauseExpiries = [];
35 public int $execCounter = \PHP_INT_MIN;
36 public ?LoggerInterface $logger = null;
37
38 /** @var array<string, true> Indexed by self::originKey() */
39 public array $ntlmRequiresFreshConnection = [];
40
41 public static array $curlVersion;
42
43 public function __construct(
44 private int $maxHostConnections,
45 private int $maxPendingPushes,
46 ) {
47 self::$curlVersion ??= curl_version();
48 $this->dnsCache = new DnsCache();
49
50 // handle and share are initialized lazily in __get()
51 unset($this->handle, $this->share);
52 }
53
54 public static function originKey(string $scheme, string $host, ?int $port = null): string
55 {
56 $scheme = strtolower(rtrim($scheme, ':'));
57 $port ??= 'https' === $scheme ? 443 : 80;
58
59 return $scheme.'://'.strtolower($host).':'.$port;
60 }
61
62 public function reset(): void
63 {
64 foreach ($this->pushedResponses as $url => $response) {
65 $this->logger?->debug(\sprintf('Unused pushed response: "%s"', $url));
66 curl_multi_remove_handle($this->handle, $response->handle);
67 unset($this->handlesActivity[(int) $response->handle]);
68 }
69
70 $this->pushedResponses = [];
71 $this->dnsCache->evictions = $this->dnsCache->evictions ?: $this->dnsCache->removals;
72 $this->dnsCache->removals = $this->dnsCache->hostnames = [];
73 $this->ntlmRequiresFreshConnection = [];
74
75 unset($this->share);
76 }
77
78 public function __get(string $name): mixed
79 {
80 if ('share' === $name) {
81 $this->share = curl_share_init();
82
83 curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_DNS);
84 curl_share_setopt($this->share, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION);
85
86 // Don't share CURL_LOCK_DATA_CONNECT: easy handles attached to the same multi handle
87 // already share the connection cache, and adding it here creates a second pool that
88 // bypasses CURLMOPT_MAX_HOST_CONNECTIONS.
89 // See https://curl.se/libcurl/c/CURLSHOPT_SHARE.html#CURLLOCKDATACONNECT
90
91 return $this->share;
92 }
93
94 if ('handle' === $name) {
95 $this->handle = curl_multi_init();
96
97 // Don't enable HTTP/1.1 pipelining: it forces responses to be sent in order
98 if (\defined('CURLPIPE_MULTIPLEX')) {
99 curl_multi_setopt($this->handle, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX);
100 }
101 $maxHostConnections = $this->maxHostConnections;
102 if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS') && 0 < $maxHostConnections) {
103 $maxHostConnections = curl_multi_setopt($this->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, $maxHostConnections) ? min(50 * $maxHostConnections, 4294967295) : $maxHostConnections;
104 }
105 if (\defined('CURLMOPT_MAXCONNECTS') && 0 < $maxHostConnections) {
106 curl_multi_setopt($this->handle, \CURLMOPT_MAXCONNECTS, $maxHostConnections);
107 }
108
109 // Skip configuring HTTP/2 push when it's unsupported or buggy, see https://bugs.php.net/77535
110 if (0 < $this->maxPendingPushes && (\defined('CURLMOPT_PUSHFUNCTION') && 0x073D00 <= self::$curlVersion['version_number'] && (\CURL_VERSION_HTTP2 & self::$curlVersion['features']))) {
111 // Clone to prevent a circular reference
112 $multi = clone $this;
113 $multi->handle = null;
114 $multi->share = null;
115 $multi->pushedResponses = &$this->pushedResponses;
116 $multi->logger = &$this->logger;
117 $multi->handlesActivity = &$this->handlesActivity;
118 $multi->openHandles = &$this->openHandles;
119
120 curl_multi_setopt($this->handle, \CURLMOPT_PUSHFUNCTION, $multi->handlePush(...));
121 }
122
123 return $this->handle;
124 }
125
126 throw new \LogicException(\sprintf('Unknown property "%s" on "%s".', $name, self::class));
127 }
128
129 private function handlePush($parent, $pushed, array $requestHeaders): int
130 {
131 $headers = [];
132 $origin = curl_getinfo($parent, \CURLINFO_EFFECTIVE_URL);
133
134 foreach ($requestHeaders as $h) {
135 if (false !== $i = strpos($h, ':', 1)) {
136 $headers[substr($h, 0, $i)][] = substr($h, 1 + $i);
137 }
138 }
139
140 if (!isset($headers[':method']) || !isset($headers[':scheme']) || !isset($headers[':authority']) || !isset($headers[':path'])) {
141 $this->logger?->debug(\sprintf('Rejecting pushed response from "%s": pushed headers are invalid', $origin));
142
143 return \CURL_PUSH_DENY;
144 }
145
146 $url = $headers[':scheme'][0].'://'.$headers[':authority'][0];
147
148 // curl before 7.65 doesn't validate the pushed ":authority" header,
149 // but this is a MUST in the HTTP/2 RFC; let's restrict pushes to the original host,
150 // ignoring domains mentioned as alt-name in the certificate for now (same as curl).
151 if (!str_starts_with($origin, $url.'/')) {
152 $this->logger?->debug(\sprintf('Rejecting pushed response from "%s": server is not authoritative for "%s"', $origin, $url));
153
154 return \CURL_PUSH_DENY;
155 }
156
157 if ($this->maxPendingPushes <= \count($this->pushedResponses)) {
158 $fifoUrl = key($this->pushedResponses);
159 unset($this->pushedResponses[$fifoUrl]);
160 $this->logger?->debug(\sprintf('Evicting oldest pushed response: "%s"', $fifoUrl));
161 }
162
163 $url .= $headers[':path'][0];
164 $this->logger?->debug(\sprintf('Queueing pushed response: "%s"', $url));
165
166 $this->pushedResponses[$url] = new PushedResponse(new CurlResponse($this, $pushed), $headers, $this->openHandles[(int) $parent][1] ?? [], $pushed);
167
168 return \CURL_PUSH_OK;
169 }
170 }