1
0
Fork 0

Precious community feedback-based refactoring

pull/649/head
Maxim Chernyshev 2012-05-02 17:21:58 +08:00
parent 1ae0a1b7af
commit 5294cb222c
2 changed files with 23 additions and 24 deletions

View File

@ -34,21 +34,23 @@ final class StreamContextFactory
// Handle system proxy // Handle system proxy
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 = isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']; $proxy = parse_url(isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']);
} else {
$proxyURL = parse_url($proxy, PHP_URL_SCHEME) . "://"; $proxy = false;
$proxyURL .= parse_url($proxy, PHP_URL_HOST); }
$proxyPort = parse_url($proxy, PHP_URL_PORT); if (false !== $proxy) {
$proxyURL = (isset($proxy['scheme']) ? $proxy['scheme'] : '') . '://';
if (isset($proxyPort)) { $proxyURL .= isset($proxy['host']) ? $proxy['host'] : '';
$proxyURL .= ":" . $proxyPort;
} else if ('http://' == substr($proxyURL, 0, 7)) { if (isset($proxy['port'])) {
$proxyURL .= ":" . $proxy['port'];
} elseif ('http://' == substr($proxyURL, 0, 7)) {
$proxyURL .= ":80"; $proxyURL .= ":80";
} else if ('https://' == substr($proxyURL, 0, 8)) { } elseif ('https://' == substr($proxyURL, 0, 8)) {
$proxyURL .= ":443"; $proxyURL .= ":443";
} }
// http(s):// is not supported in proxy // http(s):// is not supported in proxy
$proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL); $proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL);
@ -60,18 +62,14 @@ final class StreamContextFactory
'proxy' => $proxyURL, 'proxy' => $proxyURL,
'request_fulluri' => true, 'request_fulluri' => true,
); );
// Extract authentication credentials from the proxy url if (isset($proxy['user'])) {
$user = parse_url($proxy, PHP_URL_USER); $auth = $proxy['user'];
$pass = parse_url($proxy, PHP_URL_PASS); if (isset($proxy['pass'])) {
$auth .= ':' . $proxy['pass'];
if (isset($user)) {
$auth = $user;
if (isset($pass)) {
$auth .= ":{$pass}";
} }
$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";
@ -80,8 +78,9 @@ final class StreamContextFactory
} }
} }
} }
$options = array_merge_recursive($options, $defaultOptions); $options = array_merge_recursive($options, $defaultOptions);
return stream_context_create($options, $defaultParams); return stream_context_create($options, $defaultParams);
} }
} }

View File

@ -96,7 +96,7 @@ class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
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->assertEquals(array('http' => array( $this->assertEquals(array('http' => array(
'proxy' => $expected, 'proxy' => $expected,
'request_fulluri' => true, 'request_fulluri' => true,