| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Amp\CancelledException; |
| 15 |
use Amp\DeferredFuture; |
| 16 |
use Amp\Http\Client\DelegateHttpClient; |
| 17 |
use Amp\Http\Client\InterceptedHttpClient; |
| 18 |
use Amp\Http\Client\PooledHttpClient; |
| 19 |
use Amp\Http\Client\Request; |
| 20 |
use Amp\Http\HttpMessage; |
| 21 |
use Amp\Http\Tunnel\Http1TunnelConnector; |
| 22 |
use Psr\Log\LoggerAwareInterface; |
| 23 |
use Psr\Log\LoggerAwareTrait; |
| 24 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 25 |
use Symfony\Component\HttpClient\Internal\AmpClientStateV4; |
| 26 |
use Symfony\Component\HttpClient\Internal\AmpClientStateV5; |
| 27 |
use Symfony\Component\HttpClient\Response\AmpResponseV4; |
| 28 |
use Symfony\Component\HttpClient\Response\AmpResponseV5; |
| 29 |
use Symfony\Component\HttpClient\Response\ResponseStream; |
| 30 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 31 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 32 |
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 33 |
use Symfony\Contracts\Service\ResetInterface; |
| 34 |
|
| 35 |
if (!interface_exists(DelegateHttpClient::class)) { |
| 36 |
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\AmpHttpClient" as the "amphp/http-client" package is not installed. Try running "composer require amphp/http-client:^5".'); |
| 37 |
} |
| 38 |
|
| 39 |
if (\PHP_VERSION_ID < 80400 && is_subclass_of(Request::class, HttpMessage::class)) { |
| 40 |
throw new \LogicException('Using "Symfony\Component\HttpClient\AmpHttpClient" with amphp/http-client >= 5 requires PHP >= 8.4. Try running "composer require amphp/http-client:^4.2.1" or upgrade to PHP >= 8.4.'); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
final class AmpHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface |
| 49 |
{ |
| 50 |
use HttpClientTrait; |
| 51 |
use LoggerAwareTrait; |
| 52 |
|
| 53 |
public const OPTIONS_DEFAULTS = HttpClientInterface::OPTIONS_DEFAULTS + [ |
| 54 |
'crypto_method' => \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT, |
| 55 |
]; |
| 56 |
|
| 57 |
private array $defaultOptions = self::OPTIONS_DEFAULTS; |
| 58 |
private static array $emptyDefaults = self::OPTIONS_DEFAULTS; |
| 59 |
private AmpClientStateV4|AmpClientStateV5 $multi; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function __construct(array $defaultOptions = [], ?callable $clientConfigurator = null, int $maxHostConnections = 6, int $maxPendingPushes = 50) |
| 71 |
{ |
| 72 |
$this->defaultOptions['buffer'] ??= self::shouldBuffer(...); |
| 73 |
|
| 74 |
if ($defaultOptions) { |
| 75 |
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions); |
| 76 |
} |
| 77 |
|
| 78 |
if (is_subclass_of(Request::class, HttpMessage::class)) { |
| 79 |
$this->multi = new AmpClientStateV5($clientConfigurator, $maxHostConnections, $maxPendingPushes, $this->logger); |
| 80 |
} else { |
| 81 |
if (\PHP_VERSION_ID >= 80400) { |
| 82 |
trigger_deprecation('symfony/http-client', '7.4', 'Using amphp/http-client < 5 is deprecated. Try running "composer require amphp/http-client:^5".'); |
| 83 |
} |
| 84 |
$this->multi = new AmpClientStateV4($clientConfigurator, $maxHostConnections, $maxPendingPushes, $this->logger); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 92 |
{ |
| 93 |
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions); |
| 94 |
|
| 95 |
$options['proxy'] = self::getProxy($options['proxy'], $url, $options['no_proxy']); |
| 96 |
|
| 97 |
if (null !== $options['proxy'] && !class_exists(Http1TunnelConnector::class)) { |
| 98 |
throw new \LogicException('You cannot use the "proxy" option as the "amphp/http-tunnel" package is not installed. Try running "composer require amphp/http-tunnel".'); |
| 99 |
} |
| 100 |
|
| 101 |
if ($options['bindto']) { |
| 102 |
if (str_starts_with($options['bindto'], 'if!')) { |
| 103 |
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.'); |
| 104 |
} |
| 105 |
if (str_starts_with($options['bindto'], 'host!')) { |
| 106 |
$options['bindto'] = substr($options['bindto'], 5); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (('' !== $options['body'] || 'POST' === $method || isset($options['normalized_headers']['content-length'])) && !isset($options['normalized_headers']['content-type'])) { |
| 111 |
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded'; |
| 112 |
} |
| 113 |
|
| 114 |
if (!isset($options['normalized_headers']['user-agent'])) { |
| 115 |
$options['headers'][] = 'User-Agent: Symfony HttpClient (Amp)'; |
| 116 |
} |
| 117 |
|
| 118 |
if (0 < $options['max_duration']) { |
| 119 |
$options['timeout'] = min($options['max_duration'], $options['timeout']); |
| 120 |
} |
| 121 |
|
| 122 |
if ($options['resolve']) { |
| 123 |
$this->multi->dnsCache = $options['resolve'] + $this->multi->dnsCache; |
| 124 |
} |
| 125 |
|
| 126 |
if ($options['peer_fingerprint'] && !isset($options['peer_fingerprint']['pin-sha256'])) { |
| 127 |
throw new TransportException(__CLASS__.' supports only "pin-sha256" fingerprints.'); |
| 128 |
} |
| 129 |
|
| 130 |
$request = new Request(implode('', $url), $method); |
| 131 |
$request->setBodySizeLimit(0); |
| 132 |
|
| 133 |
if ($options['http_version']) { |
| 134 |
$request->setProtocolVersions(match ((float) $options['http_version']) { |
| 135 |
1.0 => ['1.0'], |
| 136 |
1.1 => ['1.1', '1.0'], |
| 137 |
default => ['2', '1.1', '1.0'], |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
foreach ($options['headers'] as $v) { |
| 142 |
$h = explode(': ', $v, 2); |
| 143 |
$request->addHeader($h[0], $h[1]); |
| 144 |
} |
| 145 |
|
| 146 |
if ($request instanceof HttpMessage) { |
| 147 |
$request->setTcpConnectTimeout($options['timeout']); |
| 148 |
$request->setTlsHandshakeTimeout($options['timeout']); |
| 149 |
$request->setTransferTimeout($options['max_duration']); |
| 150 |
} else { |
| 151 |
$request->setTcpConnectTimeout(ceil(1000 * $options['timeout'])); |
| 152 |
$request->setTlsHandshakeTimeout(ceil(1000 * $options['timeout'])); |
| 153 |
$request->setTransferTimeout(ceil(1000 * $options['max_duration'])); |
| 154 |
} |
| 155 |
if (method_exists($request, 'setInactivityTimeout')) { |
| 156 |
$request->setInactivityTimeout(0); |
| 157 |
} |
| 158 |
|
| 159 |
if ('' !== $request->getUri()->getUserInfo() && !$request->hasHeader('authorization')) { |
| 160 |
$auth = explode(':', $request->getUri()->getUserInfo(), 2); |
| 161 |
$auth = array_map('rawurldecode', $auth) + [1 => '']; |
| 162 |
$request->setHeader('Authorization', 'Basic '.base64_encode(implode(':', $auth))); |
| 163 |
} |
| 164 |
|
| 165 |
if ($request instanceof HttpMessage) { |
| 166 |
return new AmpResponseV5($this->multi, $request, $options, $this->logger); |
| 167 |
} |
| 168 |
|
| 169 |
return new AmpResponseV4($this->multi, $request, $options, $this->logger); |
| 170 |
} |
| 171 |
|
| 172 |
public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface |
| 173 |
{ |
| 174 |
if ($responses instanceof AmpResponseV4 || $responses instanceof AmpResponseV5) { |
| 175 |
$responses = [$responses]; |
| 176 |
} |
| 177 |
|
| 178 |
if ($this->multi instanceof AmpClientStateV5) { |
| 179 |
return new ResponseStream(AmpResponseV5::stream($responses, $timeout)); |
| 180 |
} |
| 181 |
|
| 182 |
return new ResponseStream(AmpResponseV4::stream($responses, $timeout)); |
| 183 |
} |
| 184 |
|
| 185 |
public function reset(): void |
| 186 |
{ |
| 187 |
$this->multi->dnsCache = []; |
| 188 |
|
| 189 |
foreach ($this->multi->pushedResponses as $pushedResponses) { |
| 190 |
foreach ($pushedResponses as [$pushedUrl, $pushDeferred]) { |
| 191 |
if ($pushDeferred instanceof DeferredFuture) { |
| 192 |
$pushDeferred->error(new CancelledException()); |
| 193 |
} else { |
| 194 |
$pushDeferred->fail(new CancelledException()); |
| 195 |
} |
| 196 |
|
| 197 |
$this->logger?->debug(\sprintf('Unused pushed response: "%s"', $pushedUrl)); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
$this->multi->pushedResponses = []; |
| 202 |
} |
| 203 |
} |