Skip to content

Commit 3d71c1d

Browse files
committed
Add curl_multi/share_errno() curl_share_strerror()
Add 3 new functions : - curl_multi_errno() - curl_share_errno() - curl_share_strerror() https://wiki.php.net/rfc/new-curl-error-functions
1 parent f7281ce commit 3d71c1d

File tree

6 files changed

+170
-6
lines changed

6 files changed

+170
-6
lines changed

ext/curl/interface.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_close, 0)
392392
ZEND_ARG_INFO(0, mh)
393393
ZEND_END_ARG_INFO()
394394

395+
ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_errno, 0)
396+
ZEND_ARG_INFO(0, mh)
397+
ZEND_END_ARG_INFO()
398+
395399
#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
396400
ZEND_BEGIN_ARG_INFO(arginfo_curl_strerror, 0)
397401
ZEND_ARG_INFO(0, errornum)
@@ -400,6 +404,10 @@ ZEND_END_ARG_INFO()
400404
ZEND_BEGIN_ARG_INFO(arginfo_curl_multi_strerror, 0)
401405
ZEND_ARG_INFO(0, errornum)
402406
ZEND_END_ARG_INFO()
407+
408+
ZEND_BEGIN_ARG_INFO(arginfo_curl_share_strerror, 0)
409+
ZEND_ARG_INFO(0, errornum)
410+
ZEND_END_ARG_INFO()
403411
#endif
404412

405413
ZEND_BEGIN_ARG_INFO(arginfo_curl_share_init, 0)
@@ -415,6 +423,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_curl_share_setopt, 0)
415423
ZEND_ARG_INFO(0, value)
416424
ZEND_END_ARG_INFO()
417425

426+
ZEND_BEGIN_ARG_INFO(arginfo_curl_share_errno, 0)
427+
ZEND_ARG_INFO(0, sh)
428+
ZEND_END_ARG_INFO()
429+
418430
#if LIBCURL_VERSION_NUM >= 0x071200 /* Available since 7.18.0 */
419431
ZEND_BEGIN_ARG_INFO(arginfo_curl_pause, 0)
420432
ZEND_ARG_INFO(0, ch)
@@ -445,6 +457,7 @@ const zend_function_entry curl_functions[] = {
445457
#if LIBCURL_VERSION_NUM >= 0x070c00 /* 7.12.0 */
446458
PHP_FE(curl_strerror, arginfo_curl_strerror)
447459
PHP_FE(curl_multi_strerror, arginfo_curl_multi_strerror)
460+
PHP_FE(curl_share_strerror, arginfo_curl_share_strerror)
448461
#endif
449462
#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
450463
PHP_FE(curl_reset, arginfo_curl_reset)
@@ -464,12 +477,14 @@ const zend_function_entry curl_functions[] = {
464477
PHP_FE(curl_multi_getcontent, arginfo_curl_multi_getcontent)
465478
PHP_FE(curl_multi_info_read, arginfo_curl_multi_info_read)
466479
PHP_FE(curl_multi_close, arginfo_curl_multi_close)
480+
PHP_FE(curl_multi_errno, arginfo_curl_multi_errno)
467481
#if LIBCURL_VERSION_NUM >= 0x070f04 /* 7.15.4 */
468482
PHP_FE(curl_multi_setopt, arginfo_curl_multi_setopt)
469483
#endif
470484
PHP_FE(curl_share_init, arginfo_curl_share_init)
471485
PHP_FE(curl_share_close, arginfo_curl_share_close)
472486
PHP_FE(curl_share_setopt, arginfo_curl_share_setopt)
487+
PHP_FE(curl_share_errno, arginfo_curl_share_errno)
473488
PHP_FE(curl_file_create, arginfo_curlfile_create)
474489
PHP_FE_END
475490
};

ext/curl/multi.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#include <unistd.h>
5050
#endif
5151

52+
#define SAVE_CURLM_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
53+
5254
/* {{{ proto resource curl_multi_init(void)
5355
Returns a new cURL multi handle */
5456
PHP_FUNCTION(curl_multi_init)
@@ -77,6 +79,7 @@ PHP_FUNCTION(curl_multi_add_handle)
7779
php_curlm *mh;
7880
php_curl *ch;
7981
zval tmp_val;
82+
CURLMcode error = CURLM_OK;
8083

8184
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &z_mh, &z_ch) == FAILURE) {
8285
return;
@@ -97,7 +100,10 @@ PHP_FUNCTION(curl_multi_add_handle)
97100

98101
zend_llist_add_element(&mh->easyh, &tmp_val);
99102

100-
RETURN_LONG((zend_long)curl_multi_add_handle(mh->multi, ch->cp));
103+
error = curl_multi_add_handle(mh->multi, ch->cp);
104+
SAVE_CURLM_ERROR(mh, error);
105+
106+
RETURN_LONG((zend_long) error);
101107
}
102108
/* }}} */
103109

