Skip to content

Commit

Permalink
fix: Use StreamContextFactory from composer package to create stream …
Browse files Browse the repository at this point in the history
…context as this method already support HTTP proxy settings

[repman-http-proxy]
  • Loading branch information
Yoann MOROCUTTI committed Apr 26, 2023
1 parent 069ce08 commit 1e7caef
Showing 1 changed file with 13 additions and 9 deletions.
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

0 comments on commit 1e7caef

Please sign in to comment.