AsyncResponse.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient\Response;
  11. use Symfony\Component\HttpClient\Chunk\ErrorChunk;
  12. use Symfony\Component\HttpClient\Chunk\FirstChunk;
  13. use Symfony\Component\HttpClient\Chunk\LastChunk;
  14. use Symfony\Component\HttpClient\Exception\TransportException;
  15. use Symfony\Contracts\HttpClient\ChunkInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. use Symfony\Contracts\HttpClient\ResponseInterface;
  21. /**
  22. * Provides a single extension point to process a response's content stream.
  23. *
  24. * @author Nicolas Grekas <p@tchwork.com>
  25. */
  26. final class AsyncResponse implements ResponseInterface, StreamableInterface
  27. {
  28. use CommonResponseTrait;
  29. private $client;
  30. private $response;
  31. private $info = ['canceled' => false];
  32. private $passthru;
  33. private $stream;
  34. private $lastYielded = false;
  35. /**
  36. * @param ?callable(ChunkInterface, AsyncContext): ?\Iterator $passthru
  37. */
  38. public function __construct(HttpClientInterface $client, string $method, string $url, array $options, callable $passthru = null)
  39. {
  40. $this->client = $client;
  41. $this->shouldBuffer = $options['buffer'] ?? true;
  42. if (null !== $onProgress = $options['on_progress'] ?? null) {
  43. $thisInfo = &$this->info;
  44. $options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) {
  45. $onProgress($dlNow, $dlSize, $thisInfo + $info);
  46. };
  47. }
  48. $this->response = $client->request($method, $url, ['buffer' => false] + $options);
  49. $this->passthru = $passthru;
  50. $this->initializer = static function (self $response) {
  51. if (null === $response->shouldBuffer) {
  52. return false;
  53. }
  54. while (true) {
  55. foreach (self::stream([$response]) as $chunk) {
  56. if ($chunk->isTimeout() && $response->passthru) {
  57. foreach (self::passthru($response->client, $response, new ErrorChunk($response->offset, new TransportException($chunk->getError()))) as $chunk) {
  58. if ($chunk->isFirst()) {
  59. return false;
  60. }
  61. }
  62. continue 2;
  63. }
  64. if ($chunk->isFirst()) {
  65. return false;
  66. }
  67. }
  68. return false;
  69. }
  70. };
  71. if (\array_key_exists('user_data', $options)) {
  72. $this->info['user_data'] = $options['user_data'];
  73. }
  74. }
  75. public function getStatusCode(): int
  76. {
  77. if ($this->initializer) {
  78. self::initialize($this);
  79. }
  80. return $this->response->getStatusCode();
  81. }
  82. public function getHeaders(bool $throw = true): array
  83. {
  84. if ($this->initializer) {
  85. self::initialize($this);
  86. }
  87. $headers = $this->response->getHeaders(false);
  88. if ($throw) {
  89. $this->checkStatusCode();
  90. }
  91. return $headers;
  92. }
  93. public function getInfo(string $type = null)
  94. {
  95. if (null !== $type) {
  96. return $this->info[$type] ?? $this->response->getInfo($type);
  97. }
  98. return $this->info + $this->response->getInfo();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function toStream(bool $throw = true)
  104. {
  105. if ($throw) {
  106. // Ensure headers arrived
  107. $this->getHeaders(true);
  108. }
  109. $handle = function () {
  110. $stream = $this->response instanceof StreamableInterface ? $this->response->toStream(false) : StreamWrapper::createResource($this->response);
  111. return stream_get_meta_data($stream)['wrapper_data']->stream_cast(\STREAM_CAST_FOR_SELECT);
  112. };
  113. $stream = StreamWrapper::createResource($this);
  114. stream_get_meta_data($stream)['wrapper_data']
  115. ->bindHandles($handle, $this->content);
  116. return $stream;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function cancel(): void
  122. {
  123. if ($this->info['canceled']) {
  124. return;
  125. }
  126. $this->info['canceled'] = true;
  127. $this->info['error'] = 'Response has been canceled.';
  128. $this->close();
  129. $client = $this->client;
  130. $this->client = null;
  131. if (!$this->passthru) {
  132. return;
  133. }
  134. try {
  135. foreach (self::passthru($client, $this, new LastChunk()) as $chunk) {
  136. // no-op
  137. }
  138. $this->passthru = null;
  139. } catch (ExceptionInterface $e) {
  140. // ignore any errors when canceling
  141. }
  142. }
  143. public function __destruct()
  144. {
  145. $httpException = null;
  146. if ($this->initializer && null === $this->getInfo('error')) {
  147. try {
  148. $this->getHeaders(true);
  149. } catch (HttpExceptionInterface $httpException) {
  150. // no-op
  151. }
  152. }
  153. if ($this->passthru && null === $this->getInfo('error')) {
  154. $this->info['canceled'] = true;
  155. try {
  156. foreach (self::passthru($this->client, $this, new LastChunk()) as $chunk) {
  157. // no-op
  158. }
  159. } catch (ExceptionInterface $e) {
  160. // ignore any errors when destructing
  161. }
  162. }
  163. if (null !== $httpException) {
  164. throw $httpException;
  165. }
  166. }
  167. /**
  168. * @internal
  169. */
  170. public static function stream(iterable $responses, float $timeout = null, string $class = null): \Generator
  171. {
  172. while ($responses) {
  173. $wrappedResponses = [];
  174. $asyncMap = new \SplObjectStorage();
  175. $client = null;
  176. foreach ($responses as $r) {
  177. if (!$r instanceof self) {
  178. throw new \TypeError(sprintf('"%s::stream()" expects parameter 1 to be an iterable of AsyncResponse objects, "%s" given.', $class ?? static::class, get_debug_type($r)));
  179. }
  180. if (null !== $e = $r->info['error'] ?? null) {
  181. yield $r => $chunk = new ErrorChunk($r->offset, new TransportException($e));
  182. $chunk->didThrow() ?: $chunk->getContent();
  183. continue;
  184. }
  185. if (null === $client) {
  186. $client = $r->client;
  187. } elseif ($r->client !== $client) {
  188. throw new TransportException('Cannot stream AsyncResponse objects with many clients.');
  189. }
  190. $asyncMap[$r->response] = $r;
  191. $wrappedResponses[] = $r->response;
  192. if ($r->stream) {
  193. yield from self::passthruStream($response = $r->response, $r, new FirstChunk(), $asyncMap);
  194. if (!isset($asyncMap[$response])) {
  195. array_pop($wrappedResponses);
  196. }
  197. if ($r->response !== $response && !isset($asyncMap[$r->response])) {
  198. $asyncMap[$r->response] = $r;
  199. $wrappedResponses[] = $r->response;
  200. }
  201. }
  202. }
  203. if (!$client) {
  204. return;
  205. }
  206. foreach ($client->stream($wrappedResponses, $timeout) as $response => $chunk) {
  207. $r = $asyncMap[$response];
  208. if (null === $chunk->getError()) {
  209. if ($chunk->isFirst()) {
  210. // Ensure no exception is thrown on destruct for the wrapped response
  211. $r->response->getStatusCode();
  212. } elseif (0 === $r->offset && null === $r->content && $chunk->isLast()) {
  213. $r->content = fopen('php://memory', 'w+');
  214. }
  215. }
  216. if (!$r->passthru) {
  217. if (null !== $chunk->getError() || $chunk->isLast()) {
  218. unset($asyncMap[$response]);
  219. } elseif (null !== $r->content && '' !== ($content = $chunk->getContent()) && \strlen($content) !== fwrite($r->content, $content)) {
  220. $chunk = new ErrorChunk($r->offset, new TransportException(sprintf('Failed writing %d bytes to the response buffer.', \strlen($content))));
  221. $r->info['error'] = $chunk->getError();
  222. $r->response->cancel();
  223. }
  224. yield $r => $chunk;
  225. continue;
  226. }
  227. foreach (self::passthru($r->client, $r, $chunk, $asyncMap) as $chunk) {
  228. yield $r => $chunk;
  229. }
  230. if ($r->response !== $response && isset($asyncMap[$response])) {
  231. break;
  232. }
  233. }
  234. if (null === $chunk->getError() && $chunk->isLast()) {
  235. $r->lastYielded = true;
  236. }
  237. if (null === $chunk->getError() && !$r->lastYielded && $r->response === $response && null !== $r->client) {
  238. throw new \LogicException('A chunk passthru must yield an "isLast()" chunk before ending a stream.');
  239. }
  240. $responses = [];
  241. foreach ($asyncMap as $response) {
  242. $r = $asyncMap[$response];
  243. if (null !== $r->client) {
  244. $responses[] = $asyncMap[$response];
  245. }
  246. }
  247. }
  248. }
  249. private static function passthru(HttpClientInterface $client, self $r, ChunkInterface $chunk, \SplObjectStorage $asyncMap = null): \Generator
  250. {
  251. $r->stream = null;
  252. $response = $r->response;
  253. $context = new AsyncContext($r->passthru, $client, $r->response, $r->info, $r->content, $r->offset);
  254. if (null === $stream = ($r->passthru)($chunk, $context)) {
  255. if ($r->response === $response && (null !== $chunk->getError() || $chunk->isLast())) {
  256. throw new \LogicException('A chunk passthru cannot swallow the last chunk.');
  257. }
  258. return;
  259. }
  260. if (!$stream instanceof \Iterator) {
  261. throw new \LogicException(sprintf('A chunk passthru must return an "Iterator", "%s" returned.', get_debug_type($stream)));
  262. }
  263. $r->stream = $stream;
  264. yield from self::passthruStream($response, $r, null, $asyncMap);
  265. }
  266. private static function passthruStream(ResponseInterface $response, self $r, ?ChunkInterface $chunk, ?\SplObjectStorage $asyncMap): \Generator
  267. {
  268. while (true) {
  269. try {
  270. if (null !== $chunk && $r->stream) {
  271. $r->stream->next();
  272. }
  273. if (!$r->stream || !$r->stream->valid() || !$r->stream) {
  274. $r->stream = null;
  275. break;
  276. }
  277. } catch (\Throwable $e) {
  278. unset($asyncMap[$response]);
  279. $r->stream = null;
  280. $r->info['error'] = $e->getMessage();
  281. $r->response->cancel();
  282. yield $r => $chunk = new ErrorChunk($r->offset, $e);
  283. $chunk->didThrow() ?: $chunk->getContent();
  284. break;
  285. }
  286. $chunk = $r->stream->current();
  287. if (!$chunk instanceof ChunkInterface) {
  288. throw new \LogicException(sprintf('A chunk passthru must yield instances of "%s", "%s" yielded.', ChunkInterface::class, get_debug_type($chunk)));
  289. }
  290. if (null !== $chunk->getError()) {
  291. // no-op
  292. } elseif ($chunk->isFirst()) {
  293. $e = $r->openBuffer();
  294. yield $r => $chunk;
  295. if ($r->initializer && null === $r->getInfo('error')) {
  296. // Ensure the HTTP status code is always checked
  297. $r->getHeaders(true);
  298. }
  299. if (null === $e) {
  300. continue;
  301. }
  302. $r->response->cancel();
  303. $chunk = new ErrorChunk($r->offset, $e);
  304. } elseif ('' !== $content = $chunk->getContent()) {
  305. if (null !== $r->shouldBuffer) {
  306. throw new \LogicException('A chunk passthru must yield an "isFirst()" chunk before any content chunk.');
  307. }
  308. if (null !== $r->content && \strlen($content) !== fwrite($r->content, $content)) {
  309. $chunk = new ErrorChunk($r->offset, new TransportException(sprintf('Failed writing %d bytes to the response buffer.', \strlen($content))));
  310. $r->info['error'] = $chunk->getError();
  311. $r->response->cancel();
  312. }
  313. }
  314. if (null !== $chunk->getError() || $chunk->isLast()) {
  315. $stream = $r->stream;
  316. $r->stream = null;
  317. unset($asyncMap[$response]);
  318. }
  319. if (null === $chunk->getError()) {
  320. $r->offset += \strlen($content);
  321. yield $r => $chunk;
  322. if (!$chunk->isLast()) {
  323. continue;
  324. }
  325. $stream->next();
  326. if ($stream->valid()) {
  327. throw new \LogicException('A chunk passthru cannot yield after an "isLast()" chunk.');
  328. }
  329. $r->passthru = null;
  330. } else {
  331. if ($chunk instanceof ErrorChunk) {
  332. $chunk->didThrow(false);
  333. } else {
  334. try {
  335. $chunk = new ErrorChunk($chunk->getOffset(), !$chunk->isTimeout() ?: $chunk->getError());
  336. } catch (TransportExceptionInterface $e) {
  337. $chunk = new ErrorChunk($chunk->getOffset(), $e);
  338. }
  339. }
  340. yield $r => $chunk;
  341. $chunk->didThrow() ?: $chunk->getContent();
  342. }
  343. break;
  344. }
  345. }
  346. private function openBuffer(): ?\Throwable
  347. {
  348. if (null === $shouldBuffer = $this->shouldBuffer) {
  349. throw new \LogicException('A chunk passthru cannot yield more than one "isFirst()" chunk.');
  350. }
  351. $e = $this->shouldBuffer = null;
  352. if ($shouldBuffer instanceof \Closure) {
  353. try {
  354. $shouldBuffer = $shouldBuffer($this->getHeaders(false));
  355. if (null !== $e = $this->response->getInfo('error')) {
  356. throw new TransportException($e);
  357. }
  358. } catch (\Throwable $e) {
  359. $this->info['error'] = $e->getMessage();
  360. $this->response->cancel();
  361. }
  362. }
  363. if (true === $shouldBuffer) {
  364. $this->content = fopen('php://temp', 'w+');
  365. } elseif (\is_resource($shouldBuffer)) {
  366. $this->content = $shouldBuffer;
  367. }
  368. return $e;
  369. }
  370. private function close(): void
  371. {
  372. $this->response->cancel();
  373. }
  374. }