TraceableResponse.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\Exception\ClientException;
  13. use Symfony\Component\HttpClient\Exception\RedirectionException;
  14. use Symfony\Component\HttpClient\Exception\ServerException;
  15. use Symfony\Component\HttpClient\TraceableHttpClient;
  16. use Symfony\Component\Stopwatch\StopwatchEvent;
  17. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  21. use Symfony\Contracts\HttpClient\HttpClientInterface;
  22. use Symfony\Contracts\HttpClient\ResponseInterface;
  23. /**
  24. * @author Nicolas Grekas <p@tchwork.com>
  25. *
  26. * @internal
  27. */
  28. class TraceableResponse implements ResponseInterface, StreamableInterface
  29. {
  30. /**
  31. * @var \Symfony\Contracts\HttpClient\HttpClientInterface
  32. */
  33. private $client;
  34. /**
  35. * @var \Symfony\Contracts\HttpClient\ResponseInterface
  36. */
  37. private $response;
  38. /**
  39. * @var mixed
  40. */
  41. private $content;
  42. /**
  43. * @var \Symfony\Component\Stopwatch\StopwatchEvent|null
  44. */
  45. private $event;
  46. /**
  47. * @param \Symfony\Contracts\HttpClient\HttpClientInterface $client
  48. * @param \Symfony\Contracts\HttpClient\ResponseInterface $response
  49. * @param \Symfony\Component\Stopwatch\StopwatchEvent|null $event
  50. */
  51. public function __construct($client, $response, &$content, $event = null)
  52. {
  53. $this->client = $client;
  54. $this->response = $response;
  55. $this->content = &$content;
  56. $this->event = $event;
  57. }
  58. public function __sleep()
  59. {
  60. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  61. }
  62. public function __wakeup()
  63. {
  64. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  65. }
  66. public function __destruct()
  67. {
  68. try {
  69. $this->response->__destruct();
  70. } finally {
  71. if (($event = $this->event) ? $event->isStarted() : null) {
  72. $this->event->stop();
  73. }
  74. }
  75. }
  76. public function getStatusCode()
  77. {
  78. try {
  79. return $this->response->getStatusCode();
  80. } finally {
  81. if (($event = $this->event) ? $event->isStarted() : null) {
  82. $this->event->lap();
  83. }
  84. }
  85. }
  86. /**
  87. * @param bool $throw
  88. */
  89. public function getHeaders($throw = true)
  90. {
  91. try {
  92. return $this->response->getHeaders($throw);
  93. } finally {
  94. if (($event = $this->event) ? $event->isStarted() : null) {
  95. $this->event->lap();
  96. }
  97. }
  98. }
  99. /**
  100. * @param bool $throw
  101. */
  102. public function getContent($throw = true)
  103. {
  104. try {
  105. if (false === $this->content) {
  106. return $this->response->getContent($throw);
  107. }
  108. return $this->content = $this->response->getContent(false);
  109. } finally {
  110. if (($event = $this->event) ? $event->isStarted() : null) {
  111. $this->event->stop();
  112. }
  113. if ($throw) {
  114. $this->checkStatusCode($this->response->getStatusCode());
  115. }
  116. }
  117. }
  118. /**
  119. * @param bool $throw
  120. */
  121. public function toArray($throw = true)
  122. {
  123. try {
  124. if (false === $this->content) {
  125. return $this->response->toArray($throw);
  126. }
  127. return $this->content = $this->response->toArray(false);
  128. } finally {
  129. if (($event = $this->event) ? $event->isStarted() : null) {
  130. $this->event->stop();
  131. }
  132. if ($throw) {
  133. $this->checkStatusCode($this->response->getStatusCode());
  134. }
  135. }
  136. }
  137. public function cancel()
  138. {
  139. $this->response->cancel();
  140. if (($event = $this->event) ? $event->isStarted() : null) {
  141. $this->event->stop();
  142. }
  143. }
  144. /**
  145. * @return mixed
  146. * @param string|null $type
  147. */
  148. public function getInfo($type = null)
  149. {
  150. return $this->response->getInfo($type);
  151. }
  152. /**
  153. * Casts the response to a PHP stream resource.
  154. *
  155. * @return resource
  156. *
  157. * @throws TransportExceptionInterface When a network error occurs
  158. * @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached
  159. * @throws ClientExceptionInterface On a 4xx when $throw is true
  160. * @throws ServerExceptionInterface On a 5xx when $throw is true
  161. * @param bool $throw
  162. */
  163. public function toStream($throw = true)
  164. {
  165. if ($throw) {
  166. // Ensure headers arrived
  167. $this->response->getHeaders(true);
  168. }
  169. if ($this->response instanceof StreamableInterface) {
  170. return $this->response->toStream(false);
  171. }
  172. return StreamWrapper::createResource($this->response, $this->client);
  173. }
  174. /**
  175. * @internal
  176. * @param \Symfony\Contracts\HttpClient\HttpClientInterface $client
  177. * @param mixed[] $responses
  178. * @param float|null $timeout
  179. */
  180. public static function stream($client, $responses, $timeout)
  181. {
  182. $wrappedResponses = [];
  183. $traceableMap = new \SplObjectStorage();
  184. foreach ($responses as $r) {
  185. if (!$r instanceof self) {
  186. throw new \TypeError(sprintf('"%s::stream()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', TraceableHttpClient::class, get_debug_type($r)));
  187. }
  188. $traceableMap[$r->response] = $r;
  189. $wrappedResponses[] = $r->response;
  190. if ($r->event && !$r->event->isStarted()) {
  191. $r->event->start();
  192. }
  193. }
  194. foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) {
  195. if ($traceableMap[$r]->event && $traceableMap[$r]->event->isStarted()) {
  196. try {
  197. if ($chunk->isTimeout() || !$chunk->isLast()) {
  198. $traceableMap[$r]->event->lap();
  199. } else {
  200. $traceableMap[$r]->event->stop();
  201. }
  202. } catch (TransportExceptionInterface $e) {
  203. $traceableMap[$r]->event->stop();
  204. if ($chunk instanceof ErrorChunk) {
  205. $chunk->didThrow(false);
  206. } else {
  207. $chunk = new ErrorChunk($chunk->getOffset(), $e);
  208. }
  209. }
  210. }
  211. yield $traceableMap[$r] => $chunk;
  212. }
  213. }
  214. /**
  215. * @param int $code
  216. */
  217. private function checkStatusCode($code)
  218. {
  219. if (500 <= $code) {
  220. throw new ServerException($this);
  221. }
  222. if (400 <= $code) {
  223. throw new ClientException($this);
  224. }
  225. if (300 <= $code) {
  226. throw new RedirectionException($this);
  227. }
  228. }
  229. }