1
0
Fork 0

Reformat some code and avoid adding proxy auth if no_proxy matched the url, refs #2017

pull/2175/head
Jordi Boggiano 2013-08-12 00:47:38 +02:00
parent 2492dea4a8
commit 13c7be2d7e
2 changed files with 37 additions and 43 deletions

View File

@ -135,10 +135,6 @@ class NoProxyPattern
// If the ip is within the range, including highest/lowest values, // If the ip is within the range, including highest/lowest values,
// then it's witin the CIDR range // then it's witin the CIDR range
if ($check >= $low && $check <= $high) { return $check >= $low && $check <= $high;
return true;
} else {
return false;
}
} }
} }

View File

@ -65,49 +65,47 @@ final class StreamContextFactory
$options['http']['proxy'] = $proxyURL; $options['http']['proxy'] = $proxyURL;
// Handle no_proxy directive // Handle no_proxy directive
if (!empty($_SERVER['no_proxy'])) { if (!empty($_SERVER['no_proxy']) && parse_url($url, PHP_URL_HOST)) {
$host = parse_url($url, PHP_URL_HOST); $pattern = new NoProxyPattern($_SERVER['no_proxy']);
if ($pattern->test($url)) {
if (!empty($host)) { unset($options['http']['proxy']);
$pattern = new NoProxyPattern($_SERVER['no_proxy']);
if ($pattern->test($url)) {
$options['http']['proxy'] = '';
}
} }
} }
// enabled request_fulluri unless it is explicitly disabled // add request_fulluri and authentication if we still have a proxy to connect to
switch (parse_url($url, PHP_URL_SCHEME)) { if (isset($options['http']['proxy'])) {
case 'http': // default request_fulluri to true // enabled request_fulluri unless it is explicitly disabled
$reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI'); switch (parse_url($url, PHP_URL_SCHEME)) {
if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) { case 'http': // default request_fulluri to true
$options['http']['request_fulluri'] = true; $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI');
} if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
break; $options['http']['request_fulluri'] = true;
case 'https': // default request_fulluri to true }
$reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI'); break;
if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) { case 'https': // default request_fulluri to true
$options['http']['request_fulluri'] = true; $reqFullUriEnv = getenv('HTTPS_PROXY_REQUEST_FULLURI');
} if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) {
break; $options['http']['request_fulluri'] = true;
} }
break;
if (isset($proxy['user'])) {
$auth = $proxy['user'];
if (isset($proxy['pass'])) {
$auth .= ':' . $proxy['pass'];
} }
$auth = base64_encode($auth);
// Preserve headers if already set in default options if (isset($proxy['user'])) {
if (isset($defaultOptions['http']['header'])) { $auth = $proxy['user'];
if (is_string($defaultOptions['http']['header'])) { if (isset($proxy['pass'])) {
$defaultOptions['http']['header'] = array($defaultOptions['http']['header']); $auth .= ':' . $proxy['pass'];
}
$auth = base64_encode($auth);
// Preserve headers if already set in default options
if (isset($defaultOptions['http']['header'])) {
if (is_string($defaultOptions['http']['header'])) {
$defaultOptions['http']['header'] = array($defaultOptions['http']['header']);
}
$defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}";
} else {
$options['http']['header'] = array("Proxy-Authorization: Basic {$auth}");
} }
$defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}";
} else {
$options['http']['header'] = array("Proxy-Authorization: Basic {$auth}");
} }
} }
} }