From a9e07568c5f405b660f242e16d084307fc271c49 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Tue, 27 Aug 2024 11:31:03 +0700 Subject: [PATCH] [PHP 8.4] Add `CURL_HTTP_VERSION_3(ONLY)` constants If the underlying Curl build supports it, setting the HTTP version should be theoretically possible. The availability of the constants do not guarantee the feature support, so declaring them in the polyfill should be same and effective. - PR that added constants: [php-src#15350](https://github.com/php/php-src/pull/15350) - libcurl doc on `CURLOPT_HTTP_VERSION`](https://curl.se/libcurl/c/CURLOPT_HTTP_VERSION.html) - [PHP.Watch: HTTP/3 Request with PHP Curl Extension](https://php.watch/articles/php-curl-http3) - Codex for [`CURL_HTTP_VERSION_3`](https://php.watch/codex/CURL_HTTP_VERSION_3) and [`CURL_HTTP_VERSION_3ONLY`](https://php.watch/codex/CURL_HTTP_VERSION_3ONLY) Fixes GH-488. --- README.md | 1 + src/Php84/README.md | 1 + src/Php84/bootstrap.php | 5 +++++ tests/Php84/Php84Test.php | 16 ++++++++++++++++ 4 files changed, 23 insertions(+) diff --git a/README.md b/README.md index 91dd2b5c1..7517fba8c 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Polyfills are provided for: - the `array_find`, `array_find_key`, `array_any` and `array_all` functions introduced in PHP 8.4; - the `Deprecated` attribute introduced in PHP 8.4; - the `mb_trim`, `mb_ltrim` and `mb_rtrim` functions introduced in PHP 8.4; +- the `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants introduced in PHP 8.4; It is strongly recommended to upgrade your PHP version and/or install the missing extensions whenever possible. This polyfill should be used only when there is no diff --git a/src/Php84/README.md b/src/Php84/README.md index 96921439d..bd56cdc95 100644 --- a/src/Php84/README.md +++ b/src/Php84/README.md @@ -6,6 +6,7 @@ This component provides features added to PHP 8.4 core: - [`mb_ucfirst` and `mb_lcfirst`](https://wiki.php.net/rfc/mb_ucfirst) - [`array_find`, `array_find_key`, `array_any` and `array_all`](https://wiki.php.net/rfc/array_find) - [`Deprecated`](https://wiki.php.net/rfc/deprecated_attribute) +- `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants More information can be found in the [main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md). diff --git a/src/Php84/bootstrap.php b/src/Php84/bootstrap.php index 5a86f206f..357c27239 100644 --- a/src/Php84/bootstrap.php +++ b/src/Php84/bootstrap.php @@ -15,6 +15,11 @@ return; } +if (!defined('CURL_HTTP_VERSION_3')) { + define('CURL_HTTP_VERSION_3', 30); + define('CURL_HTTP_VERSION_3ONLY', 31); +} + if (!function_exists('array_find')) { function array_find(array $array, callable $callback) { return p\Php84::array_find($array, $callback); } } diff --git a/tests/Php84/Php84Test.php b/tests/Php84/Php84Test.php index 1b05902d1..ff36e78c5 100644 --- a/tests/Php84/Php84Test.php +++ b/tests/Php84/Php84Test.php @@ -64,6 +64,22 @@ public function testArrayAll(array $array, callable $callback, bool $expected) $this->assertSame($expected, array_all($array, $callback)); } + public function testCurlHttp3Constants() { + if (!function_exists('curl_version')) { + $this->markTestSkipped('Curl extension is not available.'); + } + + // If the curl_version()['features'] bitmask contains the bits for CURL_VERSION_HTTP3, + // it means Curl supports HTTP3. + if (!(curl_version()['features'] & 33554432)) { // 33554432 = CURL_VERSION_HTTP3 + $this->markTestSkipped('Curl extension is not built with HTTP3 support'); + } + + $ch = curl_init(); + $this->assertTrue(curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY)); + $this->assertTrue(curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3)); + } + public static function ucFirstDataProvider(): array { return [