8000 ext/curl: Refactor cURL to only use FCC by Girgias · Pull Request #13291 · php/php-src · GitHub
[go: up one dir, main page]

Skip to content

ext/curl: Refactor cURL to only use FCC #13291

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 15 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ext/curl: Convert handlers.server_push to just be a FCC
TODO Tests for multi handler
  • Loading branch information
Girgias committed Apr 30, 2024
commit 1dfce9d7a1031422243aa1f6041ee6beb23dded9
2 changes: 1 addition & 1 deletion ext/curl/curl_private.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ typedef struct {
#define CURLOPT_SAFE_UPLOAD -1

typedef struct {
php_curl_callback *server_push;
zend_fcall_info_cache server_push;
} php_curlm_handlers;

typedef struct {
Expand Down
55 changes: 24 additions & 31 deletions ext/curl/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,51 +372,36 @@ static int _php_server_push_callback(CURL *parent_ch, CURL *easy, size_t num_hea
php_curl *parent;
php_curlm *mh = (php_curlm *)userp;
size_t rval = CURL_PUSH_DENY;
php_curl_callback *t = mh->handlers.server_push;
zval *pz_parent_ch = NULL;
zval pz_ch;
zval headers;
zval retval;
char *header;
zend_result error;
zend_fcall_info fci = empty_fcall_info;

pz_parent_ch = _php_curl_multi_find_easy_handle(mh, parent_ch);
if (pz_parent_ch == NULL) {
return rval;
}

if (UNEXPECTED(zend_fcall_info_init(&t->func_name, 0, &fci, &t->fci_cache, NULL, NULL) == FAILURE)) {
php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
return rval;
}

parent = Z_CURL_P(pz_parent_ch);

ch = init_curl_handle_into_zval(&pz_ch);
ch->cp = easy;
_php_setup_easy_copy_handlers(ch, parent);

size_t i;
array_init(&headers);
for(i=0; i<num_headers; i++) {
for (size_t i = 0; i < num_headers; i++) {
char *header;
header = curl_pushheader_bynum(push_headers, i);
add_next_index_string(&headers, header);
}

ZEND_ASSERT(pz_parent_ch);
zval call_args[3] = {*pz_parent_ch, pz_ch, headers};

fci.param_count = 3;
fci.params = call_args;
fci.retval = &retval;

error = zend_call_function(&fci, &t->fci_cache);
zend_call_known_fcc(&mh->handlers.server_push, &retval, /* param_count */ 3, call_args, /* named_params */ NULL);
zval_ptr_dtor_nogc(&headers);

if (error == FAILURE) {
php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
} else if (!Z_ISUNDEF(retval)) {
if (!Z_ISUNDEF(retval)) {
if (CURL_PUSH_DENY != zval_get_long(&retval)) {
rval = CURL_PUSH_OK;
zend_llist_add_element(&mh->easyh, &pz_ch);
Expand Down Expand Up @@ -458,21 +443,29 @@ static bool _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue
error = curl_multi_setopt(mh->multi, option, lval);
break;
}
case CURLMOPT_PUSHFUNCTION:
if (mh->handlers.server_push == NULL) {
mh->handlers.server_push = ecalloc(1, sizeof(php_curl_callback));
} else if (!Z_ISUNDEF(mh->handlers.server_push->func_name)) {
zval_ptr_dtor(&mh->handlers.server_push->func_name);
mh->handlers.server_push->fci_cache = empty_fcall_info_cache;
case CURLMOPT_PUSHFUNCTION: {
/* See php_curl_set_callable_handler */
if (ZEND_FCC_INITIALIZED(mh->handlers.server_push)) {
zend_fcc_dtor(&mh->handlers.server_push);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be set to the empty fcc?

}

ZVAL_COPY(&mh->handlers.server_push->func_name, zvalue);
char *error_str = NULL;
if (UNEXPECTED(!zend_is_callable_ex(zvalue, /* object */ NULL, /* check_flags */ 0, /* callable_name */ NULL, &mh->handlers.server_push, /* error */ &error_str))) {
if (!EG(exception)) {
zend_argument_type_error(2, "must be a valid callback for option CURLMOPT_PUSHFUNCTION, %s", error_str);
}
efree(error_str);
return false;
}
zend_fcc_addref(&mh->handlers.server_push);

error = curl_multi_setopt(mh->multi, CURLMOPT_PUSHFUNCTION, _php_server_push_callback);
if (error != CURLM_OK) {
return false;
}
error = curl_multi_setopt(mh->multi, CURLMOPT_PUSHDATA, mh);
break;
}
default:
zend_argument_value_error(2, "is not a valid cURL multi option");
error = CURLM_UNKNOWN_OPTION;
Expand Down Expand Up @@ -548,9 +541,9 @@ static void curl_multi_free_obj(zend_object *object)

curl_multi_cleanup(mh->multi);
zend_llist_clean(&mh->easyh);
if (mh->handlers.server_push) {
zval_ptr_dtor(&mh->handlers.server_push->func_name);
efree(mh->handlers.server_push);

if (ZEND_FCC_INITIALIZED(mh->handlers.server_push)) {
zend_fcc_dtor(&mh->handlers.server_push);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other places you set it then to the empty fcc, did you forget to do that here?

}

zend_object_std_dtor(&mh->std);
Expand All @@ -562,8 +555,8 @@ static HashTable *curl_multi_get_gc(zend_object *object, zval **table, int *n)

zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();

if (curl_multi->handlers.server_push) {
zend_get_gc_buffer_add_zval(gc_buffer, &curl_multi->handlers.server_push->func_name);
if (ZEND_FCC_INITIALIZED(curl_multi->handlers.server_push)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &curl_multi->handlers.server_push);
}

zend_llist_position pos;
Expand Down
17 changes: 17 additions & 0 deletions ext/curl/tests/curl_multi_setopt_callables.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test curl_multi_setopt() with options that take callabes
--EXTENSIONS--
curl
--FILE--
<?php

$mh = curl_multi_init();
try {
var_dump(curl_multi_setopt($mh, CURLMOPT_PUSHFUNCTION, 'undefined'));
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}

?>
--EXPECT--
TypeError: curl_multi_setopt(): Argument #2 ($option) must be a valid callback for option CURLMOPT_PUSHFUNCTION, function "undefined" not found or invalid function name
54 changes: 0 additions & 54 deletions ext/curl/tests/curl_pushfunction_nonexistent_callback.phpt

This file was deleted.

0