Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Packagist proxy through HTTP Proxy #646

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Service/Downloader/ReactDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Buddy\Repman\Kernel;
use Buddy\Repman\Service\Downloader;
use Clue\React\Mq\Queue;
use Composer\Util\StreamContextFactory;
use Munus\Control\Option;
use Psr\Http\Message\ResponseInterface;
use React\EventLoop\Loop;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function getContents(string $url, array $headers = [], callable $notFound
{
$retries = 3;
do {
$stream = @fopen($url, 'r', false, $this->createContext($headers));
$stream = @fopen($url, 'r', false, $this->createContext($url, $headers));
if ($stream !== false) {
return Option::some($stream);
}
Expand Down Expand Up @@ -88,15 +89,18 @@ public function run(): void
*
* @return resource
*/
private function createContext(array $headers = [])
private function createContext(string $url, array $headers = [])
{
return stream_context_create([
'http' => [
'header' => array_merge([sprintf('User-Agent: %s', $this->userAgent())], $headers),
'follow_location' => 1,
'max_redirects' => 20,
],
]);
return StreamContextFactory::getContext(
$url,
[
'http' => [
'header' => array_merge([sprintf('User-Agent: %s', $this->userAgent())], $headers),
'follow_location' => 1,
'max_redirects' => 20,
],
]
);
}

private function userAgent(): string
Expand Down
86 changes: 86 additions & 0 deletions tests/Unit/Service/Downloader/ReactDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Buddy\Repman\Tests\Unit\Service\Downloader;

use Buddy\Repman\Kernel;
use Buddy\Repman\Service\Downloader\ReactDownloader;
use Munus\Control\Option;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -58,4 +59,89 @@ public function testAsyncContent(): void
});
$downloader->run();
}

/**
* @throws \ReflectionException
*/
public function testStreamContext(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = null;

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertArrayNotHasKey('proxy', $options['http']);
}

/**
* @throws \ReflectionException
*/
public function testStreamContextHttpProxy(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertEquals('tcp://proxy.repman.io:80', $options['http']['proxy']);
}

/**
* @throws \ReflectionException
*/
public function testStreamContextNoProxy(): void
{
$_SERVER['HTTP_PROXY'] = $_SERVER['http_proxy'] = $_SERVER['HTTPS_PROXY'] = $_SERVER['https_proxy'] = 'http://proxy.repman.io';
$_SERVER['NO_PROXY'] = $_SERVER['no_proxy'] = '.repman.io';

$downloader = new ReactDownloader();
$createContextMethod = new \ReflectionMethod(ReactDownloader::class, 'createContext');
$createContextMethod->setAccessible(true);

$context = $createContextMethod->invoke($downloader, 'https://repman.io');
$options = stream_context_get_options($context);
self::assertEquals(20, $options['http']['max_redirects']);
self::assertEquals(1, $options['http']['follow_location']);
self::assertEquals(
sprintf(
'User-Agent: Repman/%s (%s; %s; %s)',
Kernel::REPMAN_VERSION,
php_uname('s'),
php_uname('r'),
'PHP '.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION
),
$options['http']['header'][0]
);
self::assertArrayNotHasKey('proxy', $options['http']);
}
}