|
@@ -35,20 +35,48 @@ final class StreamContextFactory
|
|
|
if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) {
|
|
|
// Some systems seem to rely on a lowercased version instead...
|
|
|
$proxy = isset($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY'];
|
|
|
-
|
|
|
+
|
|
|
+ $proxyURL = parse_url($proxy, PHP_URL_SCHEME) . "://";
|
|
|
+ $proxyURL .= parse_url($proxy, PHP_URL_HOST);
|
|
|
+
|
|
|
+ $proxyPort = parse_url($proxy, PHP_URL_PORT);
|
|
|
+
|
|
|
+ if (isset($proxyPort)) {
|
|
|
+ $proxyURL .= ":" . $proxyPort;
|
|
|
+ }
|
|
|
+
|
|
|
// http(s):// is not supported in proxy
|
|
|
- $proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy);
|
|
|
+ $proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL);
|
|
|
|
|
|
- if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
|
|
|
+ if (0 === strpos($proxyURL, 'ssl:') && !extension_loaded('openssl')) {
|
|
|
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
|
|
|
}
|
|
|
|
|
|
$options['http'] = array(
|
|
|
- 'proxy' => $proxy,
|
|
|
+ 'proxy' => $proxyURL,
|
|
|
'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);
|
|
|
|
|
|
return stream_context_create($options, $defaultParams);
|