Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StreamContextFactory.php

function StreamContextFactory::initOptions

@phpstan-return array{http: array{header: string[], proxy?: string, request_fulluri: bool}, ssl?: mixed[]}

Parameters

non-empty-string $url:

mixed[] $options:

bool $forCurl When true, will not add proxy values as these are handled separately:

Return value

array formatted as a stream context array

2 calls to StreamContextFactory::initOptions()
CurlDownloader::initDownload in vendor/composer/composer/src/Composer/Util/Http/CurlDownloader.php
StreamContextFactory::getContext in vendor/composer/composer/src/Composer/Util/StreamContextFactory.php
Creates a context supporting HTTP proxies

File

vendor/composer/composer/src/Composer/Util/StreamContextFactory.php, line 66

Class

StreamContextFactory
Allows the creation of a basic context supporting http proxy

Namespace

Composer\Util

Code

public static function initOptions(string $url, array $options, bool $forCurl = false) : array {
    // Make sure the headers are in an array form
    if (!isset($options['http']['header'])) {
        $options['http']['header'] = [];
    }
    if (is_string($options['http']['header'])) {
        $options['http']['header'] = explode("\r\n", $options['http']['header']);
    }
    // Add stream proxy options if there is a proxy
    if (!$forCurl) {
        $proxy = ProxyManager::getInstance()->getProxyForRequest($url);
        $proxyOptions = $proxy->getContextOptions();
        if ($proxyOptions !== null) {
            $isHttpsRequest = 0 === strpos($url, 'https://');
            if ($proxy->isSecure()) {
                if (!extension_loaded('openssl')) {
                    throw new TransportException('You must enable the openssl extension to use a secure proxy.');
                }
                if ($isHttpsRequest) {
                    throw new TransportException('You must enable the curl extension to make https requests through a secure proxy.');
                }
            }
            elseif ($isHttpsRequest && !extension_loaded('openssl')) {
                throw new TransportException('You must enable the openssl extension to make https requests through a proxy.');
            }
            // Header will be a Proxy-Authorization string or not set
            if (isset($proxyOptions['http']['header'])) {
                $options['http']['header'][] = $proxyOptions['http']['header'];
                unset($proxyOptions['http']['header']);
            }
            $options = array_replace_recursive($options, $proxyOptions);
        }
    }
    if (defined('HHVM_VERSION')) {
        $phpVersion = 'HHVM ' . HHVM_VERSION;
    }
    else {
        $phpVersion = 'PHP ' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION;
    }
    if ($forCurl) {
        $curl = curl_version();
        $httpVersion = 'cURL ' . $curl['version'];
    }
    else {
        $httpVersion = 'streams';
    }
    if (!isset($options['http']['header']) || false === stripos(implode('', $options['http']['header']), 'user-agent')) {
        $platformPhpVersion = PlatformRepository::getPlatformPhpVersion();
        $options['http']['header'][] = sprintf('User-Agent: Composer/%s (%s; %s; %s; %s%s%s)', Composer::getVersion(), function_exists('php_uname') ? php_uname('s') : 'Unknown', function_exists('php_uname') ? php_uname('r') : 'Unknown', $phpVersion, $httpVersion, $platformPhpVersion ? '; Platform-PHP ' . $platformPhpVersion : '', Platform::getEnv('CI') ? '; CI' : '');
    }
    return $options;
}
RSS feed
Powered by Drupal