| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use GuzzleHttp\Exception\ConnectException; |
| 6 |
use GuzzleHttp\Exception\RequestException; |
| 7 |
use GuzzleHttp\Promise as P; |
| 8 |
use GuzzleHttp\Promise\FulfilledPromise; |
| 9 |
use GuzzleHttp\Promise\PromiseInterface; |
| 10 |
use GuzzleHttp\Psr7\LazyOpenStream; |
| 11 |
use GuzzleHttp\TransferStats; |
| 12 |
use GuzzleHttp\TransportSharing; |
| 13 |
use GuzzleHttp\Utils; |
| 14 |
use Psr\Http\Message\RequestInterface; |
| 15 |
use Psr\Http\Message\UriInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class CurlFactory implements CurlFactoryInterface |
| 23 |
{ |
| 24 |
public const CURL_VERSION_STR = 'curl_version'; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public const LOW_CURL_VERSION_NUMBER = '7.21.2'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private $handles = []; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private $maxHandles; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private $shareHandle; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $shareMode; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct(int $maxHandles, string $shareMode = TransportSharing::NONE, $shareHandle = null) |
| 56 |
{ |
| 57 |
$this->maxHandles = $maxHandles; |
| 58 |
$this->shareMode = CurlShareHandleState::normalizeMode($shareMode, 'transport_sharing'); |
| 59 |
|
| 60 |
if ($this->shareMode === TransportSharing::NONE && $shareHandle !== null) { |
| 61 |
throw new \InvalidArgumentException('A cURL share handle cannot be provided when transport sharing is disabled.'); |
| 62 |
} |
| 63 |
|
| 64 |
if ($this->shareMode !== TransportSharing::NONE && $shareHandle === null) { |
| 65 |
throw new \InvalidArgumentException('A cURL share handle is required when transport sharing is enabled.'); |
| 66 |
} |
| 67 |
|
| 68 |
if ($shareHandle !== null && !self::isCurlShareHandle($shareHandle)) { |
| 69 |
throw new \InvalidArgumentException('A cURL share handle must be an instance of CurlShareHandle or a curl_share resource.'); |
| 70 |
} |
| 71 |
|
| 72 |
$this->shareHandle = $shareHandle; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private static function isCurlShareHandle($value): bool |
| 79 |
{ |
| 80 |
if (\PHP_VERSION_ID < 80000) { |
| 81 |
return \is_resource($value) && \get_resource_type($value) === 'curl_share'; |
| 82 |
} |
| 83 |
|
| 84 |
return $value instanceof \CurlShareHandle; |
| 85 |
} |
| 86 |
|
| 87 |
public function create(RequestInterface $request, array $options): EasyHandle |
| 88 |
{ |
| 89 |
$protocolVersion = $request->getProtocolVersion(); |
| 90 |
|
| 91 |
if ('' === $protocolVersion) { |
| 92 |
\trigger_deprecation('guzzlehttp/guzzle', '7.11', 'Sending a request with an empty protocol version is deprecated; guzzlehttp/guzzle 8.0 will reject empty protocol versions.'); |
| 93 |
|
| 94 |
$protocolVersion = '1.1'; |
| 95 |
$request = \GuzzleHttp\Psr7\Utils::modifyRequest($request, ['version' => $protocolVersion]); |
| 96 |
} |
| 97 |
|
| 98 |
if ('2' === $protocolVersion || '2.0' === $protocolVersion) { |
| 99 |
if (!self::supportsHttp2()) { |
| 100 |
throw new ConnectException('HTTP/2 is supported by the cURL handler, however libcurl is built without HTTP/2 support.', $request); |
| 101 |
} |
| 102 |
} elseif ('1.0' !== $protocolVersion && '1.1' !== $protocolVersion) { |
| 103 |
throw new ConnectException(sprintf('HTTP/%s is not supported by the cURL handler.', $protocolVersion), $request); |
| 104 |
} |
| 105 |
|
| 106 |
if (isset($options['curl']['body_as_string'])) { |
| 107 |
$options['_body_as_string'] = $options['curl']['body_as_string']; |
| 108 |
unset($options['curl']['body_as_string']); |
| 109 |
} |
| 110 |
|
| 111 |
self::triggerUnsupportedRequestOptionDeprecations($options); |
| 112 |
$this->rejectRequestLevelShareConflict($options); |
| 113 |
self::triggerConflictingCurlOptionDeprecations($options); |
| 114 |
|
| 115 |
$easy = new EasyHandle(); |
| 116 |
$easy->request = $request; |
| 117 |
$easy->options = $options; |
| 118 |
$conf = $this->getDefaultConf($easy); |
| 119 |
$this->applyMethod($easy, $conf); |
| 120 |
$this->applyHandlerOptions($easy, $conf); |
| 121 |
$this->applyHeaders($easy, $conf); |
| 122 |
unset($conf['_headers']); |
| 123 |
|
| 124 |
|
| 125 |
if (isset($options['curl'])) { |
| 126 |
$conf = \array_replace($conf, $options['curl']); |
| 127 |
} |
| 128 |
|
| 129 |
$conf[\CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy); |
| 130 |
if ($this->shareHandle !== null) { |
| 131 |
if (!\defined('CURLOPT_SHARE')) { |
| 132 |
throw new \InvalidArgumentException('The configured cURL share handle requires CURLOPT_SHARE, but it is not available in the installed PHP cURL extension.'); |
| 133 |
} |
| 134 |
|
| 135 |
$conf[(int) \constant('CURLOPT_SHARE')] = $this->shareHandle; |
| 136 |
} |
| 137 |
|
| 138 |
$handle = $this->handles ? \array_pop($this->handles) : \curl_init(); |
| 139 |
if (false === $handle) { |
| 140 |
throw new \RuntimeException('Can not initialize cURL handle.'); |
| 141 |
} |
| 142 |
$easy->handle = $handle; |
| 143 |
|
| 144 |
try { |
| 145 |
$this->applyCurlOptions($handle, $conf); |
| 146 |
} catch (\Throwable $e) { |
| 147 |
if (PHP_VERSION_ID < 80000 && \is_resource($handle)) { |
| 148 |
\curl_close($handle); |
| 149 |
} |
| 150 |
unset($easy->handle); |
| 151 |
|
| 152 |
throw $e; |
| 153 |
} |
| 154 |
|
| 155 |
return $easy; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function applyCurlOptions($handle, array $conf): void |
| 163 |
{ |
| 164 |
foreach ($conf as $option => $value) { |
| 165 |
if (!\is_int($option)) { |
| 166 |
throw new \InvalidArgumentException(\sprintf( |
| 167 |
'Invalid cURL option %s.', |
| 168 |
self::formatCurlOption($option) |
| 169 |
)); |
| 170 |
} |
| 171 |
|
| 172 |
try { |
| 173 |
$success = curl_setopt($handle, $option, $value); |
| 174 |
} catch (\Throwable $e) { |
| 175 |
throw new \InvalidArgumentException( |
| 176 |
\sprintf( |
| 177 |
'Unable to set cURL option %s: %s', |
| 178 |
self::formatCurlOption($option), |
| 179 |
$e->getMessage() |
| 180 |
), |
| 181 |
0, |
| 182 |
$e |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
if (!$success) { |
| 187 |
throw new \InvalidArgumentException(\sprintf( |
| 188 |
'Unable to set cURL option %s.', |
| 189 |
self::formatCurlOption($option) |
| 190 |
)); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private function rejectRequestLevelShareConflict(array $options): void |
| 196 |
{ |
| 197 |
if ($this->shareHandle === null) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
if ( |
| 202 |
!\defined('CURLOPT_SHARE') |
| 203 |
|| !isset($options['curl']) |
| 204 |
|| !\is_array($options['curl']) |
| 205 |
|| !\array_key_exists((int) \constant('CURLOPT_SHARE'), $options['curl']) |
| 206 |
) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
throw new \InvalidArgumentException('The request-level CURLOPT_SHARE cURL option cannot be combined with configured transport sharing.'); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private static function formatCurlOption($option): string |
| 217 |
{ |
| 218 |
if (!\is_int($option)) { |
| 219 |
return \sprintf('"%s"', $option); |
| 220 |
} |
| 221 |
|
| 222 |
static $names = null; |
| 223 |
|
| 224 |
if (null === $names) { |
| 225 |
$names = []; |
| 226 |
foreach (\get_defined_constants(true)['curl'] ?? [] as $name => $value) { |
| 227 |
if (\is_int($value) && \strpos($name, 'CURLOPT_') === 0 && !isset($names[$value])) { |
| 228 |
$names[$value] = $name; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if (isset($names[$option])) { |
| 234 |
return \sprintf('%s (%d)', $names[$option], $option); |
| 235 |
} |
| 236 |
|
| 237 |
return (string) $option; |
| 238 |
} |
| 239 |
|
| 240 |
private static function triggerConflictingCurlOptionDeprecations(array $options): void |
| 241 |
{ |
| 242 |
if (!isset($options['curl']) || !\is_array($options['curl']) || $options['curl'] === []) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
$conflictingOptions = self::conflictingCurlOptions(); |
| 247 |
|
| 248 |
foreach ($options['curl'] as $option => $_) { |
| 249 |
if (!\array_key_exists($option, $conflictingOptions)) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
$name = self::formatCurlOption($option); |
| 254 |
$replacement = $conflictingOptions[$option]; |
| 255 |
if ($replacement !== null) { |
| 256 |
\trigger_deprecation( |
| 257 |
'guzzlehttp/guzzle', |
| 258 |
'7.11', |
| 259 |
\sprintf( |
| 260 |
'Passing %s in the "curl" request option is deprecated; guzzlehttp/guzzle 8.0 will reject this option because it conflicts with Guzzle-managed request handling. Use %s instead.', |
| 261 |
$name, |
| 262 |
$replacement |
| 263 |
) |
| 264 |
); |
| 265 |
|
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
\trigger_deprecation( |
| 270 |
'guzzlehttp/guzzle', |
| 271 |
'7.11', |
| 272 |
\sprintf( |
| 273 |
'Passing %s in the "curl" request option is deprecated; guzzlehttp/guzzle 8.0 will reject this option because it conflicts with Guzzle-managed cURL internals.', |
| 274 |
$name |
| 275 |
) |
| 276 |
); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
private static function triggerUnsupportedRequestOptionDeprecations(array $options): void |
| 281 |
{ |
| 282 |
if (\array_key_exists('stream_context', $options)) { |
| 283 |
\trigger_deprecation('guzzlehttp/guzzle', '7.11', 'Passing the "stream_context" request option to a cURL handler is deprecated; guzzlehttp/guzzle 8.0 will reject this option because cURL handlers ignore PHP stream context options.'); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private static function conflictingCurlOptions(): array |
| 291 |
{ |
| 292 |
static $options = null; |
| 293 |
|
| 294 |
if ($options !== null) { |
| 295 |
return $options; |
| 296 |
} |
| 297 |
|
| 298 |
$options = []; |
| 299 |
|
| 300 |
self::addConflictingCurlOption($options, 'CURLOPT_SHARE', 'the "transport_sharing" client option or cURL handler option'); |
| 301 |
self::addConflictingCurlOption($options, 'CURLOPT_URL', 'the request URI'); |
| 302 |
self::addConflictingCurlOption($options, 'CURLOPT_PORT', 'the request URI'); |
| 303 |
self::addConflictingCurlOption($options, 'CURLOPT_CUSTOMREQUEST', 'the request method'); |
| 304 |
self::addConflictingCurlOption($options, 'CURLOPT_HTTPGET', 'the request method'); |
| 305 |
self::addConflictingCurlOption($options, 'CURLOPT_POST', 'the request method and body'); |
| 306 |
self::addConflictingCurlOption($options, 'CURLOPT_PUT', 'the request method and body'); |
| 307 |
self::addConflictingCurlOption($options, 'CURLOPT_NOBODY', 'the request method'); |
| 308 |
self::addConflictingCurlOption($options, 'CURLOPT_UPLOAD', 'the request body'); |
| 309 |
self::addConflictingCurlOption($options, 'CURLOPT_POSTFIELDS', 'the request body'); |
| 310 |
self::addConflictingCurlOption($options, 'CURLOPT_READFUNCTION', 'the request body'); |
| 311 |
self::addConflictingCurlOption($options, 'CURLOPT_READDATA', 'the request body'); |
| 312 |
self::addConflictingCurlOption($options, 'CURLOPT_INFILE', 'the request body'); |
| 313 |
self::addConflictingCurlOption($options, 'CURLOPT_INFILESIZE', 'the request body'); |
| 314 |
self::addConflictingCurlOption($options, 'CURLOPT_INFILESIZE_LARGE', 'the request body'); |
| 315 |
self::addConflictingCurlOption($options, 'CURLOPT_HTTPHEADER', 'the request headers'); |
| 316 |
self::addConflictingCurlOption($options, 'CURLOPT_USERAGENT', 'the request headers'); |
| 317 |
self::addConflictingCurlOption($options, 'CURLOPT_REFERER', 'the request headers'); |
| 318 |
self::addConflictingCurlOption($options, 'CURLOPT_HEADERFUNCTION', 'the "on_headers" request option'); |
| 319 |
self::addConflictingCurlOption($options, 'CURLOPT_WRITEFUNCTION', 'the "sink" request option'); |
| 320 |
self::addConflictingCurlOption($options, 'CURLOPT_FILE', 'the "sink" request option'); |
| 321 |
self::addConflictingCurlOption($options, 'CURLOPT_RETURNTRANSFER', null); |
| 322 |
self::addConflictingCurlOption($options, 'CURLOPT_HEADER', null); |
| 323 |
self::addConflictingCurlOption($options, 'CURLOPT_TIMEOUT', 'the "timeout" request option'); |
| 324 |
self::addConflictingCurlOption($options, 'CURLOPT_TIMEOUT_MS', 'the "timeout" request option'); |
| 325 |
self::addConflictingCurlOption($options, 'CURLOPT_CONNECTTIMEOUT', 'the "connect_timeout" request option'); |
| 326 |
self::addConflictingCurlOption($options, 'CURLOPT_CONNECTTIMEOUT_MS', 'the "connect_timeout" request option'); |
| 327 |
self::addConflictingCurlOption($options, 'CURLOPT_NOSIGNAL', 'the "timeout" or "connect_timeout" request option'); |
| 328 |
self::addConflictingCurlOption($options, 'CURLOPT_NOPROGRESS', 'the "progress" request option'); |
| 329 |
self::addConflictingCurlOption($options, 'CURLOPT_PROGRESSFUNCTION', 'the "progress" request option'); |
| 330 |
self::addConflictingCurlOption($options, 'CURLOPT_XFERINFOFUNCTION', 'the "progress" request option'); |
| 331 |
self::addConflictingCurlOption($options, 'CURLOPT_VERBOSE', 'the "debug" request option'); |
| 332 |
self::addConflictingCurlOption($options, 'CURLOPT_STDERR', 'the "debug" request option'); |
| 333 |
self::addConflictingCurlOption($options, 'CURLOPT_PROXY', 'the "proxy" request option'); |
| 334 |
self::addConflictingCurlOption($options, 'CURLOPT_NOPROXY', 'the "proxy" request option'); |
| 335 |
self::addConflictingCurlOption($options, 'CURLOPT_FOLLOWLOCATION', 'the "allow_redirects" request option'); |
| 336 |
self::addConflictingCurlOption($options, 'CURLOPT_MAXREDIRS', 'the "allow_redirects" request option'); |
| 337 |
self::addConflictingCurlOption($options, 'CURLOPT_POSTREDIR', 'the "allow_redirects" request option'); |
| 338 |
self::addConflictingCurlOption($options, 'CURLOPT_REDIR_PROTOCOLS', 'the "allow_redirects" request option'); |
| 339 |
self::addConflictingCurlOption($options, 'CURLOPT_REDIR_PROTOCOLS_STR', 'the "allow_redirects" request option'); |
| 340 |
self::addConflictingCurlOption($options, 'CURLOPT_PROTOCOLS', 'the "protocols" request option'); |
| 341 |
self::addConflictingCurlOption($options, 'CURLOPT_PROTOCOLS_STR', 'the "protocols" request option'); |
| 342 |
self::addConflictingCurlOption($options, 'CURLOPT_HTTP09_ALLOWED', null); |
| 343 |
self::addConflictingCurlOption($options, 'CURLOPT_HTTP_VERSION', 'the request protocol version'); |
| 344 |
self::addConflictingCurlOption($options, 'CURLOPT_IPRESOLVE', 'the "force_ip_resolve" request option'); |
| 345 |
self::addConflictingCurlOption($options, 'CURLOPT_SSL_VERIFYPEER', 'the "verify" request option'); |
| 346 |
self::addConflictingCurlOption($options, 'CURLOPT_SSL_VERIFYHOST', 'the "verify" request option'); |
| 347 |
self::addConflictingCurlOption($options, 'CURLOPT_CAINFO', 'the "verify" request option'); |
| 348 |
self::addConflictingCurlOption($options, 'CURLOPT_CAPATH', 'the "verify" request option'); |
| 349 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLVERSION', 'the "crypto_method" request option'); |
| 350 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLCERT', 'the "cert" request option'); |
| 351 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLCERTPASSWD', 'the "cert" request option'); |
| 352 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLCERTTYPE', 'the "cert_type" request option'); |
| 353 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLKEY', 'the "ssl_key" request option'); |
| 354 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLKEYPASSWD', 'the "ssl_key" request option'); |
| 355 |
self::addConflictingCurlOption($options, 'CURLOPT_KEYPASSWD', 'the "ssl_key" request option'); |
| 356 |
self::addConflictingCurlOption($options, 'CURLOPT_SSLKEYTYPE', 'the "ssl_key_type" request option'); |
| 357 |
self::addConflictingCurlOption($options, 'CURLOPT_COOKIE', 'the "Cookie" request header or Guzzle cookie middleware'); |
| 358 |
self::addConflictingCurlOption($options, 'CURLOPT_COOKIEFILE', 'Guzzle cookie middleware'); |
| 359 |
self::addConflictingCurlOption($options, 'CURLOPT_COOKIEJAR', 'Guzzle cookie middleware'); |
| 360 |
self::addConflictingCurlOption($options, 'CURLOPT_COOKIELIST', 'Guzzle cookie middleware'); |
| 361 |
self::addConflictingCurlOption($options, 'CURLOPT_COOKIESESSION', 'Guzzle cookie middleware'); |
| 362 |
|
| 363 |
return $options; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
private static function addConflictingCurlOption(array &$options, string $constant, ?string $replacement): void |
| 370 |
{ |
| 371 |
if (!\defined($constant)) { |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
$value = \constant($constant); |
| 376 |
if (\is_int($value)) { |
| 377 |
$options[$value] = $replacement; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
private static function supportsHttp2(): bool |
| 382 |
{ |
| 383 |
static $supportsHttp2 = null; |
| 384 |
|
| 385 |
if (null === $supportsHttp2) { |
| 386 |
$supportsHttp2 = self::supportsTls12() |
| 387 |
&& defined('CURL_VERSION_HTTP2') |
| 388 |
&& (\CURL_VERSION_HTTP2 & \curl_version()['features']); |
| 389 |
} |
| 390 |
|
| 391 |
return $supportsHttp2; |
| 392 |
} |
| 393 |
|
| 394 |
private static function supportsTls12(): bool |
| 395 |
{ |
| 396 |
static $supportsTls12 = null; |
| 397 |
|
| 398 |
if (null === $supportsTls12) { |
| 399 |
$supportsTls12 = \CURL_SSLVERSION_TLSv1_2 & \curl_version()['features']; |
| 400 |
} |
| 401 |
|
| 402 |
return $supportsTls12; |
| 403 |
} |
| 404 |
|
| 405 |
private static function supportsTls13(): bool |
| 406 |
{ |
| 407 |
static $supportsTls13 = null; |
| 408 |
|
| 409 |
if (null === $supportsTls13) { |
| 410 |
$supportsTls13 = defined('CURL_SSLVERSION_TLSv1_3') |
| 411 |
&& (\CURL_SSLVERSION_TLSv1_3 & \curl_version()['features']); |
| 412 |
} |
| 413 |
|
| 414 |
return $supportsTls13; |
| 415 |
} |
| 416 |
|
| 417 |
public function release(EasyHandle $easy): void |
| 418 |
{ |
| 419 |
$resource = $easy->handle; |
| 420 |
unset($easy->handle); |
| 421 |
|
| 422 |
if (\count($this->handles) >= $this->maxHandles) { |
| 423 |
if (PHP_VERSION_ID < 80000) { |
| 424 |
\curl_close($resource); |
| 425 |
} |
| 426 |
} else { |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
\curl_setopt($resource, \CURLOPT_HEADERFUNCTION, null); |
| 432 |
\curl_setopt($resource, \CURLOPT_READFUNCTION, null); |
| 433 |
\curl_setopt($resource, \CURLOPT_WRITEFUNCTION, null); |
| 434 |
\curl_setopt($resource, \CURLOPT_PROGRESSFUNCTION, null); |
| 435 |
\curl_reset($resource); |
| 436 |
$this->handles[] = $resource; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
public static function finish(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
| 448 |
{ |
| 449 |
if (isset($easy->options['on_stats'])) { |
| 450 |
self::invokeStats($easy); |
| 451 |
} |
| 452 |
|
| 453 |
if (!$easy->response || $easy->errno) { |
| 454 |
return self::finishError($handler, $easy, $factory); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
$factory->release($easy); |
| 459 |
|
| 460 |
|
| 461 |
$body = $easy->response->getBody(); |
| 462 |
if ($body->isSeekable()) { |
| 463 |
$body->rewind(); |
| 464 |
} |
| 465 |
|
| 466 |
return new FulfilledPromise($easy->response); |
| 467 |
} |
| 468 |
|
| 469 |
private static function invokeStats(EasyHandle $easy): void |
| 470 |
{ |
| 471 |
$curlStats = \curl_getinfo($easy->handle); |
| 472 |
$curlStats['appconnect_time'] = \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME); |
| 473 |
$stats = new TransferStats( |
| 474 |
$easy->request, |
| 475 |
$easy->response, |
| 476 |
$curlStats['total_time'], |
| 477 |
$easy->errno, |
| 478 |
$curlStats |
| 479 |
); |
| 480 |
($easy->options['on_stats'])($stats); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
private static function finishError(callable $handler, EasyHandle $easy, CurlFactoryInterface $factory): PromiseInterface |
| 487 |
{ |
| 488 |
|
| 489 |
$ctx = [ |
| 490 |
'errno' => $easy->errno, |
| 491 |
'error' => \curl_error($easy->handle), |
| 492 |
'appconnect_time' => \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME), |
| 493 |
] + \curl_getinfo($easy->handle); |
| 494 |
$ctx[self::CURL_VERSION_STR] = self::getCurlVersion(); |
| 495 |
$factory->release($easy); |
| 496 |
|
| 497 |
|
| 498 |
if (empty($easy->options['_err_message']) && (!$easy->errno || $easy->errno == 65)) { |
| 499 |
return self::retryFailedRewind($handler, $easy, $ctx); |
| 500 |
} |
| 501 |
|
| 502 |
return self::createRejection($easy, $ctx); |
| 503 |
} |
| 504 |
|
| 505 |
private static function getCurlVersion(): string |
| 506 |
{ |
| 507 |
static $curlVersion = null; |
| 508 |
|
| 509 |
if (null === $curlVersion) { |
| 510 |
$curlVersion = \curl_version()['version']; |
| 511 |
} |
| 512 |
|
| 513 |
return $curlVersion; |
| 514 |
} |
| 515 |
|
| 516 |
private static function createRejection(EasyHandle $easy, array $ctx): PromiseInterface |
| 517 |
{ |
| 518 |
static $connectionErrors = [ |
| 519 |
\CURLE_OPERATION_TIMEOUTED => true, |
| 520 |
\CURLE_COULDNT_RESOLVE_HOST => true, |
| 521 |
\CURLE_COULDNT_CONNECT => true, |
| 522 |
\CURLE_SSL_CONNECT_ERROR => true, |
| 523 |
\CURLE_GOT_NOTHING => true, |
| 524 |
]; |
| 525 |
|
| 526 |
if ($easy->createResponseException) { |
| 527 |
return P\Create::rejectionFor( |
| 528 |
new RequestException( |
| 529 |
'An error was encountered while creating the response', |
| 530 |
$easy->request, |
| 531 |
null, |
| 532 |
$easy->createResponseException, |
| 533 |
$ctx |
| 534 |
) |
| 535 |
); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
if ($easy->onHeadersException) { |
| 541 |
return P\Create::rejectionFor( |
| 542 |
new RequestException( |
| 543 |
'An error was encountered during the on_headers event', |
| 544 |
$easy->request, |
| 545 |
$easy->response, |
| 546 |
$easy->onHeadersException, |
| 547 |
$ctx |
| 548 |
) |
| 549 |
); |
| 550 |
} |
| 551 |
|
| 552 |
$uri = $easy->request->getUri(); |
| 553 |
|
| 554 |
$sanitizedError = self::sanitizeCurlError($ctx['error'] ?? '', $uri); |
| 555 |
|
| 556 |
$message = \sprintf( |
| 557 |
'cURL error %s: %s (%s)', |
| 558 |
$ctx['errno'], |
| 559 |
$sanitizedError, |
| 560 |
'see https://curl.haxx.se/libcurl/c/libcurl-errors.html' |
| 561 |
); |
| 562 |
|
| 563 |
if ('' !== $sanitizedError) { |
| 564 |
$redactedUriString = \GuzzleHttp\Psr7\Utils::redactUserInfo($uri)->__toString(); |
| 565 |
if ($redactedUriString !== '' && false === \strpos($sanitizedError, $redactedUriString)) { |
| 566 |
$message .= \sprintf(' for %s', $redactedUriString); |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
$error = isset($connectionErrors[$easy->errno]) |
| 572 |
? new ConnectException($message, $easy->request, null, $ctx) |
| 573 |
: new RequestException($message, $easy->request, $easy->response, null, $ctx); |
| 574 |
|
| 575 |
return P\Create::rejectionFor($error); |
| 576 |
} |
| 577 |
|
| 578 |
private static function sanitizeCurlError(string $error, UriInterface $uri): string |
| 579 |
{ |
| 580 |
if ('' === $error) { |
| 581 |
return $error; |
| 582 |
} |
| 583 |
|
| 584 |
$baseUri = $uri->withQuery('')->withFragment(''); |
| 585 |
$baseUriString = $baseUri->__toString(); |
| 586 |
|
| 587 |
if ('' === $baseUriString) { |
| 588 |
return $error; |
| 589 |
} |
| 590 |
|
| 591 |
$redactedUriString = \GuzzleHttp\Psr7\Utils::redactUserInfo($baseUri)->__toString(); |
| 592 |
|
| 593 |
return str_replace($baseUriString, $redactedUriString, $error); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
private function getDefaultConf(EasyHandle $easy): array |
| 600 |
{ |
| 601 |
$conf = [ |
| 602 |
'_headers' => $easy->request->getHeaders(), |
| 603 |
\CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(), |
| 604 |
\CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''), |
| 605 |
\CURLOPT_RETURNTRANSFER => false, |
| 606 |
\CURLOPT_HEADER => false, |
| 607 |
\CURLOPT_CONNECTTIMEOUT => 300, |
| 608 |
]; |
| 609 |
|
| 610 |
$protocols = Utils::normalizeProtocols($easy->options['protocols'] ?? ['http', 'https']); |
| 611 |
$scheme = $easy->request->getUri()->getScheme(); |
| 612 |
if (!\in_array($scheme, $protocols, true)) { |
| 613 |
throw new RequestException(\sprintf('The scheme "%s" is not allowed by the protocols request option.', $scheme), $easy->request); |
| 614 |
} |
| 615 |
|
| 616 |
if (\defined('CURLOPT_PROTOCOLS')) { |
| 617 |
$conf[\CURLOPT_PROTOCOLS] = self::curlProtocolMask($protocols); |
| 618 |
} |
| 619 |
|
| 620 |
$version = $easy->request->getProtocolVersion(); |
| 621 |
|
| 622 |
if ('2' === $version || '2.0' === $version) { |
| 623 |
$conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2_0; |
| 624 |
} elseif ('1.1' === $version) { |
| 625 |
$conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1; |
| 626 |
} else { |
| 627 |
$conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_0; |
| 628 |
} |
| 629 |
|
| 630 |
return $conf; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
private static function curlProtocolMask(array $protocols): int |
| 637 |
{ |
| 638 |
$mask = 0; |
| 639 |
|
| 640 |
if (\in_array('http', $protocols, true)) { |
| 641 |
$mask |= \CURLPROTO_HTTP; |
| 642 |
} |
| 643 |
|
| 644 |
if (\in_array('https', $protocols, true)) { |
| 645 |
$mask |= \CURLPROTO_HTTPS; |
| 646 |
} |
| 647 |
|
| 648 |
return $mask; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
private static function normalizeTlsFileType(string $option, $type): string |
| 655 |
{ |
| 656 |
if (!\is_string($type) || $type === '') { |
| 657 |
throw new \InvalidArgumentException(\sprintf('%s must be a non-empty string', $option)); |
| 658 |
} |
| 659 |
|
| 660 |
return \strtoupper($type); |
| 661 |
} |
| 662 |
|
| 663 |
private static function shouldValidateSslKeyFile(?string $type): bool |
| 664 |
{ |
| 665 |
return $type !== 'ENG' && $type !== 'PROV'; |
| 666 |
} |
| 667 |
|
| 668 |
private function applyMethod(EasyHandle $easy, array &$conf): void |
| 669 |
{ |
| 670 |
$body = $easy->request->getBody(); |
| 671 |
$size = $body->getSize(); |
| 672 |
|
| 673 |
if ($size === null || $size > 0) { |
| 674 |
$this->applyBody($easy->request, $easy->options, $conf); |
| 675 |
|
| 676 |
return; |
| 677 |
} |
| 678 |
|
| 679 |
$method = $easy->request->getMethod(); |
| 680 |
if ($method === 'PUT' || $method === 'POST') { |
| 681 |
|
| 682 |
if (!$easy->request->hasHeader('Content-Length')) { |
| 683 |
$conf[\CURLOPT_HTTPHEADER][] = 'Content-Length: 0'; |
| 684 |
} |
| 685 |
} elseif ($method === 'HEAD') { |
| 686 |
$conf[\CURLOPT_NOBODY] = true; |
| 687 |
unset( |
| 688 |
$conf[\CURLOPT_WRITEFUNCTION], |
| 689 |
$conf[\CURLOPT_READFUNCTION], |
| 690 |
$conf[\CURLOPT_FILE], |
| 691 |
$conf[\CURLOPT_INFILE] |
| 692 |
); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
private function applyBody(RequestInterface $request, array $options, array &$conf): void |
| 697 |
{ |
| 698 |
$size = $request->hasHeader('Content-Length') |
| 699 |
? (int) $request->getHeaderLine('Content-Length') |
| 700 |
: null; |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
if (($size !== null && $size < 1000000) || !empty($options['_body_as_string'])) { |
| 705 |
$conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody(); |
| 706 |
|
| 707 |
$this->removeHeader('Content-Length', $conf); |
| 708 |
$this->removeHeader('Transfer-Encoding', $conf); |
| 709 |
} else { |
| 710 |
$conf[\CURLOPT_UPLOAD] = true; |
| 711 |
if ($size !== null) { |
| 712 |
$conf[\CURLOPT_INFILESIZE] = $size; |
| 713 |
$this->removeHeader('Content-Length', $conf); |
| 714 |
} |
| 715 |
$body = $request->getBody(); |
| 716 |
if ($body->isSeekable()) { |
| 717 |
$body->rewind(); |
| 718 |
} |
| 719 |
$conf[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) { |
| 720 |
return $body->read($length); |
| 721 |
}; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
if (!$request->hasHeader('Expect')) { |
| 726 |
$conf[\CURLOPT_HTTPHEADER][] = 'Expect:'; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
if (!$request->hasHeader('Content-Type')) { |
| 731 |
$conf[\CURLOPT_HTTPHEADER][] = 'Content-Type:'; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
private function applyHeaders(EasyHandle $easy, array &$conf): void |
| 736 |
{ |
| 737 |
foreach ($conf['_headers'] as $name => $values) { |
| 738 |
foreach ($values as $value) { |
| 739 |
$value = (string) $value; |
| 740 |
if ($value === '') { |
| 741 |
|
| 742 |
|
| 743 |
$conf[\CURLOPT_HTTPHEADER][] = "$name;"; |
| 744 |
} else { |
| 745 |
$conf[\CURLOPT_HTTPHEADER][] = "$name: $value"; |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
if (!$easy->request->hasHeader('Accept')) { |
| 752 |
$conf[\CURLOPT_HTTPHEADER][] = 'Accept:'; |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
private function removeHeader(string $name, array &$options): void |
| 763 |
{ |
| 764 |
foreach (\array_keys($options['_headers']) as $key) { |
| 765 |
if (!\strcasecmp((string) $key, $name)) { |
| 766 |
unset($options['_headers'][$key]); |
| 767 |
|
| 768 |
return; |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
private function applyHandlerOptions(EasyHandle $easy, array &$conf): void |
| 774 |
{ |
| 775 |
$options = $easy->options; |
| 776 |
if (isset($options['verify'])) { |
| 777 |
if ($options['verify'] === false) { |
| 778 |
unset($conf[\CURLOPT_CAINFO]); |
| 779 |
$conf[\CURLOPT_SSL_VERIFYHOST] = 0; |
| 780 |
$conf[\CURLOPT_SSL_VERIFYPEER] = false; |
| 781 |
} else { |
| 782 |
$conf[\CURLOPT_SSL_VERIFYHOST] = 2; |
| 783 |
$conf[\CURLOPT_SSL_VERIFYPEER] = true; |
| 784 |
if (\is_string($options['verify'])) { |
| 785 |
|
| 786 |
if (!\file_exists($options['verify'])) { |
| 787 |
throw new \InvalidArgumentException("SSL CA bundle not found: {$options['verify']}"); |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
if ( |
| 792 |
\is_dir($options['verify']) |
| 793 |
|| ( |
| 794 |
\is_link($options['verify']) === true |
| 795 |
&& ($verifyLink = \readlink($options['verify'])) !== false |
| 796 |
&& \is_dir($verifyLink) |
| 797 |
) |
| 798 |
) { |
| 799 |
$conf[\CURLOPT_CAPATH] = $options['verify']; |
| 800 |
} else { |
| 801 |
$conf[\CURLOPT_CAINFO] = $options['verify']; |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
if (!isset($options['curl'][\CURLOPT_ENCODING]) && !empty($options['decode_content'])) { |
| 808 |
$accept = $easy->request->getHeaderLine('Accept-Encoding'); |
| 809 |
if ($accept) { |
| 810 |
$conf[\CURLOPT_ENCODING] = $accept; |
| 811 |
} else { |
| 812 |
|
| 813 |
|
| 814 |
$conf[\CURLOPT_ENCODING] = ''; |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
$conf[\CURLOPT_HTTPHEADER][] = 'Accept-Encoding:'; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
if (!isset($options['sink'])) { |
| 824 |
|
| 825 |
$options['sink'] = \GuzzleHttp\Psr7\Utils::tryFopen('php://temp', 'w+'); |
| 826 |
} |
| 827 |
$sink = $options['sink']; |
| 828 |
if (!\is_string($sink)) { |
| 829 |
$sink = \GuzzleHttp\Psr7\Utils::streamFor($sink); |
| 830 |
} elseif (!\is_dir(\dirname($sink))) { |
| 831 |
|
| 832 |
throw new \RuntimeException(\sprintf('Directory %s does not exist for sink value of %s', \dirname($sink), $sink)); |
| 833 |
} else { |
| 834 |
$sink = new LazyOpenStream($sink, 'w+'); |
| 835 |
} |
| 836 |
$easy->sink = $sink; |
| 837 |
$conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int { |
| 838 |
return $sink->write($write); |
| 839 |
}; |
| 840 |
|
| 841 |
$timeoutRequiresNoSignal = false; |
| 842 |
if (isset($options['timeout'])) { |
| 843 |
$timeoutRequiresNoSignal |= $options['timeout'] < 1; |
| 844 |
$conf[\CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
if (isset($options['force_ip_resolve'])) { |
| 849 |
if ('v4' === $options['force_ip_resolve']) { |
| 850 |
$conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V4; |
| 851 |
} elseif ('v6' === $options['force_ip_resolve']) { |
| 852 |
$conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V6; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
if (isset($options['connect_timeout'])) { |
| 857 |
$timeoutRequiresNoSignal |= $options['connect_timeout'] < 1; |
| 858 |
$conf[\CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000; |
| 859 |
} |
| 860 |
|
| 861 |
if ($timeoutRequiresNoSignal && \strtoupper(\substr(\PHP_OS, 0, 3)) !== 'WIN') { |
| 862 |
$conf[\CURLOPT_NOSIGNAL] = true; |
| 863 |
} |
| 864 |
|
| 865 |
if (isset($options['proxy'])) { |
| 866 |
if (!\is_array($options['proxy'])) { |
| 867 |
$conf[\CURLOPT_PROXY] = $options['proxy']; |
| 868 |
} else { |
| 869 |
$scheme = $easy->request->getUri()->getScheme(); |
| 870 |
if (isset($options['proxy'][$scheme])) { |
| 871 |
if ( |
| 872 |
isset($options['proxy']['no']) |
| 873 |
&& Utils::isUriInNoProxy($easy->request->getUri(), $options['proxy']['no']) |
| 874 |
) { |
| 875 |
unset($conf[\CURLOPT_PROXY]); |
| 876 |
} else { |
| 877 |
$conf[\CURLOPT_PROXY] = $options['proxy'][$scheme]; |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
if (isset($options['crypto_method'])) { |
| 884 |
$protocolVersion = $easy->request->getProtocolVersion(); |
| 885 |
|
| 886 |
|
| 887 |
if ('2' === $protocolVersion || '2.0' === $protocolVersion) { |
| 888 |
if ( |
| 889 |
\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT === $options['crypto_method'] |
| 890 |
|| \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT === $options['crypto_method'] |
| 891 |
|| \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT === $options['crypto_method'] |
| 892 |
) { |
| 893 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2; |
| 894 |
} elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) { |
| 895 |
if (!self::supportsTls13()) { |
| 896 |
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL'); |
| 897 |
} |
| 898 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3; |
| 899 |
} else { |
| 900 |
throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided'); |
| 901 |
} |
| 902 |
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT === $options['crypto_method']) { |
| 903 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_0; |
| 904 |
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT === $options['crypto_method']) { |
| 905 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_1; |
| 906 |
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT === $options['crypto_method']) { |
| 907 |
if (!self::supportsTls12()) { |
| 908 |
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.2 not supported by your version of cURL'); |
| 909 |
} |
| 910 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2; |
| 911 |
} elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) { |
| 912 |
if (!self::supportsTls13()) { |
| 913 |
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL'); |
| 914 |
} |
| 915 |
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3; |
| 916 |
} else { |
| 917 |
throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided'); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
$certType = null; |
| 922 |
if (isset($options['cert_type'])) { |
| 923 |
$certType = self::normalizeTlsFileType('cert_type', $options['cert_type']); |
| 924 |
$conf[\CURLOPT_SSLCERTTYPE] = $certType; |
| 925 |
} |
| 926 |
|
| 927 |
if (isset($options['cert'])) { |
| 928 |
$cert = $options['cert']; |
| 929 |
if (\is_array($cert)) { |
| 930 |
if (!isset($cert[0]) || !\is_string($cert[0])) { |
| 931 |
throw new \InvalidArgumentException('Invalid cert request option'); |
| 932 |
} |
| 933 |
if (isset($cert[1])) { |
| 934 |
if (!\is_string($cert[1])) { |
| 935 |
throw new \InvalidArgumentException('Invalid cert request option'); |
| 936 |
} |
| 937 |
$conf[\CURLOPT_SSLCERTPASSWD] = $cert[1]; |
| 938 |
} |
| 939 |
$cert = $cert[0]; |
| 940 |
} |
| 941 |
if (!\is_string($cert)) { |
| 942 |
throw new \InvalidArgumentException('Invalid cert request option'); |
| 943 |
} |
| 944 |
if (!\file_exists($cert)) { |
| 945 |
throw new \InvalidArgumentException("SSL certificate not found: {$cert}"); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
$ext = pathinfo($cert, \PATHINFO_EXTENSION); |
| 950 |
if ($certType === null && preg_match('#^(der|p12)$#i', $ext)) { |
| 951 |
$conf[\CURLOPT_SSLCERTTYPE] = strtoupper($ext); |
| 952 |
} |
| 953 |
$conf[\CURLOPT_SSLCERT] = $cert; |
| 954 |
} |
| 955 |
|
| 956 |
$sslKeyType = null; |
| 957 |
if (isset($options['ssl_key_type'])) { |
| 958 |
$sslKeyType = self::normalizeTlsFileType('ssl_key_type', $options['ssl_key_type']); |
| 959 |
$conf[\CURLOPT_SSLKEYTYPE] = $sslKeyType; |
| 960 |
} |
| 961 |
|
| 962 |
if (isset($options['ssl_key'])) { |
| 963 |
if (\is_array($options['ssl_key'])) { |
| 964 |
if (!isset($options['ssl_key'][0]) || !\is_string($options['ssl_key'][0])) { |
| 965 |
throw new \InvalidArgumentException('Invalid ssl_key request option'); |
| 966 |
} |
| 967 |
if (isset($options['ssl_key'][1])) { |
| 968 |
if (!\is_string($options['ssl_key'][1])) { |
| 969 |
throw new \InvalidArgumentException('Invalid ssl_key request option'); |
| 970 |
} |
| 971 |
$conf[\CURLOPT_SSLKEYPASSWD] = $options['ssl_key'][1]; |
| 972 |
} |
| 973 |
$sslKey = $options['ssl_key'][0]; |
| 974 |
} |
| 975 |
|
| 976 |
$sslKey = $sslKey ?? $options['ssl_key']; |
| 977 |
|
| 978 |
if (!\is_string($sslKey)) { |
| 979 |
throw new \InvalidArgumentException('Invalid ssl_key request option'); |
| 980 |
} |
| 981 |
|
| 982 |
if (self::shouldValidateSslKeyFile($sslKeyType) && !\file_exists($sslKey)) { |
| 983 |
throw new \InvalidArgumentException("SSL private key not found: {$sslKey}"); |
| 984 |
} |
| 985 |
$conf[\CURLOPT_SSLKEY] = $sslKey; |
| 986 |
} |
| 987 |
|
| 988 |
if (isset($options['progress'])) { |
| 989 |
$progress = $options['progress']; |
| 990 |
if (!\is_callable($progress)) { |
| 991 |
throw new \InvalidArgumentException('progress client option must be callable'); |
| 992 |
} |
| 993 |
$conf[\CURLOPT_NOPROGRESS] = false; |
| 994 |
$conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) { |
| 995 |
$progress($downloadSize, $downloaded, $uploadSize, $uploaded); |
| 996 |
}; |
| 997 |
} |
| 998 |
|
| 999 |
if (!empty($options['debug'])) { |
| 1000 |
$conf[\CURLOPT_STDERR] = Utils::debugResource($options['debug']); |
| 1001 |
$conf[\CURLOPT_VERBOSE] = true; |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
private static function retryFailedRewind(callable $handler, EasyHandle $easy, array $ctx): PromiseInterface |
| 1017 |
{ |
| 1018 |
try { |
| 1019 |
|
| 1020 |
$body = $easy->request->getBody(); |
| 1021 |
if ($body->tell() > 0) { |
| 1022 |
$body->rewind(); |
| 1023 |
} |
| 1024 |
} catch (\RuntimeException $e) { |
| 1025 |
$ctx['error'] = 'The connection unexpectedly failed without ' |
| 1026 |
.'providing an error. The request would have been retried, ' |
| 1027 |
.'but attempting to rewind the request body failed. ' |
| 1028 |
.'Exception: '.$e; |
| 1029 |
|
| 1030 |
return self::createRejection($easy, $ctx); |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
if (!isset($easy->options['_curl_retries'])) { |
| 1035 |
$easy->options['_curl_retries'] = 1; |
| 1036 |
} elseif ($easy->options['_curl_retries'] == 2) { |
| 1037 |
$ctx['error'] = 'The cURL request was retried 3 times ' |
| 1038 |
.'and did not succeed. The most likely reason for the failure ' |
| 1039 |
.'is that cURL was unable to rewind the body of the request ' |
| 1040 |
.'and subsequent retries resulted in the same error. Turn on ' |
| 1041 |
.'the debug option to see what went wrong. See ' |
| 1042 |
.'https://bugs.php.net/bug.php?id=47204 for more information.'; |
| 1043 |
|
| 1044 |
return self::createRejection($easy, $ctx); |
| 1045 |
} else { |
| 1046 |
++$easy->options['_curl_retries']; |
| 1047 |
} |
| 1048 |
|
| 1049 |
return $handler($easy->request, $easy->options); |
| 1050 |
} |
| 1051 |
|
| 1052 |
private function createHeaderFn(EasyHandle $easy): callable |
| 1053 |
{ |
| 1054 |
if (isset($easy->options['on_headers'])) { |
| 1055 |
$onHeaders = $easy->options['on_headers']; |
| 1056 |
|
| 1057 |
if (!\is_callable($onHeaders)) { |
| 1058 |
throw new \InvalidArgumentException('on_headers must be callable'); |
| 1059 |
} |
| 1060 |
} else { |
| 1061 |
$onHeaders = null; |
| 1062 |
} |
| 1063 |
|
| 1064 |
return static function ($ch, $h) use ( |
| 1065 |
$onHeaders, |
| 1066 |
$easy, |
| 1067 |
&$startingResponse |
| 1068 |
) { |
| 1069 |
$value = \trim($h); |
| 1070 |
if ($value === '') { |
| 1071 |
$startingResponse = true; |
| 1072 |
try { |
| 1073 |
$easy->createResponse(); |
| 1074 |
} catch (\Throwable $e) { |
| 1075 |
$easy->response = null; |
| 1076 |
$easy->createResponseException = $e; |
| 1077 |
|
| 1078 |
return -1; |
| 1079 |
} |
| 1080 |
if ($onHeaders !== null) { |
| 1081 |
try { |
| 1082 |
$onHeaders($easy->response); |
| 1083 |
} catch (\Throwable $e) { |
| 1084 |
|
| 1085 |
|
| 1086 |
$easy->onHeadersException = $e; |
| 1087 |
|
| 1088 |
return -1; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} elseif ($startingResponse) { |
| 1092 |
$startingResponse = false; |
| 1093 |
$easy->headers = [$value]; |
| 1094 |
} else { |
| 1095 |
$easy->headers[] = $value; |
| 1096 |
} |
| 1097 |
|
| 1098 |
return \strlen($h); |
| 1099 |
}; |
| 1100 |
} |
| 1101 |
|
| 1102 |
public function __destruct() |
| 1103 |
{ |
| 1104 |
foreach ($this->handles as $id => $handle) { |
| 1105 |
if (PHP_VERSION_ID < 80000) { |
| 1106 |
\curl_close($handle); |
| 1107 |
} |
| 1108 |
|
| 1109 |
unset($this->handles[$id]); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |