Skip to content

Commit 0b15516

Browse files
committed
Backport from 8.5:
Add support for CURLINFO_CONN_ID in curl_getinfo() (php#18984) This patch adds support for the CURLINFO_CONN_ID constant in the curl_getinfo() function when compiled with libcurl >= 8.2.0. CURLINFO_CONN_ID allows retrieving the unique identifier of the underlying connection used in the most recent transfer. This is useful for advanced features like connection reuse tracking, diagnostics, or connection pooling implementations at the PHP level. php@e84320a
1 parent c71a9b2 commit 0b15516

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed

ext/curl/curl.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,13 @@
30713071
* @cvalue CURLINFO_TOTAL_TIME_T
30723072
*/
30733073
const CURLINFO_TOTAL_TIME_T = UNKNOWN;
3074+
#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */
3075+
/**
3076+
* @var int
3077+
* @cvalue CURLINFO_CONN_ID
3078+
*/
3079+
const CURLINFO_CONN_ID = UNKNOWN;
3080+
#endif
30743081
/**
30753082
* @var int
30763083
* @cvalue CURLOPT_DISALLOW_USERNAME_IN_URL

ext/curl/curl_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/curl/interface.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,6 +2787,11 @@ PHP_FUNCTION(curl_getinfo)
27872787
if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
27882788
CAAS("cainfo", s_code);
27892789
}
2790+
#endif
2791+
#if LIBCURL_VERSION_NUM >= 0x080200 /* Available since 8.2.0 */
2792+
if (curl_easy_getinfo(ch->cp, CURLINFO_CONN_ID , &co) == CURLE_OK) {
2793+
CAAL("conn_id", co);
2794+
}
27902795
#endif
27912796
} else {
27922797
switch (option) {
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
--TEST--
2+
Curlinfo CURLINFO_CONN_ID
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x080200) die("skip: test works only with curl >= 8.2.0");
9+
?>
10+
--FILE--
11+
<?php
12+
include 'server.inc';
13+
14+
$host = curl_cli_server_start();
15+
$port = (int) (explode(':', $host))[1];
16+
17+
$ch = curl_init();
18+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=get");
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
21+
$info = curl_getinfo($ch);
22+
var_dump(isset($info['conn_id']));
23+
var_dump($info['conn_id'] === -1);
24+
25+
$result = curl_exec($ch);
26+
27+
$info = curl_getinfo($ch);
28+
var_dump(isset($info['conn_id']));
29+
var_dump(is_int($info['conn_id']));
30+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
31+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === 0);
32+
33+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=get");
34+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
35+
$result = curl_exec($ch);
36+
37+
$info = curl_getinfo($ch);
38+
var_dump(isset($info['conn_id']));
39+
var_dump(is_int($info['conn_id']));
40+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
41+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === 1);
42+
43+
$ch1=curl_init();
44+
$ch2=curl_init();
45+
$cmh=curl_multi_init();
46+
47+
foreach([$ch1, $ch2] as $ch) {
48+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=getpost&get_param=Curl%20Handle");
49+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50+
$info = curl_getinfo($ch);
51+
var_dump(isset($info['conn_id']));
52+
var_dump($info['conn_id'] === -1);
53+
curl_multi_add_handle($cmh,$ch);
54+
}
55+
56+
$running=0;
57+
do {
58+
curl_multi_exec($cmh,$running);
59+
} while ($running>0);
60+
61+
foreach([$ch1, $ch2] as $key => $ch) {
62+
$result = curl_multi_getcontent($ch);
63+
$info = curl_getinfo($ch);
64+
var_dump(isset($info['conn_id']));
65+
var_dump(is_int($info['conn_id']));
66+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $info['conn_id']);
67+
var_dump(curl_getinfo($ch, CURLINFO_CONN_ID) === $key);
68+
}
69+
70+
$csh = curl_share_init();
71+
72+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
73+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
74+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
75+
curl_share_setopt($csh, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
76+
77+
78+
$ch1=curl_init();
79+
$ch2=curl_init();
80+
81+
foreach([$ch1, $ch2] as $ch) {
82+
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=getpost&get_param=Curl%20Handle");
83+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84+
$info = curl_getinfo($ch);
85+
var_dump(isset($info['conn_id']));
86+
var_dump($info['conn_id'] === -1);
87+
}
88+
89+
90+
curl_setopt($ch1, CURLOPT_SHARE, $csh);
91+
92+
$result = curl_exec($ch1);
93+
94+
$info = curl_getinfo($ch1);
95+
var_dump(isset($info['conn_id']));
96+
var_dump(is_int($info['conn_id']));
97+
var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === $info['conn_id']);
98+
var_dump(curl_getinfo($ch1, CURLINFO_CONN_ID) === 0);
99+
100+
curl_setopt($ch2, CURLOPT_SHARE, $csh);
101+
102+
$result = curl_exec($ch2);
103+
104+
$info = curl_getinfo($ch2);
105+
var_dump(isset($info['conn_id']));
106+
var_dump(is_int($info['conn_id']));
107+
var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === $info['conn_id']);
108+
var_dump(curl_getinfo($ch2, CURLINFO_CONN_ID) === 1);
109+
110+
?>
111+
--EXPECT--
112+
bool(true)
113+
bool(true)
114+
bool(true)
115+
bool(true)
116+
bool(true)
117+
bool(true)
118+
bool(true)
119+
bool(true)
120+
bool(true)
121+
bool(true)
122+
bool(true)
123+
bool(true)
124+
bool(true)
125+
bool(true)
126+
bool(true)
127+
bool(true)
128+
bool(true)
129+
bool(true)
130+
bool(true)
131+
bool(true)
132+
bool(true)
133+
bool(true)
134+
bool(true)
135+
bool(true)
136+
bool(true)
137+
bool(true)
138+
bool(true)
139+
bool(true)
140+
bool(true)
141+
bool(true)
142+
bool(true)
143+
bool(true)
144+
bool(true)
145+
bool(true)
146+

0 commit comments

Comments
 (0)