Skip to content

Commit eb5a6eb

Browse files
committed
feat: share Connection & DNS Cache to CURLRequest
1 parent 59a4819 commit eb5a6eb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

system/HTTP/CURLRequest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\HTTP\Exceptions\HTTPException;
1818
use Config\App;
1919
use Config\CURLRequest as ConfigCURLRequest;
20+
use CurlShareHandle;
2021

2122
/**
2223
* A lightweight HTTP client for sending synchronous HTTP requests via cURL.
@@ -101,6 +102,14 @@ class CURLRequest extends OutgoingRequest
101102
*/
102103
private readonly bool $shareOptions;
103104

105+
/**
106+
* Whether share options between requests or not.
107+
*
108+
* If true, all the options won't be reset between requests.
109+
* It may cause an error request with unnecessary headers.
110+
*/
111+
private readonly CurlShareHandle $shareConnection;
112+
104113
/**
105114
* Takes an array of options to set the following possible class properties:
106115
*
@@ -129,6 +138,11 @@ public function __construct(App $config, URI $uri, ?ResponseInterface $response
129138

130139
$this->config = $this->defaultConfig;
131140
$this->parseOptions($options);
141+
142+
// 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);
132146
}
133147

134148
/**
@@ -363,6 +377,7 @@ public function send(string $method, string $url)
363377
}
364378

365379
$curlOptions[CURLOPT_URL] = $url;
380+
$curlOptions[CURLOPT_SHARE] = $this->shareConnection;
366381
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
367382
$curlOptions[CURLOPT_HEADER] = true;
368383
// Disable @file uploads in post data.

0 commit comments

Comments
 (0)