1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Sterling Hughes <sterling@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24 #include "Zend/zend_smart_str.h"
25
26 #include "curl_private.h"
27
28 #include <curl/curl.h>
29 #include <curl/multi.h>
30
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif
34
35 #ifdef HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38
39 #ifdef HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 #define SAVE_CURLM_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
48
49 /* CurlMultiHandle class */
50
51 zend_class_entry *curl_multi_ce;
52
curl_multi_from_obj(zend_object * obj)53 static inline php_curlm *curl_multi_from_obj(zend_object *obj) {
54 return (php_curlm *)((char *)(obj) - XtOffsetOf(php_curlm, std));
55 }
56
57 #define Z_CURL_MULTI_P(zv) curl_multi_from_obj(Z_OBJ_P(zv))
58
59 /* {{{ Returns a new cURL multi handle */
PHP_FUNCTION(curl_multi_init)60 PHP_FUNCTION(curl_multi_init)
61 {
62 php_curlm *mh;
63
64 ZEND_PARSE_PARAMETERS_NONE();
65
66 object_init_ex(return_value, curl_multi_ce);
67 mh = Z_CURL_MULTI_P(return_value);
68 mh->multi = curl_multi_init();
69
70 zend_llist_init(&mh->easyh, sizeof(zval), _php_curl_multi_cleanup_list, 0);
71 }
72 /* }}} */
73
74 /* {{{ Add a normal cURL handle to a cURL multi handle */
PHP_FUNCTION(curl_multi_add_handle)75 PHP_FUNCTION(curl_multi_add_handle)
76 {
77 zval *z_mh;
78 zval *z_ch;
79 php_curlm *mh;
80 php_curl *ch;
81 CURLMcode error = CURLM_OK;
82
83 ZEND_PARSE_PARAMETERS_START(2,2)
84 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
85 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
86 ZEND_PARSE_PARAMETERS_END();
87
88 mh = Z_CURL_MULTI_P(z_mh);
89 ch = Z_CURL_P(z_ch);
90
91 _php_curl_verify_handlers(ch, 1);
92
93 _php_curl_cleanup_handle(ch);
94
95 Z_ADDREF_P(z_ch);
96 zend_llist_add_element(&mh->easyh, z_ch);
97
98 error = curl_multi_add_handle(mh->multi, ch->cp);
99 SAVE_CURLM_ERROR(mh, error);
100
101 RETURN_LONG((zend_long) error);
102 }
103 /* }}} */
104
_php_curl_multi_cleanup_list(void * data)105 void _php_curl_multi_cleanup_list(void *data) /* {{{ */
106 {
107 zval *z_ch = (zval *)data;
108
109 zval_ptr_dtor(z_ch);
110 }
111 /* }}} */
112
113 /* Used internally as comparison routine passed to zend_list_del_element */
curl_compare_objects(zval * z1,zval * z2)114 static int curl_compare_objects( zval *z1, zval *z2 ) /* {{{ */
115 {
116 return (Z_TYPE_P(z1) == Z_TYPE_P(z2) &&
117 Z_TYPE_P(z1) == IS_OBJECT &&
118 Z_OBJ_P(z1) == Z_OBJ_P(z2));
119 }
120 /* }}} */
121
122 /* Used to find the php_curl resource for a given curl easy handle */
_php_curl_multi_find_easy_handle(php_curlm * mh,CURL * easy)123 static zval *_php_curl_multi_find_easy_handle(php_curlm *mh, CURL *easy) /* {{{ */
124 {
125 php_curl *tmp_ch;
126 zend_llist_position pos;
127 zval *pz_ch_temp;
128
129 for(pz_ch_temp = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch_temp;
130 pz_ch_temp = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
131 tmp_ch = Z_CURL_P(pz_ch_temp);
132
133 if (tmp_ch->cp == easy) {
134 return pz_ch_temp;
135 }
136 }
137
138 return NULL;
139 }
140 /* }}} */
141
142 /* {{{ Remove a multi handle from a set of cURL handles */
PHP_FUNCTION(curl_multi_remove_handle)143 PHP_FUNCTION(curl_multi_remove_handle)
144 {
145 zval *z_mh;
146 zval *z_ch;
147 php_curlm *mh;
148 php_curl *ch;
149 CURLMcode error = CURLM_OK;
150
151 ZEND_PARSE_PARAMETERS_START(2,2)
152 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
153 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
154 ZEND_PARSE_PARAMETERS_END();
155
156 mh = Z_CURL_MULTI_P(z_mh);
157 ch = Z_CURL_P(z_ch);
158
159 error = curl_multi_remove_handle(mh->multi, ch->cp);
160 SAVE_CURLM_ERROR(mh, error);
161
162 RETVAL_LONG((zend_long) error);
163 zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_objects);
164
165 }
166 /* }}} */
167
168 /* {{{ Get all the sockets associated with the cURL extension, which can then be "selected" */
PHP_FUNCTION(curl_multi_select)169 PHP_FUNCTION(curl_multi_select)
170 {
171 zval *z_mh;
172 php_curlm *mh;
173 double timeout = 1.0;
174 int numfds = 0;
175 CURLMcode error = CURLM_OK;
176
177 ZEND_PARSE_PARAMETERS_START(1,2)
178 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
179 Z_PARAM_OPTIONAL
180 Z_PARAM_DOUBLE(timeout)
181 ZEND_PARSE_PARAMETERS_END();
182
183 mh = Z_CURL_MULTI_P(z_mh);
184
185 error = curl_multi_wait(mh->multi, NULL, 0, (unsigned long) (timeout * 1000.0), &numfds);
186 if (CURLM_OK != error) {
187 SAVE_CURLM_ERROR(mh, error);
188 RETURN_LONG(-1);
189 }
190
191 RETURN_LONG(numfds);
192 }
193 /* }}} */
194
195 /* {{{ Run the sub-connections of the current cURL handle */
PHP_FUNCTION(curl_multi_exec)196 PHP_FUNCTION(curl_multi_exec)
197 {
198 zval *z_mh;
199 zval *z_still_running;
200 php_curlm *mh;
201 int still_running;
202 CURLMcode error = CURLM_OK;
203
204 ZEND_PARSE_PARAMETERS_START(2, 2)
205 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
206 Z_PARAM_ZVAL(z_still_running)
207 ZEND_PARSE_PARAMETERS_END();
208
209 mh = Z_CURL_MULTI_P(z_mh);
210
211 {
212 zend_llist_position pos;
213 php_curl *ch;
214 zval *pz_ch;
215
216 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
217 pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
218 ch = Z_CURL_P(pz_ch);
219
220 _php_curl_verify_handlers(ch, 1);
221 }
222 }
223
224 still_running = zval_get_long(z_still_running);
225 error = curl_multi_perform(mh->multi, &still_running);
226 ZEND_TRY_ASSIGN_REF_LONG(z_still_running, still_running);
227
228 SAVE_CURLM_ERROR(mh, error);
229 RETURN_LONG((zend_long) error);
230 }
231 /* }}} */
232
233 /* {{{ Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
PHP_FUNCTION(curl_multi_getcontent)234 PHP_FUNCTION(curl_multi_getcontent)
235 {
236 zval *z_ch;
237 php_curl *ch;
238
239 ZEND_PARSE_PARAMETERS_START(1, 1)
240 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
241 ZEND_PARSE_PARAMETERS_END();
242
243 ch = Z_CURL_P(z_ch);
244
245 if (ch->handlers.write->method == PHP_CURL_RETURN) {
246 if (!ch->handlers.write->buf.s) {
247 RETURN_EMPTY_STRING();
248 }
249 smart_str_0(&ch->handlers.write->buf);
250 RETURN_STR_COPY(ch->handlers.write->buf.s);
251 }
252
253 RETURN_NULL();
254 }
255 /* }}} */
256
257 /* {{{ Get information about the current transfers */
PHP_FUNCTION(curl_multi_info_read)258 PHP_FUNCTION(curl_multi_info_read)
259 {
260 zval *z_mh;
261 php_curlm *mh;
262 CURLMsg *tmp_msg;
263 int queued_msgs;
264 zval *zmsgs_in_queue = NULL;
265 php_curl *ch;
266
267 ZEND_PARSE_PARAMETERS_START(1, 2)
268 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
269 Z_PARAM_OPTIONAL
270 Z_PARAM_ZVAL(zmsgs_in_queue)
271 ZEND_PARSE_PARAMETERS_END();
272
273 mh = Z_CURL_MULTI_P(z_mh);
274
275 tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs);
276 if (tmp_msg == NULL) {
277 RETURN_FALSE;
278 }
279
280 if (zmsgs_in_queue) {
281 ZEND_TRY_ASSIGN_REF_LONG(zmsgs_in_queue, queued_msgs);
282 }
283
284 array_init(return_value);
285 add_assoc_long(return_value, "msg", tmp_msg->msg);
286 add_assoc_long(return_value, "result", tmp_msg->data.result);
287
288 /* find the original easy curl handle */
289 {
290 zval *pz_ch = _php_curl_multi_find_easy_handle(mh, tmp_msg->easy_handle);
291 if (pz_ch != NULL) {
292 /* we must save result to be able to read error message */
293 ch = Z_CURL_P(pz_ch);
294 SAVE_CURL_ERROR(ch, tmp_msg->data.result);
295
296 Z_ADDREF_P(pz_ch);
297 add_assoc_zval(return_value, "handle", pz_ch);
298 }
299 }
300 }
301 /* }}} */
302
303 /* {{{ Close a set of cURL handles */
PHP_FUNCTION(curl_multi_close)304 PHP_FUNCTION(curl_multi_close)
305 {
306 php_curlm *mh;
307 zval *z_mh;
308
309 zend_llist_position pos;
310 zval *pz_ch;
311
312 ZEND_PARSE_PARAMETERS_START(1,1)
313 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
314 ZEND_PARSE_PARAMETERS_END();
315
316 mh = Z_CURL_MULTI_P(z_mh);
317
318 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
319 pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
320 php_curl *ch = Z_CURL_P(pz_ch);
321 _php_curl_verify_handlers(ch, 1);
322 curl_multi_remove_handle(mh->multi, ch->cp);
323 }
324 zend_llist_clean(&mh->easyh);
325 }
326 /* }}} */
327
328 /* {{{ Return an integer containing the last multi curl error number */
PHP_FUNCTION(curl_multi_errno)329 PHP_FUNCTION(curl_multi_errno)
330 {
331 zval *z_mh;
332 php_curlm *mh;
333
334 ZEND_PARSE_PARAMETERS_START(1,1)
335 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
336 ZEND_PARSE_PARAMETERS_END();
337
338 mh = Z_CURL_MULTI_P(z_mh);
339
340 RETURN_LONG(mh->err.no);
341 }
342 /* }}} */
343
344 /* {{{ return string describing error code */
PHP_FUNCTION(curl_multi_strerror)345 PHP_FUNCTION(curl_multi_strerror)
346 {
347 zend_long code;
348 const char *str;
349
350 ZEND_PARSE_PARAMETERS_START(1,1)
351 Z_PARAM_LONG(code)
352 ZEND_PARSE_PARAMETERS_END();
353
354 str = curl_multi_strerror(code);
355 if (str) {
356 RETURN_STRING(str);
357 } else {
358 RETURN_NULL();
359 }
360 }
361 /* }}} */
362
363 #if LIBCURL_VERSION_NUM >= 0x072C00 /* Available since 7.44.0 */
364
_php_server_push_callback(CURL * parent_ch,CURL * easy,size_t num_headers,struct curl_pushheaders * push_headers,void * userp)365 static int _php_server_push_callback(CURL *parent_ch, CURL *easy, size_t num_headers, struct curl_pushheaders *push_headers, void *userp) /* {{{ */
366 {
367 php_curl *ch;
368 php_curl *parent;
369 php_curlm *mh = (php_curlm *)userp;
370 size_t rval = CURL_PUSH_DENY;
371 php_curl_callback *t = mh->handlers.server_push;
372 zval *pz_parent_ch = NULL;
373 zval pz_ch;
374 zval headers;
375 zval retval;
376 char *header;
377 int error;
378 zend_fcall_info fci = empty_fcall_info;
379
380 pz_parent_ch = _php_curl_multi_find_easy_handle(mh, parent_ch);
381 if (pz_parent_ch == NULL) {
382 return rval;
383 }
384
385 if (UNEXPECTED(zend_fcall_info_init(&t->func_name, 0, &fci, &t->fci_cache, NULL, NULL) == FAILURE)) {
386 php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
387 return rval;
388 }
389
390 parent = Z_CURL_P(pz_parent_ch);
391
392 ch = init_curl_handle_into_zval(&pz_ch);
393 ch->cp = easy;
394 _php_setup_easy_copy_handlers(ch, parent);
395
396 size_t i;
397 array_init(&headers);
398 for(i=0; i<num_headers; i++) {
399 header = curl_pushheader_bynum(push_headers, i);
400 add_next_index_string(&headers, header);
401 }
402
403 zend_fcall_info_argn(
404 &fci, 3,
405 pz_parent_ch,
406 &pz_ch,
407 &headers
408 );
409
410 fci.retval = &retval;
411
412 error = zend_call_function(&fci, &t->fci_cache);
413 zend_fcall_info_args_clear(&fci, 1);
414 zval_ptr_dtor_nogc(&headers);
415
416 if (error == FAILURE) {
417 php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
418 } else if (!Z_ISUNDEF(retval)) {
419 if (CURL_PUSH_DENY != zval_get_long(&retval)) {
420 rval = CURL_PUSH_OK;
421 zend_llist_add_element(&mh->easyh, &pz_ch);
422 } else {
423 /* libcurl will free this easy handle, avoid double free */
424 ch->cp = NULL;
425 }
426 }
427
428 return rval;
429 }
430 /* }}} */
431
432 #endif
433
_php_curl_multi_setopt(php_curlm * mh,zend_long option,zval * zvalue,zval * return_value)434 static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
435 {
436 CURLMcode error = CURLM_OK;
437
438 switch (option) {
439 case CURLMOPT_PIPELINING:
440 case CURLMOPT_MAXCONNECTS:
441 #if LIBCURL_VERSION_NUM >= 0x071e00 /* 7.30.0 */
442 case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
443 case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
444 case CURLMOPT_MAX_HOST_CONNECTIONS:
445 case CURLMOPT_MAX_PIPELINE_LENGTH:
446 case CURLMOPT_MAX_TOTAL_CONNECTIONS:
447 #endif
448 {
449 zend_long lval = zval_get_long(zvalue);
450
451 if (option == CURLMOPT_PIPELINING && (lval & 1)) {
452 #if LIBCURL_VERSION_NUM >= 0x073e00 /* 7.62.0 */
453 php_error_docref(NULL, E_WARNING, "CURLPIPE_HTTP1 is no longer supported");
454 #else
455 php_error_docref(NULL, E_DEPRECATED, "CURLPIPE_HTTP1 is deprecated");
456 #endif
457 }
458 error = curl_multi_setopt(mh->multi, option, lval);
459 break;
460 }
461 #if LIBCURL_VERSION_NUM > 0x072D00 /* Available since 7.45.0 */
462 case CURLMOPT_PUSHFUNCTION:
463 if (mh->handlers.server_push == NULL) {
464 mh->handlers.server_push = ecalloc(1, sizeof(php_curl_callback));
465 } else if (!Z_ISUNDEF(mh->handlers.server_push->func_name)) {
466 zval_ptr_dtor(&mh->handlers.server_push->func_name);
467 mh->handlers.server_push->fci_cache = empty_fcall_info_cache;
468 }
469
470 ZVAL_COPY(&mh->handlers.server_push->func_name, zvalue);
471 error = curl_multi_setopt(mh->multi, option, _php_server_push_callback);
472 if (error != CURLM_OK) {
473 return 0;
474 }
475 error = curl_multi_setopt(mh->multi, CURLMOPT_PUSHDATA, mh);
476 break;
477 #endif
478 default:
479 zend_argument_value_error(2, "is not a valid cURL multi option");
480 error = CURLM_UNKNOWN_OPTION;
481 break;
482 }
483
484 SAVE_CURLM_ERROR(mh, error);
485
486 return error != CURLM_OK;
487 }
488 /* }}} */
489
490 /* {{{ Set an option for the curl multi handle */
PHP_FUNCTION(curl_multi_setopt)491 PHP_FUNCTION(curl_multi_setopt)
492 {
493 zval *z_mh, *zvalue;
494 zend_long options;
495 php_curlm *mh;
496
497 ZEND_PARSE_PARAMETERS_START(3,3)
498 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
499 Z_PARAM_LONG(options)
500 Z_PARAM_ZVAL(zvalue)
501 ZEND_PARSE_PARAMETERS_END();
502
503 mh = Z_CURL_MULTI_P(z_mh);
504
505 if (!_php_curl_multi_setopt(mh, options, zvalue, return_value)) {
506 RETURN_TRUE;
507 } else {
508 RETURN_FALSE;
509 }
510 }
511 /* }}} */
512
513 /* CurlMultiHandle class */
514
515 static zend_object_handlers curl_multi_handlers;
516
curl_multi_create_object(zend_class_entry * class_type)517 static zend_object *curl_multi_create_object(zend_class_entry *class_type) {
518 php_curlm *intern = zend_object_alloc(sizeof(php_curlm), class_type);
519
520 zend_object_std_init(&intern->std, class_type);
521 object_properties_init(&intern->std, class_type);
522 intern->std.handlers = &curl_multi_handlers;
523
524 return &intern->std;
525 }
526
curl_multi_get_constructor(zend_object * object)527 static zend_function *curl_multi_get_constructor(zend_object *object) {
528 zend_throw_error(NULL, "Cannot directly construct CurlMultiHandle, use curl_multi_init() instead");
529 return NULL;
530 }
531
curl_multi_free_obj(zend_object * object)532 void curl_multi_free_obj(zend_object *object)
533 {
534 php_curlm *mh = curl_multi_from_obj(object);
535
536 zend_llist_position pos;
537 php_curl *ch;
538 zval *pz_ch;
539
540 if (!mh->multi) {
541 /* Can happen if constructor throws. */
542 zend_object_std_dtor(&mh->std);
543 return;
544 }
545
546 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
547 pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
548 if (!(OBJ_FLAGS(Z_OBJ_P(pz_ch)) & IS_OBJ_FREE_CALLED)) {
549 ch = Z_CURL_P(pz_ch);
550 _php_curl_verify_handlers(ch, 0);
551 }
552 }
553
554 curl_multi_cleanup(mh->multi);
555 zend_llist_clean(&mh->easyh);
556 if (mh->handlers.server_push) {
557 zval_ptr_dtor(&mh->handlers.server_push->func_name);
558 efree(mh->handlers.server_push);
559 }
560
561 zend_object_std_dtor(&mh->std);
562 }
563
curl_multi_get_gc(zend_object * object,zval ** table,int * n)564 static HashTable *curl_multi_get_gc(zend_object *object, zval **table, int *n)
565 {
566 php_curlm *curl_multi = curl_multi_from_obj(object);
567
568 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
569
570 if (curl_multi->handlers.server_push) {
571 zend_get_gc_buffer_add_zval(gc_buffer, &curl_multi->handlers.server_push->func_name);
572 }
573
574 zend_llist_position pos;
575 for (zval *pz_ch = (zval *) zend_llist_get_first_ex(&curl_multi->easyh, &pos); pz_ch;
576 pz_ch = (zval *) zend_llist_get_next_ex(&curl_multi->easyh, &pos)) {
577 zend_get_gc_buffer_add_zval(gc_buffer, pz_ch);
578 }
579
580 zend_get_gc_buffer_use(gc_buffer, table, n);
581
582 return zend_std_get_properties(object);
583 }
584
curl_multi_register_handlers(void)585 void curl_multi_register_handlers(void) {
586 curl_multi_ce->create_object = curl_multi_create_object;
587
588 memcpy(&curl_multi_handlers, &std_object_handlers, sizeof(zend_object_handlers));
589 curl_multi_handlers.offset = XtOffsetOf(php_curlm, std);
590 curl_multi_handlers.free_obj = curl_multi_free_obj;
591 curl_multi_handlers.get_gc = curl_multi_get_gc;
592 curl_multi_handlers.get_constructor = curl_multi_get_constructor;
593 curl_multi_handlers.clone_obj = NULL;
594 curl_multi_handlers.cast_object = curl_cast_object;
595 curl_multi_handlers.compare = zend_objects_not_comparable;
596 }
597