HttplugWaitLoop.php 5.0 KB

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