1
0
Fork 0

Fix default options handling in StreamContextFactory

pull/658/head
Jordi Boggiano 2012-05-06 13:50:18 +02:00
parent 00e4d53bcf
commit 072f4397a0
2 changed files with 19 additions and 6 deletions

View File

@ -35,11 +35,9 @@ final class StreamContextFactory
if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) {
// Some systems seem to rely on a lowercased version instead... // Some systems seem to rely on a lowercased version instead...
$proxy = parse_url(isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']); $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['scheme']) ? $proxy['scheme'] : '') . '://';
$proxyURL .= isset($proxy['host']) ? $proxy['host'] : ''; $proxyURL .= isset($proxy['host']) ? $proxy['host'] : '';
@ -70,16 +68,16 @@ final class StreamContextFactory
} }
$auth = base64_encode($auth); $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'])) { if (isset($defaultOptions['http']['header'])) {
$defaultOptions['http']['header'] .= "Proxy-Authorization: Basic {$auth}\r\n"; $defaultOptions['http']['header'] .= "Proxy-Authorization: Basic {$auth}\r\n";
} else { } else {
$options['http']['header'] = "Proxy-Authorization: Basic {$auth}\r\n"; $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); return stream_context_create($options, $defaultParams);
} }

View File

@ -71,6 +71,21 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
)), $options); )), $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() public function testHttpProxyWithoutPort()
{ {
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net'; $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';