| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Amp\Http\Client\Connection\ConnectionLimitingPool; |
| 15 |
use Amp\Promise; |
| 16 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 |
|
| 18 |
/** |
| 19 |
* A factory to instantiate the best possible HTTP client for the runtime. |
| 20 |
* |
| 21 |
* @author Nicolas Grekas <[email protected]> |
| 22 |
*/ |
| 23 |
final class HttpClient |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @param array $defaultOptions Default request's options |
| 27 |
* @param int $maxHostConnections The maximum number of connections to a single host |
| 28 |
* @param int $maxPendingPushes The maximum number of pushed responses to accept in the queue |
| 29 |
* |
| 30 |
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options |
| 31 |
*/ |
| 32 |
public static function create(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface |
| 33 |
{ |
| 34 |
if ($amp = class_exists(ConnectionLimitingPool::class) && interface_exists(Promise::class)) { |
| 35 |
if (!\extension_loaded('curl')) { |
| 36 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 37 |
} |
| 38 |
|
| 39 |
// Skip curl when HTTP/2 push is unsupported or buggy, see https://bugs.php.net/77535 |
| 40 |
if (\PHP_VERSION_ID < 70217 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304) || !\defined('CURLMOPT_PUSHFUNCTION')) { |
| 41 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 42 |
} |
| 43 |
|
| 44 |
static $curlVersion = null; |
| 45 |
$curlVersion = $curlVersion ?? curl_version(); |
| 46 |
|
| 47 |
// HTTP/2 push crashes before curl 7.61 |
| 48 |
if (0x073D00 > $curlVersion['version_number'] || !(\CURL_VERSION_HTTP2 & $curlVersion['features'])) { |
| 49 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (\extension_loaded('curl')) { |
| 54 |
if ('\\' !== \DIRECTORY_SEPARATOR || isset($defaultOptions['cafile']) || isset($defaultOptions['capath']) || \ini_get('curl.cainfo') || \ini_get('openssl.cafile') || \ini_get('openssl.capath')) { |
| 55 |
return new CurlHttpClient($defaultOptions, $maxHostConnections, $maxPendingPushes); |
| 56 |
} |
| 57 |
|
| 58 |
@trigger_error('Configure the "curl.cainfo", "openssl.cafile" or "openssl.capath" php.ini setting to enable the CurlHttpClient', \E_USER_WARNING); |
| 59 |
} |
| 60 |
|
| 61 |
if ($amp) { |
| 62 |
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes); |
| 63 |
} |
| 64 |
|
| 65 |
@trigger_error((\extension_loaded('curl') ? 'Upgrade' : 'Install').' the curl extension or run "composer require amphp/http-client:^4.2.1" to perform async HTTP operations, including full HTTP/2 support', \E_USER_NOTICE); |
| 66 |
|
| 67 |
return new NativeHttpClient($defaultOptions, $maxHostConnections); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Creates a client that adds options (e.g. authentication headers) only when the request URL matches the provided base URI. |
| 72 |
*/ |
| 73 |
public static function createForBaseUri(string $baseUri, array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface |
| 74 |
{ |
| 75 |
$client = self::create([], $maxHostConnections, $maxPendingPushes); |
| 76 |
|
| 77 |
return ScopingHttpClient::forBaseUri($client, $baseUri, $defaultOptions); |
| 78 |
} |
| 79 |
} |
| 80 |
|