Skip to content

Commit 3c20b47

Browse files
committed
feat: share connection in CURLRequest
1 parent eb5a6eb commit 3c20b47

File tree

7 files changed

+101
-7
lines changed

7 files changed

+101
-7
lines changed

app/Config/CURLRequest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66

77
class CURLRequest extends BaseConfig
88
{
9+
/**
10+
* --------------------------------------------------------------------------
11+
* CURLRequest Share Connection
12+
* --------------------------------------------------------------------------
13+
*
14+
* Whether share connection between requests.
15+
*
16+
* @var list<int>
17+
*
18+
* @see https://www.php.net/manual/en/curl.constants.php#constant.curl-lock-data-connect
19+
*/
20+
public array $shareConnection = [
21+
CURL_LOCK_DATA_CONNECT,
22+
CURL_LOCK_DATA_DNS,
23+
];
24+
925
/**
1026
* --------------------------------------------------------------------------
1127
* CURLRequest Share Options

system/HTTP/CURLRequest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class CURLRequest extends OutgoingRequest
108108
* If true, all the options won't be reset between requests.
109109
* It may cause an error request with unnecessary headers.
110110
*/
111-
private readonly CurlShareHandle $shareConnection;
111+
protected ?CurlShareHandle $shareConnection = null;
112112

113113
/**
114114
* Takes an array of options to set the following possible class properties:
@@ -140,9 +140,18 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
140140
$this->parseOptions($options);
141141

142142
// Share Connection
143-
$this->shareConnection = curl_share_init();
144-
curl_share_setopt($this->shareConnection, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
145-
curl_share_setopt($this->shareConnection, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
143+
$optShareConnection = config(ConfigCURLRequest::class)->shareConnection ?? [
144+
CURL_LOCK_DATA_CONNECT,
145+
CURL_LOCK_DATA_DNS,
146+
];
147+
148+
if ($optShareConnection !== []) {
149+
$this->shareConnection = curl_share_init();
150+
151+
foreach (array_unique($optShareConnection) as $opt) {
152+
curl_share_setopt($this->shareConnection, CURLSHOPT_SHARE, $opt);
153+
}
154+
}
146155
}
147156

148157
/**
@@ -377,9 +386,13 @@ public function send(string $method, string $url)
377386
}
378387

379388
$curlOptions[CURLOPT_URL] = $url;
380-
$curlOptions[CURLOPT_SHARE] = $this->shareConnection;
381389
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
382-
$curlOptions[CURLOPT_HEADER] = true;
390+
391+
if ($this->shareConnection instanceof CurlShareHandle) {
392+
$curlOptions[CURLOPT_SHARE] = $this->shareConnection;
393+
}
394+
395+
$curlOptions[CURLOPT_HEADER] = true;
383396
// Disable @file uploads in post data.
384397
$curlOptions[CURLOPT_SAFE_UPLOAD] = true;
385398

tests/system/HTTP/CURLRequestTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,18 @@ protected function setUp(): void
4646
/**
4747
* @param array<string, mixed> $options
4848
*/
49-
protected function getRequest(array $options = []): MockCURLRequest
49+
protected function getRequest(array $options = [], bool $emptyShareConnection = false): MockCURLRequest
5050
{
5151
$uri = isset($options['baseURI']) ? new URI($options['baseURI']) : new URI();
5252
$app = new App();
5353

5454
$config = new ConfigCURLRequest();
5555
$config->shareOptions = false;
56+
57+
if ($emptyShareConnection) {
58+
$config->shareConnection = [];
59+
}
60+
5661
Factories::injectMock('config', 'CURLRequest', $config);
5762

5863
return new MockCURLRequest(($app), $uri, new Response($app), $options);
@@ -1235,6 +1240,25 @@ public function testForceResolveIPUnknown(): void
12351240
$this->assertSame(\CURL_IPRESOLVE_WHATEVER, $options[CURLOPT_IPRESOLVE]);
12361241
}
12371242

1243+
public function testShareConnectionDefault(): void
1244+
{
1245+
$this->request->request('GET', 'http://example.com');
1246+
1247+
$options = $this->request->curl_options;
1248+
1249+
$this->assertArrayHasKey(CURLOPT_SHARE, $options);
1250+
}
1251+
1252+
public function testShareConnectionEmpty(): void
1253+
{
1254+
$request = $this->getRequest(emptyShareConnection: true);
1255+
$request->request('GET', 'http://example.com');
1256+
1257+
$options = $request->curl_options;
1258+
1259+
$this->assertArrayNotHasKey(CURLOPT_SHARE, $options);
1260+
}
1261+
12381262
/**
12391263
* @return iterable<string, array{input: int|string|null, expectedHasKey: bool, expectedValue?: int}>
12401264
*

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Enhancements
3838
Libraries
3939
=========
4040

41+
- **CURLRequest:** Added ``shareConnection`` config item to change default share connection.
4142
- **CURLRequest:** Added ``dns_cache_timeout`` option to change default DNS cache timeout.
4243
- **CURLRequest:** Added ``fresh_connect`` options to enable/disabled request fresh connection.
4344
- **Email:** Added support for choosing the SMTP authorization method. You can change it via ``Config\Email::$SMTPAuthMethod`` option.

user_guide_src/source/libraries/curlrequest.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ to change very little to move over to use Guzzle.
2323
Config for CURLRequest
2424
**********************
2525

26+
.. _curlrequest-sharing-connection:
27+
28+
Sharing Connection
29+
==================
30+
31+
.. versionadded:: 4.7.0
32+
33+
If you want to share connection between requests, set ``$shareConnection`` with array constant `CURL_LOCK_DATA_* <https://www.php.net/manual/en/curl.constants.php#constant.curl-lock-data-connect>`_ in **app/Config/CURLRequest.php**:
34+
35+
.. literalinclude:: curlrequest/039.php
36+
37+
or when you want to disable it, just change to empty array:
38+
39+
.. literalinclude:: curlrequest/040.php
40+
2641
.. _curlrequest-sharing-options:
2742

2843
Sharing Options
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class CURLRequest extends BaseConfig
8+
{
9+
// ...
10+
public array $shareConnection = [
11+
CURL_LOCK_DATA_CONNECT,
12+
CURL_LOCK_DATA_DNS,
13+
];
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
class CURLRequest extends BaseConfig
8+
{
9+
// ...
10+
public array $shareConnection = [];
11+
}

0 commit comments

Comments
 (0)