HttplugWaitLoop.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Internal;
  11. use Http\Client\Exception\NetworkException;
  12. use Psr\Http\Message\ResponseFactoryInterface;
  13. use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
  14. use Psr\Http\Message\StreamFactoryInterface;
  15. use Symfony\Component\HttpClient\Response\StreamableInterface;
  16. use Symfony\Component\HttpClient\Response\StreamWrapper;
  17. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  18. use Symfony\Contracts\HttpClient\HttpClientInterface;
  19. use Symfony\Contracts\HttpClient\ResponseInterface;
  20. /**
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. *
  23. * @internal
  24. */
  25. final class HttplugWaitLoop
  26. {
  27. private $client;
  28. private $promisePool;
  29. private $responseFactory;
  30. private $streamFactory;
  31. public function __construct(HttpClientInterface $client, ?\SplObjectStorage $promisePool, ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
  32. {
  33. $this->client = $client;
  34. $this->promisePool = $promisePool;
  35. $this->responseFactory = $responseFactory;
  36. $this->streamFactory = $streamFactory;
  37. }
  38. public function wait(?ResponseInterface $pendingResponse, float $maxDuration = null, float $idleTimeout = null): int
  39. {
  40. if (!$this->promisePool) {
  41. return 0;
  42. }
  43. $guzzleQueue = \GuzzleHttp\Promise\queue();
  44. if (0.0 === $remainingDuration = $maxDuration) {
  45. $idleTimeout = 0.0;
  46. } elseif (null !== $maxDuration) {
  47. $startTime = microtime(true);
  48. $idleTimeout = max(0.0, min($maxDuration / 5, $idleTimeout ?? $maxDuration));
  49. }
  50. do {
  51. foreach ($this->client->stream($this->promisePool, $idleTimeout) as $response => $chunk) {
  52. try {
  53. if (null !== $maxDuration && $chunk->isTimeout()) {
  54. goto check_duration;
  55. }
  56. if ($chunk->isFirst()) {
  57. // Deactivate throwing on 3/4/5xx
  58. $response->getStatusCode();
  59. }
  60. if (!$chunk->isLast()) {
  61. goto check_duration;
  62. }
  63. if ([, $promise] = $this->promisePool[$response] ?? null) {
  64. unset($this->promisePool[$response]);
  65. $promise->resolve($this->createPsr7Response($response, true));
  66. }
  67. } catch (\Exception $e) {
  68. if ([$request, $promise] = $this->promisePool[$response] ?? null) {
  69. unset($this->promisePool[$response]);
  70. if ($e instanceof TransportExceptionInterface) {
  71. $e = new NetworkException($e->getMessage(), $request, $e);
  72. }
  73. $promise->reject($e);
  74. }
  75. }
  76. $guzzleQueue->run();
  77. if ($pendingResponse === $response) {
  78. return $this->promisePool->count();
  79. }
  80. check_duration:
  81. if (null !== $maxDuration && $idleTimeout && $idleTimeout > $remainingDuration = max(0.0, $maxDuration - microtime(true) + $startTime)) {
  82. $idleTimeout = $remainingDuration / 5;
  83. break;
  84. }
  85. }
  86. if (!$count = $this->promisePool->count()) {
  87. return 0;
  88. }
  89. } while (null === $maxDuration || 0 < $remainingDuration);
  90. return $count;
  91. }
  92. public function createPsr7Response(ResponseInterface $response, bool $buffer = false): Psr7ResponseInterface
  93. {
  94. $psrResponse = $this->responseFactory->createResponse($response->getStatusCode());
  95. foreach ($response->getHeaders(false) as $name => $values) {
  96. foreach ($values as $value) {
  97. $psrResponse = $psrResponse->withAddedHeader($name, $value);
  98. }
  99. }
  100. if ($response instanceof StreamableInterface) {
  101. $body = $this->streamFactory->createStreamFromResource($response->toStream(false));
  102. } elseif (!$buffer) {
  103. $body = $this->streamFactory->createStreamFromResource(StreamWrapper::createResource($response, $this->client));
  104. } else {
  105. $body = $this->streamFactory->createStream($response->getContent(false));
  106. }
  107. if ($body->isSeekable()) {
  108. $body->seek(0);
  109. }
  110. return $psrResponse->withBody($body);
  111. }
  112. }