Skip to content

Commit 99f25a4

Browse files
committed
ext/curl: Add CURLOPT_PREREQFUNCTION
Curl >= 7.80.0 supports declaring a function for `CURLOPT_PREREQFUNCTION` option that gets called after Curl establishes a connection (including the TLS handshake for HTTPS connections), but before the actual request is made. The callable must return either `CURL_PREREQFUNC_OK` or `CURL_PREREQFUNC_ABORT` to allow or abort the request. This adds support for it to PHP with required ifdef. - libc: https://curl.se/libcurl/c/CURLOPT_PREREQFUNCTION.html
1 parent 2d66993 commit 99f25a4

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

UPGRADING

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ PHP 8.4 UPGRADE NOTES
186186
. curl_version() returns an additional feature_list value, which is an
187187
associative array of all known Curl features, and whether they are
188188
supported (true) or not (false).
189+
. Added CURLOPT_PREREQFUNCTION option which accepts a callable that gets
190+
called after a connection is established (including TLS handshake), but
191+
before the request is made. The function is called with the CurlHandle
192+
object, primary IP, local IP, primary port, and the local port, and it
193+
must return CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT to allow or abort
194+
the request.
189195

190196
- Date:
191197
. Added static methods
@@ -588,6 +594,11 @@ PHP 8.4 UPGRADE NOTES
588594
- Core:
589595
. PHP_OUTPUT_HANDLER_PROCESSED.
590596

597+
- Curl:
598+
. CURLOPT_PREREQFUNCTION.
599+
. CURL_PREREQFUNC_OK.
600+
. CURL_PREREQFUNC_ABORT.
601+
591602
- Intl:
592603
. The IntlDateFormatter class exposes now the new PATTERN constant
593604
reflecting udat api's UDAT_PATTERN.

ext/curl/curl.stub.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,21 @@
34853485
* @cvalue CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256
34863486
*/
34873487
const CURLOPT_SSH_HOST_PUBLIC_KEY_SHA256 = UNKNOWN;
3488+
/**
3489+
* @var int
3490+
* @cvalue CURLOPT_PREREQFUNCTION
3491+
*/
3492+
const CURLOPT_PREREQFUNCTION = UNKNOWN;
3493+
/**
3494+
* @var int
3495+
* @cvalue CURL_PREREQFUNC_OK
3496+
*/
3497+
const CURL_PREREQFUNC_OK = UNKNOWN;
3498+
/**
3499+
* @var int
3500+
* @cvalue CURL_PREREQFUNC_ABORT
3501+
*/
3502+
const CURL_PREREQFUNC_ABORT = UNKNOWN;
34883503
#endif
34893504

34903505
#if LIBCURL_VERSION_NUM >= 0x075100 /* Available since 7.81.0 */

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,9 @@ void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
12121212
php_curl_copy_fcc_with_option(ch, CURLOPT_PROGRESSDATA, &ch->handlers.progress, &source->handlers.progress);
12131213
php_curl_copy_fcc_with_option(ch, CURLOPT_XFERINFODATA, &ch->handlers.xferinfo, &source->handlers.xferinfo);
12141214
php_curl_copy_fcc_with_option(ch, CURLOPT_FNMATCH_DATA, &ch->handlers.fnmatch, &source->handlers.fnmatch);
1215+
#if LIBCURL_VERSION_NUM >= 0x075000 /* Available since 7.80.0 */
1216+
php_curl_copy_fcc_with_option(ch, CURLOPT_PREREQFUNCTION, &ch->handlers.prereq, &source->handlers.prereq);
1217+
#endif
12151218
#if LIBCURL_VERSION_NUM >= 0x075400 /* Available since 7.84.0 */
12161219
php_curl_copy_fcc_with_option(ch, CURLOPT_SSH_HOSTKEYDATA, &ch->handlers.sshhostkey, &source->handlers.sshhostkey);
12171220
#endif
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
Curl option CURLOPT_PREREQFUNCTION
3+
--EXTENSIONS--
4+
curl
5+
--SKIPIF--
6+
<?php
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x075000) die("skip: test works only with curl >= 7.80.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);
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
20+
//curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
21+
22+
var_dump(CURLOPT_PREREQFUNCTION);
23+
var_dump(CURL_PREREQFUNC_OK);
24+
var_dump(CURL_PREREQFUNC_ABORT);
25+
26+
curl_setopt($ch, CURLOPT_PREREQFUNCTION, function() use ($port) {
27+
var_dump('callback');
28+
var_dump(func_num_args());
29+
$args = func_get_args();
30+
var_dump(get_class($args[0]));
31+
var_dump(filter_var($args[1], FILTER_VALIDATE_IP) !== false);
32+
var_dump(filter_var($args[2], FILTER_VALIDATE_IP) !== false);
33+
var_dump($port === $args[3]);
34+
var_dump(is_int($args[4]));
35+
36+
return CURL_PREREQFUNC_ABORT;
37+
});
38+
39+
$result = curl_exec($ch);
40+
41+
var_dump($result);
42+
var_dump(curl_error($ch));
43+
var_dump(curl_errno($ch));
44+
45+
var_dump('finished');
46+
?>
47+
--EXPECT--
48+
int(20312)
49+
int(0)
50+
int(1)
51+
string(8) "callback"
52+
int(5)
53+
string(10) "CurlHandle"
54+
bool(true)
55+
bool(true)
56+
bool(true)
57+
bool(true)
58+
bool(false)
59+
string(41) "operation aborted by pre-request callback"
60+
int(42)
61+
string(8) "finished"

0 commit comments

Comments
 (0)