1
0
Fork 0

Tweaks to new proxying code, refs #9324

pull/9330/head
Jordi Boggiano 2020-10-24 10:36:39 +02:00
parent 62fd612e63
commit 62eff8e979
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
6 changed files with 31 additions and 24 deletions

View File

@ -234,7 +234,7 @@ class CurlDownloader
'reject' => $reject, 'reject' => $reject,
); );
$usingProxy = $proxy->getLastProxy(' using proxy (%s)'); $usingProxy = $proxy->getFormattedUrl(' using proxy (%s)');
$ifModified = false !== stripos(implode(',', $options['http']['header']), 'if-modified-since:') ? ' if modified' : ''; $ifModified = false !== stripos(implode(',', $options['http']['header']), 'if-modified-since:') ? ' if modified' : '';
if ($attributes['redirects'] === 0) { if ($attributes['redirects'] === 0) {
$this->io->writeError('Downloading ' . Url::sanitize($url) . $usingProxy . $ifModified, true, IOInterface::DEBUG); $this->io->writeError('Downloading ' . Url::sanitize($url) . $usingProxy . $ifModified, true, IOInterface::DEBUG);

View File

@ -85,34 +85,42 @@ class ProxyManager
$scheme = parse_url($requestUrl, PHP_URL_SCHEME) ?: 'http'; $scheme = parse_url($requestUrl, PHP_URL_SCHEME) ?: 'http';
$proxyUrl = ''; $proxyUrl = '';
$options = array(); $options = array();
$lastProxy = ''; $formattedProxyUrl = '';
if ($this->hasProxy && $this->fullProxy[$scheme]) { if ($this->hasProxy && $this->fullProxy[$scheme]) {
if ($this->noProxy($requestUrl)) { if ($this->noProxy($requestUrl)) {
$lastProxy = 'excluded by no_proxy'; $formattedProxyUrl = 'excluded by no_proxy';
} else { } else {
$proxyUrl = $this->fullProxy[$scheme]; $proxyUrl = $this->fullProxy[$scheme];
$options = $this->streams[$scheme]['options']; $options = $this->streams[$scheme]['options'];
ProxyHelper::setRequestFullUri($requestUrl, $options); ProxyHelper::setRequestFullUri($requestUrl, $options);
$lastProxy = $this->safeProxy[$scheme]; $formattedProxyUrl = $this->safeProxy[$scheme];
} }
} }
return new RequestProxy($proxyUrl, $options, $lastProxy); return new RequestProxy($proxyUrl, $options, $formattedProxyUrl);
} }
/** /**
* Returns true if a proxy is being used * Returns true if a proxy is being used
* *
* @param string|null $message Set to safe proxy values
* @return bool If false any error will be in $message * @return bool If false any error will be in $message
*/ */
public function getStatus(&$message) public function isProxying()
{ {
$message = $this->hasProxy ? $this->info : $this->error;
return $this->hasProxy; return $this->hasProxy;
} }
/**
* Returns proxy configuration info which can be shown to the user
*
* @return string|null Safe proxy URL or an error message if setting up proxy failed or null if no proxy was configured
*/
public function getFormattedProxy()
{
return $this->hasProxy ? $this->info : $this->error;
}
/** /**
* Initializes proxy values from the environment * Initializes proxy values from the environment
*/ */

View File

