Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions system/HTTP/CURLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ protected function setCURLOptions(array $curlOptions = [], array $config = [])
}
}

// DNS Cache Timeout
if (isset($config['dns_cache_timeout']) && is_numeric($config['dns_cache_timeout']) && $config['dns_cache_timeout'] >= -1) {
$curlOptions[CURLOPT_DNS_CACHE_TIMEOUT] = (int) $config['dns_cache_timeout'];
}

// Timeout
$curlOptions[CURLOPT_TIMEOUT_MS] = (float) $config['timeout'] * 1000;

Expand Down
71 changes: 71 additions & 0 deletions tests/system/HTTP/CURLRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Config\CURLRequest as ConfigCURLRequest;
use CURLFile;
use PHPUnit\Framework\Attributes\BackupGlobals;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -1212,6 +1213,76 @@ public function testForceResolveIPUnknown(): void
$this->assertSame(\CURL_IPRESOLVE_WHATEVER, $options[CURLOPT_IPRESOLVE]);
}

/**
* @return array<string, array<string, bool|int|string>>
*
* @see https://curl.se/libcurl/c/CURLOPT_DNS_CACHE_TIMEOUT.html
*/
public static function provideDNSCacheTimeout(): iterable
{
yield from [
'valid timeout (integer)' => [
'input' => 160,
'expectedHasKey' => true,
'expectedValue' => 160,
],
'valid timeout (numeric string)' => [
'input' => '180',
'expectedHasKey' => true,
'expectedValue' => 180,
],
'valid timeout (zero / disabled)' => [
'input' => 0,
'expectedHasKey' => true,
'expectedValue' => 0,
],
'valid timeout (zero / disabled using string)' => [
'input' => '0',
'expectedHasKey' => true,
'expectedValue' => 0,
],
'valid timeout (forever)' => [
'input' => -1,
'expectedHasKey' => true,
'expectedValue' => -1,
],
'valid timeout (forever using string)' => [
'input' => '-1',
'expectedHasKey' => true,
'expectedValue' => -1,
],
'invalid timeout (null)' => [
'input' => null,
'expectedHasKey' => false,
],
'invalid timeout (string)' => [
'input' => 'is_wrong',
'expectedHasKey' => false,
],
'invalid timeout (negative number / below -1)' => [
'input' => -2,
'expectedHasKey' => false,
],
];
}

#[DataProvider('provideDNSCacheTimeout')]
public function testDNSCacheTimeoutOption(int|string|null $input, bool $expectedHasKey, int|string|null $expectedValue = null): void
{
$this->request->request('POST', '/post', [
'dns_cache_timeout' => $input,
]);

$options = $this->request->curl_options;

if ($expectedHasKey) {
$this->assertArrayHasKey(CURLOPT_DNS_CACHE_TIMEOUT, $options);
$this->assertSame($expectedValue, $options[CURLOPT_DNS_CACHE_TIMEOUT]);
} else {
$this->assertArrayNotHasKey(CURLOPT_DNS_CACHE_TIMEOUT, $options);
}
}

public function testCookieOption(): void
{
$holder = SUPPORTPATH . 'HTTP/Files/CookiesHolder.txt';
Expand Down
11 changes: 4 additions & 7 deletions user_guide_src/source/changelogs/v4.7.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Enhancements
Libraries
=========

- **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout.
- **Email:** Added support for choosing the SMTP authorization method. You can change it via ``Config\Email::$SMTPAuthMethod`` option.
- **Image:** The ``ImageMagickHandler`` has been rewritten to rely solely on the PHP ``imagick`` extension.
- **Image:** Added ``ImageMagickHandler::clearMetadata()`` method to remove image metadata for privacy protection.
- **Time:** added methods ``Time::addCalendarMonths()`` and ``Time::subCalendarMonths()``

Commands
Expand All @@ -61,13 +65,6 @@ Others
Model
=====

Libraries
=========

**Email:** Added support for choosing the SMTP authorization method. You can change it via ``Config\Email::$SMTPAuthMethod`` option.
**Image:** The ``ImageMagickHandler`` has been rewritten to rely solely on the PHP ``imagick`` extension.
**Image:** Added ``ImageMagickHandler::clearMetadata()`` method to remove image metadata for privacy protection.

Helpers and Functions
=====================

Expand Down
12 changes: 12 additions & 0 deletions user_guide_src/source/libraries/curlrequest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ Allows you to pause a number of milliseconds before sending the request:

.. literalinclude:: curlrequest/023.php

dns_cache_timeout
=================

.. versionadded:: 4.7.0

By default PHP, CodeIgniter does not change the DNS Cache Timeout value (``120`` seconds). If you need to
modify this value, you can do so by passing an amount of time in seconds with the ``dns_cache_timeout`` option.

.. literalinclude:: curlrequest/037.php

.. note:: Based on documentation `libcurl <https://curl.se/libcurl/c/CURLOPT_DNS_CACHE_TIMEOUT.html>`__, you can set to zero to completely disable caching, or set to -1 to make the cached entries remain forever.

form_params
===========

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

// Modify default DNS Cache Timeout
$client->request('GET', '/', ['dns_cache_timeout' => 360]); // seconds
Loading