-
Notifications
You must be signed in to change notification settings - Fork 7.9k
curl: Add curl_multi_get_handles()
#16363
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,6 +174,29 @@ PHP_FUNCTION(curl_multi_remove_handle) | |
} | ||
/* }}} */ | ||
|
||
PHP_FUNCTION(curl_multi_get_handles) | ||
{ | ||
zval *z_mh; | ||
php_curlm *mh; | ||
|
||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||
Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce) | ||
ZEND_PARSE_PARAMETERS_END(); | ||
|
||
mh = Z_CURL_MULTI_P(z_mh); | ||
|
||
array_init(return_value); | ||
zend_llist_position pos; | ||
zval *pz_ch; | ||
|
||
for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could merge the declaration within the for loop. |
||
pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) { | ||
|
||
Z_TRY_ADDREF_P(pz_ch); | ||
add_next_index_zval(return_value, pz_ch); | ||
} | ||
} | ||
|
||
/* {{{ Get all the sockets associated with the cURL extension, which can then be "selected" */ | ||
PHP_FUNCTION(curl_multi_select) | ||
{ | ||
|
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,71 @@ | ||
--TEST-- | ||
array curl_multi_get_handles ( CurlMultiHandle $mh ); | ||
--EXTENSIONS-- | ||
curl | ||
--FILE-- | ||
<?php | ||
$urls = array( | ||
"file://".__DIR__."/curl_testdata1.txt", | ||
"file://".__DIR__."/curl_testdata2.txt", | ||
); | ||
|
||
$mh = curl_multi_init(); | ||
$map = new WeakMap(); | ||
|
||
foreach ($urls as $url) { | ||
echo "Initializing {$url}.", PHP_EOL; | ||
$ch = curl_init($url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | ||
curl_multi_add_handle($mh, $ch); | ||
printf("%d handles are attached\n", count(curl_multi_get_handles($mh))); | ||
$map[$ch] = $url; | ||
} | ||
|
||
do { | ||
$status = curl_multi_exec($mh, $active); | ||
if ($status !== CURLM_OK) { | ||
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh))); | ||
} | ||
|
||
if ($active) { | ||
$activity = curl_multi_select($mh); | ||
if ($activity === -1) { | ||
throw new \Exception(curl_multi_strerror(curl_multi_errno($mh))); | ||
} | ||
} | ||
|
||
while (($info = curl_multi_info_read($mh)) !== false) { | ||
if ($info['msg'] === CURLMSG_DONE) { | ||
$handle = $info['handle']; | ||
$url = $map[$handle]; | ||
echo "Request to {$url} finished.", PHP_EOL; | ||
printf("%d handles are attached\n", count(curl_multi_get_handles($mh))); | ||
curl_multi_remove_handle($mh, $handle); | ||
printf("%d handles are attached\n", count(curl_multi_get_handles($mh))); | ||
|
||
if ($info['result'] === CURLE_OK) { | ||
echo "Success.", PHP_EOL; | ||
} else { | ||
echo "Failure.", PHP_EOL; | ||
} | ||
} | ||
} | ||
} while ($active); | ||
|
||
printf("%d handles are attached\n", count(curl_multi_get_handles($mh))); | ||
|
||
?> | ||
--EXPECTF-- | ||
Initializing %scurl_testdata1.txt. | ||
1 handles are attached | ||
Initializing %scurl_testdata2.txt. | ||
2 handles are attached | ||
Request to %scurl_testdata%d.txt finished. | ||
2 handles are attached | ||
1 handles are attached | ||
Success. | ||
Request to %scurl_testdata%d.txt finished. | ||
1 handles are attached | ||
0 handles are attached | ||
Success. | ||
0 handles are attached |
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.
The tab character seems unintentional here?
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.
I've stolen the logic as-is from
curl_multi_exec
and did not adjust the code style to keep the file internally consistent. This also applies to the other remark.There would be more to clean up here, for example the removal of the useless
(zval *)
cast, because avoid*
casts implicitly to anything.Perhaps it would even make sense to migrate from
zend_llist
toHashTable
.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.
Sure, let's move this to a follow-up PR then.