diff --git a/src/Composer/Util/StreamContextFactory.php b/src/Composer/Util/StreamContextFactory.php index df61b3c3e..8860e7f17 100644 --- a/src/Composer/Util/StreamContextFactory.php +++ b/src/Composer/Util/StreamContextFactory.php @@ -35,11 +35,9 @@ final class StreamContextFactory if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { // Some systems seem to rely on a lowercased version instead... $proxy = parse_url(isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']); - } else { - $proxy = false; } - if (false !== $proxy) { + if (!empty($proxy)) { $proxyURL = (isset($proxy['scheme']) ? $proxy['scheme'] : '') . '://'; $proxyURL .= isset($proxy['host']) ? $proxy['host'] : ''; @@ -70,16 +68,16 @@ final class StreamContextFactory } $auth = base64_encode($auth); - // Preserve headers if already set in default options + // Preserve headers if already set in default options if (isset($defaultOptions['http']['header'])) { - $defaultOptions['http']['header'] .= "Proxy-Authorization: Basic {$auth}\r\n"; + $defaultOptions['http']['header'] .= "Proxy-Authorization: Basic {$auth}\r\n"; } else { $options['http']['header'] = "Proxy-Authorization: Basic {$auth}\r\n"; } } } - $options = array_merge_recursive($options, $defaultOptions); + $options = array_replace_recursive($options, $defaultOptions); return stream_context_create($options, $defaultParams); } diff --git a/tests/Composer/Test/Util/StreamContextFactoryTest.php b/tests/Composer/Test/Util/StreamContextFactoryTest.php index dc60fe3bb..691625a94 100644 --- a/tests/Composer/Test/Util/StreamContextFactoryTest.php +++ b/tests/Composer/Test/Util/StreamContextFactoryTest.php @@ -71,6 +71,21 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase )), $options); } + public function testOptionsArePreserved() + { + $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/'; + + $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET', 'header' => "X-Foo: bar\r\n", 'request_fulluri' => false))); + $options = stream_context_get_options($context); + + $this->assertEquals(array('http' => array( + 'proxy' => 'tcp://proxyserver.net:3128', + 'request_fulluri' => false, + 'method' => 'GET', + 'header' => "X-Foo: bar\r\nProxy-Authorization: Basic " . base64_encode('username:password') . "\r\n" + )), $options); + } + public function testHttpProxyWithoutPort() { $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';