| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Response; |
| 13 |
|
| 14 |
use Psr\Log\LoggerInterface; |
| 15 |
use Symfony\Component\HttpClient\Chunk\DataChunk; |
| 16 |
use Symfony\Component\HttpClient\Chunk\ErrorChunk; |
| 17 |
use Symfony\Component\HttpClient\Chunk\FirstChunk; |
| 18 |
use Symfony\Component\HttpClient\Chunk\LastChunk; |
| 19 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 20 |
use Symfony\Component\HttpClient\Internal\Canary; |
| 21 |
use Symfony\Component\HttpClient\Internal\ClientState; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
trait TransportResponseTrait |
| 31 |
{ |
| 32 |
private Canary $canary; |
| 33 |
|
| 34 |
private array $headers = []; |
| 35 |
private array $info = [ |
| 36 |
'response_headers' => [], |
| 37 |
'http_code' => 0, |
| 38 |
'error' => null, |
| 39 |
'canceled' => false, |
| 40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
private $handle; |
| 44 |
private int|string $id; |
| 45 |
private ?float $timeout = 0; |
| 46 |
private \InflateContext|bool|null $inflate = null; |
| 47 |
private ?array $finalInfo = null; |
| 48 |
private ?LoggerInterface $logger = null; |
| 49 |
private bool $didTimeout = false; |
| 50 |
|
| 51 |
public function getStatusCode(): int |
| 52 |
{ |
| 53 |
if ($this->initializer) { |
| 54 |
self::initialize($this); |
| 55 |
} |
| 56 |
|
| 57 |
return $this->info['http_code']; |
| 58 |
} |
| 59 |
|
| 60 |
public function getHeaders(bool $throw = true): array |
| 61 |
{ |
| 62 |
if ($this->initializer) { |
| 63 |
self::initialize($this); |
| 64 |
} |
| 65 |
|
| 66 |
if ($throw) { |
| 67 |
$this->checkStatusCode(); |
| 68 |
} |
| 69 |
|
| 70 |
return $this->headers; |
| 71 |
} |
| 72 |
|
| 73 |
public function cancel(): void |
| 74 |
{ |
| 75 |
$this->info['canceled'] = true; |
| 76 |
$this->info['error'] = 'Response has been canceled.'; |
| 77 |
$this->close(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected function close(): void |
| 84 |
{ |
| 85 |
$this->canary->cancel(); |
| 86 |
$this->inflate = null; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
abstract protected static function schedule(self $response, array &$runningResponses): void; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
abstract protected static function perform(ClientState $multi, array $responses): void; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
abstract protected static function select(ClientState $multi, float $timeout): int; |
| 103 |
|
| 104 |
private static function addResponseHeaders(array $responseHeaders, array &$info, array &$headers, string &$debug = ''): void |
| 105 |
{ |
| 106 |
foreach ($responseHeaders as $h) { |
| 107 |
if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? (\d\d\d)(?: |$)#', $h, $m)) { |
| 108 |
if ($headers) { |
| 109 |
$debug .= "< \r\n"; |
| 110 |
$headers = []; |
| 111 |
} |
| 112 |
$info['http_code'] = (int) $m[1]; |
| 113 |
} elseif (2 === \count($m = explode(':', $h, 2))) { |
| 114 |
$headers[strtolower($m[0])][] = ltrim($m[1]); |
| 115 |
} |
| 116 |
|
| 117 |
$debug .= "< {$h}\r\n"; |
| 118 |
$info['response_headers'][] = $h; |
| 119 |
} |
| 120 |
|
| 121 |
$debug .= "< \r\n"; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
private function doDestruct(): void |
| 128 |
{ |
| 129 |
$this->shouldBuffer = true; |
| 130 |
|
| 131 |
if ($this->initializer && null === $this->info['error'] && !$this->didTimeout) { |
| 132 |
self::initialize($this); |
| 133 |
$this->checkStatusCode(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public static function stream(iterable $responses, ?float $timeout = null): \Generator |
| 145 |
{ |
| 146 |
$runningResponses = []; |
| 147 |
|
| 148 |
foreach ($responses as $response) { |
| 149 |
self::schedule($response, $runningResponses); |
| 150 |
} |
| 151 |
|
| 152 |
$lastActivity = hrtime(true) / 1E9; |
| 153 |
$elapsedTimeout = 0; |
| 154 |
|
| 155 |
if ((0.0 === $timeout && '-0' === (string) $timeout) || 0 > $timeout) { |
| 156 |
$timeout = $timeout ? -$timeout : null; |
| 157 |
|
| 158 |
|
| 159 |
foreach ($runningResponses as [$multi]) { |
| 160 |
if (null !== $multi->lastTimeout) { |
| 161 |
$elapsedTimeout = max($elapsedTimeout, $lastActivity - $multi->lastTimeout); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
while (true) { |
| 167 |
$hasActivity = false; |
| 168 |
$timeoutMax = 0; |
| 169 |
$timeoutMin = $timeout ?? \INF; |
| 170 |
|
| 171 |
|
| 172 |
foreach ($runningResponses as $i => [$multi, &$responses]) { |
| 173 |
self::perform($multi, $responses); |
| 174 |
|
| 175 |
foreach ($responses as $j => $response) { |
| 176 |
$timeoutMax = $timeout ?? max($timeoutMax, $response->timeout); |
| 177 |
$timeoutMin = min($timeoutMin, $response->timeout, 1); |
| 178 |
$chunk = false; |
| 179 |
|
| 180 |
if (isset($multi->handlesActivity[$j])) { |
| 181 |
$multi->lastTimeout = null; |
| 182 |
$elapsedTimeout = 0; |
| 183 |
} elseif (!isset($multi->openHandles[$j])) { |
| 184 |
$hasActivity = true; |
| 185 |
unset($responses[$j]); |
| 186 |
continue; |
| 187 |
} elseif ($elapsedTimeout >= $timeoutMax) { |
| 188 |
$response->didTimeout = true; |
| 189 |
$multi->handlesActivity[$j] = [new ErrorChunk($response->offset, \sprintf('Idle timeout reached for "%s".', $response->getInfo('url')))]; |
| 190 |
$multi->lastTimeout ??= $lastActivity; |
| 191 |
$elapsedTimeout = $timeoutMax; |
| 192 |
} else { |
| 193 |
continue; |
| 194 |
} |
| 195 |
|
| 196 |
$lastActivity = null; |
| 197 |
$hasActivity = true; |
| 198 |
|
| 199 |
while ($multi->handlesActivity[$j] ?? false) { |
| 200 |
if (\is_string($chunk = array_shift($multi->handlesActivity[$j]))) { |
| 201 |
if (null !== $response->inflate && false === $chunk = @inflate_add($response->inflate, $chunk)) { |
| 202 |
$multi->handlesActivity[$j] = [null, new TransportException(\sprintf('Error while processing content unencoding for "%s".', $response->getInfo('url')))]; |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
if ('' !== $chunk && null !== $response->content && \strlen($chunk) !== fwrite($response->content, $chunk)) { |
| 207 |
$multi->handlesActivity[$j] = [null, new TransportException(\sprintf('Failed writing %d bytes to the response buffer.', \strlen($chunk)))]; |
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
$chunkLen = \strlen($chunk); |
| 212 |
$chunk = new DataChunk($response->offset, $chunk); |
| 213 |
$response->offset += $chunkLen; |
| 214 |
} elseif (null === $chunk) { |
| 215 |
$e = $multi->handlesActivity[$j][0]; |
| 216 |
unset($responses[$j], $multi->handlesActivity[$j]); |
| 217 |
$response->close(); |
| 218 |
|
| 219 |
if (null !== $e) { |
| 220 |
$response->info['error'] = $e->getMessage(); |
| 221 |
|
| 222 |
if ($e instanceof \Error) { |
| 223 |
throw $e; |
| 224 |
} |
| 225 |
|
| 226 |
$chunk = new ErrorChunk($response->offset, $e); |
| 227 |
} else { |
| 228 |
if (0 === $response->offset && null === $response->content) { |
| 229 |
$response->content = fopen('php://memory', 'w+'); |
| 230 |
} |
| 231 |
|
| 232 |
$chunk = new LastChunk($response->offset); |
| 233 |
} |
| 234 |
} elseif ($chunk instanceof ErrorChunk) { |
| 235 |
unset($responses[$j]); |
| 236 |
} elseif ($chunk instanceof FirstChunk) { |
| 237 |
if ($response->logger) { |
| 238 |
$info = $response->getInfo(); |
| 239 |
$response->logger->info('Response: "{http_code} {url}" {total_time} seconds', [ |
| 240 |
'http_code' => $info['http_code'], |
| 241 |
'url' => $info['url'], |
| 242 |
'total_time' => $info['total_time'], |
| 243 |
]); |
| 244 |
} |
| 245 |
|
| 246 |
$response->inflate = \extension_loaded('zlib') && $response->inflate && 'gzip' === ($response->headers['content-encoding'][0] ?? null) ? inflate_init(\ZLIB_ENCODING_GZIP) : null; |
| 247 |
|
| 248 |
if ($response->shouldBuffer instanceof \Closure) { |
| 249 |
try { |
| 250 |
$response->shouldBuffer = ($response->shouldBuffer)($response->headers); |
| 251 |
|
| 252 |
if (null !== $response->info['error']) { |
| 253 |
throw new TransportException($response->info['error']); |
| 254 |
} |
| 255 |
} catch (\Throwable $e) { |
| 256 |
$response->close(); |
| 257 |
$multi->handlesActivity[$j] = [null, $e]; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (true === $response->shouldBuffer) { |
| 262 |
$response->content = fopen('php://temp', 'w+'); |
| 263 |
} elseif (\is_resource($response->shouldBuffer)) { |
| 264 |
$response->content = $response->shouldBuffer; |
| 265 |
} |
| 266 |
$response->shouldBuffer = null; |
| 267 |
|
| 268 |
yield $response => $chunk; |
| 269 |
|
| 270 |
if ($response->initializer && null === $response->info['error']) { |
| 271 |
|
| 272 |
$response->getHeaders(true); |
| 273 |
} |
| 274 |
|
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
yield $response => $chunk; |
| 279 |
} |
| 280 |
|
| 281 |
unset($multi->handlesActivity[$j]); |
| 282 |
|
| 283 |
if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) { |
| 284 |
|
| 285 |
$chunk->getContent(); |
| 286 |
throw new \LogicException('A transport exception should have been thrown.'); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (!$responses) { |
| 291 |
$hasActivity = true; |
| 292 |
unset($runningResponses[$i]); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
$multi->handlesActivity = $multi->handlesActivity ?: []; |
| 297 |
$multi->openHandles = $multi->openHandles ?: []; |
| 298 |
} |
| 299 |
|
| 300 |
if (!$runningResponses) { |
| 301 |
break; |
| 302 |
} |
| 303 |
|
| 304 |
if ($hasActivity) { |
| 305 |
$lastActivity ??= hrtime(true) / 1E9; |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
if (-1 === self::select($multi, min($timeoutMin, max(0, $timeoutMax - $elapsedTimeout)))) { |
| 310 |
usleep((int) min(500, 1E6 * $timeoutMin)); |
| 311 |
} |
| 312 |
|
| 313 |
$elapsedTimeout = hrtime(true) / 1E9 - $lastActivity; |
| 314 |
} |
| 315 |
} |
| 316 |
} |