Skip to content

Commit 1c47f1d

Browse files
committed
ext/curl: Add CURLINFO_{USED_PROXY,HTTPAUTH_USED,PROXYAUTH_USED}
Adds support for `curl_getinfo()` info keys and additional array keys: - [`CURLINFO_USED_PROXY`](https://curl.se/libcurl/c/CURLINFO_USED_PROXY.html) - `libcurl` >= 8.7.9 - Zero if no proxy was used in the previous transfer or a non-zero value if a proxy was used. - [`CURLINFO_HTTPAUTH_USED`](https://github.com/curl/curl/blob/curl-8_12_0/docs/libcurl/opts/CURLINFO_HTTPAUTH_USED.md) - `libcurl` >= 8.7.9 - Bitmask indicating the authentication method that was used in the previous HTTP request. - [`CURLINFO_PROXYAUTH_USED`](https://github.com/curl/curl/blob/curl-8_12_0/docs/libcurl/opts/CURLINFO_PROXYAUTH_USED.md) - `libcurl` >= 8.12.0 - Bitmask indicating the authentication method that was used in the previous request done over an HTTP proxy. ```php curl_getinfo($ch); ``` ```php [ // ... "used_proxy" => 0, "httpauth_used" => 0, "proxyauth_used" => 0, ] ``` This also updates the `Caddyfile` for curl tests to add a new route that supports HTTP basic auth.
1 parent c7d62cf commit 1c47f1d

9 files changed

+204
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ PHP NEWS
2828
- Curl:
2929
. Added curl_multi_get_handles(). (timwolla)
3030
. Added curl_share_init_persistent(). (enorris)
31+
. Added CURLINFO_USED_PROXY, CURLINFO_HTTPAUTH_USED, and CURLINFO_PROXYAUTH_USED
32+
support to curl_getinfo. (Ayesh Karunaratne)
3133

3234
- Date:
3335
. Fix undefined behaviour problems regarding integer overflow in extreme edge

UPGRADING

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ PHP 8.5 UPGRADE NOTES
9595
. Added support for share handles that are persisted across multiple PHP
9696
requests, safely allowing for more effective connection reuse.
9797
RFC: https://wiki.php.net/rfc/curl_share_persistence_improvement
98+
. Added support for CURLINFO_USED_PROXY (libcurl >= 8.7.0),
99+
CURLINFO_HTTPAUTH_USED, and CURLINFO_PROXYAUTH_USED
100+
(libcurl >= 8.12.0) to the curl_getinfo() function.
101+
When curl_getinfo() returns an array, the same information
102+
is available as "used_proxy", "httpauth_used", and "proxyauth_used"
103+
keys.
104+
CURLINFO_USED_PROXY gets zero set if no proxy was used in the
105+
previous transfer or a non-zero value if a proxy was used.
106+
CURLINFO_HTTPAUTH_USED and CURLINFO_PROXYAUTH_USED get bitmasks
107+
indicating the http and proxy authentication methods that were
108+
used in the previous request. See CURLAUTH_* constants for
109+
possible values.
98110

99111
- DOM:
100112
. Added Dom\Element::$outerHTML.
@@ -262,6 +274,11 @@ PHP 8.5 UPGRADE NOTES
262274
- Core:
263275
. PHP_BUILD_DATE.
264276

277+
- Curl:
278+
. CURLINFO_USED_PROXY.
279+
. CURLINFO_HTTPAUTH_USED.
280+
. CURLINFO_PROXYAUTH_USED.
281+
265282
- POSIX:
266283
. POSIX_SC_OPEN_MAX.
267284

ext/curl/curl.stub.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,18 @@
10001000
*/
10011001
const CURLINFO_CAINFO = UNKNOWN;
10021002
#endif
1003+
#if LIBCURL_VERSION_NUM >= 0x080c00 /* Available since 8.12.0 */
1004+
/**
1005+
* @var int
1006+
* @cvalue CURLINFO_HTTPAUTH_USED
1007+
*/
1008+
const CURLINFO_HTTPAUTH_USED = UNKNOWN;
1009+
/**
1010+
* @var int
1011+
* @cvalue CURLINFO_PROXYAUTH_USED
1012+
*/
1013+
const CURLINFO_PROXYAUTH_USED = UNKNOWN;
1014+
#endif
10031015

10041016
/* Other */
10051017
/**
@@ -3054,6 +3066,13 @@
30543066
* @cvalue CURLINFO_TOTAL_TIME_T
30553067
*/
30563068
const CURLINFO_TOTAL_TIME_T = UNKNOWN;
3069+
#if LIBCURL_VERSION_NUM >= 0x080700 /* Available since 8.7.0 */
3070+
/**
3071+
* @var int
3072+
* @cvalue CURLINFO_USED_PROXY
3073+
*/
3074+
const CURLINFO_USED_PROXY = UNKNOWN;
3075+
#endif
30573076
#if LIBCURL_VERSION_NUM >= 0x080a00 /* Available since 8.10.0 */
30583077
/**
30593078
* @var int

ext/curl/curl_arginfo.h

Lines changed: 10 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,19 @@ PHP_FUNCTION(curl_getinfo)
26622662
if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
26632663
CAAS("cainfo", s_code);
26642664
}
2665+
#endif
2666+
#if LIBCURL_VERSION_NUM >= 0x080700 /* Available since 8.7.0 */
2667+
if (curl_easy_getinfo(ch->cp, CURLINFO_USED_PROXY, &l_code) == CURLE_OK) {
2668+
CAAL("used_proxy", l_code);
2669+
}
2670+
#endif
2671+
#if LIBCURL_VERSION_NUM >= 0x080c00 /* Available since 8.12.0 */
2672+
if (curl_easy_getinfo(ch->cp, CURLINFO_HTTPAUTH_USED, &l_code) == CURLE_OK) {
2673+
CAAL("httpauth_used", l_code);
2674+
}
2675+
if (curl_easy_getinfo(ch->cp, CURLINFO_PROXYAUTH_USED, &l_code) == CURLE_OK) {
2676+
CAAL("proxyauth_used", l_code);
2677+
}
26652678
#endif
26662679
} else {
26672680
switch (option) {

ext/curl/tests/Caddyfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ respond / "Caddy is up and running"
1111
respond /serverpush "main response"
1212
respond /serverpush/pushed "pushed response"
1313
push /serverpush /serverpush/pushed
14+
15+
basicauth /http-basic-auth {
16+
# bcrypt password hash for "password", calculated with 'caddy hash-password'
17+
user $2a$14$yUKl9SGqVTAAqPTzLup.DefsbXXx3kfreNnzpJOUHcIrKnr5lgef2
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
curl_getinfo - CURLINFO_HTTPAUTH_USED - online test
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x080c00) die("skip: test works only with curl >= 8.12.0");
9+
?>
10+
--FILE--
11+
<?php
12+
13+
include 'skipif-nocaddy.inc';
14+
15+
$ch = curl_init();
16+
curl_setopt($ch, CURLOPT_URL, "https://localhost/http-basic-auth");
17+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
18+
19+
$info = curl_getinfo($ch);
20+
var_dump($info['httpauth_used'] === 0); // this is always 0 before executing the transfer
21+
22+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST);
23+
curl_setopt($ch, CURLOPT_USERNAME, "foo");
24+
curl_setopt($ch, CURLOPT_PASSWORD, "bar");
25+
26+
$result = curl_exec($ch);
27+
$info = curl_getinfo($ch);
28+
29+
var_dump($info['httpauth_used'] === CURLAUTH_BASIC);
30+
31+
?>
32+
--EXPECT--
33+
bool(true)
34+
bool(true)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
curl_getinfo - CURLINFO_HTTPAUTH_USED
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x080c00) die("skip: test works only with curl >= 8.12.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=file");
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
21+
echo "httpauth_used and proxyauth_used empty\n";
22+
23+
$info = curl_getinfo($ch);
24+
var_dump(isset($info['httpauth_used']));
25+
var_dump(isset($info['proxyauth_used']));
26+
var_dump($info['httpauth_used'] === 0); // this is always 0 before executing the transfer
27+
var_dump($info['proxyauth_used'] === 0); // this is always 0 before executing the transfer
28+
29+
$result = curl_exec($ch);
30+
echo "httpauth_used and proxyauth_used empty after request\n";
31+
$info = curl_getinfo($ch);
32+
var_dump(isset($info['httpauth_used']));
33+
var_dump(isset($info['proxyauth_used']));
34+
var_dump($info['httpauth_used'] === 0);
35+
var_dump($info['proxyauth_used'] === 0);
36+
var_dump(curl_getinfo($ch, CURLINFO_HTTPAUTH_USED) === $info['used_proxy']);
37+
var_dump(curl_getinfo($ch, CURLINFO_PROXYAUTH_USED) === $info['proxyauth_used']);
38+
39+
echo "httpauth_used set after request\n";
40+
41+
$ch = curl_init();
42+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST);
43+
curl_setopt($ch, CURLOPT_USERNAME, "foo");
44+
curl_setopt($ch, CURLOPT_PASSWORD, "bar");
45+
46+
$result = curl_exec($ch);
47+
$info = curl_getinfo($ch);
48+
49+
var_dump($info['httpauth_used']); // Built-in server does not support auth
50+
51+
?>
52+
--EXPECT--
53+
httpauth_used and proxyauth_used empty
54+
bool(true)
55+
bool(true)
56+
bool(true)
57+
bool(true)
58+
httpauth_used and proxyauth_used empty after request
59+
bool(true)
60+
bool(true)
61+
bool(true)
62+
bool(true)
63+
bool(true)
64+
bool(true)
65+
httpauth_used set after request
66+
int(0)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
curl_getinfo - CURLINFO_USED_PROXY
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x080700) die("skip: test works only with curl >= 8.7.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=file");
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
21+
$info = curl_getinfo($ch);
22+
var_dump(isset($info['used_proxy']));
23+
var_dump($info['used_proxy'] === 0); // this is always 0 before executing the transfer
24+
25+
$result = curl_exec($ch);
26+
27+
$info = curl_getinfo($ch);
28+
var_dump(isset($info['used_proxy']));
29+
var_dump($info['used_proxy'] === 0);
30+
var_dump(curl_getinfo($ch, CURLINFO_USED_PROXY) === $info['used_proxy']);
31+
32+
?>
33+
--EXPECT--
34+
bool(true)
35+
bool(true)
36+
bool(true)
37+
bool(true)
38+
bool(true)

0 commit comments

Comments
 (0)