CurlDownloader.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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\Http;
  12. use Composer\Config;
  13. use Composer\IO\IOInterface;
  14. use Composer\Downloader\TransportException;
  15. use Composer\CaBundle\CaBundle;
  16. use Composer\Util\RemoteFilesystem;
  17. use Composer\Util\StreamContextFactory;
  18. use Composer\Util\AuthHelper;
  19. use Composer\Util\Url;
  20. use Psr\Log\LoggerInterface;
  21. use React\Promise\Promise;
  22. /**
  23. * @author Jordi Boggiano <j.boggiano@seld.be>
  24. * @author Nicolas Grekas <p@tchwork.com>
  25. */
  26. class CurlDownloader
  27. {
  28. private $multiHandle;
  29. private $shareHandle;
  30. private $jobs = array();
  31. /** @var IOInterface */
  32. private $io;
  33. /** @var Config */
  34. private $config;
  35. /** @var AuthHelper */
  36. private $authHelper;
  37. private $selectTimeout = 5.0;
  38. private $maxRedirects = 20;
  39. protected $multiErrors = array(
  40. CURLM_BAD_HANDLE => array('CURLM_BAD_HANDLE', 'The passed-in handle is not a valid CURLM handle.'),
  41. CURLM_BAD_EASY_HANDLE => array('CURLM_BAD_EASY_HANDLE', "An easy handle was not good/valid. It could mean that it isn't an easy handle at all, or possibly that the handle already is in used by this or another multi handle."),
  42. CURLM_OUT_OF_MEMORY => array('CURLM_OUT_OF_MEMORY', 'You are doomed.'),
  43. CURLM_INTERNAL_ERROR => array('CURLM_INTERNAL_ERROR', 'This can only be returned if libcurl bugs. Please report it to us!')
  44. );
  45. private static $options = array(
  46. 'http' => array(
  47. 'method' => CURLOPT_CUSTOMREQUEST,
  48. 'content' => CURLOPT_POSTFIELDS,
  49. 'proxy' => CURLOPT_PROXY,
  50. 'header' => CURLOPT_HTTPHEADER,
  51. ),
  52. 'ssl' => array(
  53. 'ciphers' => CURLOPT_SSL_CIPHER_LIST,
  54. 'cafile' => CURLOPT_CAINFO,
  55. 'capath' => CURLOPT_CAPATH,
  56. ),
  57. );
  58. private static $timeInfo = array(
  59. 'total_time' => true,
  60. 'namelookup_time' => true,
  61. 'connect_time' => true,
  62. 'pretransfer_time' => true,
  63. 'starttransfer_time' => true,
  64. 'redirect_time' => true,
  65. );
  66. public function __construct(IOInterface $io, Config $config, array $options = array(), $disableTls = false)
  67. {
  68. $this->io = $io;
  69. $this->config = $config;
  70. $this->multiHandle = $mh = curl_multi_init();
  71. if (function_exists('curl_multi_setopt')) {
  72. curl_multi_setopt($mh, CURLMOPT_PIPELINING, /*CURLPIPE_HTTP1 | CURLPIPE_MULTIPLEX*/ 3);
  73. if (defined('CURLMOPT_MAX_HOST_CONNECTIONS')) {
  74. curl_multi_setopt($mh, CURLMOPT_MAX_HOST_CONNECTIONS, 8);
  75. }
  76. }
  77. if (function_exists('curl_share_init')) {
  78. $this->shareHandle = $sh = curl_share_init();
  79. curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  80. curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  81. curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  82. }
  83. $this->authHelper = new AuthHelper($io, $config);
  84. }
  85. public function download($resolve, $reject, $origin, $url, $options, $copyTo = null)
  86. {
  87. $attributes = array();
  88. if (isset($options['retry-auth-failure'])) {
  89. $attributes['retryAuthFailure'] = $options['retry-auth-failure'];
  90. unset($options['retry-auth-failure']);
  91. }
  92. return $this->initDownload($resolve, $reject, $origin, $url, $options, $copyTo, $attributes);
  93. }
  94. private function initDownload($resolve, $reject, $origin, $url, $options, $copyTo = null, array $attributes = array())
  95. {
  96. $attributes = array_merge(array(
  97. 'retryAuthFailure' => true,
  98. 'redirects' => 0,
  99. 'storeAuth' => false,
  100. ), $attributes);
  101. $originalOptions = $options;
  102. // check URL can be accessed (i.e. is not insecure)
  103. $this->config->prohibitUrlByConfig($url, $this->io);
  104. $curlHandle = curl_init();
  105. $headerHandle = fopen('php://temp/maxmemory:32768', 'w+b');
  106. if ($copyTo) {
  107. $errorMessage = '';
  108. set_error_handler(function ($code, $msg) use (&$errorMessage) {
  109. if ($errorMessage) {
  110. $errorMessage .= "\n";
  111. }
  112. $errorMessage .= preg_replace('{^fopen\(.*?\): }', '', $msg);
  113. });
  114. $bodyHandle = fopen($copyTo.'~', 'w+b');
  115. restore_error_handler();
  116. if (!$bodyHandle) {
  117. throw new TransportException('The "'.$url.'" file could not be written to '.$copyTo.': '.$errorMessage);
  118. }
  119. } else {
  120. $bodyHandle = @fopen('php://temp/maxmemory:524288', 'w+b');
  121. }
  122. curl_setopt($curlHandle, CURLOPT_URL, $url);
  123. curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, false);
  124. //curl_setopt($curlHandle, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
  125. curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 10);
  126. curl_setopt($curlHandle, CURLOPT_TIMEOUT, 60);
  127. curl_setopt($curlHandle, CURLOPT_WRITEHEADER, $headerHandle);
  128. curl_setopt($curlHandle, CURLOPT_FILE, $bodyHandle);
  129. curl_setopt($curlHandle, CURLOPT_ENCODING, "gzip");
  130. curl_setopt($curlHandle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
  131. if (defined('CURLOPT_SSL_FALSESTART')) {
  132. curl_setopt($curlHandle, CURLOPT_SSL_FALSESTART, true);
  133. }
  134. if (function_exists('curl_share_init')) {
  135. curl_setopt($curlHandle, CURLOPT_SHARE, $this->shareHandle);
  136. }
  137. if (!isset($options['http']['header'])) {
  138. $options['http']['header'] = array();
  139. }
  140. $options['http']['header'] = array_diff($options['http']['header'], array('Connection: close'));
  141. $options['http']['header'][] = 'Connection: keep-alive';
  142. $version = curl_version();
  143. $features = $version['features'];
  144. if (0 === strpos($url, 'https://') && \defined('CURL_VERSION_HTTP2') && \defined('CURL_HTTP_VERSION_2_0') && (CURL_VERSION_HTTP2 & $features)) {
  145. curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  146. }
  147. $options['http']['header'] = $this->authHelper->addAuthenticationHeader($options['http']['header'], $origin, $url);
  148. $options = StreamContextFactory::initOptions($url, $options);
  149. foreach (self::$options as $type => $curlOptions) {
  150. foreach ($curlOptions as $name => $curlOption) {
  151. if (isset($options[$type][$name])) {
  152. curl_setopt($curlHandle, $curlOption, $options[$type][$name]);
  153. }
  154. }
  155. }
  156. $progress = array_diff_key(curl_getinfo($curlHandle), self::$timeInfo);
  157. $this->jobs[(int) $curlHandle] = array(
  158. 'url' => $url,
  159. 'origin' => $origin,
  160. 'attributes' => $attributes,
  161. 'options' => $originalOptions,
  162. 'progress' => $progress,
  163. 'curlHandle' => $curlHandle,
  164. 'filename' => $copyTo,
  165. 'headerHandle' => $headerHandle,
  166. 'bodyHandle' => $bodyHandle,
  167. 'resolve' => $resolve,
  168. 'reject' => $reject,
  169. );
  170. $usingProxy = !empty($options['http']['proxy']) ? ' using proxy ' . $options['http']['proxy'] : '';
  171. $ifModified = false !== strpos(strtolower(implode(',', $options['http']['header'])), 'if-modified-since:') ? ' if modified' : '';
  172. if ($attributes['redirects'] === 0) {
  173. $this->io->writeError('Downloading ' . $url . $usingProxy . $ifModified, true, IOInterface::DEBUG);
  174. }
  175. $this->checkCurlResult(curl_multi_add_handle($this->multiHandle, $curlHandle));
  176. // TODO progress
  177. //$params['notification'](STREAM_NOTIFY_RESOLVE, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0, false);
  178. }
  179. public function tick()
  180. {
  181. if (!$this->jobs) {
  182. return;
  183. }
  184. $active = true;
  185. $this->checkCurlResult(curl_multi_exec($this->multiHandle, $active));
  186. if (-1 === curl_multi_select($this->multiHandle, $this->selectTimeout)) {
  187. // sleep in case select returns -1 as it can happen on old php versions or some platforms where curl does not manage to do the select
  188. usleep(150);
  189. }
  190. while ($progress = curl_multi_info_read($this->multiHandle)) {
  191. $curlHandle = $progress['handle'];
  192. $i = (int) $curlHandle;
  193. if (!isset($this->jobs[$i])) {
  194. continue;
  195. }
  196. $progress = array_diff_key(curl_getinfo($curlHandle), self::$timeInfo);
  197. $job = $this->jobs[$i];
  198. unset($this->jobs[$i]);
  199. curl_multi_remove_handle($this->multiHandle, $curlHandle);
  200. $error = curl_error($curlHandle);
  201. $errno = curl_errno($curlHandle);
  202. curl_close($curlHandle);
  203. $headers = null;
  204. $statusCode = null;
  205. $response = null;
  206. try {
  207. // TODO progress
  208. //$this->onProgress($curlHandle, $job['callback'], $progress, $job['progress']);
  209. if (CURLE_OK !== $errno) {
  210. throw new TransportException($error);
  211. }
  212. $statusCode = $progress['http_code'];
  213. rewind($job['headerHandle']);
  214. $headers = explode("\r\n", rtrim(stream_get_contents($job['headerHandle'])));
  215. fclose($job['headerHandle']);
  216. // prepare response object
  217. if ($job['filename']) {
  218. fclose($job['bodyHandle']);
  219. $response = new Response(array('url' => $progress['url']), $statusCode, $headers, $job['filename'].'~');
  220. $this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG);
  221. } else {
  222. rewind($job['bodyHandle']);
  223. $contents = stream_get_contents($job['bodyHandle']);
  224. fclose($job['bodyHandle']);
  225. $response = new Response(array('url' => $progress['url']), $statusCode, $headers, $contents);
  226. $this->io->writeError('['.$statusCode.'] '.$progress['url'], true, IOInterface::DEBUG);
  227. }
  228. $result = $this->isAuthenticatedRetryNeeded($job, $response);
  229. if ($result['retry']) {
  230. if ($job['filename']) {
  231. @unlink($job['filename'].'~');
  232. }
  233. $this->restartJob($job, $job['url'], array('storeAuth' => $result['storeAuth']));
  234. continue;
  235. }
  236. // handle 3xx redirects, 304 Not Modified is excluded
  237. if ($statusCode >= 300 && $statusCode <= 399 && $statusCode !== 304 && $job['attributes']['redirects'] < $this->maxRedirects) {
  238. $location = $this->handleRedirect($job, $response);
  239. if ($location) {
  240. $this->restartJob($job, $location, array('redirects' => $job['attributes']['redirects'] + 1));
  241. continue;
  242. }
  243. }
  244. // fail 4xx and 5xx responses and capture the response
  245. if ($statusCode >= 400 && $statusCode <= 599) {
  246. throw $this->failResponse($job, $response, $response->getStatusMessage());
  247. // TODO progress
  248. // $this->io->overwriteError("Downloading (<error>failed</error>)", false);
  249. }
  250. if ($job['attributes']['storeAuth']) {
  251. $this->authHelper->storeAuth($job['origin'], $job['attributes']['storeAuth']);
  252. }
  253. // resolve promise
  254. if ($job['filename']) {
  255. rename($job['filename'].'~', $job['filename']);
  256. call_user_func($job['resolve'], true);
  257. } else {
  258. call_user_func($job['resolve'], $response);
  259. }
  260. } catch (\Exception $e) {
  261. if ($e instanceof TransportException && $headers) {
  262. $e->setHeaders($headers);
  263. $e->setStatusCode($statusCode);
  264. }
  265. if ($e instanceof TransportException && $response) {
  266. $e->setResponse($response->getBody());
  267. }
  268. if (is_resource($job['headerHandle'])) {
  269. fclose($job['headerHandle']);
  270. }
  271. if (is_resource($job['bodyHandle'])) {
  272. fclose($job['bodyHandle']);
  273. }
  274. if ($job['filename']) {
  275. @unlink($job['filename'].'~');
  276. }
  277. call_user_func($job['reject'], $e);
  278. }
  279. }
  280. foreach ($this->jobs as $i => $curlHandle) {
  281. if (!isset($this->jobs[$i])) {
  282. continue;
  283. }
  284. $curlHandle = $this->jobs[$i]['curlHandle'];
  285. $progress = array_diff_key(curl_getinfo($curlHandle), self::$timeInfo);
  286. if ($this->jobs[$i]['progress'] !== $progress) {
  287. $previousProgress = $this->jobs[$i]['progress'];
  288. $this->jobs[$i]['progress'] = $progress;
  289. // TODO
  290. //$this->onProgress($curlHandle, $this->jobs[$i]['callback'], $progress, $previousProgress);
  291. }
  292. }
  293. }
  294. private function handleRedirect(array $job, Response $response)
  295. {
  296. if ($locationHeader = $response->getHeader('location')) {
  297. if (parse_url($locationHeader, PHP_URL_SCHEME)) {
  298. // Absolute URL; e.g. https://example.com/composer
  299. $targetUrl = $locationHeader;
  300. } elseif (parse_url($locationHeader, PHP_URL_HOST)) {
  301. // Scheme relative; e.g. //example.com/foo
  302. $targetUrl = parse_url($job['url'], PHP_URL_SCHEME).':'.$locationHeader;
  303. } elseif ('/' === $locationHeader[0]) {
  304. // Absolute path; e.g. /foo
  305. $urlHost = parse_url($job['url'], PHP_URL_HOST);
  306. // Replace path using hostname as an anchor.
  307. $targetUrl = preg_replace('{^(.+(?://|@)'.preg_quote($urlHost).'(?::\d+)?)(?:[/\?].*)?$}', '\1'.$locationHeader, $job['url']);
  308. } else {
  309. // Relative path; e.g. foo
  310. // This actually differs from PHP which seems to add duplicate slashes.
  311. $targetUrl = preg_replace('{^(.+/)[^/?]*(?:\?.*)?$}', '\1'.$locationHeader, $job['url']);
  312. }
  313. }
  314. if (!empty($targetUrl)) {
  315. $this->io->writeError(sprintf('Following redirect (%u) %s', $job['attributes']['redirects'] + 1, $targetUrl), true, IOInterface::DEBUG);
  316. return $targetUrl;
  317. }
  318. throw new TransportException('The "'.$job['url'].'" file could not be downloaded, got redirect without Location ('.$response->getStatusMessage().')');
  319. }
  320. private function isAuthenticatedRetryNeeded(array $job, Response $response)
  321. {
  322. if (in_array($response->getStatusCode(), array(401, 403)) && $job['attributes']['retryAuthFailure']) {
  323. $warning = null;
  324. if ($response->getHeader('content-type') === 'application/json') {
  325. $data = json_decode($response->getBody(), true);
  326. if (!empty($data['warning'])) {
  327. $warning = $data['warning'];
  328. }
  329. }
  330. $result = $this->authHelper->promptAuthIfNeeded($job['url'], $job['origin'], $response->getStatusCode(), $response->getStatusMessage(), $warning, $response->getHeaders());
  331. if ($result['retry']) {
  332. return $result;
  333. }
  334. }
  335. $locationHeader = $response->getHeader('location');
  336. $needsAuthRetry = false;
  337. // check for bitbucket login page asking to authenticate
  338. if (
  339. $job['origin'] === 'bitbucket.org'
  340. && !$this->authHelper->isPublicBitBucketDownload($job['url'])
  341. && substr($job['url'], -4) === '.zip'
  342. && (!$locationHeader || substr($locationHeader, -4) !== '.zip')
  343. && preg_match('{^text/html\b}i', $response->getHeader('content-type'))
  344. ) {
  345. $needsAuthRetry = 'Bitbucket requires authentication and it was not provided';
  346. }
  347. // check for gitlab 404 when downloading archives
  348. if (
  349. $response->getStatusCode() === 404
  350. && $this->config && in_array($job['origin'], $this->config->get('gitlab-domains'), true)
  351. && false !== strpos($job['url'], 'archive.zip')
  352. ) {
  353. $needsAuthRetry = 'GitLab requires authentication and it was not provided';
  354. }
  355. if ($needsAuthRetry) {
  356. if ($job['attributes']['retryAuthFailure']) {
  357. $result = $this->authHelper->promptAuthIfNeeded($job['url'], $job['origin'], 401);
  358. if ($result['retry']) {
  359. return $result;
  360. }
  361. }
  362. throw $this->failResponse($job, $response, $needsAuthRetry);
  363. }
  364. return array('retry' => false, 'storeAuth' => false);
  365. }
  366. private function restartJob(array $job, $url, array $attributes = array())
  367. {
  368. $attributes = array_merge($job['attributes'], $attributes);
  369. $origin = Url::getOrigin($this->config, $url);
  370. $this->initDownload($job['resolve'], $job['reject'], $origin, $url, $job['options'], $job['filename'], $attributes);
  371. }
  372. private function failResponse(array $job, Response $response, $errorMessage)
  373. {
  374. return new TransportException('The "'.$job['url'].'" file could not be downloaded ('.$errorMessage.')', $response->getStatusCode());
  375. }
  376. private function onProgress($curlHandle, callable $notify, array $progress, array $previousProgress)
  377. {
  378. // TODO add support for progress
  379. if (300 <= $progress['http_code'] && $progress['http_code'] < 400) {
  380. return;
  381. }
  382. if ($previousProgress['download_content_length'] < $progress['download_content_length']) {
  383. $notify(STREAM_NOTIFY_FILE_SIZE_IS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, (int) $progress['download_content_length'], false);
  384. }
  385. if ($previousProgress['size_download'] < $progress['size_download']) {
  386. $notify(STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, (int) $progress['size_download'], (int) $progress['download_content_length'], false);
  387. }
  388. }
  389. private function checkCurlResult($code)
  390. {
  391. if ($code != CURLM_OK && $code != CURLM_CALL_MULTI_PERFORM) {
  392. throw new \RuntimeException(isset($this->multiErrors[$code])
  393. ? "cURL error: {$code} ({$this->multiErrors[$code][0]}): cURL message: {$this->multiErrors[$code][1]}"
  394. : 'Unexpected cURL error: ' . $code
  395. );
  396. }
  397. }
  398. }