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 | http://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_interfaces.h"
25 #include "Zend/zend_smart_str.h"
26
27 #include "curl_private.h"
28
29 #include <curl/curl.h>
30 #include <curl/multi.h>
31
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
34 #endif
35
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
42 #endif
43
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47
48 #define SAVE_CURLM_ERROR(__handle, __err) (__handle)->err.no = (int) __err;
49
50 /* CurlMultiHandle class */
51
52 zend_class_entry *curl_multi_ce;
53
curl_multi_from_obj(zend_object * obj)54 static inline php_curlm *curl_multi_from_obj(zend_object *obj) {
55 return (php_curlm *)((char *)(obj) - XtOffsetOf(php_curlm, std));
56 }
57
58 #define Z_CURL_MULTI_P(zv) curl_multi_from_obj(Z_OBJ_P(zv))
59
60 /* {{{ Returns a new cURL multi handle */
PHP_FUNCTION(curl_multi_init)61 PHP_FUNCTION(curl_multi_init)
62 {
63 php_curlm *mh;
64
65 ZEND_PARSE_PARAMETERS_NONE();
66
67 object_init_ex(return_value, curl_multi_ce);
68 mh = Z_CURL_MULTI_P(return_value);
69 mh->multi = curl_multi_init();
70 mh->handlers = ecalloc(1, sizeof(php_curlm_handlers));
71
72 zend_llist_init(&mh->easyh, sizeof(zval), _php_curl_multi_cleanup_list, 0);
73 }
74 /* }}} */
75
76 /* {{{ Add a normal cURL handle to a cURL multi handle */
PHP_FUNCTION(curl_multi_add_handle)77 PHP_FUNCTION(curl_multi_add_handle)
78 {
79 zval *z_mh;
80 zval *z_ch;
81 php_curlm *mh;
82 php_curl *ch;
83 CURLMcode error = CURLM_OK;
84
85 ZEND_PARSE_PARAMETERS_START(2,2)
86 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
87 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
88 ZEND_PARSE_PARAMETERS_END();
89
90 mh = Z_CURL_MULTI_P(z_mh);
91 ch = Z_CURL_P(z_ch);
92
93 _php_curl_verify_handlers(ch, 1);
94
95 _php_curl_cleanup_handle(ch);
96
97 Z_ADDREF_P(z_ch);
98 zend_llist_add_element(&mh->easyh, z_ch);
99
100 error = curl_multi_add_handle(mh->multi, ch->cp);
101 SAVE_CURLM_ERROR(mh, error);
102
103 RETURN_LONG((zend_long) error);
104 }
105 /* }}} */
106
_php_curl_multi_cleanup_list(void * data)107 void _php_curl_multi_cleanup_list(void *data) /* {{{ */
108 {
109 zval *z_ch = (zval *)data;
110
111 zval_ptr_dtor(z_ch);
112 }
113 /* }}} */
114
115 /* Used internally as comparison routine passed to zend_list_del_element */
curl_compare_objects(zval * z1,zval * z2)116 static int curl_compare_objects( zval *z1, zval *z2 ) /* {{{ */
117 {
118 return (Z_TYPE_P(z1) == Z_TYPE_P(z2) &&
119 Z_TYPE_P(z1) == IS_OBJECT &&
120 Z_OBJ_P(z1) == Z_OBJ_P(z2));
121 }
122 /* }}} */
123
124 /* Used to find the php_curl resource for a given curl easy handle */
_php_curl_multi_find_easy_handle(php_curlm * mh,CURL * easy)125 static zval *_php_curl_multi_find_easy_handle(php_curlm *mh, CURL *easy) /* {{{ */
126 {
127 php_curl *tmp_ch;
128 zend_llist_position pos;
129 zval *pz_ch_temp;
130
131 for(pz_ch_temp = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch_temp;
132 pz_ch_temp = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
133 tmp_ch = Z_CURL_P(pz_ch_temp);
134
135 if (tmp_ch->cp == easy) {
136 return pz_ch_temp;
137 }
138 }
139
140 return NULL;
141 }
142 /* }}} */
143
144 /* {{{ Remove a multi handle from a set of cURL handles */
PHP_FUNCTION(curl_multi_remove_handle)145 PHP_FUNCTION(curl_multi_remove_handle)
146 {
147 zval *z_mh;
148 zval *z_ch;
149 php_curlm *mh;
150 php_curl *ch;
151 CURLMcode error = CURLM_OK;
152
153 ZEND_PARSE_PARAMETERS_START(2,2)
154 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
155 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
156 ZEND_PARSE_PARAMETERS_END();
157
158 mh = Z_CURL_MULTI_P(z_mh);
159 ch = Z_CURL_P(z_ch);
160
161 error = curl_multi_remove_handle(mh->multi, ch->cp);
162 SAVE_CURLM_ERROR(mh, error);
163
164 RETVAL_LONG((zend_long) error);
165 zend_llist_del_element(&mh->easyh, z_ch, (int (*)(void *, void *))curl_compare_objects);
166
167 }
168 /* }}} */
169
170 /* {{{ Get all the sockets associated with the cURL extension, which can then be "selected" */
PHP_FUNCTION(curl_multi_select)171 PHP_FUNCTION(curl_multi_select)
172 {
173 zval *z_mh;
174 php_curlm *mh;
175 double timeout = 1.0;
176 int numfds = 0;
177 CURLMcode error = CURLM_OK;
178
179 ZEND_PARSE_PARAMETERS_START(1,2)
180 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
181 Z_PARAM_OPTIONAL
182 Z_PARAM_DOUBLE(timeout)
183 ZEND_PARSE_PARAMETERS_END();
184
185 mh = Z_CURL_MULTI_P(z_mh);
186
187 error = curl_multi_wait(mh->multi, NULL, 0, (unsigned long) (timeout * 1000.0), &numfds);
188 if (CURLM_OK != error) {
189 SAVE_CURLM_ERROR(mh, error);
190 RETURN_LONG(-1);
191 }
192
193 RETURN_LONG(numfds);
194 }
195 /* }}} */
196
197 /* {{{ Run the sub-connections of the current cURL handle */
PHP_FUNCTION(curl_multi_exec)198 PHP_FUNCTION(curl_multi_exec)
199 {
200 zval *z_mh;
201 zval *z_still_running;
202 php_curlm *mh;
203 int still_running;
204 CURLMcode error = CURLM_OK;
205
206 ZEND_PARSE_PARAMETERS_START(2, 2)
207 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
208 Z_PARAM_ZVAL(z_still_running)
209 ZEND_PARSE_PARAMETERS_END();
210
211 mh = Z_CURL_MULTI_P(z_mh);
212
213 {
214 zend_llist_position pos;
215 php_curl *ch;
216 zval *pz_ch;
217
218 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
219 pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
220 ch = Z_CURL_P(pz_ch);
221
222 _php_curl_verify_handlers(ch, 1);
223 }
224 }
225
226 still_running = zval_get_long(z_still_running);
227 error = curl_multi_perform(mh->multi, &still_running);
228 ZEND_TRY_ASSIGN_REF_LONG(z_still_running, still_running);
229
230 SAVE_CURLM_ERROR(mh, error);
231 RETURN_LONG((zend_long) error);
232 }
233 /* }}} */
234
235 /* {{{ Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set */
PHP_FUNCTION(curl_multi_getcontent)236 PHP_FUNCTION(curl_multi_getcontent)
237 {
238 zval *z_ch;
239 php_curl *ch;
240
241 ZEND_PARSE_PARAMETERS_START(1, 1)
242 Z_PARAM_OBJECT_OF_CLASS(z_ch, curl_ce)
243 ZEND_PARSE_PARAMETERS_END();
244
245 ch = Z_CURL_P(z_ch);
246
247 if (ch->handlers->write->method == PHP_CURL_RETURN) {
248 if (!ch->handlers->write->buf.s) {
249 RETURN_EMPTY_STRING();
250 }
251 smart_str_0(&ch->handlers->write->buf);
252 RETURN_STR_COPY(ch->handlers->write->buf.s);
253 }
254
255 RETURN_NULL();
256 }
257 /* }}} */
258
259 /* {{{ Get information about the current transfers */
PHP_FUNCTION(curl_multi_info_read)260 PHP_FUNCTION(curl_multi_info_read)
261 {
262 zval *z_mh;
263 php_curlm *mh;
264 CURLMsg *tmp_msg;
265 int queued_msgs;
266 zval *zmsgs_in_queue = NULL;
267 php_curl *ch;
268
269 ZEND_PARSE_PARAMETERS_START(1, 2)
270 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
271 Z_PARAM_OPTIONAL
272 Z_PARAM_ZVAL(zmsgs_in_queue)
273 ZEND_PARSE_PARAMETERS_END();
274
275 mh = Z_CURL_MULTI_P(z_mh);
276
277 tmp_msg = curl_multi_info_read(mh->multi, &queued_msgs);
278 if (tmp_msg == NULL) {
279 RETURN_FALSE;
280 }
281
282 if (zmsgs_in_queue) {
283 ZEND_TRY_ASSIGN_REF_LONG(zmsgs_in_queue, queued_msgs);
284 }
285
286 array_init(return_value);
287 add_assoc_long(return_value, "msg", tmp_msg->msg);
288 add_assoc_long(return_value, "result", tmp_msg->data.result);
289
290 /* find the original easy curl handle */
291 {
292 zval *pz_ch = _php_curl_multi_find_easy_handle(mh, tmp_msg->easy_handle);
293 if (pz_ch != NULL) {
294 /* we must save result to be able to read error message */
295 ch = Z_CURL_P(pz_ch);
296 SAVE_CURL_ERROR(ch, tmp_msg->data.result);
297
298 Z_ADDREF_P(pz_ch);
299 add_assoc_zval(return_value, "handle", pz_ch);
300 }
301 }
302 }
303 /* }}} */
304
305 /* {{{ Close a set of cURL handles */
PHP_FUNCTION(curl_multi_close)306 PHP_FUNCTION(curl_multi_close)
307 {
308 php_curlm *mh;
309 zval *z_mh;
310
311 zend_llist_position pos;
312 zval *pz_ch;
313
314 ZEND_PARSE_PARAMETERS_START(1,1)
315 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
316 ZEND_PARSE_PARAMETERS_END();
317
318 mh = Z_CURL_MULTI_P(z_mh);
319
320 for (pz_ch = (zval *)zend_llist_get_first_ex(&mh->easyh, &pos); pz_ch;
321 pz_ch = (zval *)zend_llist_get_next_ex(&mh->easyh, &pos)) {
322 php_curl *ch = Z_CURL_P(pz_ch);
323 _php_curl_verify_handlers(ch, 1);
324 curl_multi_remove_handle(mh->multi, ch->cp);
325 }
326 zend_llist_clean(&mh->easyh);
327 }
328 /* }}} */
329
330 /* {{{ Return an integer containing the last multi curl error number */
PHP_FUNCTION(curl_multi_errno)331 PHP_FUNCTION(curl_multi_errno)
332 {
333 zval *z_mh;
334 php_curlm *mh;
335
336 ZEND_PARSE_PARAMETERS_START(1,1)
337 Z_PARAM_OBJECT_OF_CLASS(z_mh, curl_multi_ce)
338 ZEND_PARSE_PARAMETERS_END();
339
340 mh = Z_CURL_MULTI_P(z_mh);
341
342 RETURN_LONG(mh->err.no);
343 }
344 /* }}} */
345
346 /* {{{ return string describing error code */
PHP_FUNCTION(curl_multi_strerror)347 PHP_FUNCTION(curl_multi_strerror)
348 {
349 zend_long code;
350 const char *str;
351
352 ZEND_PARSE_PARAMETERS_START(1,1)
353 Z_PARAM_LONG(code)
354 ZEND_PARSE_PARAMETERS_END();
355
356 str = curl_multi_strerror(code);
357 if (str) {
358 RETURN_STRING(str);
359 } else {
360 RETURN_NULL();
361 }
362 }
363 /* }}} */
364
365 #if LIBCURL_VERSION_NUM >= 0x072C00 /* Available since 7.44.0 */
366
_php_server_push_callback(CURL * parent_ch,CURL * easy,size_t num_headers,struct curl_pushheaders * push_headers,void * userp)367 static int _php_server_push_callback(CURL *parent_ch, CURL *easy, size_t num_headers, struct curl_pushheaders *push_headers, void *userp) /* {{{ */
368 {
369 php_curl *ch;
370 php_curl *parent;
371 php_curlm *mh = (php_curlm *)userp;
372 size_t rval = CURL_PUSH_DENY;
373 php_curlm_server_push *t = mh->handlers->server_push;
374 zval *pz_parent_ch = NULL;
375 zval pz_ch;
376 zval headers;
377 zval retval;
378 char *header;
379 int error;
380 zend_fcall_info fci = empty_fcall_info;
381
382 pz_parent_ch = _php_curl_multi_find_easy_handle(mh, parent_ch);
383 if (pz_parent_ch == NULL) {
384 return rval;
385 }
386
387 parent = Z_CURL_P(pz_parent_ch);
388
389 ch = init_curl_handle_into_zval(&pz_ch);
390 ch->cp = easy;
391 _php_setup_easy_copy_handlers(ch, parent);
392
393 size_t i;
394 array_init(&headers);
395 for(i=0; i<num_headers; i++) {
396 header = curl_pushheader_bynum(push_headers, i);
397 add_next_index_string(&headers, header);
398 }
399
400 zend_fcall_info_init(&t->func_name, 0, &fci, &t->fci_cache, NULL, NULL);
401
402 zend_fcall_info_argn(
403 &fci, 3,
404 pz_parent_ch,
405 &pz_ch,
406 &headers
407 );
408
409 fci.retval = &retval;
410
411 error = zend_call_function(&fci, &t->fci_cache);
412 zend_fcall_info_args_clear(&fci, 1);
413 zval_ptr_dtor_nogc(&headers);
414
415 if (error == FAILURE) {
416 php_error_docref(NULL, E_WARNING, "Cannot call the CURLMOPT_PUSHFUNCTION");
417 } else if (!Z_ISUNDEF(retval)) {
418 if (CURL_PUSH_DENY != zval_get_long(&retval)) {
419 rval = CURL_PUSH_OK;
420 zend_llist_add_element(&mh->easyh, &pz_ch);
421 } else {
422 /* libcurl will free this easy handle, avoid double free */
423 ch->cp = NULL;
424 }
425 }
426
427 return rval;
428 }
429 /* }}} */
430
431 #endif
432
_php_curl_multi_setopt(php_curlm * mh,zend_long option,zval * zvalue,zval * return_value)433 static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue, zval *return_value) /* {{{ */
434 {
435 CURLMcode error = CURLM_OK;
436
437 switch (option) {
438 case CURLMOPT_PIPELINING:
439 case CURLMOPT_MAXCONNECTS:
440 #if LIBCURL_VERSION_NUM >= 0x071e00 /* 7.30.0 */
441 case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
442 case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
443 case CURLMOPT_MAX_HOST_CONNECTIONS:
444 case CURLMOPT_MAX_PIPELINE_LENGTH:
445 case CURLMOPT_MAX_TOTAL_CONNECTIONS:
446 #endif
447 {
448 zend_long lval = zval_get_long(zvalue);
449
450 if (option == CURLMOPT_PIPELINING && (lval & 1)) {
451 #if LIBCURL_VERSION_NUM >= 0x073e00 /* 7.62.0 */
452 php_error_docref(NULL, E_WARNING, "CURLPIPE_HTTP1 is no longer supported");
453 #else
454 php_error_docref(NULL, E_DEPRECATED, "CURLPIPE_HTTP1 is deprecated");
455 #endif
456 }
457 error = curl_multi_setopt(mh->multi, option, lval);
458 break;
459 }
460 #if LIBCURL_VERSION_NUM > 0x072D00 /* Available since 7.45.0 */
461 case CURLMOPT_PUSHFUNCTION:
462 if (mh->handlers->server_push == NULL) {
463 mh->handlers->server_push = ecalloc(1, sizeof(php_curlm_server_push));
464 } else if (!Z_ISUNDEF(mh->handlers->server_push->func_name)) {
465 zval_ptr_dtor(&mh->handlers->server_push->func_name);
466 mh->handlers->server_push->fci_cache = empty_fcall_info_cache;
467 }
468
469 ZVAL_COPY(&mh->handlers->server_push->func_name, zvalue);
470 mh->handlers->server_push->method = PHP_CURL_USER;
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 if (mh->handlers) {
561 efree(mh->handlers);
562 }
563
564 zend_object_std_dtor(&mh->std);
565 }
566
curl_multi_get_gc(zend_object * object,zval ** table,int * n)567 static HashTable *curl_multi_get_gc(zend_object *object, zval **table, int *n)
568 {
569 php_curlm *curl_multi = curl_multi_from_obj(object);
570
571 zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
572
573 if (curl_multi->handlers) {
574 if (curl_multi->handlers->server_push) {
575 zend_get_gc_buffer_add_zval(gc_buffer, &curl_multi->handlers->server_push->func_name);
576 }
577 }
578
579 zend_llist_position pos;
580 for (zval *pz_ch = (zval *) zend_llist_get_first_ex(&curl_multi->easyh, &pos); pz_ch;
581 pz_ch = (zval *) zend_llist_get_next_ex(&curl_multi->easyh, &pos)) {
582 zend_get_gc_buffer_add_zval(gc_buffer, pz_ch);
583 }
584
585 zend_get_gc_buffer_use(gc_buffer, table, n);
586
587 return zend_std_get_properties(object);
588 }
589
curl_multi_register_class(const zend_function_entry * method_entries)590 void curl_multi_register_class(const zend_function_entry *method_entries) {
591 zend_class_entry ce_multi;
592 INIT_CLASS_ENTRY(ce_multi, "CurlMultiHandle", method_entries);
593 curl_multi_ce = zend_register_internal_class(&ce_multi);
594 curl_multi_ce->ce_flags |= ZEND_ACC_FINAL | ZEND_ACC_NO_DYNAMIC_PROPERTIES;
595 curl_multi_ce->create_object = curl_multi_create_object;
596 curl_multi_ce->serialize = zend_class_serialize_deny;
597 curl_multi_ce->unserialize = zend_class_unserialize_deny;
598
599 memcpy(&curl_multi_handlers, &std_object_handlers, sizeof(zend_object_handlers));
600 curl_multi_handlers.offset = XtOffsetOf(php_curlm, std);
601 curl_multi_handlers.free_obj = curl_multi_free_obj;
602 curl_multi_handlers.get_gc = curl_multi_get_gc;
603 curl_multi_handlers.get_constructor = curl_multi_get_constructor;
604 curl_multi_handlers.clone_obj = NULL;
605 curl_multi_handlers.cast_object = curl_cast_object;
606 curl_multi_handlers.compare = zend_objects_not_comparable;
607 }
608