Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/Internal/HttplugWaitLoop.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 Http\Client\Exception\NetworkException;
15 use Http\Promise\Promise;
16 use Psr\Http\Message\RequestInterface as Psr7RequestInterface;
17 use Psr\Http\Message\ResponseFactoryInterface;
18 use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
19 use Psr\Http\Message\StreamFactoryInterface;
20 use Symfony\Component\HttpClient\Response\StreamableInterface;
21 use Symfony\Component\HttpClient\Response\StreamWrapper;
22 use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23 use Symfony\Contracts\HttpClient\HttpClientInterface;
24 use Symfony\Contracts\HttpClient\ResponseInterface;
25
26 /**
27 * @author Nicolas Grekas <[email protected]>
28 *
29 * @internal
30 */
31 final class HttplugWaitLoop
32 {
33 /**
34 * @param \SplObjectStorage<ResponseInterface, array{Psr7RequestInterface, Promise}>|null $promisePool
35 */
36 public function __construct(
37 private HttpClientInterface $client,
38 private ?\SplObjectStorage $promisePool,
39 private ResponseFactoryInterface $responseFactory,
40 private StreamFactoryInterface $streamFactory,
41 ) {
42 }
43
44 public function wait(?ResponseInterface $pendingResponse, ?float $maxDuration = null, ?float $idleTimeout = null): int
45 {
46 if (!$this->promisePool) {
47 return 0;
48 }
49
50 $guzzleQueue = \GuzzleHttp\Promise\Utils::queue();
51
52 if (0.0 === $remainingDuration = $maxDuration) {
53 $idleTimeout = 0.0;
54 } elseif (null !== $maxDuration) {
55 $startTime = hrtime(true) / 1E9;
56 $idleTimeout = max(0.0, min($maxDuration / 5, $idleTimeout ?? $maxDuration));
57 }
58
59 do {
60 foreach ($this->client->stream($this->promisePool, $idleTimeout) as $response => $chunk) {
61 try {
62 if (null !== $maxDuration && $chunk->isTimeout()) {
63 goto check_duration;
64 }
65
66 if ($chunk->isFirst()) {
67 // Deactivate throwing on 3/4/5xx
68 $response->getStatusCode();
69 }
70
71 if (!$chunk->isLast()) {
72 goto check_duration;
73 }
74
75 if ([, $promise] = $this->promisePool[$response] ?? null) {
76 unset($this->promisePool[$response]);
77 $promise->resolve(self::createPsr7Response($this->responseFactory, $this->streamFactory, $this->client, $response, true));
78 }
79 } catch (\Exception $e) {
80 if ([$request, $promise] = $this->promisePool[$response] ?? null) {
81 unset($this->promisePool[$response]);
82
83 if ($e instanceof TransportExceptionInterface) {
84 $e = new NetworkException($e->getMessage(), $request, $e);
85 }
86
87 $promise->reject($e);
88 }
89 }
90
91 $guzzleQueue->run();
92
93 if ($pendingResponse === $response) {
94 return $this->promisePool->count();
95 }
96
97 check_duration:
98 if (null !== $maxDuration && $idleTimeout && $idleTimeout > $remainingDuration = max(0.0, $maxDuration - hrtime(true) / 1E9 + $startTime)) {
99 $idleTimeout = $remainingDuration / 5;
100 break;
101 }
102 }
103
104 if (!$count = $this->promisePool->count()) {
105 return 0;
106 }
107 } while (null === $maxDuration || 0 < $remainingDuration);
108
109 return $count;
110 }
111
112 public static function createPsr7Response(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory, HttpClientInterface $client, ResponseInterface $response, bool $buffer): Psr7ResponseInterface
113 {
114 $responseParameters = [$response->getStatusCode()];
115
116 foreach ($response->getInfo('response_headers') as $h) {
117 if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? (?:\d\d\d) (.+)#', $h, $m)) {
118 $responseParameters[1] = $m[1];
119 }
120 }
121
122 $psrResponse = $responseFactory->createResponse(...$responseParameters);
123
124 foreach ($response->getHeaders(false) as $name => $values) {
125 foreach ($values as $value) {
126 try {
127 $psrResponse = $psrResponse->withAddedHeader($name, $value);
128 } catch (\InvalidArgumentException $e) {
129 // ignore invalid header
130 }
131 }
132 }
133
134 if ($response instanceof StreamableInterface) {
135 $body = $streamFactory->createStreamFromResource($response->toStream(false));
136 } elseif (!$buffer) {
137 $body = $streamFactory->createStreamFromResource(StreamWrapper::createResource($response, $client));
138 } else {
139 $body = $streamFactory->createStream($response->getContent(false));
140 }
141
142 if ($body->isSeekable()) {
143 try {
144 $body->seek(0);
145 } catch (\RuntimeException) {
146 // ignore
147 }
148 }
149
150 return $psrResponse->withBody($body);
151 }
152 }