File: curl_setopt_CURLOPT_PREREQFUNCTION.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (182 lines) | stat: -rw-r--r-- 4,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
--TEST--
Curl option CURLOPT_PREREQFUNCTION
--EXTENSIONS--
curl
filter
--SKIPIF--
<?php
$curl_version = curl_version();
if ($curl_version['version_number'] < 0x075000) die("skip: test works only with curl >= 7.80.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);

$result = curl_exec($ch);

var_dump(CURLOPT_PREREQFUNCTION);
var_dump(CURL_PREREQFUNC_OK);
var_dump(CURL_PREREQFUNC_ABORT);

$returnValue = CURL_PREREQFUNC_ABORT;

echo "\nTesting with CURL_PREREQFUNC_ABORT\n";
$callback = function() use ($port, &$returnValue) {
	var_dump('callback');
	var_dump(func_num_args());
	$args = func_get_args();
	var_dump(get_class($args[0]));
	var_dump(filter_var($args[1], FILTER_VALIDATE_IP) !== false);
	var_dump(filter_var($args[2], FILTER_VALIDATE_IP) !== false);
	var_dump($port === $args[3]);
	var_dump(is_int($args[4]));

	return $returnValue;
};

curl_setopt($ch, CURLOPT_PREREQFUNCTION, $callback);

$result = curl_exec($ch);

var_dump($result);
var_dump(curl_error($ch));
var_dump(curl_errno($ch));

$returnValue = CURL_PREREQFUNC_OK;

echo "\nTesting with CURL_PREREQFUNC_OK\n";
$result = curl_exec($ch);

var_dump($result);
var_dump(curl_error($ch));
var_dump(curl_errno($ch));

echo "\nTesting with curl_copy_handle\n";
$ch2 = curl_copy_handle($ch);
$result = curl_exec($ch2);
var_dump($result);
var_dump(curl_error($ch2));
var_dump(curl_errno($ch2));

echo "\nTesting with no return type\n";
curl_setopt($ch, CURLOPT_PREREQFUNCTION, function() use ($port) {
	// returns nothing
});
try {
    curl_exec($ch);
} catch (\TypeError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

echo "\nTesting with invalid type\n";
curl_setopt($ch, CURLOPT_PREREQFUNCTION, function() use ($port) {
	return 'this should be an integer';
});
try {
    curl_exec($ch);
} catch (\TypeError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

echo "\nTesting with invalid value\n";
curl_setopt($ch, CURLOPT_PREREQFUNCTION, function() use ($port) {
	return 42;
});
try {
    curl_exec($ch);
} catch (\ValueError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

echo "\nTesting with invalid option value\n";
try {
    curl_setopt($ch, CURLOPT_PREREQFUNCTION, 42);
} catch (\TypeError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

echo "\nTesting with invalid option callback\n";
try {
    curl_setopt($ch, CURLOPT_PREREQFUNCTION, 'function_does_not_exist');
} catch (\TypeError $e) {
    echo $e->getMessage() . \PHP_EOL;
}

echo "\nTesting with null as the callback\n";
var_dump(curl_setopt($ch, CURLOPT_PREREQFUNCTION, null));
var_dump(curl_exec($ch));
var_dump(curl_error($ch));
var_dump(curl_errno($ch));

echo "\nDone";
?>
--EXPECT--
int(20312)
int(0)
int(1)

Testing with CURL_PREREQFUNC_ABORT
string(8) "callback"
int(5)
string(10) "CurlHandle"
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
string(41) "operation aborted by pre-request callback"
int(42)

Testing with CURL_PREREQFUNC_OK
string(8) "callback"
int(5)
string(10) "CurlHandle"
bool(true)
bool(true)
bool(true)
bool(true)
string(0) ""
string(0) ""
int(0)

Testing with curl_copy_handle
string(8) "callback"
int(5)
string(10) "CurlHandle"
bool(true)
bool(true)
bool(true)
bool(true)
string(0) ""
string(0) ""
int(0)

Testing with no return type
The CURLOPT_PREREQFUNCTION callback must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT

Testing with invalid type
The CURLOPT_PREREQFUNCTION callback must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT

Testing with invalid value
The CURLOPT_PREREQFUNCTION callback must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT

Testing with invalid option value
curl_setopt(): Argument #3 ($value) must be a valid callback for option CURLOPT_PREREQFUNCTION, no array or string given

Testing with invalid option callback
curl_setopt(): Argument #3 ($value) must be a valid callback for option CURLOPT_PREREQFUNCTION, function "function_does_not_exist" not found or invalid function name

Testing with null as the callback
bool(true)
string(0) ""
string(0) ""
int(0)

Done