HttplugWaitLoop.php 5.4 KB

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