Skip to content

Commit 40ab8cc

Browse files
committed
Reduce some redundant code in curl
1 parent 0c27edc commit 40ab8cc

File tree

1 file changed

+27
-50
lines changed

1 file changed

+27
-50
lines changed

ext/curl/interface.c

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,17 @@ PHP_FUNCTION(curl_init)
12041204
}
12051205
/* }}} */
12061206

1207+
static void _php_copy_callback(php_curl *ch, php_curl_callback **new_callback, php_curl_callback *source_callback, CURLoption option)
1208+
{
1209+
if (source_callback) {
1210+
*new_callback = ecalloc(1, sizeof(php_curl_callback));
1211+
if (!Z_ISUNDEF(source_callback->func_name)) {
1212+
ZVAL_COPY(&(*new_callback)->func_name, &source_callback->func_name);
1213+
}
1214+
curl_easy_setopt(ch->cp, option, (void *) ch);
1215+
}
1216+
}
1217+
12071218
void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
12081219
{
12091220
if (!Z_ISUNDEF(source->handlers.write->stream)) {
@@ -1243,40 +1254,13 @@ void _php_setup_easy_copy_handlers(php_curl *ch, php_curl *source)
12431254
curl_easy_setopt(ch->cp, CURLOPT_WRITEHEADER, (void *) ch);
12441255
curl_easy_setopt(ch->cp, CURLOPT_DEBUGDATA, (void *) ch);
12451256

1246-
if (source->handlers.progress) {
1247-
ch->handlers.progress = ecalloc(1, sizeof(php_curl_callback));
1248-
if (!Z_ISUNDEF(source->handlers.progress->func_name)) {
1249-
ZVAL_COPY(&ch->handlers.progress->func_name, &source->handlers.progress->func_name);
1250-
}
1251-
curl_easy_setopt(ch->cp, CURLOPT_PROGRESSDATA, (void *) ch);
1252-
}
1253-
1257+
_php_copy_callback(ch, &ch->handlers.progress, source->handlers.progress, CURLOPT_PROGRESSDATA);
12541258
#if LIBCURL_VERSION_NUM >= 0x072000
1255-
if (source->handlers.xferinfo) {
1256-
ch->handlers.xferinfo = ecalloc(1, sizeof(php_curl_callback));
1257-
if (!Z_ISUNDEF(source->handlers.xferinfo->func_name)) {
1258-
ZVAL_COPY(&ch->handlers.xferinfo->func_name, &source->handlers.xferinfo->func_name);
1259-
}
1260-
curl_easy_setopt(ch->cp, CURLOPT_XFERINFODATA, (void *) ch);
1261-
}
1259+
_php_copy_callback(ch, &ch->handlers.xferinfo, source->handlers.xferinfo, CURLOPT_XFERINFODATA);
12621260
#endif
1263-
1264-
if (source->handlers.fnmatch) {
1265-
ch->handlers.fnmatch = ecalloc(1, sizeof(php_curl_callback));
1266-
if (!Z_ISUNDEF(source->handlers.fnmatch->func_name)) {
1267-
ZVAL_COPY(&ch->handlers.fnmatch->func_name, &source->handlers.fnmatch->func_name);
1268-
}
1269-
curl_easy_setopt(ch->cp, CURLOPT_FNMATCH_DATA, (void *) ch);
1270-
}
1271-
1261+
_php_copy_callback(ch, &ch->handlers.fnmatch, source->handlers.fnmatch, CURLOPT_FNMATCH_DATA);
12721262
#if LIBCURL_VERSION_NUM >= 0x075400
1273-
if (source->handlers.sshhostkey) {
1274-
ch->handlers.sshhostkey = ecalloc(1, sizeof(php_curl_callback));
1275-
if (!Z_ISUNDEF(source->handlers.sshhostkey->func_name)) {
1276-
ZVAL_COPY(&ch->handlers.sshhostkey->func_name, &source->handlers.sshhostkey->func_name);
1277-
}
1278-
curl_easy_setopt(ch->cp, CURLOPT_SSH_HOSTKEYDATA, (void *) ch);
1279-
}
1263+
_php_copy_callback(ch, &ch->handlers.sshhostkey, source->handlers.sshhostkey, CURLOPT_SSH_HOSTKEYDATA);
12801264
#endif
12811265

12821266
ZVAL_COPY(&ch->private_data, &source->private_data);
@@ -2912,6 +2896,14 @@ PHP_FUNCTION(curl_close)
29122896
}
29132897
/* }}} */
29142898

2899+
static void _php_curl_free_callback(php_curl_callback* callback)
2900+
{
2901+
if (callback) {
2902+
zval_ptr_dtor(&callback->func_name);
2903+
efree(callback);
2904+
}
2905+
}
2906+
29152907
static void curl_free_obj(zend_object *object)
29162908
{
29172909
php_curl *ch = curl_from_obj(object);
@@ -2976,28 +2968,13 @@ static void curl_free_obj(zend_object *object)
29762968
efree(ch->handlers.write_header);
29772969
efree(ch->handlers.read);
29782970

2979-
if (ch->handlers.progress) {
2980-
zval_ptr_dtor(&ch->handlers.progress->func_name);
2981-
efree(ch->handlers.progress);
2982-
}
2983-
2971+
_php_curl_free_callback(ch->handlers.progress);
29842972
#if LIBCURL_VERSION_NUM >= 0x072000
2985-
if (ch->handlers.xferinfo) {
2986-
zval_ptr_dtor(&ch->handlers.xferinfo->func_name);
2987-
efree(ch->handlers.xferinfo);
2988-
}
2973+
_php_curl_free_callback(ch->handlers.xferinfo);
29892974
#endif
2990-
2991-
if (ch->handlers.fnmatch) {
2992-
zval_ptr_dtor(&ch->handlers.fnmatch->func_name);
2993-
efree(ch->handlers.fnmatch);
2994-
}
2995-
2975+
_php_curl_free_callback(ch->handlers.fnmatch);
29962976
#if LIBCURL_VERSION_NUM >= 0x075400
2997-
if (ch->handlers.sshhostkey) {
2998-
zval_ptr_dtor(&ch->handlers.sshhostkey->func_name);
2999-
efree(ch->handlers.sshhostkey);
3000-
}
2977+
_php_curl_free_callback(ch->handlers.sshhostkey);
30012978
#endif
30022979

30032980
zval_ptr_dtor(&ch->postfields);

0 commit comments

Comments
 (0)