AsyncResponse.php 17 KB

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