Skip to content
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

feat: CURL option force_ip_resolve #9194

Open
wants to merge 7 commits into
base: 4.6
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,17 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
$this->setHeader('Content-Length', (string) strlen($json));
}

// 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;
}
}

// version
if (! empty($config['version'])) {
$version = sprintf('%.1F', $config['version']);
Expand Down
35 changes: 35 additions & 0 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,41 @@ 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 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';
Expand Down
9 changes: 9 additions & 0 deletions user_guide_src/source/libraries/curlrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======

Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/libraries/curlrequest/036.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Force ipv4 resolve
$client->request('GET', '/', ['force_ip_resolve' => 'v4']); // v4 or v6
Loading