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