Skip to content

Commit 64595a5

Browse files
committed
Add curl_multi_setopt and clean curl_share_setopt
curl_multi_setopt is now available and supports CURLMOPT_PIPELINING and CURLMOPT_MAXCONNECTS
1 parent ded889e commit 64595a5

File tree

6 files changed

+120
-6
lines changed

6 files changed

+120
-6
lines changed

ext/curl/interface.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ ZEND_BEGIN_ARG_INFO(arginfo_curl_unescape, 0)
330330
ZEND_ARG_INFO(0, ch)
331331
ZEND_ARG_INFO(0, str)
332332
ZEND_END_ARG_INFO()
333+
334+
ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_setopt, 0)
335+
ZEND_ARG_INFO(0, sh)
336+
ZEND_ARG_INFO(0, option)
337+
ZEND_ARG_INFO(0, value)
338+
ZEND_END_ARG_INFO()
333339
#endif
334340

335341
ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_init, 0)
@@ -410,6 +416,9 @@ const zend_function_entry curl_functions[] = {
410416
PHP_FE(curl_multi_getcontent, arginfo_curl_multi_getcontent)
411417
PHP_FE(curl_multi_info_read, arginfo_curl_multi_info_read)
412418
PHP_FE(curl_multi_close, arginfo_curl_multi_close)
419+
#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
420+
PHP_FE(curl_multi_setopt, arginfo_curl_multi_setopt)
421+
#endif
413422
PHP_FE(curl_share_init, arginfo_curl_share_init)
414423
PHP_FE(curl_share_close, arginfo_curl_share_close)
415424
PHP_FE(curl_share_setopt, arginfo_curl_share_setopt)
@@ -928,6 +937,7 @@ PHP_MINIT_FUNCTION(curl)
928937

929938
#if LIBCURL_VERSION_NUM >= 0x071000 /* Available since 7.16.0 */
930939
REGISTER_CURL_CONSTANT(CURLOPT_SSL_SESSIONID_CACHE);
940+
REGISTER_CURL_CONSTANT(CURLMOPT_PIPELINING);
931941
#endif
932942

933943
#if LIBCURL_VERSION_NUM >= 0x071001 /* Available since 7.16.1 */
@@ -948,6 +958,10 @@ PHP_MINIT_FUNCTION(curl)
948958
REGISTER_CURL_CONSTANT(CURLOPT_TIMEOUT_MS);
949959
#endif
950960

961+
#if LIBCURL_VERSION_NUM >= 0x071003 /* Available since 7.16.3 */
962+
REGISTER_CURL_CONSTANT(CURLMOPT_MAXCONNECTS);
963+
#endif
964+
951965
#if LIBCURL_VERSION_NUM >= 0x071004 /* Available since 7.16.4 */
952966
REGISTER_CURL_CONSTANT(CURLOPT_KRBLEVEL);
953967
REGISTER_CURL_CONSTANT(CURLOPT_NEW_DIRECTORY_PERMS);

ext/curl/multi.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,60 @@ void _php_curl_multi_close(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
359359
}
360360
/* }}} */
361361

362+
#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
363+
static int _php_curl_multi_setopt(php_curlm *mh, long option, zval **zvalue, zval *return_value TSRMLS_DC) /* {{{ */
364+
{
365+
CURLMcode error = CURLM_OK;
366+
367+
switch (option) {
368+
#if LIBCURL_VERSION_NUM >= 0x071000 /* 7.16.0 */
369+
case CURLMOPT_PIPELINING:
370+
#endif
371+
#if LIBCURL_VERSION_NUM >= 0x071003 /* 7.16.3 */
372+
case CURLMOPT_MAXCONNECTS:
373+
#endif
374+
convert_to_long_ex(zvalue);
375+
error = curl_multi_setopt(mh->multi, option, Z_LVAL_PP(zvalue));
376+
break;
377+
378+
default:
379+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl multi configuration option");
380+
error = CURLM_UNKNOWN_OPTION;
381+
break;
382+
}
383+
384+
if (error != CURLM_OK) {
385+
return 1;
386+
} else {
387+
return 0;
388+
}
389+
}
390+
/* }}} */
391+
392+
393+
/* {{{ proto int curl_multi_setopt(resource mh, int option, mixed value)
394+
Set an option for the curl multi handle */
395+
PHP_FUNCTION(curl_multi_setopt)
396+
{
397+
zval *z_mh, **zvalue;
398+
long options;
399+
php_curlm *mh;
400+
401+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlZ", &z_mh, &options, &zvalue) == FAILURE) {
402+
return;
403+
}
404+
405+
ZEND_FETCH_RESOURCE(mh, php_curlm *, &z_mh, -1, le_curl_multi_handle_name, le_curl_multi_handle);
406+
407+
if (!_php_curl_multi_setopt(mh, options, zvalue, return_value TSRMLS_CC)) {
408+
RETURN_TRUE;
409+
} else {
410+
RETURN_FALSE;
411+
}
412+
}
413+
/* }}} */
414+
#endif
415+
362416
#endif
363417