@@ -137,6 +143,7 @@ PHP_FUNCTION(curl_multi_remove_handle)
137143
zval *z_ch;
138144
php_curlm *mh;
139145
php_curl *ch;
146+
CURLMcode error = CURLM_OK;
140147

141148
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr", &z_mh, &z_ch) == FAILURE) {
142149
return;
@@ -150,7 +157,10 @@ PHP_FUNCTION(curl_multi_remove_handle)
150157
RETURN_FALSE;
151158
}
152159

153-
RETVAL_LONG((zend_long)curl_multi_remove_handle(mh->multi, ch->cp));
160+
error = curl_multi_remove_handle(mh->multi, ch->cp);
161+
SAVE_CURLM_ERROR(mh, error);
162+
163+
RETVAL_LONG((zend_long) error);
154164
zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_resources);
155165

156166
}
@@ -178,6 +188,7 @@ PHP_FUNCTION(curl_multi_select)
178188
int maxfd;
179189
double timeout = 1.0;
180190
struct timeval to;
191+
CURLMcode error = CURLM_OK;
181192

182193
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|d", &z_mh, &timeout) == FAILURE) {
183194
return;
@@ -193,7 +204,9 @@ PHP_FUNCTION(curl_multi_select)
193204
FD_ZERO(&writefds);
194205
FD_ZERO(&exceptfds);
195206

196-
curl_multi_fdset(mh->multi, &readfds, &writefds, &exceptfds, &maxfd);
207+
error = curl_multi_fdset(mh->multi, &readfds, &writefds, &exceptfds, &maxfd);
208+
SAVE_CURLM_ERROR(mh, error);
209+
197210
if (maxfd == -1) {
198211
RETURN_LONG(-1);
199212
}
@@ -209,7 +222,7 @@ PHP_FUNCTION(curl_multi_exec)
209222
zval *z_still_running;
210223
php_curlm *mh;
211224
int still_running;
212-
int result;
225+
CURLMcode error = CURLM_OK;
213226

214227
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/", &z_mh, &z_still_running) == FAILURE) {
215228
return;
@@ -237,10 +250,11 @@ PHP_FUNCTION(curl_multi_exec)
237250

238251
convert_to_long(z_still_running);
239252
still_running = Z_LVAL_P(z_still_running);
240-
result = curl_multi_perform(mh->multi, &still_running);
253+
error = curl_multi_perform(mh->multi, &still_running);
241254
ZVAL_LONG(z_still_running, still_running);
242255

243-
RETURN_LONG(result);
256+
SAVE_CURLM_ERROR(mh, error);
257+
RETURN_LONG((zend_long) error);
244258
}
245259
/* }}} */
246260

@@ -383,6 +397,25 @@ void _php_curl_multi_close(zend_resource *rsrc) /* {{{ */
383397
}
384398
/* }}} */
385399

400+
/* {{{ proto int curl_multi_errno(resource mh)
401+
Return an integer containing the last multi curl error number */
402+
PHP_FUNCTION(curl_multi_errno)
403+
{
404+
zval *z_mh;
405+
php_curlm *mh;
406+
407+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_mh) == FAILURE) {
408+
return;
409+
}
410+
411+
if ((mh = (php_curlm *)zend_fetch_resource(Z_RES_P(z_mh), le_curl_multi_handle_name, le_curl_multi_handle)) == NULL) {
412+
RETURN_FALSE;
413+
}
414+
415+
RETURN_LONG(mh->err.no);
416+
}
417+
/* }}} */
418+
386419
#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
387420
/* {{{ proto bool curl_multi_strerror(int code)
388421
return string describing error code */
@@ -433,6 +466,7 @@ static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue,
433466
break;
434467
}
435468

