AmpResponse.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 Amp\ByteStream\StreamException;
  12. use Amp\CancellationTokenSource;
  13. use Amp\Coroutine;
  14. use Amp\Deferred;
  15. use Amp\Http\Client\HttpException;
  16. use Amp\Http\Client\Request;
  17. use Amp\Http\Client\Response;
  18. use Amp\Loop;
  19. use Amp\Promise;
  20. use Amp\Success;
  21. use Psr\Log\LoggerInterface;
  22. use Symfony\Component\HttpClient\Chunk\FirstChunk;
  23. use Symfony\Component\HttpClient\Chunk\InformationalChunk;
  24. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  25. use Symfony\Component\HttpClient\Exception\TransportException;
  26. use Symfony\Component\HttpClient\HttpClientTrait;
  27. use Symfony\Component\HttpClient\Internal\AmpBody;
  28. use Symfony\Component\HttpClient\Internal\AmpClientState;
  29. use Symfony\Component\HttpClient\Internal\Canary;
  30. use Symfony\Component\HttpClient\Internal\ClientState;
  31. use Symfony\Contracts\HttpClient\ResponseInterface;
  32. /**
  33. * @author Nicolas Grekas <p@tchwork.com>
  34. *
  35. * @internal
  36. */
  37. final class AmpResponse implements ResponseInterface, StreamableInterface
  38. {
  39. use CommonResponseTrait;
  40. use TransportResponseTrait;
  41. private static $nextId = 'a';
  42. private $multi;
  43. private $options;
  44. private $canceller;
  45. private $onProgress;
  46. private static $delay;
  47. /**
  48. * @internal
  49. */
  50. public function __construct(AmpClientState $multi, Request $request, array $options, ?LoggerInterface $logger)
  51. {
  52. $this->multi = $multi;
  53. $this->options = &$options;
  54. $this->logger = $logger;
  55. $this->timeout = $options['timeout'];
  56. $this->shouldBuffer = $options['buffer'];
  57. if ($this->inflate = \extension_loaded('zlib') && !$request->hasHeader('accept-encoding')) {
  58. $request->setHeader('Accept-Encoding', 'gzip');
  59. }
  60. $this->initializer = static function (self $response) {
  61. return null !== $response->options;
  62. };
  63. $info = &$this->info;
  64. $headers = &$this->headers;
  65. $canceller = $this->canceller = new CancellationTokenSource();
  66. $handle = &$this->handle;
  67. $info['url'] = (string) $request->getUri();
  68. $info['http_method'] = $request->getMethod();
  69. $info['start_time'] = null;
  70. $info['redirect_url'] = null;
  71. $info['redirect_time'] = 0.0;
  72. $info['redirect_count'] = 0;
  73. $info['size_upload'] = 0.0;
  74. $info['size_download'] = 0.0;
  75. $info['upload_content_length'] = -1.0;
  76. $info['download_content_length'] = -1.0;
  77. $info['user_data'] = $options['user_data'];
  78. $info['debug'] = '';
  79. $onProgress = $options['on_progress'] ?? static function () {};
  80. $onProgress = $this->onProgress = static function () use (&$info, $onProgress) {
  81. $info['total_time'] = microtime(true) - $info['start_time'];
  82. $onProgress((int) $info['size_download'], ((int) (1 + $info['download_content_length']) ?: 1) - 1, (array) $info);
  83. };
  84. $pauseDeferred = new Deferred();
  85. $pause = new Success();
  86. $throttleWatcher = null;
  87. $this->id = $id = self::$nextId++;
  88. Loop::defer(static function () use ($request, $multi, &$id, &$info, &$headers, $canceller, &$options, $onProgress, &$handle, $logger, &$pause) {
  89. return new Coroutine(self::generateResponse($request, $multi, $id, $info, $headers, $canceller, $options, $onProgress, $handle, $logger, $pause));
  90. });
  91. $info['pause_handler'] = static function (float $duration) use (&$throttleWatcher, &$pauseDeferred, &$pause) {
  92. if (null !== $throttleWatcher) {
  93. Loop::cancel($throttleWatcher);
  94. }
  95. $pause = $pauseDeferred->promise();
  96. if ($duration <= 0) {
  97. $deferred = $pauseDeferred;
  98. $pauseDeferred = new Deferred();
  99. $deferred->resolve();
  100. } else {
  101. $throttleWatcher = Loop::delay(ceil(1000 * $duration), static function () use (&$pauseDeferred) {
  102. $deferred = $pauseDeferred;
  103. $pauseDeferred = new Deferred();
  104. $deferred->resolve();
  105. });
  106. }
  107. };
  108. $multi->openHandles[$id] = $id;
  109. ++$multi->responseCount;
  110. $this->canary = new Canary(static function () use ($canceller, $multi, $id) {
  111. $canceller->cancel();
  112. unset($multi->openHandles[$id], $multi->handlesActivity[$id]);
  113. });
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getInfo(string $type = null)
  119. {
  120. return null !== $type ? $this->info[$type] ?? null : $this->info;
  121. }
  122. public function __sleep()
  123. {
  124. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  125. }
  126. public function __wakeup()
  127. {
  128. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  129. }
  130. public function __destruct()
  131. {
  132. try {
  133. $this->doDestruct();
  134. } finally {
  135. // Clear the DNS cache when all requests completed
  136. if (0 >= --$this->multi->responseCount) {
  137. $this->multi->responseCount = 0;
  138. $this->multi->dnsCache = [];
  139. }
  140. }
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. private static function schedule(self $response, array &$runningResponses): void
  146. {
  147. if (isset($runningResponses[0])) {
  148. $runningResponses[0][1][$response->id] = $response;
  149. } else {
  150. $runningResponses[0] = [$response->multi, [$response->id => $response]];
  151. }
  152. if (!isset($response->multi->openHandles[$response->id])) {
  153. $response->multi->handlesActivity[$response->id][] = null;
  154. $response->multi->handlesActivity[$response->id][] = null !== $response->info['error'] ? new TransportException($response->info['error']) : null;
  155. }
  156. }
  157. /**
  158. * {@inheritdoc}
  159. *
  160. * @param AmpClientState $multi
  161. */
  162. private static function perform(ClientState $multi, array &$responses = null): void
  163. {
  164. if ($responses) {
  165. foreach ($responses as $response) {
  166. try {
  167. if ($response->info['start_time']) {
  168. $response->info['total_time'] = microtime(true) - $response->info['start_time'];
  169. ($response->onProgress)();
  170. }
  171. } catch (\Throwable $e) {
  172. $multi->handlesActivity[$response->id][] = null;
  173. $multi->handlesActivity[$response->id][] = $e;
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * {@inheritdoc}
  180. *
  181. * @param AmpClientState $multi
  182. */
  183. private static function select(ClientState $multi, float $timeout): int
  184. {
  185. $timeout += microtime(true);
  186. self::$delay = Loop::defer(static function () use ($timeout) {
  187. if (0 < $timeout -= microtime(true)) {
  188. self::$delay = Loop::delay(ceil(1000 * $timeout), [Loop::class, 'stop']);
  189. } else {
  190. Loop::stop();
  191. }
  192. });
  193. Loop::run();
  194. return null === self::$delay ? 1 : 0;
  195. }
  196. private static function generateResponse(Request $request, AmpClientState $multi, string $id, array &$info, array &$headers, CancellationTokenSource $canceller, array &$options, \Closure $onProgress, &$handle, ?LoggerInterface $logger, Promise &$pause)
  197. {
  198. $request->setInformationalResponseHandler(static function (Response $response) use ($multi, $id, &$info, &$headers) {
  199. self::addResponseHeaders($response, $info, $headers);
  200. $multi->handlesActivity[$id][] = new InformationalChunk($response->getStatus(), $response->getHeaders());
  201. self::stopLoop();
  202. });
  203. try {
  204. /* @var Response $response */
  205. if (null === $response = yield from self::getPushedResponse($request, $multi, $info, $headers, $options, $logger)) {
  206. $logger && $logger->info(sprintf('Request: "%s %s"', $info['http_method'], $info['url']));
  207. $response = yield from self::followRedirects($request, $multi, $info, $headers, $canceller, $options, $onProgress, $handle, $logger, $pause);
  208. }
  209. $options = null;
  210. $multi->handlesActivity[$id][] = new FirstChunk();
  211. if ('HEAD' === $response->getRequest()->getMethod() || \in_array($info['http_code'], [204, 304], true)) {
  212. $multi->handlesActivity[$id][] = null;
  213. $multi->handlesActivity[$id][] = null;
  214. self::stopLoop();
  215. return;
  216. }
  217. if ($response->hasHeader('content-length')) {
  218. $info['download_content_length'] = (float) $response->getHeader('content-length');
  219. }
  220. $body = $response->getBody();
  221. while (true) {
  222. self::stopLoop();
  223. yield $pause;
  224. if (null === $data = yield $body->read()) {
  225. break;
  226. }
  227. $info['size_download'] += \strlen($data);
  228. $multi->handlesActivity[$id][] = $data;
  229. }
  230. $multi->handlesActivity[$id][] = null;
  231. $multi->handlesActivity[$id][] = null;
  232. } catch (\Throwable $e) {
  233. $multi->handlesActivity[$id][] = null;
  234. $multi->handlesActivity[$id][] = $e;
  235. } finally {
  236. $info['download_content_length'] = $info['size_download'];
  237. }
  238. self::stopLoop();
  239. }
  240. private static function followRedirects(Request $originRequest, AmpClientState $multi, array &$info, array &$headers, CancellationTokenSource $canceller, array $options, \Closure $onProgress, &$handle, ?LoggerInterface $logger, Promise &$pause)
  241. {
  242. yield $pause;
  243. $originRequest->setBody(new AmpBody($options['body'], $info, $onProgress));
  244. $response = yield $multi->request($options, $originRequest, $canceller->getToken(), $info, $onProgress, $handle);
  245. $previousUrl = null;
  246. while (true) {
  247. self::addResponseHeaders($response, $info, $headers);
  248. $status = $response->getStatus();
  249. if (!\in_array($status, [301, 302, 303, 307, 308], true) || null === $location = $response->getHeader('location')) {
  250. return $response;
  251. }
  252. $urlResolver = new class() {
  253. use HttpClientTrait {
  254. parseUrl as public;
  255. resolveUrl as public;
  256. }
  257. };
  258. try {
  259. $previousUrl = $previousUrl ?? $urlResolver::parseUrl($info['url']);
  260. $location = $urlResolver::parseUrl($location);
  261. $location = $urlResolver::resolveUrl($location, $previousUrl);
  262. $info['redirect_url'] = implode('', $location);
  263. } catch (InvalidArgumentException $e) {
  264. return $response;
  265. }
  266. if (0 >= $options['max_redirects'] || $info['redirect_count'] >= $options['max_redirects']) {
  267. return $response;
  268. }
  269. $logger && $logger->info(sprintf('Redirecting: "%s %s"', $status, $info['url']));
  270. try {
  271. // Discard body of redirects
  272. while (null !== yield $response->getBody()->read()) {
  273. }
  274. } catch (HttpException | StreamException $e) {
  275. // Ignore streaming errors on previous responses
  276. }
  277. ++$info['redirect_count'];
  278. $info['url'] = $info['redirect_url'];
  279. $info['redirect_url'] = null;
  280. $previousUrl = $location;
  281. $request = new Request($info['url'], $info['http_method']);
  282. $request->setProtocolVersions($originRequest->getProtocolVersions());
  283. $request->setTcpConnectTimeout($originRequest->getTcpConnectTimeout());
  284. $request->setTlsHandshakeTimeout($originRequest->getTlsHandshakeTimeout());
  285. $request->setTransferTimeout($originRequest->getTransferTimeout());
  286. if (\in_array($status, [301, 302, 303], true)) {
  287. $originRequest->removeHeader('transfer-encoding');
  288. $originRequest->removeHeader('content-length');
  289. $originRequest->removeHeader('content-type');
  290. // Do like curl and browsers: turn POST to GET on 301, 302 and 303
  291. if ('POST' === $response->getRequest()->getMethod() || 303 === $status) {
  292. $info['http_method'] = 'HEAD' === $response->getRequest()->getMethod() ? 'HEAD' : 'GET';
  293. $request->setMethod($info['http_method']);
  294. }
  295. } else {
  296. $request->setBody(AmpBody::rewind($response->getRequest()->getBody()));
  297. }
  298. foreach ($originRequest->getRawHeaders() as [$name, $value]) {
  299. $request->setHeader($name, $value);
  300. }
  301. if ($request->getUri()->getAuthority() !== $originRequest->getUri()->getAuthority()) {
  302. $request->removeHeader('authorization');
  303. $request->removeHeader('cookie');
  304. $request->removeHeader('host');
  305. }
  306. yield $pause;
  307. $response = yield $multi->request($options, $request, $canceller->getToken(), $info, $onProgress, $handle);
  308. $info['redirect_time'] = microtime(true) - $info['start_time'];
  309. }
  310. }
  311. private static function addResponseHeaders(Response $response, array &$info, array &$headers): void
  312. {
  313. $info['http_code'] = $response->getStatus();
  314. if ($headers) {
  315. $info['debug'] .= "< \r\n";
  316. $headers = [];
  317. }
  318. $h = sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatus(), $response->getReason());
  319. $info['debug'] .= "< {$h}\r\n";
  320. $info['response_headers'][] = $h;
  321. foreach ($response->getRawHeaders() as [$name, $value]) {
  322. $headers[strtolower($name)][] = $value;
  323. $h = $name.': '.$value;
  324. $info['debug'] .= "< {$h}\r\n";
  325. $info['response_headers'][] = $h;
  326. }
  327. $info['debug'] .= "< \r\n";
  328. }
  329. /**
  330. * Accepts pushed responses only if their headers related to authentication match the request.
  331. */
  332. private static function getPushedResponse(Request $request, AmpClientState $multi, array &$info, array &$headers, array $options, ?LoggerInterface $logger)
  333. {
  334. if ('' !== $options['body']) {
  335. return null;
  336. }
  337. $authority = $request->getUri()->getAuthority();
  338. foreach ($multi->pushedResponses[$authority] ?? [] as $i => [$pushedUrl, $pushDeferred, $pushedRequest, $pushedResponse, $parentOptions]) {
  339. if ($info['url'] !== $pushedUrl || $info['http_method'] !== $pushedRequest->getMethod()) {
  340. continue;
  341. }
  342. foreach ($parentOptions as $k => $v) {
  343. if ($options[$k] !== $v) {
  344. continue 2;
  345. }
  346. }
  347. foreach (['authorization', 'cookie', 'range', 'proxy-authorization'] as $k) {
  348. if ($pushedRequest->getHeaderArray($k) !== $request->getHeaderArray($k)) {
  349. continue 2;
  350. }
  351. }
  352. $response = yield $pushedResponse;
  353. foreach ($response->getHeaderArray('vary') as $vary) {
  354. foreach (preg_split('/\s*+,\s*+/', $vary) as $v) {
  355. if ('*' === $v || ($pushedRequest->getHeaderArray($v) !== $request->getHeaderArray($v) && 'accept-encoding' !== strtolower($v))) {
  356. $logger && $logger->debug(sprintf('Skipping pushed response: "%s"', $info['url']));
  357. continue 3;
  358. }
  359. }
  360. }
  361. $pushDeferred->resolve();
  362. $logger && $logger->debug(sprintf('Accepting pushed response: "%s %s"', $info['http_method'], $info['url']));
  363. self::addResponseHeaders($response, $info, $headers);
  364. unset($multi->pushedResponses[$authority][$i]);
  365. if (!$multi->pushedResponses[$authority]) {
  366. unset($multi->pushedResponses[$authority]);
  367. }
  368. return $response;
  369. }
  370. }
  371. private static function stopLoop(): void
  372. {
  373. if (null !== self::$delay) {
  374. Loop::cancel(self::$delay);
  375. self::$delay = null;
  376. }
  377. Loop::defer([Loop::class, 'stop']);
  378. }
  379. }