364418
/*

ext/curl/php_curl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ PHP_FUNCTION(curl_reset);
9191
#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
9292
PHP_FUNCTION(curl_escape);
9393
PHP_FUNCTION(curl_unescape);
94+
95+
PHP_FUNCTION(curl_multi_setopt);
9496
#endif
9597

9698
void _php_curl_multi_close(zend_rsrc_list_entry * TSRMLS_DC);

ext/curl/share.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,14 @@ static int _php_curl_share_setopt(php_curlsh *sh, long option, zval **zvalue, zv
7676
convert_to_long_ex(zvalue);
7777
error = curl_share_setopt(sh->share, option, Z_LVAL_PP(zvalue));
7878
break;
79+
80+
default:
81+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl share configuration option");
82+
error = CURLSHE_BAD_OPTION;
83+
break;
7984
}
8085

81-
if (error != CURLE_OK) {
86+
if (error != CURLSHE_OK) {
8287
return 1;
8388
} else {
8489
return 0;
@@ -100,11 +105,6 @@ PHP_FUNCTION(curl_share_setopt)
100105

101106
ZEND_FETCH_RESOURCE(sh, php_curlsh *, &zid, -1, le_curl_share_handle_name, le_curl_share_handle);
102107

103-
if (options <= 0) {
104-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid curl share configuration option");
105-
RETURN_FALSE;
106-
}
107-
108108
if (!_php_curl_share_setopt(sh, options, zvalue, return_value TSRMLS_CC)) {
109109
RETURN_TRUE;
110110
} else {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
curl_multi_setopt basic test
3+
--SKIPIF--
4+
if (!extension_loaded("curl")) {
5+
exit("skip curl extension not loaded");
6+
}
7+
$curl_version = curl_version();
8+
if ($curl_version['version_number'] < 0x071000) {
9+
exit("skip: test works only with curl >= 7.16.0");
10+
}
11+
--FILE--
12+
<?php
13+
14+
$mh = curl_multi_init();
15+
var_dump(curl_multi_setopt($mh, CURLMOPT_PIPELINING, 0));
16+
var_dump(curl_multi_setopt($mh, -1, 0));
17+
18+
?>
19+
--EXPECTF--
20+
bool(true)
21+
22+
Warning: curl_multi_setopt(): Invalid curl multi configuration option in %s on line %d
23+
bool(false)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
curl_share_setopt basic test
3+
--SKIPIF--
4+
if (!extension_loaded("curl")) {
5+
exit("skip curl extension not loaded");
6+
}
7+
--FILE--
8+
<?php
9+
10+
$sh = curl_share_init();
11+
var_dump(curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE));
12+
var_dump(curl_share_setopt($sh, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS));
13+
var_dump(curl_share_setopt($sh, -1, 0));
14+
15+
?>
16+
--EXPECTF--
17+
bool(true)
18+
bool(true)
19+
20+
Warning: curl_share_setopt(): Invalid curl share configuration option in %s on line %d
21+
bool(false)

0 commit comments

Comments
 (0)