1
0
Fork 0

Proxy credentials are now passed as part of HTTP headers

pull/649/head
Maxim Chernyshev 2012-05-02 08:26:27 +08:00
parent 7f0a0857c0
commit 419a1cedf0
2 changed files with 39 additions and 12 deletions

View File

@ -36,17 +36,45 @@ final class StreamContextFactory
// Some systems seem to rely on a lowercased version instead... // Some systems seem to rely on a lowercased version instead...
$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 $proxyURL = parse_url($proxy, PHP_URL_SCHEME) . "://";
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy); $proxyURL .= parse_url($proxy, PHP_URL_HOST);
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) { $proxyPort = parse_url($proxy, PHP_URL_PORT);
if (isset($proxyPort)) {
$proxyURL .= ":" . $proxyPort;
}
// http(s):// is not supported in proxy
$proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL);
if (0 === strpos($proxyURL, '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');
} }
$options['http'] = array( $options['http'] = array(
'proxy' => $proxy, 'proxy' => $proxyURL,
'request_fulluri' => true, 'request_fulluri' => true,
); );
// Extract authentication credentials from the proxy url
$user = parse_url($proxy, PHP_URL_USER);
$pass = parse_url($proxy, PHP_URL_PASS);
if (isset($user)) {
$auth = $user;
if (isset($pass)) {
$auth .= ":{$pass}";
}
$auth = base64_encode($auth);
// Preserve headers if already set in default options
if (isset($defaultOptions['http']) && isset($defaultOptions['http']['header'])) {
$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_merge_recursive($options, $defaultOptions);

View File

@ -57,31 +57,30 @@ 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:1234';
$_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')));
$options = stream_context_get_options($context); $options = stream_context_get_options($context);
$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://proxyserver.net:1234',
'request_fulluri' => true, 'request_fulluri' => true,
'method' => 'GET', 'method' => 'GET',
'header' => "Proxy-Authorization: Basic " . base64_encode('username:password') . "\r\n"
)), $options); )), $options);
} }
public function testSSLProxy() public function testSSLProxy()
{ {
$_SERVER['http_proxy'] = 'https://proxyserver/'; $_SERVER['http_proxy'] = 'https://proxyserver';
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->assertSame(array('http' => array(
'proxy' => 'ssl://proxyserver/', 'proxy' => 'ssl://proxyserver',
'request_fulluri' => true, 'request_fulluri' => true,
)), $options); )), $options);
} else { } else {