| 1 |
<?php |
| 2 |
|
| 3 |
namespace GuzzleHttp\Handler; |
| 4 |
|
| 5 |
use GuzzleHttp\TransportSharing; |
| 6 |
use GuzzleHttp\Utils; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
final class CurlShareHandleState |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public $handle; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public $mode; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private function __construct(string $mode, $handle) |
| 27 |
{ |
| 28 |
$this->mode = $mode; |
| 29 |
$this->handle = $handle; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static function fromOption($sharing): ?self |
| 36 |
{ |
| 37 |
if ($sharing instanceof self) { |
| 38 |
return $sharing; |
| 39 |
} |
| 40 |
|
| 41 |
$mode = self::normalizeMode($sharing, 'transport_sharing'); |
| 42 |
if ($mode === TransportSharing::NONE) { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
if ($mode === TransportSharing::HANDLER_PREFER) { |
| 47 |
return self::createHandlerShareOrNull($mode); |
| 48 |
} |
| 49 |
|
| 50 |
return self::createHandlerShare($mode); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function normalizeMode($sharing, string $option): string |
| 57 |
{ |
| 58 |
if ($sharing instanceof self) { |
| 59 |
return $sharing->mode; |
| 60 |
} |
| 61 |
|
| 62 |
if ($sharing === null || $sharing === TransportSharing::NONE) { |
| 63 |
return TransportSharing::NONE; |
| 64 |
} |
| 65 |
|
| 66 |
if ($sharing === TransportSharing::HANDLER_PREFER || $sharing === TransportSharing::HANDLER_REQUIRE) { |
| 67 |
return $sharing; |
| 68 |
} |
| 69 |
|
| 70 |
throw new \InvalidArgumentException(\sprintf( |
| 71 |
'The "%s" option must be null or a GuzzleHttp\\TransportSharing::* constant; received %s.', |
| 72 |
$option, |
| 73 |
Utils::describeType($sharing) |
| 74 |
)); |
| 75 |
} |
| 76 |
|
| 77 |
public static function assertNoRequiredSharingCustomFactoryConflict(array $options, string $handlerName): void |
| 78 |
{ |
| 79 |
if (!\array_key_exists('handle_factory', $options) || $options['handle_factory'] === null) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
$mode = self::normalizeMode($options['transport_sharing'] ?? null, 'transport_sharing'); |
| 84 |
if ($mode !== TransportSharing::HANDLER_REQUIRE) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
throw new \InvalidArgumentException(\sprintf( |
| 89 |
'The "transport_sharing" %s option cannot require sharing with a custom "handle_factory" because Guzzle cannot ensure that the custom factory applies CURLOPT_SHARE.', |
| 90 |
$handlerName |
| 91 |
)); |
| 92 |
} |
| 93 |
|
| 94 |
private static function createHandlerShareOrNull(string $mode): ?self |
| 95 |
{ |
| 96 |
try { |
| 97 |
return self::createHandlerShare($mode); |
| 98 |
} catch (\Throwable $e) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
private static function createHandlerShare(string $mode): self |
| 104 |
{ |
| 105 |
if (!\function_exists('curl_share_init') || !\function_exists('curl_share_setopt')) { |
| 106 |
throw new \InvalidArgumentException('The "transport_sharing" option requires cURL share support.'); |
| 107 |
} |
| 108 |
|
| 109 |
self::requireCurlConstant('CURLOPT_SHARE'); |
| 110 |
$shareOption = self::requireCurlConstant('CURLSHOPT_SHARE'); |
| 111 |
$locks = self::handlerLocks(); |
| 112 |
$handle = curl_share_init(); |
| 113 |
|
| 114 |
try { |
| 115 |
foreach ($locks as $lock) { |
| 116 |
try { |
| 117 |
$success = curl_share_setopt($handle, $shareOption, $lock); |
| 118 |
} catch (\Throwable $e) { |
| 119 |
throw new \InvalidArgumentException('Unable to configure cURL share handle: '.$e->getMessage(), 0, $e); |
| 120 |
} |
| 121 |
|
| 122 |
if (!$success) { |
| 123 |
throw new \InvalidArgumentException(\sprintf('Unable to configure cURL share handle with lock data %d.', $lock)); |
| 124 |
} |
| 125 |
} |
| 126 |
} catch (\Throwable $e) { |
| 127 |
self::closeHandlerShareHandleOnPhp7($handle); |
| 128 |
|
| 129 |
throw $e; |
| 130 |
} |
| 131 |
|
| 132 |
return new self($mode, $handle); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private static function handlerLocks(): array |
| 139 |
{ |
| 140 |
return [ |
| 141 |
self::requireCurlConstant('CURL_LOCK_DATA_DNS'), |
| 142 |
self::requireCurlConstant('CURL_LOCK_DATA_SSL_SESSION'), |
| 143 |
]; |
| 144 |
} |
| 145 |
|
| 146 |
private static function requireCurlConstant(string $constant): int |
| 147 |
{ |
| 148 |
if (!\defined($constant)) { |
| 149 |
throw new \InvalidArgumentException(\sprintf( |
| 150 |
'The "transport_sharing" option requires %s, but it is not available in the installed PHP cURL extension.', |
| 151 |
$constant |
| 152 |
)); |
| 153 |
} |
| 154 |
|
| 155 |
$value = \constant($constant); |
| 156 |
if (!\is_int($value)) { |
| 157 |
throw new \InvalidArgumentException(\sprintf('The cURL constant %s must resolve to an integer.', $constant)); |
| 158 |
} |
| 159 |
|
| 160 |
return $value; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private static function closeHandlerShareHandleOnPhp7($handle): void |
| 167 |
{ |
| 168 |
if (\PHP_VERSION_ID < 80000 && \is_resource($handle)) { |
| 169 |
curl_share_close($handle); |
| 170 |
} |
| 171 |
} |
| 172 |
} |