Преглед изворни кода

Add COMPOSER_DISABLE_NETWORK env var for debugging

Jordi Boggiano пре 6 година
родитељ
комит
fc03ab9bba
3 измењених фајлова са 26 додато и 1 уклоњено
  1. 5 0
      doc/03-cli.md
  2. 6 1
      src/Composer/Repository/ComposerRepository.php
  3. 15 0
      src/Composer/Util/HttpDownloader.php

+ 5 - 0
doc/03-cli.md

@@ -928,4 +928,9 @@ repository options.
 Defaults to `1`. If set to `0`, Composer will not create `.htaccess` files in the
 Defaults to `1`. If set to `0`, Composer will not create `.htaccess` files in the
 composer home, cache, and data directories.
 composer home, cache, and data directories.
 
 
+### COMPOSER_DISABLE_NETWORK
+
+If set to `1`, disables network access (best effort). This can be used for debugging or
+to run Composer on a plane or a starship with poor connectivity.
+
 ← [Libraries](02-libraries.md)  |  [Schema](04-schema.md) →
 ← [Libraries](02-libraries.md)  |  [Schema](04-schema.md) →

+ 6 - 1
src/Composer/Repository/ComposerRepository.php

@@ -1044,7 +1044,12 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito
                 return false;
                 return false;
             }
             }
 
 
-            if (--$retries) {
+            // special error code returned when network is being artificially disabled
+            if ($e instanceof TransportException && $e->getStatusCode() === 499) {
+                $retries = 0;
+            }
+
+            if (--$retries > 0) {
                 usleep(100000);
                 usleep(100000);
 
 
                 return $httpDownloader->add($filename, $options)->then($accept, $reject);
                 return $httpDownloader->add($filename, $options)->then($accept, $reject);

+ 15 - 0
src/Composer/Util/HttpDownloader.php

@@ -16,6 +16,7 @@ use Composer\Config;
 use Composer\IO\IOInterface;
 use Composer\IO\IOInterface;
 use Composer\Downloader\TransportException;
 use Composer\Downloader\TransportException;
 use Composer\CaBundle\CaBundle;
 use Composer\CaBundle\CaBundle;
+use Composer\Util\Http\Response;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LoggerInterface;
 use React\Promise\Promise;
 use React\Promise\Promise;
 
 
@@ -40,6 +41,7 @@ class HttpDownloader
     private $curl;
     private $curl;
     private $rfs;
     private $rfs;
     private $idGen = 0;
     private $idGen = 0;
+    private $disabled;
 
 
     /**
     /**
      * @param IOInterface $io         The IO instance
      * @param IOInterface $io         The IO instance
@@ -51,6 +53,8 @@ class HttpDownloader
     {
     {
         $this->io = $io;
         $this->io = $io;
 
 
+        $this->disabled = (bool) getenv('COMPOSER_DISABLE_NETWORK');
+
         // Setup TLS options
         // Setup TLS options
         // The cafile option can be set via config.json
         // The cafile option can be set via config.json
         if ($disableTls === false) {
         if ($disableTls === false) {
@@ -216,6 +220,17 @@ class HttpDownloader
         $options = $job['request']['options'];
         $options = $job['request']['options'];
         $origin = $job['origin'];
         $origin = $job['origin'];
 
 
+        if ($this->disabled) {
+            if (isset($job['request']['options']['http']['header']) && false !== stripos(implode('', $job['request']['options']['http']['header']), 'if-modified-since')) {
+                $resolve(new Response(array('url' => $url), 304, array(), ''));
+            } else {
+                $e = new TransportException('Network disabled', 499);
+                $e->setStatusCode(499);
+                $reject($e);
+            }
+            return;
+        }
+
         if ($job['request']['copyTo']) {
         if ($job['request']['copyTo']) {
             $this->curl->download($resolve, $reject, $origin, $url, $options, $job['request']['copyTo']);
             $this->curl->download($resolve, $reject, $origin, $url, $options, $job['request']['copyTo']);
         } else {
         } else {