-
Notifications
You must be signed in to change notification settings - Fork 7.8k
ext/curl: Add CURLOPT_PREREQFUNCTION
#13255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to have the test not rely on the filter extension? Found this out while trying it locally without ext/filter compiled.
Otherwise it needs to be added to the EXTENSIONS section as I did.