1
0
Fork 0

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

pull/630/head
Danny Berger 2012-04-26 21:38:12 -04:00
parent 762f469cd9
commit e7ba0c38a8
2 changed files with 36 additions and 7 deletions

View File

@ -37,7 +37,11 @@ final class StreamContextFactory
$proxy = isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']; $proxy = isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY'];
// http(s):// is not supported in 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')) { if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');

View File

@ -57,7 +57,7 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
public function testHttpProxy() 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/'; $_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
$context = StreamContextFactory::getContext(array('http' => array('method' => 'GET'))); $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->assertSame('http://proxyserver/', $_SERVER['HTTP_PROXY']);
$this->assertEquals(array('http' => array( $this->assertEquals(array('http' => array(
'proxy' => 'tcp://username:password@proxyserver.net:port/', 'proxy' => 'tcp://username:password@proxyserver.net:3128',
'request_fulluri' => true, 'request_fulluri' => true,
'method' => 'GET', 'method' => 'GET',
)), $options); )), $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')) { if (extension_loaded('openssl')) {
$context = StreamContextFactory::getContext(); $context = StreamContextFactory::getContext();
$options = stream_context_get_options($context); $options = stream_context_get_options($context);
$this->assertSame(array('http' => array( $this->assertEquals(array('http' => array(
'proxy' => 'ssl://proxyserver/', 'proxy' => $expected,
'request_fulluri' => true, 'request_fulluri' => true,
)), $options); )), $options);
} else { } 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'),
);
}
} }