From 742dbc84c4c4cf614ce0d3b6388610e30e3557d4 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 20 Sep 2024 10:11:02 +0700 Subject: [PATCH 1/9] feat: CURL option force_ip_resolve --- system/HTTP/CURLRequest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 6411e0df6353..468870445773 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -649,6 +649,17 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) $this->setHeader('Content-Length', (string) strlen($json)); } + // Resolve IP + if (! empty($config['force_ip_resolve'])) { + $protocolVersion = $config['force_ip_resolve']; + + if ($protocolVersion === 'v4') { + $curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4; + } elseif ($protocolVersion === 'v6') { + $curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6; + } + } + // version if (! empty($config['version'])) { $version = sprintf('%.1F', $config['version']); From 7cd7b0f770c3d69b33bccc4d670ce3d4c19507c0 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 20 Sep 2024 10:11:35 +0700 Subject: [PATCH 2/9] test: CURL option force_ip_resolve --- tests/system/HTTP/CURLRequestTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index 32ae20c1b2da..7f1a4d2491e3 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -1187,6 +1187,30 @@ public function testHTTPv3(): void $this->assertSame(CURL_HTTP_VERSION_3, $options[CURLOPT_HTTP_VERSION]); } + public function testForceResolveIPv4(): void + { + $this->request->request('POST', '/post', [ + 'force_ip_resolve' => 'v4', + ]); + + $options = $this->request->curl_options; + + $this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options); + $this->assertSame(CURL_IPRESOLVE_V4, $options[CURLOPT_IPRESOLVE]); + } + + public function testForceResolveIPv6(): void + { + $this->request->request('POST', '/post', [ + 'force_ip_resolve' => 'v6', + ]); + + $options = $this->request->curl_options; + + $this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options); + $this->assertSame(CURL_IPRESOLVE_V6, $options[CURLOPT_IPRESOLVE]); + } + public function testCookieOption(): void { $holder = SUPPORTPATH . 'HTTP/Files/CookiesHolder.txt'; From f5fa4c0459086b3498bc6d9c821fa9342d5a7cef Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 20 Sep 2024 10:11:55 +0700 Subject: [PATCH 3/9] docs: CURL option force_ip_resolve --- user_guide_src/source/libraries/curlrequest.rst | 9 +++++++++ user_guide_src/source/libraries/curlrequest/036.php | 4 ++++ 2 files changed, 13 insertions(+) create mode 100644 user_guide_src/source/libraries/curlrequest/036.php diff --git a/user_guide_src/source/libraries/curlrequest.rst b/user_guide_src/source/libraries/curlrequest.rst index 1e06b69cc484..49540613e1cc 100644 --- a/user_guide_src/source/libraries/curlrequest.rst +++ b/user_guide_src/source/libraries/curlrequest.rst @@ -364,6 +364,15 @@ is true: .. _curlrequest-version: +force_ip_resolve +================ + +.. versionadded:: 4.6.0 + +To set the HTTP handlers to use ``v4`` only ipv4 protocol or ``v6`` for ipv6 protocol: + +.. literalinclude:: curlrequest/036.php + version ======= diff --git a/user_guide_src/source/libraries/curlrequest/036.php b/user_guide_src/source/libraries/curlrequest/036.php new file mode 100644 index 000000000000..a02787d184bb --- /dev/null +++ b/user_guide_src/source/libraries/curlrequest/036.php @@ -0,0 +1,4 @@ +request('GET', '/', ['force_ip_resolve' => 'v4']); // v4 or v6 From 3b9deaea0d891048ab5e657fe07c528f395b7f5b Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 20 Sep 2024 10:22:45 +0700 Subject: [PATCH 4/9] fix: PHPStan error --- phpstan-baseline.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 4965de176bee..9169c0b89543 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -5410,7 +5410,7 @@ $ignoreErrors[] = [ // identifier: empty.notAllowed 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 10, + 'count' => 11, 'path' => __DIR__ . '/system/HTTP/CURLRequest.php', ]; $ignoreErrors[] = [ From 8b7f3a8b84c2f88703c32a508928ad031f07da2b Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Fri, 20 Sep 2024 11:32:06 +0700 Subject: [PATCH 5/9] fix: prevent using empty() --- phpstan-baseline.php | 2 +- system/HTTP/CURLRequest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 9169c0b89543..4965de176bee 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -5410,7 +5410,7 @@ $ignoreErrors[] = [ // identifier: empty.notAllowed 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 11, + 'count' => 10, 'path' => __DIR__ . '/system/HTTP/CURLRequest.php', ]; $ignoreErrors[] = [ diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 468870445773..527a3a259c60 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -650,7 +650,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) } // Resolve IP - if (! empty($config['force_ip_resolve'])) { + if (array_key_exists('force_ip_resolve', $config) && $config['force_ip_resolve']) { $protocolVersion = $config['force_ip_resolve']; if ($protocolVersion === 'v4') { From ec2cc6e2efa0594b7ec56936b92a5d9fcb4f35ba Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Tue, 24 Sep 2024 17:40:30 +0700 Subject: [PATCH 6/9] fix: strict check --- system/HTTP/CURLRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 527a3a259c60..30da34e4be9f 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -650,7 +650,7 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) } // Resolve IP - if (array_key_exists('force_ip_resolve', $config) && $config['force_ip_resolve']) { + if (array_key_exists('force_ip_resolve', $config) && is_string($config['force_ip_resolve']) && $config['force_ip_resolve'] !== '') { $protocolVersion = $config['force_ip_resolve']; if ($protocolVersion === 'v4') { From 89b51b1abd6f86b735fa2191b608258b71150d62 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Tue, 24 Sep 2024 17:47:25 +0700 Subject: [PATCH 7/9] tests: unknown value in option force_ip_resolve --- tests/system/HTTP/CURLRequestTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index 7f1a4d2491e3..cfd9ff91221e 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -1211,6 +1211,17 @@ public function testForceResolveIPv6(): void $this->assertSame(CURL_IPRESOLVE_V6, $options[CURLOPT_IPRESOLVE]); } + public function testForceResolveIPUnknown(): void + { + $this->request->request('POST', '/post', [ + 'force_ip_resolve' => 'v?', + ]); + + $options = $this->request->curl_options; + + $this->assertArrayNotHasKey(CURLOPT_IPRESOLVE, $options); + } + public function testCookieOption(): void { $holder = SUPPORTPATH . 'HTTP/Files/CookiesHolder.txt'; From 83d29d985ebcaf84b8ed13b2244c263761ea4ede Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Sun, 1 Dec 2024 20:32:10 +0700 Subject: [PATCH 8/9] fix: using default match --- system/HTTP/CURLRequest.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 30da34e4be9f..3a606be97442 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -650,14 +650,12 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) } // Resolve IP - if (array_key_exists('force_ip_resolve', $config) && is_string($config['force_ip_resolve']) && $config['force_ip_resolve'] !== '') { - $protocolVersion = $config['force_ip_resolve']; - - if ($protocolVersion === 'v4') { - $curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4; - } elseif ($protocolVersion === 'v6') { - $curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6; - } + if (array_key_exists('force_ip_resolve', $config)) { + $curlOptions[CURLOPT_IPRESOLVE] = match ($config['force_ip_resolve']) { + 'v4' => CURL_IPRESOLVE_V4, + 'v6' => CURL_IPRESOLVE_V6, + default => CURL_IPRESOLVE_WHATEVER + }; } // version From 83075de8623b16856b683c29bea3916f019e83e9 Mon Sep 17 00:00:00 2001 From: Denny Septian Panggabean Date: Sun, 1 Dec 2024 20:34:53 +0700 Subject: [PATCH 9/9] fix: PHPUnit option resolved_force_ip --- tests/system/HTTP/CURLRequestTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/system/HTTP/CURLRequestTest.php b/tests/system/HTTP/CURLRequestTest.php index cfd9ff91221e..18f94d78a287 100644 --- a/tests/system/HTTP/CURLRequestTest.php +++ b/tests/system/HTTP/CURLRequestTest.php @@ -1219,7 +1219,8 @@ public function testForceResolveIPUnknown(): void $options = $this->request->curl_options; - $this->assertArrayNotHasKey(CURLOPT_IPRESOLVE, $options); + $this->assertArrayHasKey(CURLOPT_IPRESOLVE, $options); + $this->assertSame(\CURL_IPRESOLVE_WHATEVER, $options[CURLOPT_IPRESOLVE]); } public function testCookieOption(): void