Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 31, 2024
1 parent f882674 commit 3ddd184
Showing 1 changed file with 53 additions and 12 deletions.
65 changes: 53 additions & 12 deletions src/Handler/CurlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function create(RequestInterface $request, array $options): EasyHandle
$protocolVersion = $request->getProtocolVersion();

if ('2' === $protocolVersion || '2.0' === $protocolVersion) {
if (!defined('CURL_VERSION_HTTP2') || !defined('CURL_SSLVERSION_TLSv1_2')) {
throw new ConnectException('HTTP/2 is supported by the cURL handler, however the version of libcurl is too old or built with HTTP/2 and TLS 1.2 support disabled.', $request);
if (!self::supportsHttp2()) {
throw new ConnectException('HTTP/2 is supported by the cURL handler, however libcurl is built without HTTP/2 support.', $request);
}
} elseif ('1.0' !== $protocolVersion && '1.1' !== $protocolVersion) {
throw new ConnectException(sprintf('HTTP/%s is not supported by the cURL handler.', $protocolVersion), $request);
Expand Down Expand Up @@ -82,6 +82,42 @@ public function create(RequestInterface $request, array $options): EasyHandle
return $easy;
}

private static function supportsHttp2(): bool
{
static $supportsHttp2 = null;

if (null === $supportsHttp2) {
$supportsHttp2 = self::supportsTls12()
&& defined('CURL_VERSION_HTTP2')
&& (\CURL_VERSION_HTTP2 & \curl_version()['features']);

Check failure on line 92 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 92 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 92 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.
}

return $supportsHttp2;
}

private static function supportsTls12(): bool
{
static $supportsTls12 = null;

if (null === $supportsTls12) {
$supportsTls12 = \CURL_SSLVERSION_TLSv1_2 & \curl_version()['features'];

Check failure on line 103 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 103 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 103 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.
}

return $supportsTls12;
}

private static function supportsTls13(): bool
{
static $supportsTls13 = null;

if (null === $supportsTls13) {
$supportsTls13 = defined('CURL_SSLVERSION_TLSv1_3')
&& (\CURL_SSLVERSION_TLSv1_3 & \curl_version()['features']);

Check failure on line 115 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 115 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.

Check failure on line 115 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Cannot access offset 'features' on array|false.
}

return $supportsTls12;

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $supportsTls12

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Handler/CurlFactory.php:118:16: UndefinedVariable: Cannot find referenced variable $supportsTls12 (see https://psalm.dev/024)

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $supportsTls12

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Handler/CurlFactory.php:118:16: UndefinedVariable: Cannot find referenced variable $supportsTls12 (see https://psalm.dev/024)

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan

Undefined variable: $supportsTls12

Check failure on line 118 in src/Handler/CurlFactory.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Handler/CurlFactory.php:118:16: UndefinedVariable: Cannot find referenced variable $supportsTls12 (see https://psalm.dev/024)
}

public function release(EasyHandle $easy): void
{
$resource = $easy->handle;
Expand Down Expand Up @@ -157,7 +193,7 @@ private static function finishError(callable $handler, EasyHandle $easy, CurlFac
'error' => \curl_error($easy->handle),
'appconnect_time' => \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME),
] + \curl_getinfo($easy->handle);
$ctx[self::CURL_VERSION_STR] = \curl_version()['version'];
$ctx[self::CURL_VERSION_STR] = self::getCurlVersion();
$factory->release($easy);

// Retry when nothing is present or when curl failed to rewind.
Expand All @@ -168,6 +204,17 @@ private static function finishError(callable $handler, EasyHandle $easy, CurlFac
return self::createRejection($easy, $ctx);
}

private static function getCurlVersion(): string
{
static $curlVersion = null;

if (null === $curlVersion) {
$curlVersion = \curl_version()['version'];
}

return $curlVersion;
}

private static function createRejection(EasyHandle $easy, array $ctx): PromiseInterface
{
static $connectionErrors = [
Expand Down Expand Up @@ -477,30 +524,24 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
) {
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2;
} elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) {
if (!defined('CURL_SSLVERSION_TLSv1_3')) {
if (!self::supportsTls12()) {
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL');
}
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3;
} else {
throw new \InvalidArgumentException('Invalid crypto_method request option: unknown version provided');
}
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT === $options['crypto_method']) {
if (!defined('CURL_SSLVERSION_TLSv1_0')) {
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.0 not supported by your version of cURL');
}
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_0;
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT === $options['crypto_method']) {
if (!defined('CURL_SSLVERSION_TLSv1_1')) {
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.1 not supported by your version of cURL');
}
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_1;
} elseif (\STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT === $options['crypto_method']) {
if (!defined('CURL_SSLVERSION_TLSv1_2')) {
if (!self::supportsTls12()) {
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.2 not supported by your version of cURL');
}
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_2;
} elseif (defined('STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT') && \STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT === $options['crypto_method']) {
if (!defined('CURL_SSLVERSION_TLSv1_3')) {
if (!self::supportsTls13()) {
throw new \InvalidArgumentException('Invalid crypto_method request option: TLS 1.3 not supported by your version of cURL');
}
$conf[\CURLOPT_SSLVERSION] = \CURL_SSLVERSION_TLSv1_3;
Expand Down

0 comments on commit 3ddd184

Please sign in to comment.