HttpDownloader.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Downloader\TransportException;
  15. use Composer\CaBundle\CaBundle;
  16. use Psr\Log\LoggerInterface;
  17. use React\Promise\Promise;
  18. /**
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class HttpDownloader
  22. {
  23. const STATUS_QUEUED = 1;
  24. const STATUS_STARTED = 2;
  25. const STATUS_COMPLETED = 3;
  26. const STATUS_FAILED = 4;
  27. private $io;
  28. private $config;
  29. private $jobs = array();
  30. private $index;
  31. private $progress;
  32. private $lastProgress;
  33. private $disableTls = false;
  34. private $curl;
  35. private $rfs;
  36. private $idGen = 0;
  37. /**
  38. * Constructor.
  39. *
  40. * @param IOInterface $io The IO instance
  41. * @param Config $config The config
  42. * @param array $options The options
  43. * @param bool $disableTls
  44. */
  45. public function __construct(IOInterface $io, Config $config, array $options = array(), $disableTls = false)
  46. {
  47. $this->io = $io;
  48. // Setup TLS options
  49. // The cafile option can be set via config.json
  50. if ($disableTls === false) {
  51. $logger = $io instanceof LoggerInterface ? $io : null;
  52. $this->options = StreamContextFactory::getTlsDefaults($options, $logger);
  53. } else {
  54. $this->disableTls = true;
  55. }
  56. // handle the other externally set options normally.
  57. $this->options = array_replace_recursive($this->options, $options);
  58. $this->config = $config;
  59. if (extension_loaded('curl')) {
  60. $this->curl = new Http\CurlDownloader($io, $config, $options, $disableTls);
  61. }
  62. $this->rfs = new RemoteFilesystem($io, $config, $options, $disableTls);
  63. }
  64. public function get($url, $options = array())
  65. {
  66. list($job, $promise) = $this->addJob(array('url' => $url, 'options' => $options, 'copyTo' => false), true);
  67. $this->wait($job['id']);
  68. return $this->getResponse($job['id']);
  69. }
  70. public function add($url, $options = array())
  71. {
  72. list($job, $promise) = $this->addJob(array('url' => $url, 'options' => $options, 'copyTo' => false));
  73. return $promise;
  74. }
  75. public function copy($url, $to, $options = array())
  76. {
  77. list($job, $promise) = $this->addJob(array('url' => $url, 'options' => $options, 'copyTo' => $to), true);
  78. $this->wait($job['id']);
  79. return $this->getResponse($job['id']);
  80. }
  81. public function addCopy($url, $to, $options = array())
  82. {
  83. list($job, $promise) = $this->addJob(array('url' => $url, 'options' => $options, 'copyTo' => $to));
  84. return $promise;
  85. }
  86. /**
  87. * Retrieve the options set in the constructor
  88. *
  89. * @return array Options
  90. */
  91. public function getOptions()
  92. {
  93. return $this->options;
  94. }
  95. /**
  96. * Merges new options
  97. *
  98. * @return array $options
  99. */
  100. public function setOptions(array $options)
  101. {
  102. $this->options = array_replace_recursive($this->options, $options);
  103. }
  104. private function addJob($request, $sync = false)
  105. {
  106. $job = array(
  107. 'id' => $this->idGen++,
  108. 'status' => self::STATUS_QUEUED,
  109. 'request' => $request,
  110. 'sync' => $sync,
  111. );
  112. $curl = $this->curl;
  113. $rfs = $this->rfs;
  114. $io = $this->io;
  115. $origin = $this->getOrigin($job['request']['url']);
  116. // TODO experiment with allowing file:// through curl too
  117. if ($curl && preg_match('{^https?://}i', $job['request']['url'])) {
  118. $resolver = function ($resolve, $reject) use (&$job, $curl, $origin) {
  119. // start job
  120. $url = $job['request']['url'];
  121. $options = $job['request']['options'];
  122. $job['status'] = HttpDownloader::STATUS_STARTED;
  123. if ($job['request']['copyTo']) {
  124. $curl->download($resolve, $reject, $origin, $url, $options, $job['request']['copyTo']);
  125. } else {
  126. $curl->download($resolve, $reject, $origin, $url, $options);
  127. }
  128. };
  129. } else {
  130. $resolver = function ($resolve, $reject) use (&$job, $rfs, $curl, $origin) {
  131. // start job
  132. $url = $job['request']['url'];
  133. $options = $job['request']['options'];
  134. $job['status'] = HttpDownloader::STATUS_STARTED;
  135. if ($job['request']['copyTo']) {
  136. $result = $rfs->copy($origin, $url, $job['request']['copyTo'], false /* TODO progress */, $options);
  137. $resolve($result);
  138. } else {
  139. $body = $rfs->getContents($origin, $url, false /* TODO progress */, $options);
  140. $headers = $rfs->getLastHeaders();
  141. $response = new Http\Response($job['request'], $rfs->findStatusCode($headers), $headers, $body);
  142. $resolve($response);
  143. }
  144. };
  145. }
  146. $canceler = function () {};
  147. $promise = new Promise($resolver, $canceler);
  148. $promise->then(function ($response) use (&$job) {
  149. $job['status'] = HttpDownloader::STATUS_COMPLETED;
  150. $job['response'] = $response;
  151. // TODO look for more jobs to start once we throttle to max X jobs
  152. }, function ($e) use ($io, &$job) {
  153. var_dump(__CLASS__ . __LINE__);
  154. var_dump(gettype($e));
  155. var_dump($e->getMessage());
  156. die;
  157. $job['status'] = HttpDownloader::STATUS_FAILED;
  158. $job['exception'] = $e;
  159. });
  160. $this->jobs[$job['id']] =& $job;
  161. return array($job, $promise);
  162. }
  163. public function wait($index = null, $progress = false)
  164. {
  165. while (true) {
  166. if ($this->curl) {
  167. $this->curl->tick();
  168. }
  169. if (null !== $index) {
  170. if ($this->jobs[$index]['status'] === self::STATUS_COMPLETED || $this->jobs[$index]['status'] === self::STATUS_FAILED) {
  171. return;
  172. }
  173. } else {
  174. $done = true;
  175. foreach ($this->jobs as $job) {
  176. if (!in_array($job['status'], array(self::STATUS_COMPLETED, self::STATUS_FAILED), true)) {
  177. $done = false;
  178. break;
  179. } elseif (!$job['sync']) {
  180. unset($this->jobs[$job['id']]);
  181. }
  182. }
  183. if ($done) {
  184. return;
  185. }
  186. }
  187. usleep(1000);
  188. }
  189. }
  190. private function getResponse($index)
  191. {
  192. if (!isset($this->jobs[$index])) {
  193. throw new \LogicException('Invalid request id');
  194. }
  195. if ($this->jobs[$index]['status'] === self::STATUS_FAILED) {
  196. throw $this->jobs[$index]['exception'];
  197. }
  198. if (!isset($this->jobs[$index]['response'])) {
  199. throw new \LogicException('Response not available yet, call wait() first');
  200. }
  201. $resp = $this->jobs[$index]['response'];
  202. unset($this->jobs[$index]);
  203. return $resp;
  204. }
  205. private function getOrigin($url)
  206. {
  207. $origin = parse_url($url, PHP_URL_HOST);
  208. if ($origin === 'api.github.com') {
  209. return 'github.com';
  210. }
  211. if ($origin === 'repo.packagist.org') {
  212. return 'packagist.org';
  213. }
  214. return $origin ?: $url;
  215. }
  216. }