1
0
Fork 0

Merge remote-tracking branch 'jalliot/proxy2'

Conflicts:
	src/Composer/Downloader/FileDownloader.php
	src/Composer/Repository/PearRepository.php
pull/300/head
Jordi Boggiano 2012-02-14 17:01:15 +01:00
commit 256bfedea1
5 changed files with 78 additions and 30 deletions

View File

@ -13,6 +13,7 @@
namespace Composer\Command;
use Composer\Composer;
use Composer\Util\StreamContextFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
@ -39,7 +40,9 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
$latest = trim(file_get_contents('http://getcomposer.org/version'));
$ctx = StreamContextFactory::getContext();
$latest = trim(file_get_contents('http://getcomposer.org/version'), false, $ctx);
if (Composer::VERSION !== $latest) {
$output->writeln(sprintf("Updating to version <info>%s</info>.", $latest));
@ -47,7 +50,7 @@ EOT
$remoteFilename = 'http://getcomposer.org/composer.phar';
$localFilename = $_SERVER['argv'][0];
file_put_contents($localFilename, file_get_contents($remoteFilename));
copy($remoteFilename, $localFilename, $ctx);
} else {
$output->writeln("<info>You are using the latest composer version.</info>");
}

View File

@ -14,6 +14,7 @@ namespace Composer\Downloader;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
use Composer\Util\Filesystem;
use Composer\Util\StreamContextFactory;
/**
* Base downloader for file packages
@ -78,31 +79,14 @@ abstract class FileDownloader implements DownloaderInterface
}
}
// Handle system proxy
$params = array('http' => array());
if (isset($_SERVER['HTTP_PROXY'])) {
// http(s):// is not supported in proxy
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $_SERVER['HTTP_PROXY']);
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
}
$params['http'] = array(
'proxy' => $proxy,
'request_fulluri' => true,
);
}
$options = array();
if ($this->io->hasAuthorization($package->getSourceUrl())) {
$auth = $this->io->getAuthorization($package->getSourceUrl());
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$params['http'] = array_merge($params['http'], array('header' => "Authorization: Basic $authStr\r\n"));
$options['http']['header'] = "Authorization: Basic $authStr\r\n";
}
$ctx = stream_context_create($params);
stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));
$ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet')));
$this->io->overwrite(" Downloading: <comment>connection...</comment>", false);
@copy($url, $fileName, $ctx);

View File

@ -14,6 +14,7 @@ namespace Composer\Json;
use Composer\Repository\RepositoryManager;
use Composer\Composer;
use Composer\Util\StreamContextFactory;
/**
* Reads/writes json files.
@ -59,11 +60,12 @@ class JsonFile
*/
public function read()
{
$context = stream_context_create(array(
'http' => array('header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n")
));
$ctx = StreamContextFactory::getContext(array(
'http' => array(
'header' => 'User-Agent: Composer/'.Composer::VERSION."\r\n"
)));
$json = file_get_contents($this->path, false, $context);
$json = file_get_contents($this->path, false, $ctx);
if (!$json) {
throw new \RuntimeException('Could not read '.$this->path.', you are probably offline');
}

View File

@ -13,6 +13,7 @@
namespace Composer\Repository;
use Composer\Package\Loader\ArrayLoader;
use Composer\Util\StreamContextFactory;
/**
* @author Benjamin Eberlei <kontakt@beberlei.de>
@ -20,7 +21,8 @@ use Composer\Package\Loader\ArrayLoader;
*/
class PearRepository extends ArrayRepository
{
protected $url;
private $url;
private $streamContext;
public function __construct(array $config)
{
@ -41,6 +43,7 @@ class PearRepository extends ArrayRepository
set_error_handler(function($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $severity, $file, $line);
});
$this->streamContext = StreamContextFactory::getContext();
$this->fetchFromServer();
restore_error_handler();
}
@ -106,7 +109,7 @@ class PearRepository extends ArrayRepository
);
try {
$deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt");
$deps = file_get_contents($releaseLink . "/deps.".$pearVersion.".txt", false, $this->streamContext);
} catch (\ErrorException $e) {
if (strpos($e->getMessage(), '404')) {
continue;
@ -274,7 +277,7 @@ class PearRepository extends ArrayRepository
*/
private function requestXml($url)
{
$content = file_get_contents($url);
$content = file_get_contents($url, false, $this->streamContext);
if (!$content) {
throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.');
}
@ -283,4 +286,4 @@ class PearRepository extends ArrayRepository
return $dom;
}
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Util;
/**
* Allows the creation of a basic context supporting http proxy
*
* @author Jordan Alliot <jordan.alliot@gmail.com>
*/
final class StreamContextFactory
{
/**
* Creates a context supporting HTTP proxies
*
* @param array $defaultOptions Options to merge with the default
* @param array $defaultParams Parameters to specify on the context
* @return resource Default context
* @throws \RuntimeException if https proxy required and OpenSSL uninstalled
*/
static public function getContext(array $defaultOptions = array(), array $defaultParams = array())
{
$options = array('http' => array());
// Handle system proxy
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'];
// http(s):// is not supported in proxy
$proxy = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxy);
if (0 === strpos($proxy, 'ssl:') && !extension_loaded('openssl')) {
throw new \RuntimeException('You must enable the openssl extension to use a proxy over https');
}
$options['http'] = array(
'proxy' => $proxy,
'request_fulluri' => true,
);
}
$options = array_merge_recursive($options, $defaultOptions);
return stream_context_create($options, $defaultParams);
}
}