Skip to content

Commit

Permalink
ext/curl: Add CURLINFO_{USED_PROXY,HTTPAUTH_USED,PROXYAUTH_USED}
Browse files Browse the repository at this point in the history
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,
]
```

Because the built-in server ext/curl tests use does not support HTTP
authentication, there is an online test that uses httpbin. It will
be skipped by default unless online tests are run.
  • Loading branch information
Ayesh committed Feb 15, 2025
1 parent c7d62cf commit f6eccf2
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ PHP NEWS
- Curl:
. Added curl_multi_get_handles(). (timwolla)
. Added curl_share_init_persistent(). (enorris)
. Added CURLINFO_USED_PROXY, CURLINFO_HTTPAUTH_USED, and CURLINFO_PROXYAUTH_USED
support to curl_getinfo. (Ayesh Karunaratne)

- Date:
. Fix undefined behaviour problems regarding integer overflow in extreme edge
Expand Down
17 changes: 17 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ PHP 8.5 UPGRADE NOTES
. Added support for share handles that are persisted across multiple PHP
requests, safely allowing for more effective connection reuse.
RFC: https://wiki.php.net/rfc/curl_share_persistence_improvement
. Added support for CURLINFO_USED_PROXY (libcurl >= 8.7.0),
CURLINFO_HTTPAUTH_USED, and CURLINFO_PROXYAUTH_USED
(libcurl >= 8.12.0) to the curl_getinfo() function.
When curl_getinfo() returns an array, the same information
is available as "used_proxy", "httpauth_used", and "proxyauth_used"
keys.
CURLINFO_USED_PROXY gets zero set if no proxy was used in the
previous transfer or a non-zero value if a proxy was used.
CURLINFO_HTTPAUTH_USED and CURLINFO_PROXYAUTH_USED get bitmasks
indicating the http and proxy authentication methods that were
used in the previous request. See CURLAUTH_* constants for
possible values.

- DOM:
. Added Dom\Element::$outerHTML.
Expand Down Expand Up @@ -262,6 +274,11 @@ PHP 8.5 UPGRADE NOTES
- Core:
. PHP_BUILD_DATE.

- Curl:
. CURLINFO_USED_PROXY.
. CURLINFO_HTTPAUTH_USED.
. CURLINFO_PROXYAUTH_USED.

- POSIX:
. POSIX_SC_OPEN_MAX.

Expand Down
19 changes: 19 additions & 0 deletions ext/curl/curl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,18 @@
*/
const CURLINFO_CAINFO = UNKNOWN;
#endif
#if LIBCURL_VERSION_NUM >= 0x080c00 /* Available since 8.12.0 */
/**
* @var int
* @cvalue CURLINFO_HTTPAUTH_USED
*/
const CURLINFO_HTTPAUTH_USED = UNKNOWN;
/**
* @var int
* @cvalue CURLINFO_PROXYAUTH_USED
*/
const CURLINFO_PROXYAUTH_USED = UNKNOWN;
#endif

/* Other */
/**
Expand Down Expand Up @@ -3054,6 +3066,13 @@
* @cvalue CURLINFO_TOTAL_TIME_T
*/
const CURLINFO_TOTAL_TIME_T = UNKNOWN;
#if LIBCURL_VERSION_NUM >= 0x080700 /* Available since 8.7.0 */
/**
* @var int
* @cvalue CURLINFO_USED_PROXY
*/
const CURLINFO_USED_PROXY = UNKNOWN;
#endif
#if LIBCURL_VERSION_NUM >= 0x080a00 /* Available since 8.10.0 */
/**
* @var int
Expand Down
11 changes: 10 additions & 1 deletion ext/curl/curl_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,19 @@ PHP_FUNCTION(curl_getinfo)
if (curl_easy_getinfo(ch->cp, CURLINFO_CAINFO, &s_code) == CURLE_OK) {
CAAS("cainfo", s_code);
}
#endif
#if LIBCURL_VERSION_NUM >= 0x080700 /* Available since 8.7.0 */
if (curl_easy_getinfo(ch->cp, CURLINFO_USED_PROXY, &l_code) == CURLE_OK) {
CAAL("used_proxy", l_code);
}
#endif
#if LIBCURL_VERSION_NUM >= 0x080c00 /* Available since 8.12.0 */
if (curl_easy_getinfo(ch->cp, CURLINFO_HTTPAUTH_USED, &l_code) == CURLE_OK) {
CAAL("httpauth_used", l_code);
}
if (curl_easy_getinfo(ch->cp, CURLINFO_PROXYAUTH_USED, &l_code) == CURLE_OK) {
CAAL("proxyauth_used", l_code);
}
#endif
} else {
switch (option) {
Expand Down
33 changes: 33 additions & 0 deletions ext/curl/tests/curl_getinfo_CURLINFO_HTTPAUTH_USED-online.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
curl_getinfo - CURLINFO_HTTPAUTH_USED - online test
--EXTENSIONS--
curl
--SKIPIF--
<?php
$curl_version = curl_version();
if ($curl_version['version_number'] < 0x080c00) die("skip: test works only with curl >= 8.12.0");
if (getenv("SKIP_ONLINE_TESTS")) die('skip online test');
?>
--FILE--
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://httpbin.org/basic-auth/foo/bar");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$info = curl_getinfo($ch);
var_dump($info['httpauth_used'] === 0); // this is always 0 before executing the transfer

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERNAME, "foo");
curl_setopt($ch, CURLOPT_PASSWORD, "bar");

$result = curl_exec($ch);
$info = curl_getinfo($ch);

var_dump($info['httpauth_used'] === CURLAUTH_BASIC);

?>
--EXPECT--
bool(true)
bool(true)
66 changes: 66 additions & 0 deletions ext/curl/tests/curl_getinfo_CURLINFO_HTTPAUTH_USED.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
--TEST--
curl_getinfo - CURLINFO_HTTPAUTH_USED
--EXTENSIONS--
curl
--SKIPIF--
<?php
$curl_version = curl_version();
if ($curl_version['version_number'] < 0x080c00) die("skip: test works only with curl >= 8.12.0");
?>
--FILE--
<?php
include 'server.inc';

$host = curl_cli_server_start();
$port = (int) (explode(':', $host))[1];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

echo "httpauth_used and proxyauth_used empty\n";

$info = curl_getinfo($ch);
var_dump(isset($info['httpauth_used']));
var_dump(isset($info['proxyauth_used']));
var_dump($info['httpauth_used'] === 0); // this is always 0 before executing the transfer
var_dump($info['proxyauth_used'] === 0); // this is always 0 before executing the transfer

$result = curl_exec($ch);
echo "httpauth_used and proxyauth_used empty after request\n";
$info = curl_getinfo($ch);
var_dump(isset($info['httpauth_used']));
var_dump(isset($info['proxyauth_used']));
var_dump($info['httpauth_used'] === 0);
var_dump($info['proxyauth_used'] === 0);
var_dump(curl_getinfo($ch, CURLINFO_HTTPAUTH_USED) === $info['used_proxy']);
var_dump(curl_getinfo($ch, CURLINFO_PROXYAUTH_USED) === $info['proxyauth_used']);

echo "httpauth_used set after request\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERNAME, "foo");
curl_setopt($ch, CURLOPT_PASSWORD, "bar");

$result = curl_exec($ch);
$info = curl_getinfo($ch);

var_dump($info['httpauth_used']); // Built-in server does not support auth

?>
--EXPECT--
httpauth_used and proxyauth_used empty
bool(true)
bool(true)
bool(true)
bool(true)
httpauth_used and proxyauth_used empty after request
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
httpauth_used set after request
int(0)
38 changes: 38 additions & 0 deletions ext/curl/tests/curl_getinfo_CURLINFO_USED_PROXY.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
curl_getinfo - CURLINFO_USED_PROXY
--EXTENSIONS--
curl
--SKIPIF--
<?php
$curl_version = curl_version();
if ($curl_version['version_number'] < 0x080700) die("skip: test works only with curl >= 8.7.0");
?>
--FILE--
<?php
include 'server.inc';

$host = curl_cli_server_start();
$port = (int) (explode(':', $host))[1];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$info = curl_getinfo($ch);
var_dump(isset($info['used_proxy']));
var_dump($info['used_proxy'] === 0); // this is always 0 before executing the transfer

$result = curl_exec($ch);

$info = curl_getinfo($ch);
var_dump(isset($info['used_proxy']));
var_dump($info['used_proxy'] === 0);
var_dump(curl_getinfo($ch, CURLINFO_USED_PROXY) === $info['used_proxy']);

?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

0 comments on commit f6eccf2

Please sign in to comment.