Skip to content

[HttpClient] make HttpClient::create() return an AmpHttpClient when amphp/http-client is found but curl is not or too old #35924

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

Merged
merged 1 commit into from
Mar 16, 2020
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* added `NoPrivateNetworkHttpClient` decorator
* added `AmpHttpClient`, a portable HTTP/2 implementation based on Amp
* added `LoggerAwareInterface` to `ScopingHttpClient` and `TraceableHttpClient`
* made `HttpClient::create()` return an `AmpHttpClient` when `amphp/http-client` is found but curl is not or too old

4.4.0
-----
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpClient;

use Amp\Http\Client\Connection\ConnectionLimitingPool;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
Expand All @@ -29,6 +30,25 @@ final class HttpClient
*/
public static function create(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50): HttpClientInterface
{
if ($amp = class_exists(ConnectionLimitingPool::class)) {
if (!\extension_loaded('curl')) {
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
}

// Skip curl when HTTP/2 push is unsupported or buggy, see https://bugs.php.net/77535
if (\PHP_VERSION_ID < 70217 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304) || !\defined('CURLMOPT_PUSHFUNCTION')) {
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
}

static $curlVersion = null;
$curlVersion = $curlVersion ?? curl_version();

// HTTP/2 push crashes before curl 7.61
if (0x073d00 > $curlVersion['version_number'] || !(CURL_VERSION_HTTP2 & $curlVersion['features'])) {
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
}
}

if (\extension_loaded('curl')) {
if ('\\' !== \DIRECTORY_SEPARATOR || ini_get('curl.cainfo') || ini_get('openssl.cafile') || ini_get('openssl.capath')) {
return new CurlHttpClient($defaultOptions, $maxHostConnections, $maxPendingPushes);
Expand All @@ -37,6 +57,10 @@ public static function create(array $defaultOptions = [], int $maxHostConnection
@trigger_error('Configure the "curl.cainfo", "openssl.cafile" or "openssl.capath" php.ini setting to enable the CurlHttpClient', E_USER_WARNING);
}

if ($amp) {
return new AmpHttpClient($defaultOptions, null, $maxHostConnections, $maxPendingPushes);
}

return new NativeHttpClient($defaultOptions, $maxHostConnections);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpClient/Internal/AmpClientState.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ public function connect(string $uri, ?ConnectContext $context = null, ?Cancellat
};
$connector->connector = new DnsConnector(new AmpResolver($this->dnsCache));

$context = (new ConnectContext())->withTlsContext($context);
$context = (new ConnectContext())
->withTcpNoDelay()
->withTlsContext($context);

if ($options['bindto']) {
if (file_exists($options['bindto'])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
namespace Symfony\Component\HttpClient\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\AmpHttpClient;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;

class HttpClientTest extends TestCase
{
public function testCreateClient()
{
if (\extension_loaded('curl') && ('\\' !== \DIRECTORY_SEPARATOR || ini_get('curl.cainfo') || ini_get('openssl.cafile') || ini_get('openssl.capath'))) {
if (\extension_loaded('curl') && ('\\' !== \DIRECTORY_SEPARATOR || ini_get('curl.cainfo') || ini_get('openssl.cafile') || ini_get('openssl.capath')) && 0x073d00 <= curl_version()['version_number']) {
$this->assertInstanceOf(CurlHttpClient::class, HttpClient::create());
} else {
$this->assertInstanceOf(NativeHttpClient::class, HttpClient::create());
$this->assertInstanceOf(AmpHttpClient::class, HttpClient::create());
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"symfony/service-contracts": "^1.0|^2"
},
"require-dev": {
"amphp/http-client": "^4.2",
"amphp/http-client": "^4.2.1",
"amphp/http-tunnel": "^1.0",
"amphp/socket": "^1.1",
"guzzlehttp/promises": "^1.3.1",
Expand Down