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