فهرست منبع

Add default http(s) ports to proxy configuration if they are missing.

Danny Berger 13 سال پیش
والد
کامیت
e7ba0c38a8
2فایلهای تغییر یافته به همراه36 افزوده شده و 7 حذف شده
  1. 5 1
      src/Composer/Util/StreamContextFactory.php
  2. 31 6
      tests/Composer/Test/Util/StreamContextFactoryTest.php

+ 5 - 1
src/Composer/Util/StreamContextFactory.php

@@ -37,7 +37,11 @@ final class StreamContextFactory
             $proxy = isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY'];
 
             // http(s):// is not supported in proxy
-            $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy);
+            if ('http://' == substr($proxy, 0, 7)) {
+                $proxy = 'tcp://' . rtrim(substr($proxy, 7), '/') . (parse_url($proxy, PHP_URL_PORT) ? '' : ':80');
+            } else if ('https://' == substr($proxy, 0, 8)) {
+                $proxy = 'ssl://' . rtrim(substr($proxy, 8), '/') . (parse_url($proxy, PHP_URL_PORT) ? '' : ':443');
+            }
 
             if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
                 throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');

+ 31 - 6
tests/Composer/Test/Util/StreamContextFactoryTest.php

@@ -57,7 +57,7 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testHttpProxy()
     {
-        $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:port/';
+        $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
         $_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
 
         $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
@@ -66,22 +66,39 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('http://proxyserver/', $_SERVER['HTTP_PROXY']);
 
         $this->assertEquals(array('http' => array(
-            'proxy' => 'tcp://username:password@proxyserver.net:port/',
+            'proxy' => 'tcp://username:password@proxyserver.net:3128',
             'request_fulluri' => true,
             'method' => 'GET',
         )), $options);
     }
 
-    public function testSSLProxy()
+    public function testHttpProxyWithoutPort()
     {
-        $_SERVER['http_proxy'] = 'https://proxyserver/';
+        $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
+
+        $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
+        $options = stream_context_get_options($context);
+
+        $this->assertEquals(array('http' => array(
+            'proxy' => 'tcp://username:password@proxyserver.net:80',
+            'request_fulluri' => true,
+            'method' => 'GET',
+        )), $options);
+    }
+
+    /**
+     * @dataProvider dataSSLProxy
+     */
+    public function testSSLProxy($expected, $proxy)
+    {
+        $_SERVER['http_proxy'] = $proxy;
 
         if (extension_loaded('openssl')) {
             $context = StreamContextFactory::getContext();
             $options = stream_context_get_options($context);
 
-            $this->assertSame(array('http' => array(
-                'proxy' => 'ssl://proxyserver/',
+            $this->assertEquals(array('http' => array(
+                'proxy' => $expected,
                 'request_fulluri' => true,
             )), $options);
         } else {
@@ -93,4 +110,12 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
             }
         }
     }
+
+    public function dataSSLProxy()
+    {
+        return array(
+            array('ssl://proxyserver:443', 'https://proxyserver/'),
+            array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
+        );
+    }
 }