@ -22,21 +22,19 @@ class RequestProxy
{ {
private $contextOptions; private $contextOptions;
private $isSecure; private $isSecure;
private $lastProxy; private $formattedUrl;
private $safeUrl;
private $url; private $url;
/** /**
* @param string $url * @param string $url
* @param array $contextOptions * @param array $contextOptions
* @param string $lastProxy * @param string $formattedUrl
*/ */
public function __construct($url, array $contextOptions, $lastProxy) public function __construct($url, array $contextOptions, $formattedUrl)
{ {
$this->url = $url; $this->url = $url;
$this->contextOptions = $contextOptions; $this->contextOptions = $contextOptions;
$this->lastProxy = $lastProxy; $this->formattedUrl = $formattedUrl;
$this->safeUrl = Url::sanitize($url);
$this->isSecure = 0 === strpos($url, 'https://'); $this->isSecure = 0 === strpos($url, 'https://');
} }
@ -56,12 +54,12 @@ class RequestProxy
* @param string|null $format Output format specifier * @param string|null $format Output format specifier
* @return string Safe proxy, no proxy or empty * @return string Safe proxy, no proxy or empty
*/ */
public function getLastProxy($format = '') public function getFormattedUrl($format = '')
{ {
$result = ''; $result = '';
if ($this->lastProxy) { if ($this->formattedUrl) {
$format = $format ?: '%s'; $format = $format ?: '%s';
$result = sprintf($format, $this->lastProxy); $result = sprintf($format, $this->formattedUrl);
} }
return $result; return $result;

View File

@ -255,7 +255,7 @@ class RemoteFilesystem
$ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet'))); $ctx = StreamContextFactory::getContext($fileUrl, $options, array('notification' => array($this, 'callbackGet')));
$proxy = ProxyManager::getInstance()->getProxyForRequest($fileUrl); $proxy = ProxyManager::getInstance()->getProxyForRequest($fileUrl);
$usingProxy = $proxy->getLastProxy(' using proxy (%s)'); $usingProxy = $proxy->getFormattedUrl(' using proxy (%s)');
$this->io->writeError((strpos($origFileUrl, 'http') === 0 ? 'Downloading ' : 'Reading ') . Url::sanitize($origFileUrl) . $usingProxy, true, IOInterface::DEBUG); $this->io->writeError((strpos($origFileUrl, 'http') === 0 ? 'Downloading ' : 'Reading ') . Url::sanitize($origFileUrl) . $usingProxy, true, IOInterface::DEBUG);
unset($origFileUrl, $proxy, $usingProxy); unset($origFileUrl, $proxy, $usingProxy);

View File

@ -82,7 +82,7 @@ class ProxyManagerTest extends TestCase
$this->assertSame($expectedOptions, $proxy->getContextOptions()); $this->assertSame($expectedOptions, $proxy->getContextOptions());
$this->assertSame($expectedSecure, $proxy->isSecure()); $this->assertSame($expectedSecure, $proxy->isSecure());
$message = $proxy->getLastProxy(); $message = $proxy->getFormattedUrl();
if ($expectedMessage) { if ($expectedMessage) {
$condition = stripos($message, $expectedMessage) !== false; $condition = stripos($message, $expectedMessage) !== false;
@ -134,7 +134,8 @@ class ProxyManagerTest extends TestCase
{ {
$_SERVER = array_merge($_SERVER, $server); $_SERVER = array_merge($_SERVER, $server);
$proxyManager = ProxyManager::getInstance(); $proxyManager = ProxyManager::getInstance();
$status = $proxyManager->getStatus($message); $status = $proxyManager->isProxying();
$message = $proxyManager->getFormattedProxy();
$this->assertSame($expectedStatus, $status); $this->assertSame($expectedStatus, $status);

View File

@ -38,17 +38,17 @@ class RequestProxyTest extends TestCase
} }
/** /**
* @dataProvider dataLastProxy * @dataProvider dataProxyUrl
*/ */
public function testGetLastProxyFormat($url, $format, $expected) public function testGetFormattedUrlFormat($url, $format, $expected)
{ {
$proxy = new RequestProxy($url, array(), $url); $proxy = new RequestProxy($url, array(), $url);
$message = $proxy->getLastProxy($format); $message = $proxy->getFormattedUrl($format);
$this->assertSame($expected, $message); $this->assertSame($expected, $message);
} }
public function dataLastProxy() public function dataProxyUrl()
{ {
$format = 'proxy (%s)'; $format = 'proxy (%s)';