469+
SAVE_CURLM_ERROR(mh, error);
436470
if (error != CURLM_OK) {
437471
return 1;
438472
} else {

ext/curl/php_curl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,17 @@ PHP_FUNCTION(curl_multi_info_read);
9090
PHP_FUNCTION(curl_multi_init);
9191
PHP_FUNCTION(curl_multi_remove_handle);
9292
PHP_FUNCTION(curl_multi_select);
93+
PHP_FUNCTION(curl_multi_errno);
9394

9495
PHP_FUNCTION(curl_share_close);
9596
PHP_FUNCTION(curl_share_init);
9697
PHP_FUNCTION(curl_share_setopt);
98+
PHP_FUNCTION(curl_share_errno);
9799

98100
#if LIBCURL_VERSION_NUM >= 0x070c00 /* 7.12.0 */
99101
PHP_FUNCTION(curl_strerror);
100102
PHP_FUNCTION(curl_multi_strerror);
103+
PHP_FUNCTION(curl_share_strerror);
101104
#endif
102105

103106
#if LIBCURL_VERSION_NUM >= 0x070c01 /* 7.12.1 */
@@ -114,6 +117,7 @@ PHP_FUNCTION(curl_multi_setopt);
114117
#if LIBCURL_VERSION_NUM >= 0x071200 /* 7.18.0 */
115118
PHP_FUNCTION(curl_pause);
116119
#endif
120+
117121
PHP_FUNCTION(curl_file_create);
118122

119123

@@ -190,10 +194,16 @@ typedef struct {
190194
int still_running;
191195
CURLM *multi;
192196
zend_llist easyh;
197+
struct {
198+
int no;
199+
} err;
193200
} php_curlm;
194201

195202
typedef struct {
196203
CURLSH *share;
204+
struct {
205+
int no;
206+
} err;
197207
} php_curlsh;
198208

199209
void _php_curl_cleanup_handle(php_curl *);

ext/curl/share.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include <curl/curl.h>
3434

35+
#define SAVE_CURLSH_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
36+
3537
/* {{{ proto void curl_share_init()
3638
Initialize a share curl handle */
3739
PHP_FUNCTION(curl_share_init)
@@ -85,6 +87,7 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
8587
break;
8688
}
8789

90+
SAVE_CURLSH_ERROR(sh, error);
8891
if (error != CURLSHE_OK) {
8992
return 1;
9093
} else {
@@ -128,6 +131,48 @@ void _php_curl_share_close(zend_resource *rsrc) /* {{{ */
128131
}
129132
/* }}} */
130133

134+
/* {{{ proto int curl_share_errno(resource mh)
135+
Return an integer containing the last share curl error number */
136+
PHP_FUNCTION(curl_share_errno)
137+
{
138+
zval *z_sh;
139+
php_curlsh *sh;
140+
141+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_sh) == FAILURE) {
142+
return;
143+
}
144+
145+
if ((sh = (php_curlsh *)zend_fetch_resource(Z_RES_P(z_sh), le_curl_share_handle_name, le_curl_share_handle)) == NULL) {
146+
RETURN_FALSE;
147+
}
148+
149+
RETURN_LONG(sh->err.no);
150+
}
151+
/* }}} */
152+
153+
154+
#if LIBCURL_VERSION_NUM >= 0x070c00 /* Available since 7.12.0 */
155+
/* {{{ proto bool curl_share_strerror(int code)
156+
return string describing error code */
157+
PHP_FUNCTION(curl_share_strerror)
158+
{
159+
zend_long code;
160+
const char *str;
161+
162+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
163+
return;
164+
}
165+
166+
str = curl_share_strerror(code);
167+
if (str) {
168+
RETURN_STRING(str);
169+
} else {
170+
RETURN_NULL();
171+
}
172+
}
173+
/* }}} */
174+
#endif
175+
131176
#endif
132177

133178
/*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
curl_multi_errno and curl_multi_strerror basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("curl")) {
6+
exit("skip curl extension not loaded");
7+
}
8+
$curl_version = curl_version();
9+
if ($curl_version['version_number'] < 0x070f04) {
10+
exit("skip: test works only with curl >= 7.15.4");
11+
}
12+
?>
13+
--FILE--
14+
<?php
15+
16+
$mh = curl_multi_init();
17+
$errno = curl_multi_errno($mh);
18+
echo $errno . PHP_EOL;
19+
echo curl_multi_strerror($errno) . PHP_EOL;
20+
21+
@curl_multi_setopt($mh, -1, -1);
22+
$errno = curl_multi_errno($mh);
23+
echo $errno . PHP_EOL;
24+
echo curl_multi_strerror($errno) . PHP_EOL;
25+
?>
26+
--EXPECTF--
27+
0
28+
No error
29+
6
30+
Unknown option
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
curl_share_errno and curl_share_strerror basic test
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("curl")) {
6+
exit("skip curl extension not loaded");
7+
}
8+
$curl_version = curl_version();
9+
if ($curl_version['version_number'] < 0x070c00) {
10+
exit("skip: test works only with curl >= 7.12.0");
11+
}
12+
?>
13+
--FILE--
14+
<?php
15+
16+
$sh = curl_share_init();
17+
$errno = curl_share_errno($sh);
18+
echo $errno . PHP_EOL;
19+
echo curl_share_strerror($errno) . PHP_EOL;
20+
21+
@curl_share_setopt($sh, -1, -1);
22+
$errno = curl_share_errno($sh);
23+
echo $errno . PHP_EOL;
24+
echo curl_share_strerror($errno) . PHP_EOL;
25+
?>
26+
--EXPECTF--
27+
0
28+
No error
29+
1
30+
Unknown share option

0 commit comments

Comments
 (0)